OSDN Git Service

* collect2.c (main): Only export initfunc and finifunc if
[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 TREE_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                                             TREE_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                                             TREE_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 TREE_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) + TREE_HASH (TREE_VALUE (list));
1496   return h;
1497 }
1498
1499 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
1500    to speed 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, TREE_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 /* Emit SEQ into basic block just before INSN (that is assumed to be
1633    first instruction of some existing BB and return the newly
1634    produced block.  */
1635 static basic_block
1636 emit_to_new_bb_before (rtx seq, rtx insn)
1637 {
1638   rtx last;
1639   basic_block bb;
1640
1641   last = emit_insn_before (seq, insn);
1642   if (GET_CODE (last) == BARRIER)
1643     last = PREV_INSN (last);
1644   bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
1645   update_bb_for_insn (bb);
1646   bb->flags |= BB_SUPERBLOCK;
1647   return bb;
1648 }
1649
1650 /* Generate the code to actually handle exceptions, which will follow the
1651    landing pads.  */
1652
1653 static void
1654 build_post_landing_pads (void)
1655 {
1656   int i;
1657
1658   for (i = cfun->eh->last_region_number; i > 0; --i)
1659     {
1660       struct eh_region *region = cfun->eh->region_array[i];
1661       rtx seq;
1662
1663       /* Mind we don't process a region more than once.  */
1664       if (!region || region->region_number != i)
1665         continue;
1666
1667       switch (region->type)
1668         {
1669         case ERT_TRY:
1670           /* ??? Collect the set of all non-overlapping catch handlers
1671                all the way up the chain until blocked by a cleanup.  */
1672           /* ??? Outer try regions can share landing pads with inner
1673              try regions if the types are completely non-overlapping,
1674              and there are no intervening cleanups.  */
1675
1676           region->post_landing_pad = gen_label_rtx ();
1677
1678           start_sequence ();
1679
1680           emit_label (region->post_landing_pad);
1681
1682           /* ??? It is mighty inconvenient to call back into the
1683              switch statement generation code in expand_end_case.
1684              Rapid prototyping sez a sequence of ifs.  */
1685           {
1686             struct eh_region *c;
1687             for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1688               {
1689                 if (c->u.catch.type_list == NULL)
1690                   emit_jump (c->label);
1691                 else
1692                   {
1693                     /* Need for one cmp/jump per type caught. Each type
1694                        list entry has a matching entry in the filter list
1695                        (see assign_filter_values).  */
1696                     tree tp_node = c->u.catch.type_list;
1697                     tree flt_node = c->u.catch.filter_list;
1698
1699                     for (; tp_node; )
1700                       {
1701                         emit_cmp_and_jump_insns
1702                           (cfun->eh->filter,
1703                            GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1704                            EQ, NULL_RTX, word_mode, 0, c->label);
1705
1706                         tp_node = TREE_CHAIN (tp_node);
1707                         flt_node = TREE_CHAIN (flt_node);
1708                       }
1709                   }
1710               }
1711           }
1712
1713           /* We delay the generation of the _Unwind_Resume until we generate
1714              landing pads.  We emit a marker here so as to get good control
1715              flow data in the meantime.  */
1716           region->resume
1717             = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1718           emit_barrier ();
1719
1720           seq = get_insns ();
1721           end_sequence ();
1722
1723           emit_to_new_bb_before (seq, region->u.try.catch->label);
1724
1725           break;
1726
1727         case ERT_ALLOWED_EXCEPTIONS:
1728           region->post_landing_pad = gen_label_rtx ();
1729
1730           start_sequence ();
1731
1732           emit_label (region->post_landing_pad);
1733
1734           emit_cmp_and_jump_insns (cfun->eh->filter,
1735                                    GEN_INT (region->u.allowed.filter),
1736                                    EQ, NULL_RTX, word_mode, 0, region->label);
1737
1738           /* We delay the generation of the _Unwind_Resume until we generate
1739              landing pads.  We emit a marker here so as to get good control
1740              flow data in the meantime.  */
1741           region->resume
1742             = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1743           emit_barrier ();
1744
1745           seq = get_insns ();
1746           end_sequence ();
1747
1748           emit_to_new_bb_before (seq, region->label);
1749           break;
1750
1751         case ERT_CLEANUP:
1752         case ERT_MUST_NOT_THROW:
1753           region->post_landing_pad = region->label;
1754           break;
1755
1756         case ERT_CATCH:
1757         case ERT_THROW:
1758           /* Nothing to do.  */
1759           break;
1760
1761         default:
1762           abort ();
1763         }
1764     }
1765 }
1766
1767 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1768    _Unwind_Resume otherwise.  */
1769
1770 static void
1771 connect_post_landing_pads (void)
1772 {
1773   int i;
1774
1775   for (i = cfun->eh->last_region_number; i > 0; --i)
1776     {
1777       struct eh_region *region = cfun->eh->region_array[i];
1778       struct eh_region *outer;
1779       rtx seq;
1780       rtx barrier;
1781
1782       /* Mind we don't process a region more than once.  */
1783       if (!region || region->region_number != i)
1784         continue;
1785
1786       /* If there is no RESX, or it has been deleted by flow, there's
1787          nothing to fix up.  */
1788       if (! region->resume || INSN_DELETED_P (region->resume))
1789         continue;
1790
1791       /* Search for another landing pad in this function.  */
1792       for (outer = region->outer; outer ; outer = outer->outer)
1793         if (outer->post_landing_pad)
1794           break;
1795
1796       start_sequence ();
1797
1798       if (outer)
1799         {
1800           edge e;
1801           basic_block src, dest;
1802
1803           emit_jump (outer->post_landing_pad);
1804           src = BLOCK_FOR_INSN (region->resume);
1805           dest = BLOCK_FOR_INSN (outer->post_landing_pad);
1806           while (src->succ)
1807             remove_edge (src->succ);
1808           e = make_edge (src, dest, 0);
1809           e->probability = REG_BR_PROB_BASE;
1810           e->count = src->count;
1811         }
1812       else
1813         emit_library_call (unwind_resume_libfunc, LCT_THROW,
1814                            VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1815
1816       seq = get_insns ();
1817       end_sequence ();
1818       barrier = emit_insn_before (seq, region->resume);
1819       /* Avoid duplicate barrier.  */
1820       if (GET_CODE (barrier) != BARRIER)
1821         abort ();
1822       delete_insn (barrier);
1823       delete_insn (region->resume);
1824     }
1825 }
1826
1827 \f
1828 static void
1829 dw2_build_landing_pads (void)
1830 {
1831   int i;
1832   unsigned int j;
1833
1834   for (i = cfun->eh->last_region_number; i > 0; --i)
1835     {
1836       struct eh_region *region = cfun->eh->region_array[i];
1837       rtx seq;
1838       basic_block bb;
1839       bool clobbers_hard_regs = false;
1840       edge e;
1841
1842       /* Mind we don't process a region more than once.  */
1843       if (!region || region->region_number != i)
1844         continue;
1845
1846       if (region->type != ERT_CLEANUP
1847           && region->type != ERT_TRY
1848           && region->type != ERT_ALLOWED_EXCEPTIONS)
1849         continue;
1850
1851       start_sequence ();
1852
1853       region->landing_pad = gen_label_rtx ();
1854       emit_label (region->landing_pad);
1855
1856 #ifdef HAVE_exception_receiver
1857       if (HAVE_exception_receiver)
1858         emit_insn (gen_exception_receiver ());
1859       else
1860 #endif
1861 #ifdef HAVE_nonlocal_goto_receiver
1862         if (HAVE_nonlocal_goto_receiver)
1863           emit_insn (gen_nonlocal_goto_receiver ());
1864         else
1865 #endif
1866           { /* Nothing */ }
1867
1868       /* If the eh_return data registers are call-saved, then we
1869          won't have considered them clobbered from the call that
1870          threw.  Kill them now.  */
1871       for (j = 0; ; ++j)
1872         {
1873           unsigned r = EH_RETURN_DATA_REGNO (j);
1874           if (r == INVALID_REGNUM)
1875             break;
1876           if (! call_used_regs[r])
1877             {
1878               emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1879               clobbers_hard_regs = true;
1880             }
1881         }
1882
1883       if (clobbers_hard_regs)
1884         {
1885           /* @@@ This is a kludge.  Not all machine descriptions define a
1886              blockage insn, but we must not allow the code we just generated
1887              to be reordered by scheduling.  So emit an ASM_INPUT to act as
1888              blockage insn.  */
1889           emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1890         }
1891
1892       emit_move_insn (cfun->eh->exc_ptr,
1893                       gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1894       emit_move_insn (cfun->eh->filter,
1895                       gen_rtx_REG (word_mode, EH_RETURN_DATA_REGNO (1)));
1896
1897       seq = get_insns ();
1898       end_sequence ();
1899
1900       bb = emit_to_new_bb_before (seq, region->post_landing_pad);
1901       e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1902       e->count = bb->count;
1903       e->probability = REG_BR_PROB_BASE;
1904     }
1905 }
1906
1907 \f
1908 struct sjlj_lp_info
1909 {
1910   int directly_reachable;
1911   int action_index;
1912   int dispatch_index;
1913   int call_site_index;
1914 };
1915
1916 static bool
1917 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1918 {
1919   rtx insn;
1920   bool found_one = false;
1921
1922   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1923     {
1924       struct eh_region *region;
1925       enum reachable_code rc;
1926       tree type_thrown;
1927       rtx note;
1928
1929       if (! INSN_P (insn))
1930         continue;
1931
1932       note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1933       if (!note || INTVAL (XEXP (note, 0)) <= 0)
1934         continue;
1935
1936       region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
1937
1938       type_thrown = NULL_TREE;
1939       if (region->type == ERT_THROW)
1940         {
1941           type_thrown = region->u.throw.type;
1942           region = region->outer;
1943         }
1944
1945       /* Find the first containing region that might handle the exception.
1946          That's the landing pad to which we will transfer control.  */
1947       rc = RNL_NOT_CAUGHT;
1948       for (; region; region = region->outer)
1949         {
1950           rc = reachable_next_level (region, type_thrown, 0);
1951           if (rc != RNL_NOT_CAUGHT)
1952             break;
1953         }
1954       if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1955         {
1956           lp_info[region->region_number].directly_reachable = 1;
1957           found_one = true;
1958         }
1959     }
1960
1961   return found_one;
1962 }
1963
1964 static void
1965 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1966 {
1967   htab_t ar_hash;
1968   int i, index;
1969
1970   /* First task: build the action table.  */
1971
1972   VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
1973   ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1974
1975   for (i = cfun->eh->last_region_number; i > 0; --i)
1976     if (lp_info[i].directly_reachable)
1977       {
1978         struct eh_region *r = cfun->eh->region_array[i];
1979         r->landing_pad = dispatch_label;
1980         lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1981         if (lp_info[i].action_index != -1)
1982           cfun->uses_eh_lsda = 1;
1983       }
1984
1985   htab_delete (ar_hash);
1986
1987   /* Next: assign dispatch values.  In dwarf2 terms, this would be the
1988      landing pad label for the region.  For sjlj though, there is one
1989      common landing pad from which we dispatch to the post-landing pads.
1990
1991      A region receives a dispatch index if it is directly reachable
1992      and requires in-function processing.  Regions that share post-landing
1993      pads may share dispatch indices.  */
1994   /* ??? Post-landing pad sharing doesn't actually happen at the moment
1995      (see build_post_landing_pads) so we don't bother checking for it.  */
1996
1997   index = 0;
1998   for (i = cfun->eh->last_region_number; i > 0; --i)
1999     if (lp_info[i].directly_reachable)
2000       lp_info[i].dispatch_index = index++;
2001
2002   /* Finally: assign call-site values.  If dwarf2 terms, this would be
2003      the region number assigned by convert_to_eh_region_ranges, but
2004      handles no-action and must-not-throw differently.  */
2005
2006   call_site_base = 1;
2007   for (i = cfun->eh->last_region_number; i > 0; --i)
2008     if (lp_info[i].directly_reachable)
2009       {
2010         int action = lp_info[i].action_index;
2011
2012         /* Map must-not-throw to otherwise unused call-site index 0.  */
2013         if (action == -2)
2014           index = 0;
2015         /* Map no-action to otherwise unused call-site index -1.  */
2016         else if (action == -1)
2017           index = -1;
2018         /* Otherwise, look it up in the table.  */
2019         else
2020           index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
2021
2022         lp_info[i].call_site_index = index;
2023       }
2024 }
2025
2026 static void
2027 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
2028 {
2029   int last_call_site = -2;
2030   rtx insn, mem;
2031
2032   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
2033     {
2034       struct eh_region *region;
2035       int this_call_site;
2036       rtx note, before, p;
2037
2038       /* Reset value tracking at extended basic block boundaries.  */
2039       if (GET_CODE (insn) == CODE_LABEL)
2040         last_call_site = -2;
2041
2042       if (! INSN_P (insn))
2043         continue;
2044
2045       note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2046       if (!note)
2047         {
2048           /* Calls (and trapping insns) without notes are outside any
2049              exception handling region in this function.  Mark them as
2050              no action.  */
2051           if (GET_CODE (insn) == CALL_INSN
2052               || (flag_non_call_exceptions
2053                   && may_trap_p (PATTERN (insn))))
2054             this_call_site = -1;
2055           else
2056             continue;
2057         }
2058       else
2059         {
2060           /* Calls that are known to not throw need not be marked.  */
2061           if (INTVAL (XEXP (note, 0)) <= 0)
2062             continue;
2063
2064           region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2065           this_call_site = lp_info[region->region_number].call_site_index;
2066         }
2067
2068       if (this_call_site == last_call_site)
2069         continue;
2070
2071       /* Don't separate a call from it's argument loads.  */
2072       before = insn;
2073       if (GET_CODE (insn) == CALL_INSN)
2074         before = find_first_parameter_load (insn, NULL_RTX);
2075
2076       start_sequence ();
2077       mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
2078                             sjlj_fc_call_site_ofs);
2079       emit_move_insn (mem, GEN_INT (this_call_site));
2080       p = get_insns ();
2081       end_sequence ();
2082
2083       emit_insn_before (p, before);
2084       last_call_site = this_call_site;
2085     }
2086 }
2087
2088 /* Construct the SjLj_Function_Context.  */
2089
2090 static void
2091 sjlj_emit_function_enter (rtx dispatch_label)
2092 {
2093   rtx fn_begin, fc, mem, seq;
2094
2095   fc = cfun->eh->sjlj_fc;
2096
2097   start_sequence ();
2098
2099   /* We're storing this libcall's address into memory instead of
2100      calling it directly.  Thus, we must call assemble_external_libcall
2101      here, as we can not depend on emit_library_call to do it for us.  */
2102   assemble_external_libcall (eh_personality_libfunc);
2103   mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
2104   emit_move_insn (mem, eh_personality_libfunc);
2105
2106   mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
2107   if (cfun->uses_eh_lsda)
2108     {
2109       char buf[20];
2110       rtx sym;
2111
2112       ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
2113       sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
2114       SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
2115       emit_move_insn (mem, sym);
2116     }
2117   else
2118     emit_move_insn (mem, const0_rtx);
2119
2120 #ifdef DONT_USE_BUILTIN_SETJMP
2121   {
2122     rtx x, note;
2123     x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
2124                                  TYPE_MODE (integer_type_node), 1,
2125                                  plus_constant (XEXP (fc, 0),
2126                                                 sjlj_fc_jbuf_ofs), Pmode);
2127
2128     note = emit_note (NOTE_INSN_EXPECTED_VALUE);
2129     NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
2130
2131     emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
2132                              TYPE_MODE (integer_type_node), 0, dispatch_label);
2133   }
2134 #else
2135   expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
2136                                dispatch_label);
2137 #endif
2138
2139   emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
2140                      1, XEXP (fc, 0), Pmode);
2141
2142   seq = get_insns ();
2143   end_sequence ();
2144
2145   /* ??? Instead of doing this at the beginning of the function,
2146      do this in a block that is at loop level 0 and dominates all
2147      can_throw_internal instructions.  */
2148
2149   for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
2150     if (GET_CODE (fn_begin) == NOTE
2151         && (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG
2152             || NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_BASIC_BLOCK))
2153       break;
2154   if (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2155     insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
2156   else
2157     {
2158       rtx last = BB_END (ENTRY_BLOCK_PTR->succ->dest);
2159       for (; ; fn_begin = NEXT_INSN (fn_begin))
2160         if ((GET_CODE (fn_begin) == NOTE
2161              && NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2162             || fn_begin == last)
2163           break;
2164       emit_insn_after (seq, fn_begin);
2165     }
2166 }
2167
2168 /* Call back from expand_function_end to know where we should put
2169    the call to unwind_sjlj_unregister_libfunc if needed.  */
2170
2171 void
2172 sjlj_emit_function_exit_after (rtx after)
2173 {
2174   cfun->eh->sjlj_exit_after = after;
2175 }
2176
2177 static void
2178 sjlj_emit_function_exit (void)
2179 {
2180   rtx seq;
2181   edge e;
2182
2183   start_sequence ();
2184
2185   emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2186                      1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
2187
2188   seq = get_insns ();
2189   end_sequence ();
2190
2191   /* ??? Really this can be done in any block at loop level 0 that
2192      post-dominates all can_throw_internal instructions.  This is
2193      the last possible moment.  */
2194
2195   for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
2196     if (e->flags & EDGE_FALLTHRU)
2197       break;
2198   if (e)
2199     {
2200       rtx insn;
2201
2202       /* Figure out whether the place we are supposed to insert libcall
2203          is inside the last basic block or after it.  In the other case
2204          we need to emit to edge.  */
2205       if (e->src->next_bb != EXIT_BLOCK_PTR)
2206         abort ();
2207       for (insn = NEXT_INSN (BB_END (e->src)); insn; insn = NEXT_INSN (insn))
2208         if (insn == cfun->eh->sjlj_exit_after)
2209           break;
2210       if (insn)
2211         insert_insn_on_edge (seq, e);
2212       else
2213         {
2214           insn = cfun->eh->sjlj_exit_after;
2215           if (GET_CODE (insn) == CODE_LABEL)
2216             insn = NEXT_INSN (insn);
2217           emit_insn_after (seq, insn);
2218         }
2219     }
2220 }
2221
2222 static void
2223 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
2224 {
2225   int i, first_reachable;
2226   rtx mem, dispatch, seq, fc;
2227   rtx before;
2228   basic_block bb;
2229   edge e;
2230
2231   fc = cfun->eh->sjlj_fc;
2232
2233   start_sequence ();
2234
2235   emit_label (dispatch_label);
2236
2237 #ifndef DONT_USE_BUILTIN_SETJMP
2238   expand_builtin_setjmp_receiver (dispatch_label);
2239 #endif
2240
2241   /* Load up dispatch index, exc_ptr and filter values from the
2242      function context.  */
2243   mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2244                         sjlj_fc_call_site_ofs);
2245   dispatch = copy_to_reg (mem);
2246
2247   mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
2248   if (word_mode != ptr_mode)
2249     {
2250 #ifdef POINTERS_EXTEND_UNSIGNED
2251       mem = convert_memory_address (ptr_mode, mem);
2252 #else
2253       mem = convert_to_mode (ptr_mode, mem, 0);
2254 #endif
2255     }
2256   emit_move_insn (cfun->eh->exc_ptr, mem);
2257
2258   mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
2259   emit_move_insn (cfun->eh->filter, mem);
2260
2261   /* Jump to one of the directly reachable regions.  */
2262   /* ??? This really ought to be using a switch statement.  */
2263
2264   first_reachable = 0;
2265   for (i = cfun->eh->last_region_number; i > 0; --i)
2266     {
2267       if (! lp_info[i].directly_reachable)
2268         continue;
2269
2270       if (! first_reachable)
2271         {
2272           first_reachable = i;
2273           continue;
2274         }
2275
2276       emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2277                                EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2278                                cfun->eh->region_array[i]->post_landing_pad);
2279     }
2280
2281   seq = get_insns ();
2282   end_sequence ();
2283
2284   before = cfun->eh->region_array[first_reachable]->post_landing_pad;
2285
2286   bb = emit_to_new_bb_before (seq, before);
2287   e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
2288   e->count = bb->count;
2289   e->probability = REG_BR_PROB_BASE;
2290 }
2291
2292 static void
2293 sjlj_build_landing_pads (void)
2294 {
2295   struct sjlj_lp_info *lp_info;
2296
2297   lp_info = xcalloc (cfun->eh->last_region_number + 1,
2298                      sizeof (struct sjlj_lp_info));
2299
2300   if (sjlj_find_directly_reachable_regions (lp_info))
2301     {
2302       rtx dispatch_label = gen_label_rtx ();
2303
2304       cfun->eh->sjlj_fc
2305         = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2306                               int_size_in_bytes (sjlj_fc_type_node),
2307                               TYPE_ALIGN (sjlj_fc_type_node));
2308
2309       sjlj_assign_call_site_values (dispatch_label, lp_info);
2310       sjlj_mark_call_sites (lp_info);
2311
2312       sjlj_emit_function_enter (dispatch_label);
2313       sjlj_emit_dispatch_table (dispatch_label, lp_info);
2314       sjlj_emit_function_exit ();
2315     }
2316
2317   free (lp_info);
2318 }
2319
2320 void
2321 finish_eh_generation (void)
2322 {
2323   basic_block bb;
2324
2325   /* Nothing to do if no regions created.  */
2326   if (cfun->eh->region_tree == NULL)
2327     return;
2328
2329   /* The object here is to provide find_basic_blocks with detailed
2330      information (via reachable_handlers) on how exception control
2331      flows within the function.  In this first pass, we can include
2332      type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2333      regions, and hope that it will be useful in deleting unreachable
2334      handlers.  Subsequently, we will generate landing pads which will
2335      connect many of the handlers, and then type information will not
2336      be effective.  Still, this is a win over previous implementations.  */
2337
2338   cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2339
2340   /* These registers are used by the landing pads.  Make sure they
2341      have been generated.  */
2342   get_exception_pointer (cfun);
2343   get_exception_filter (cfun);
2344
2345   /* Construct the landing pads.  */
2346
2347   assign_filter_values ();
2348   build_post_landing_pads ();
2349   connect_post_landing_pads ();
2350   if (USING_SJLJ_EXCEPTIONS)
2351     sjlj_build_landing_pads ();
2352   else
2353     dw2_build_landing_pads ();
2354
2355   cfun->eh->built_landing_pads = 1;
2356
2357   /* We've totally changed the CFG.  Start over.  */
2358   find_exception_handler_labels ();
2359   break_superblocks ();
2360   if (USING_SJLJ_EXCEPTIONS)
2361     commit_edge_insertions ();
2362   FOR_EACH_BB (bb)
2363     {
2364       edge e, next;
2365       bool eh = false;
2366       for (e = bb->succ; e; e = next)
2367         {
2368           next = e->succ_next;
2369           if (e->flags & EDGE_EH)
2370             {
2371               remove_edge (e);
2372               eh = true;
2373             }
2374         }
2375       if (eh)
2376         make_eh_edge (NULL, bb, BB_END (bb));
2377     }
2378   cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2379 }
2380 \f
2381 static hashval_t
2382 ehl_hash (const void *pentry)
2383 {
2384   struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2385
2386   /* 2^32 * ((sqrt(5) - 1) / 2) */
2387   const hashval_t scaled_golden_ratio = 0x9e3779b9;
2388   return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2389 }
2390
2391 static int
2392 ehl_eq (const void *pentry, const void *pdata)
2393 {
2394   struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2395   struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2396
2397   return entry->label == data->label;
2398 }
2399
2400 /* This section handles removing dead code for flow.  */
2401
2402 /* Remove LABEL from exception_handler_label_map.  */
2403
2404 static void
2405 remove_exception_handler_label (rtx label)
2406 {
2407   struct ehl_map_entry **slot, tmp;
2408
2409   /* If exception_handler_label_map was not built yet,
2410      there is nothing to do.  */
2411   if (cfun->eh->exception_handler_label_map == NULL)
2412     return;
2413
2414   tmp.label = label;
2415   slot = (struct ehl_map_entry **)
2416     htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2417   if (! slot)
2418     abort ();
2419
2420   htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2421 }
2422
2423 /* Splice REGION from the region tree etc.  */
2424
2425 static void
2426 remove_eh_handler (struct eh_region *region)
2427 {
2428   struct eh_region **pp, **pp_start, *p, *outer, *inner;
2429   rtx lab;
2430
2431   /* For the benefit of efficiently handling REG_EH_REGION notes,
2432      replace this region in the region array with its containing
2433      region.  Note that previous region deletions may result in
2434      multiple copies of this region in the array, so we have a
2435      list of alternate numbers by which we are known.  */
2436
2437   outer = region->outer;
2438   cfun->eh->region_array[region->region_number] = outer;
2439   if (region->aka)
2440     {
2441       int i;
2442       EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i,
2443         { cfun->eh->region_array[i] = outer; });
2444     }
2445
2446   if (outer)
2447     {
2448       if (!outer->aka)
2449         outer->aka = BITMAP_GGC_ALLOC ();
2450       if (region->aka)
2451         bitmap_a_or_b (outer->aka, outer->aka, region->aka);
2452       bitmap_set_bit (outer->aka, region->region_number);
2453     }
2454
2455   if (cfun->eh->built_landing_pads)
2456     lab = region->landing_pad;
2457   else
2458     lab = region->label;
2459   if (lab)
2460     remove_exception_handler_label (lab);
2461
2462   if (outer)
2463     pp_start = &outer->inner;
2464   else
2465     pp_start = &cfun->eh->region_tree;
2466   for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2467     continue;
2468   *pp = region->next_peer;
2469
2470   inner = region->inner;
2471   if (inner)
2472     {
2473       for (p = inner; p->next_peer ; p = p->next_peer)
2474         p->outer = outer;
2475       p->outer = outer;
2476
2477       p->next_peer = *pp_start;
2478       *pp_start = inner;
2479     }
2480
2481   if (region->type == ERT_CATCH)
2482     {
2483       struct eh_region *try, *next, *prev;
2484
2485       for (try = region->next_peer;
2486            try->type == ERT_CATCH;
2487            try = try->next_peer)
2488         continue;
2489       if (try->type != ERT_TRY)
2490         abort ();
2491
2492       next = region->u.catch.next_catch;
2493       prev = region->u.catch.prev_catch;
2494
2495       if (next)
2496         next->u.catch.prev_catch = prev;
2497       else
2498         try->u.try.last_catch = prev;
2499       if (prev)
2500         prev->u.catch.next_catch = next;
2501       else
2502         {
2503           try->u.try.catch = next;
2504           if (! next)
2505             remove_eh_handler (try);
2506         }
2507     }
2508 }
2509
2510 /* LABEL heads a basic block that is about to be deleted.  If this
2511    label corresponds to an exception region, we may be able to
2512    delete the region.  */
2513
2514 void
2515 maybe_remove_eh_handler (rtx label)
2516 {
2517   struct ehl_map_entry **slot, tmp;
2518   struct eh_region *region;
2519
2520   /* ??? After generating landing pads, it's not so simple to determine
2521      if the region data is completely unused.  One must examine the
2522      landing pad and the post landing pad, and whether an inner try block
2523      is referencing the catch handlers directly.  */
2524   if (cfun->eh->built_landing_pads)
2525     return;
2526
2527   tmp.label = label;
2528   slot = (struct ehl_map_entry **)
2529     htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2530   if (! slot)
2531     return;
2532   region = (*slot)->region;
2533   if (! region)
2534     return;
2535
2536   /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2537      because there is no path to the fallback call to terminate.
2538      But the region continues to affect call-site data until there
2539      are no more contained calls, which we don't see here.  */
2540   if (region->type == ERT_MUST_NOT_THROW)
2541     {
2542       htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2543       region->label = NULL_RTX;
2544     }
2545   else
2546     remove_eh_handler (region);
2547 }
2548
2549 /* Invokes CALLBACK for every exception handler label.  Only used by old
2550    loop hackery; should not be used by new code.  */
2551
2552 void
2553 for_each_eh_label (void (*callback) (rtx))
2554 {
2555   htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2556                  (void *) &callback);
2557 }
2558
2559 static int
2560 for_each_eh_label_1 (void **pentry, void *data)
2561 {
2562   struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2563   void (*callback) (rtx) = *(void (**) (rtx)) data;
2564
2565   (*callback) (entry->label);
2566   return 1;
2567 }
2568 \f
2569 /* This section describes CFG exception edges for flow.  */
2570
2571 /* For communicating between calls to reachable_next_level.  */
2572 struct reachable_info GTY(())
2573 {
2574   tree types_caught;
2575   tree types_allowed;
2576   rtx handlers;
2577 };
2578
2579 /* A subroutine of reachable_next_level.  Return true if TYPE, or a
2580    base class of TYPE, is in HANDLED.  */
2581
2582 static int
2583 check_handled (tree handled, tree type)
2584 {
2585   tree t;
2586
2587   /* We can check for exact matches without front-end help.  */
2588   if (! lang_eh_type_covers)
2589     {
2590       for (t = handled; t ; t = TREE_CHAIN (t))
2591         if (TREE_VALUE (t) == type)
2592           return 1;
2593     }
2594   else
2595     {
2596       for (t = handled; t ; t = TREE_CHAIN (t))
2597         if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2598           return 1;
2599     }
2600
2601   return 0;
2602 }
2603
2604 /* A subroutine of reachable_next_level.  If we are collecting a list
2605    of handlers, add one.  After landing pad generation, reference
2606    it instead of the handlers themselves.  Further, the handlers are
2607    all wired together, so by referencing one, we've got them all.
2608    Before landing pad generation we reference each handler individually.
2609
2610    LP_REGION contains the landing pad; REGION is the handler.  */
2611
2612 static void
2613 add_reachable_handler (struct reachable_info *info, struct eh_region *lp_region, struct eh_region *region)
2614 {
2615   if (! info)
2616     return;
2617
2618   if (cfun->eh->built_landing_pads)
2619     {
2620       if (! info->handlers)
2621         info->handlers = alloc_INSN_LIST (lp_region->landing_pad, NULL_RTX);
2622     }
2623   else
2624     info->handlers = alloc_INSN_LIST (region->label, info->handlers);
2625 }
2626
2627 /* Process one level of exception regions for reachability.
2628    If TYPE_THROWN is non-null, then it is the *exact* type being
2629    propagated.  If INFO is non-null, then collect handler labels
2630    and caught/allowed type information between invocations.  */
2631
2632 static enum reachable_code
2633 reachable_next_level (struct eh_region *region, tree type_thrown,
2634                       struct reachable_info *info)
2635 {
2636   switch (region->type)
2637     {
2638     case ERT_CLEANUP:
2639       /* Before landing-pad generation, we model control flow
2640          directly to the individual handlers.  In this way we can
2641          see that catch handler types may shadow one another.  */
2642       add_reachable_handler (info, region, region);
2643       return RNL_MAYBE_CAUGHT;
2644
2645     case ERT_TRY:
2646       {
2647         struct eh_region *c;
2648         enum reachable_code ret = RNL_NOT_CAUGHT;
2649
2650         for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2651           {
2652             /* A catch-all handler ends the search.  */
2653             if (c->u.catch.type_list == NULL)
2654               {
2655                 add_reachable_handler (info, region, c);
2656                 return RNL_CAUGHT;
2657               }
2658
2659             if (type_thrown)
2660               {
2661                 /* If we have at least one type match, end the search.  */
2662                 tree tp_node = c->u.catch.type_list;
2663
2664                 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2665                   {
2666                     tree type = TREE_VALUE (tp_node);
2667
2668                     if (type == type_thrown
2669                         || (lang_eh_type_covers
2670                             && (*lang_eh_type_covers) (type, type_thrown)))
2671                       {
2672                         add_reachable_handler (info, region, c);
2673                         return RNL_CAUGHT;
2674                       }
2675                   }
2676
2677                 /* If we have definitive information of a match failure,
2678                    the catch won't trigger.  */
2679                 if (lang_eh_type_covers)
2680                   return RNL_NOT_CAUGHT;
2681               }
2682
2683             /* At this point, we either don't know what type is thrown or
2684                don't have front-end assistance to help deciding if it is
2685                covered by one of the types in the list for this region.
2686
2687                We'd then like to add this region to the list of reachable
2688                handlers since it is indeed potentially reachable based on the
2689                information we have.
2690
2691                Actually, this handler is for sure not reachable if all the
2692                types it matches have already been caught. That is, it is only
2693                potentially reachable if at least one of the types it catches
2694                has not been previously caught.  */
2695
2696             if (! info)
2697               ret = RNL_MAYBE_CAUGHT;
2698             else
2699               {
2700                 tree tp_node = c->u.catch.type_list;
2701                 bool maybe_reachable = false;
2702
2703                 /* Compute the potential reachability of this handler and
2704                    update the list of types caught at the same time.  */
2705                 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2706                   {
2707                     tree type = TREE_VALUE (tp_node);
2708
2709                     if (! check_handled (info->types_caught, type))
2710                       {
2711                         info->types_caught
2712                           = tree_cons (NULL, type, info->types_caught);
2713
2714                         maybe_reachable = true;
2715                       }
2716                   }
2717
2718                 if (maybe_reachable)
2719                   {
2720                     add_reachable_handler (info, region, c);
2721
2722                     /* ??? If the catch type is a base class of every allowed
2723                        type, then we know we can stop the search.  */
2724                     ret = RNL_MAYBE_CAUGHT;
2725                   }
2726               }
2727           }
2728
2729         return ret;
2730       }
2731
2732     case ERT_ALLOWED_EXCEPTIONS:
2733       /* An empty list of types definitely ends the search.  */
2734       if (region->u.allowed.type_list == NULL_TREE)
2735         {
2736           add_reachable_handler (info, region, region);
2737           return RNL_CAUGHT;
2738         }
2739
2740       /* Collect a list of lists of allowed types for use in detecting
2741          when a catch may be transformed into a catch-all.  */
2742       if (info)
2743         info->types_allowed = tree_cons (NULL_TREE,
2744                                          region->u.allowed.type_list,
2745                                          info->types_allowed);
2746
2747       /* If we have definitive information about the type hierarchy,
2748          then we can tell if the thrown type will pass through the
2749          filter.  */
2750       if (type_thrown && lang_eh_type_covers)
2751         {
2752           if (check_handled (region->u.allowed.type_list, type_thrown))
2753             return RNL_NOT_CAUGHT;
2754           else
2755             {
2756               add_reachable_handler (info, region, region);
2757               return RNL_CAUGHT;
2758             }
2759         }
2760
2761       add_reachable_handler (info, region, region);
2762       return RNL_MAYBE_CAUGHT;
2763
2764     case ERT_CATCH:
2765       /* Catch regions are handled by their controlling try region.  */
2766       return RNL_NOT_CAUGHT;
2767
2768     case ERT_MUST_NOT_THROW:
2769       /* Here we end our search, since no exceptions may propagate.
2770          If we've touched down at some landing pad previous, then the
2771          explicit function call we generated may be used.  Otherwise
2772          the call is made by the runtime.  */
2773       if (info && info->handlers)
2774         {
2775           add_reachable_handler (info, region, region);
2776           return RNL_CAUGHT;
2777         }
2778       else
2779         return RNL_BLOCKED;
2780
2781     case ERT_THROW:
2782     case ERT_FIXUP:
2783     case ERT_UNKNOWN:
2784       /* Shouldn't see these here.  */
2785       break;
2786     }
2787
2788   abort ();
2789 }
2790
2791 /* Retrieve a list of labels of exception handlers which can be
2792    reached by a given insn.  */
2793
2794 rtx
2795 reachable_handlers (rtx insn)
2796 {
2797   struct reachable_info info;
2798   struct eh_region *region;
2799   tree type_thrown;
2800   int region_number;
2801
2802   if (GET_CODE (insn) == JUMP_INSN
2803       && GET_CODE (PATTERN (insn)) == RESX)
2804     region_number = XINT (PATTERN (insn), 0);
2805   else
2806     {
2807       rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2808       if (!note || INTVAL (XEXP (note, 0)) <= 0)
2809         return NULL;
2810       region_number = INTVAL (XEXP (note, 0));
2811     }
2812
2813   memset (&info, 0, sizeof (info));
2814
2815   region = cfun->eh->region_array[region_number];
2816
2817   type_thrown = NULL_TREE;
2818   if (GET_CODE (insn) == JUMP_INSN
2819       && GET_CODE (PATTERN (insn)) == RESX)
2820     {
2821       /* A RESX leaves a region instead of entering it.  Thus the
2822          region itself may have been deleted out from under us.  */
2823       if (region == NULL)
2824         return NULL;
2825       region = region->outer;
2826     }
2827   else if (region->type == ERT_THROW)
2828     {
2829       type_thrown = region->u.throw.type;
2830       region = region->outer;
2831     }
2832
2833   while (region)
2834     {
2835       if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2836         break;
2837       /* If we have processed one cleanup, there is no point in
2838          processing any more of them.  Each cleanup will have an edge
2839          to the next outer cleanup region, so the flow graph will be
2840          accurate.  */
2841       if (region->type == ERT_CLEANUP)
2842         region = region->u.cleanup.prev_try;
2843       else
2844         region = region->outer;
2845     }
2846
2847   return info.handlers;
2848 }
2849
2850 /* Determine if the given INSN can throw an exception that is caught
2851    within the function.  */
2852
2853 bool
2854 can_throw_internal (rtx insn)
2855 {
2856   struct eh_region *region;
2857   tree type_thrown;
2858   rtx note;
2859
2860   if (! INSN_P (insn))
2861     return false;
2862
2863   if (GET_CODE (insn) == INSN
2864       && GET_CODE (PATTERN (insn)) == SEQUENCE)
2865     insn = XVECEXP (PATTERN (insn), 0, 0);
2866
2867   if (GET_CODE (insn) == CALL_INSN
2868       && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2869     {
2870       int i;
2871       for (i = 0; i < 3; ++i)
2872         {
2873           rtx sub = XEXP (PATTERN (insn), i);
2874           for (; sub ; sub = NEXT_INSN (sub))
2875             if (can_throw_internal (sub))
2876               return true;
2877         }
2878       return false;
2879     }
2880
2881   /* Every insn that might throw has an EH_REGION note.  */
2882   note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2883   if (!note || INTVAL (XEXP (note, 0)) <= 0)
2884     return false;
2885
2886   region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2887
2888   type_thrown = NULL_TREE;
2889   if (region->type == ERT_THROW)
2890     {
2891       type_thrown = region->u.throw.type;
2892       region = region->outer;
2893     }
2894
2895   /* If this exception is ignored by each and every containing region,
2896      then control passes straight out.  The runtime may handle some
2897      regions, which also do not require processing internally.  */
2898   for (; region; region = region->outer)
2899     {
2900       enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2901       if (how == RNL_BLOCKED)
2902         return false;
2903       if (how != RNL_NOT_CAUGHT)
2904         return true;
2905     }
2906
2907   return false;
2908 }
2909
2910 /* Determine if the given INSN can throw an exception that is
2911    visible outside the function.  */
2912
2913 bool
2914 can_throw_external (rtx insn)
2915 {
2916   struct eh_region *region;
2917   tree type_thrown;
2918   rtx note;
2919
2920   if (! INSN_P (insn))
2921     return false;
2922
2923   if (GET_CODE (insn) == INSN
2924       && GET_CODE (PATTERN (insn)) == SEQUENCE)
2925     insn = XVECEXP (PATTERN (insn), 0, 0);
2926
2927   if (GET_CODE (insn) == CALL_INSN
2928       && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2929     {
2930       int i;
2931       for (i = 0; i < 3; ++i)
2932         {
2933           rtx sub = XEXP (PATTERN (insn), i);
2934           for (; sub ; sub = NEXT_INSN (sub))
2935             if (can_throw_external (sub))
2936               return true;
2937         }
2938       return false;
2939     }
2940
2941   note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2942   if (!note)
2943     {
2944       /* Calls (and trapping insns) without notes are outside any
2945          exception handling region in this function.  We have to
2946          assume it might throw.  Given that the front end and middle
2947          ends mark known NOTHROW functions, this isn't so wildly
2948          inaccurate.  */
2949       return (GET_CODE (insn) == CALL_INSN
2950               || (flag_non_call_exceptions
2951                   && may_trap_p (PATTERN (insn))));
2952     }
2953   if (INTVAL (XEXP (note, 0)) <= 0)
2954     return false;
2955
2956   region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2957
2958   type_thrown = NULL_TREE;
2959   if (region->type == ERT_THROW)
2960     {
2961       type_thrown = region->u.throw.type;
2962       region = region->outer;
2963     }
2964
2965   /* If the exception is caught or blocked by any containing region,
2966      then it is not seen by any calling function.  */
2967   for (; region ; region = region->outer)
2968     if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2969       return false;
2970
2971   return true;
2972 }
2973
2974 /* Set current_function_nothrow and cfun->all_throwers_are_sibcalls.  */
2975
2976 void
2977 set_nothrow_function_flags (void)
2978 {
2979   rtx insn;
2980
2981   current_function_nothrow = 1;
2982
2983   /* Assume cfun->all_throwers_are_sibcalls until we encounter
2984      something that can throw an exception.  We specifically exempt
2985      CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2986      and can't throw.  Most CALL_INSNs are not SIBLING_CALL_P, so this
2987      is optimistic.  */
2988
2989   cfun->all_throwers_are_sibcalls = 1;
2990
2991   if (! flag_exceptions)
2992     return;
2993
2994   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2995     if (can_throw_external (insn))
2996       {
2997         current_function_nothrow = 0;
2998
2999         if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
3000           {
3001             cfun->all_throwers_are_sibcalls = 0;
3002             return;
3003           }
3004       }
3005
3006   for (insn = current_function_epilogue_delay_list; insn;
3007        insn = XEXP (insn, 1))
3008     if (can_throw_external (insn))
3009       {
3010         current_function_nothrow = 0;
3011
3012         if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
3013           {
3014             cfun->all_throwers_are_sibcalls = 0;
3015             return;
3016           }
3017       }
3018 }
3019
3020 \f
3021 /* Various hooks for unwind library.  */
3022
3023 /* Do any necessary initialization to access arbitrary stack frames.
3024    On the SPARC, this means flushing the register windows.  */
3025
3026 void
3027 expand_builtin_unwind_init (void)
3028 {
3029   /* Set this so all the registers get saved in our frame; we need to be
3030      able to copy the saved values for any registers from frames we unwind.  */
3031   current_function_has_nonlocal_label = 1;
3032
3033 #ifdef SETUP_FRAME_ADDRESSES
3034   SETUP_FRAME_ADDRESSES ();
3035 #endif
3036 }
3037
3038 rtx
3039 expand_builtin_eh_return_data_regno (tree arglist)
3040 {
3041   tree which = TREE_VALUE (arglist);
3042   unsigned HOST_WIDE_INT iwhich;
3043
3044   if (TREE_CODE (which) != INTEGER_CST)
3045     {
3046       error ("argument of `__builtin_eh_return_regno' must be constant");
3047       return constm1_rtx;
3048     }
3049
3050   iwhich = tree_low_cst (which, 1);
3051   iwhich = EH_RETURN_DATA_REGNO (iwhich);
3052   if (iwhich == INVALID_REGNUM)
3053     return constm1_rtx;
3054
3055 #ifdef DWARF_FRAME_REGNUM
3056   iwhich = DWARF_FRAME_REGNUM (iwhich);
3057 #else
3058   iwhich = DBX_REGISTER_NUMBER (iwhich);
3059 #endif
3060
3061   return GEN_INT (iwhich);
3062 }
3063
3064 /* Given a value extracted from the return address register or stack slot,
3065    return the actual address encoded in that value.  */
3066
3067 rtx
3068 expand_builtin_extract_return_addr (tree addr_tree)
3069 {
3070   rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
3071
3072   if (GET_MODE (addr) != Pmode
3073       && GET_MODE (addr) != VOIDmode)
3074     {
3075 #ifdef POINTERS_EXTEND_UNSIGNED
3076       addr = convert_memory_address (Pmode, addr);
3077 #else
3078       addr = convert_to_mode (Pmode, addr, 0);
3079 #endif
3080     }
3081
3082   /* First mask out any unwanted bits.  */
3083 #ifdef MASK_RETURN_ADDR
3084   expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
3085 #endif
3086
3087   /* Then adjust to find the real return address.  */
3088 #if defined (RETURN_ADDR_OFFSET)
3089   addr = plus_constant (addr, RETURN_ADDR_OFFSET);
3090 #endif
3091
3092   return addr;
3093 }
3094
3095 /* Given an actual address in addr_tree, do any necessary encoding
3096    and return the value to be stored in the return address register or
3097    stack slot so the epilogue will return to that address.  */
3098
3099 rtx
3100 expand_builtin_frob_return_addr (tree addr_tree)
3101 {
3102   rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
3103
3104   addr = convert_memory_address (Pmode, addr);
3105
3106 #ifdef RETURN_ADDR_OFFSET
3107   addr = force_reg (Pmode, addr);
3108   addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
3109 #endif
3110
3111   return addr;
3112 }
3113
3114 /* Set up the epilogue with the magic bits we'll need to return to the
3115    exception handler.  */
3116
3117 void
3118 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
3119                           tree handler_tree)
3120 {
3121   rtx tmp;
3122
3123 #ifdef EH_RETURN_STACKADJ_RTX
3124   tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
3125   tmp = convert_memory_address (Pmode, tmp);
3126   if (!cfun->eh->ehr_stackadj)
3127     cfun->eh->ehr_stackadj = copy_to_reg (tmp);
3128   else if (tmp != cfun->eh->ehr_stackadj)
3129     emit_move_insn (cfun->eh->ehr_stackadj, tmp);
3130 #endif
3131
3132   tmp = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
3133   tmp = convert_memory_address (Pmode, tmp);
3134   if (!cfun->eh->ehr_handler)
3135     cfun->eh->ehr_handler = copy_to_reg (tmp);
3136   else if (tmp != cfun->eh->ehr_handler)
3137     emit_move_insn (cfun->eh->ehr_handler, tmp);
3138
3139   if (!cfun->eh->ehr_label)
3140     cfun->eh->ehr_label = gen_label_rtx ();
3141   emit_jump (cfun->eh->ehr_label);
3142 }
3143
3144 void
3145 expand_eh_return (void)
3146 {
3147   rtx around_label;
3148
3149   if (! cfun->eh->ehr_label)
3150     return;
3151
3152   current_function_calls_eh_return = 1;
3153
3154 #ifdef EH_RETURN_STACKADJ_RTX
3155   emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
3156 #endif
3157
3158   around_label = gen_label_rtx ();
3159   emit_jump (around_label);
3160
3161   emit_label (cfun->eh->ehr_label);
3162   clobber_return_register ();
3163
3164 #ifdef EH_RETURN_STACKADJ_RTX
3165   emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
3166 #endif
3167
3168 #ifdef HAVE_eh_return
3169   if (HAVE_eh_return)
3170     emit_insn (gen_eh_return (cfun->eh->ehr_handler));
3171   else
3172 #endif
3173     {
3174 #ifdef EH_RETURN_HANDLER_RTX
3175       emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
3176 #else
3177       error ("__builtin_eh_return not supported on this target");
3178 #endif
3179     }
3180
3181   emit_label (around_label);
3182 }
3183
3184 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
3185    POINTERS_EXTEND_UNSIGNED and return it.  */
3186
3187 rtx
3188 expand_builtin_extend_pointer (tree addr_tree)
3189 {
3190   rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
3191   int extend;
3192
3193 #ifdef POINTERS_EXTEND_UNSIGNED
3194   extend = POINTERS_EXTEND_UNSIGNED;
3195 #else
3196   /* The previous EH code did an unsigned extend by default, so we do this also
3197      for consistency.  */
3198   extend = 1;
3199 #endif
3200
3201   return convert_modes (word_mode, ptr_mode, addr, extend);
3202 }
3203 \f
3204 /* In the following functions, we represent entries in the action table
3205    as 1-based indices.  Special cases are:
3206
3207          0:     null action record, non-null landing pad; implies cleanups
3208         -1:     null action record, null landing pad; implies no action
3209         -2:     no call-site entry; implies must_not_throw
3210         -3:     we have yet to process outer regions
3211
3212    Further, no special cases apply to the "next" field of the record.
3213    For next, 0 means end of list.  */
3214
3215 struct action_record
3216 {
3217   int offset;
3218   int filter;
3219   int next;
3220 };
3221
3222 static int
3223 action_record_eq (const void *pentry, const void *pdata)
3224 {
3225   const struct action_record *entry = (const struct action_record *) pentry;
3226   const struct action_record *data = (const struct action_record *) pdata;
3227   return entry->filter == data->filter && entry->next == data->next;
3228 }
3229
3230 static hashval_t
3231 action_record_hash (const void *pentry)
3232 {
3233   const struct action_record *entry = (const struct action_record *) pentry;
3234   return entry->next * 1009 + entry->filter;
3235 }
3236
3237 static int
3238 add_action_record (htab_t ar_hash, int filter, int next)
3239 {
3240   struct action_record **slot, *new, tmp;
3241
3242   tmp.filter = filter;
3243   tmp.next = next;
3244   slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3245
3246   if ((new = *slot) == NULL)
3247     {
3248       new = xmalloc (sizeof (*new));
3249       new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3250       new->filter = filter;
3251       new->next = next;
3252       *slot = new;
3253
3254       /* The filter value goes in untouched.  The link to the next
3255          record is a "self-relative" byte offset, or zero to indicate
3256          that there is no next record.  So convert the absolute 1 based
3257          indices we've been carrying around into a displacement.  */
3258
3259       push_sleb128 (&cfun->eh->action_record_data, filter);
3260       if (next)
3261         next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3262       push_sleb128 (&cfun->eh->action_record_data, next);
3263     }
3264
3265   return new->offset;
3266 }
3267
3268 static int
3269 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3270 {
3271   struct eh_region *c;
3272   int next;
3273
3274   /* If we've reached the top of the region chain, then we have
3275      no actions, and require no landing pad.  */
3276   if (region == NULL)
3277     return -1;
3278
3279   switch (region->type)
3280     {
3281     case ERT_CLEANUP:
3282       /* A cleanup adds a zero filter to the beginning of the chain, but
3283          there are special cases to look out for.  If there are *only*
3284          cleanups along a path, then it compresses to a zero action.
3285          Further, if there are multiple cleanups along a path, we only
3286          need to represent one of them, as that is enough to trigger
3287          entry to the landing pad at runtime.  */
3288       next = collect_one_action_chain (ar_hash, region->outer);
3289       if (next <= 0)
3290         return 0;
3291       for (c = region->outer; c ; c = c->outer)
3292         if (c->type == ERT_CLEANUP)
3293           return next;
3294       return add_action_record (ar_hash, 0, next);
3295
3296     case ERT_TRY:
3297       /* Process the associated catch regions in reverse order.
3298          If there's a catch-all handler, then we don't need to
3299          search outer regions.  Use a magic -3 value to record
3300          that we haven't done the outer search.  */
3301       next = -3;
3302       for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3303         {
3304           if (c->u.catch.type_list == NULL)
3305             {
3306               /* Retrieve the filter from the head of the filter list
3307                  where we have stored it (see assign_filter_values).  */
3308               int filter
3309                 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3310
3311               next = add_action_record (ar_hash, filter, 0);
3312             }
3313           else
3314             {
3315               /* Once the outer search is done, trigger an action record for
3316                  each filter we have.  */
3317               tree flt_node;
3318
3319               if (next == -3)
3320                 {
3321                   next = collect_one_action_chain (ar_hash, region->outer);
3322
3323                   /* If there is no next action, terminate the chain.  */
3324                   if (next == -1)
3325                     next = 0;
3326                   /* If all outer actions are cleanups or must_not_throw,
3327                      we'll have no action record for it, since we had wanted
3328                      to encode these states in the call-site record directly.
3329                      Add a cleanup action to the chain to catch these.  */
3330                   else if (next <= 0)
3331                     next = add_action_record (ar_hash, 0, 0);
3332                 }
3333
3334               flt_node = c->u.catch.filter_list;
3335               for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3336                 {
3337                   int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3338                   next = add_action_record (ar_hash, filter, next);
3339                 }
3340             }
3341         }
3342       return next;
3343
3344     case ERT_ALLOWED_EXCEPTIONS:
3345       /* An exception specification adds its filter to the
3346          beginning of the chain.  */
3347       next = collect_one_action_chain (ar_hash, region->outer);
3348       return add_action_record (ar_hash, region->u.allowed.filter,
3349                                 next < 0 ? 0 : next);
3350
3351     case ERT_MUST_NOT_THROW:
3352       /* A must-not-throw region with no inner handlers or cleanups
3353          requires no call-site entry.  Note that this differs from
3354          the no handler or cleanup case in that we do require an lsda
3355          to be generated.  Return a magic -2 value to record this.  */
3356       return -2;
3357
3358     case ERT_CATCH:
3359     case ERT_THROW:
3360       /* CATCH regions are handled in TRY above.  THROW regions are
3361          for optimization information only and produce no output.  */
3362       return collect_one_action_chain (ar_hash, region->outer);
3363
3364     default:
3365       abort ();
3366     }
3367 }
3368
3369 static int
3370 add_call_site (rtx landing_pad, int action)
3371 {
3372   struct call_site_record *data = cfun->eh->call_site_data;
3373   int used = cfun->eh->call_site_data_used;
3374   int size = cfun->eh->call_site_data_size;
3375
3376   if (used >= size)
3377     {
3378       size = (size ? size * 2 : 64);
3379       data = ggc_realloc (data, sizeof (*data) * size);
3380       cfun->eh->call_site_data = data;
3381       cfun->eh->call_site_data_size = size;
3382     }
3383
3384   data[used].landing_pad = landing_pad;
3385   data[used].action = action;
3386
3387   cfun->eh->call_site_data_used = used + 1;
3388
3389   return used + call_site_base;
3390 }
3391
3392 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3393    The new note numbers will not refer to region numbers, but
3394    instead to call site entries.  */
3395
3396 void
3397 convert_to_eh_region_ranges (void)
3398 {
3399   rtx insn, iter, note;
3400   htab_t ar_hash;
3401   int last_action = -3;
3402   rtx last_action_insn = NULL_RTX;
3403   rtx last_landing_pad = NULL_RTX;
3404   rtx first_no_action_insn = NULL_RTX;
3405   int call_site = 0;
3406
3407   if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3408     return;
3409
3410   VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3411
3412   ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3413
3414   for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3415     if (INSN_P (iter))
3416       {
3417         struct eh_region *region;
3418         int this_action;
3419         rtx this_landing_pad;
3420
3421         insn = iter;
3422         if (GET_CODE (insn) == INSN
3423             && GET_CODE (PATTERN (insn)) == SEQUENCE)
3424           insn = XVECEXP (PATTERN (insn), 0, 0);
3425
3426         note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3427         if (!note)
3428           {
3429             if (! (GET_CODE (insn) == CALL_INSN
3430                    || (flag_non_call_exceptions
3431                        && may_trap_p (PATTERN (insn)))))
3432               continue;
3433             this_action = -1;
3434             region = NULL;
3435           }
3436         else
3437           {
3438             if (INTVAL (XEXP (note, 0)) <= 0)
3439               continue;
3440             region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
3441             this_action = collect_one_action_chain (ar_hash, region);
3442           }
3443
3444         /* Existence of catch handlers, or must-not-throw regions
3445            implies that an lsda is needed (even if empty).  */
3446         if (this_action != -1)
3447           cfun->uses_eh_lsda = 1;
3448
3449         /* Delay creation of region notes for no-action regions
3450            until we're sure that an lsda will be required.  */
3451         else if (last_action == -3)
3452           {
3453             first_no_action_insn = iter;
3454             last_action = -1;
3455           }
3456
3457         /* Cleanups and handlers may share action chains but not
3458            landing pads.  Collect the landing pad for this region.  */
3459         if (this_action >= 0)
3460           {
3461             struct eh_region *o;
3462             for (o = region; ! o->landing_pad ; o = o->outer)
3463               continue;
3464             this_landing_pad = o->landing_pad;
3465           }
3466         else
3467           this_landing_pad = NULL_RTX;
3468
3469         /* Differing actions or landing pads implies a change in call-site
3470            info, which implies some EH_REGION note should be emitted.  */
3471         if (last_action != this_action
3472             || last_landing_pad != this_landing_pad)
3473           {
3474             /* If we'd not seen a previous action (-3) or the previous
3475                action was must-not-throw (-2), then we do not need an
3476                end note.  */
3477             if (last_action >= -1)
3478               {
3479                 /* If we delayed the creation of the begin, do it now.  */
3480                 if (first_no_action_insn)
3481                   {
3482                     call_site = add_call_site (NULL_RTX, 0);
3483                     note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3484                                              first_no_action_insn);
3485                     NOTE_EH_HANDLER (note) = call_site;
3486                     first_no_action_insn = NULL_RTX;
3487                   }
3488
3489                 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3490                                         last_action_insn);
3491                 NOTE_EH_HANDLER (note) = call_site;
3492               }
3493
3494             /* If the new action is must-not-throw, then no region notes
3495                are created.  */
3496             if (this_action >= -1)
3497               {
3498                 call_site = add_call_site (this_landing_pad,
3499                                            this_action < 0 ? 0 : this_action);
3500                 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3501                 NOTE_EH_HANDLER (note) = call_site;
3502               }
3503
3504             last_action = this_action;
3505             last_landing_pad = this_landing_pad;
3506           }
3507         last_action_insn = iter;
3508       }
3509
3510   if (last_action >= -1 && ! first_no_action_insn)
3511     {
3512       note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3513       NOTE_EH_HANDLER (note) = call_site;
3514     }
3515
3516   htab_delete (ar_hash);
3517 }
3518
3519 \f
3520 static void
3521 push_uleb128 (varray_type *data_area, unsigned int value)
3522 {
3523   do
3524     {
3525       unsigned char byte = value & 0x7f;
3526       value >>= 7;
3527       if (value)
3528         byte |= 0x80;
3529       VARRAY_PUSH_UCHAR (*data_area, byte);
3530     }
3531   while (value);
3532 }
3533
3534 static void
3535 push_sleb128 (varray_type *data_area, int value)
3536 {
3537   unsigned char byte;
3538   int more;
3539
3540   do
3541     {
3542       byte = value & 0x7f;
3543       value >>= 7;
3544       more = ! ((value == 0 && (byte & 0x40) == 0)
3545                 || (value == -1 && (byte & 0x40) != 0));
3546       if (more)
3547         byte |= 0x80;
3548       VARRAY_PUSH_UCHAR (*data_area, byte);
3549     }
3550   while (more);
3551 }
3552
3553 \f
3554 #ifndef HAVE_AS_LEB128
3555 static int
3556 dw2_size_of_call_site_table (void)
3557 {
3558   int n = cfun->eh->call_site_data_used;
3559   int size = n * (4 + 4 + 4);
3560   int i;
3561
3562   for (i = 0; i < n; ++i)
3563     {
3564       struct call_site_record *cs = &cfun->eh->call_site_data[i];
3565       size += size_of_uleb128 (cs->action);
3566     }
3567
3568   return size;
3569 }
3570
3571 static int
3572 sjlj_size_of_call_site_table (void)
3573 {
3574   int n = cfun->eh->call_site_data_used;
3575   int size = 0;
3576   int i;
3577
3578   for (i = 0; i < n; ++i)
3579     {
3580       struct call_site_record *cs = &cfun->eh->call_site_data[i];
3581       size += size_of_uleb128 (INTVAL (cs->landing_pad));
3582       size += size_of_uleb128 (cs->action);
3583     }
3584
3585   return size;
3586 }
3587 #endif
3588
3589 static void
3590 dw2_output_call_site_table (void)
3591 {
3592   const char *const function_start_lab
3593     = IDENTIFIER_POINTER (current_function_func_begin_label);
3594   int n = cfun->eh->call_site_data_used;
3595   int i;
3596
3597   for (i = 0; i < n; ++i)
3598     {
3599       struct call_site_record *cs = &cfun->eh->call_site_data[i];
3600       char reg_start_lab[32];
3601       char reg_end_lab[32];
3602       char landing_pad_lab[32];
3603
3604       ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3605       ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3606
3607       if (cs->landing_pad)
3608         ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3609                                      CODE_LABEL_NUMBER (cs->landing_pad));
3610
3611       /* ??? Perhaps use insn length scaling if the assembler supports
3612          generic arithmetic.  */
3613       /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3614          data4 if the function is small enough.  */
3615 #ifdef HAVE_AS_LEB128
3616       dw2_asm_output_delta_uleb128 (reg_start_lab, function_start_lab,
3617                                     "region %d start", i);
3618       dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3619                                     "length");
3620       if (cs->landing_pad)
3621         dw2_asm_output_delta_uleb128 (landing_pad_lab, function_start_lab,
3622                                       "landing pad");
3623       else
3624         dw2_asm_output_data_uleb128 (0, "landing pad");
3625 #else
3626       dw2_asm_output_delta (4, reg_start_lab, function_start_lab,
3627                             "region %d start", i);
3628       dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3629       if (cs->landing_pad)
3630         dw2_asm_output_delta (4, landing_pad_lab, function_start_lab,
3631                               "landing pad");
3632       else
3633         dw2_asm_output_data (4, 0, "landing pad");
3634 #endif
3635       dw2_asm_output_data_uleb128 (cs->action, "action");
3636     }
3637
3638   call_site_base += n;
3639 }
3640
3641 static void
3642 sjlj_output_call_site_table (void)
3643 {
3644   int n = cfun->eh->call_site_data_used;
3645   int i;
3646
3647   for (i = 0; i < n; ++i)
3648     {
3649       struct call_site_record *cs = &cfun->eh->call_site_data[i];
3650
3651       dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3652                                    "region %d landing pad", i);
3653       dw2_asm_output_data_uleb128 (cs->action, "action");
3654     }
3655
3656   call_site_base += n;
3657 }
3658
3659 /* Tell assembler to switch to the section for the exception handling
3660    table.  */
3661
3662 void
3663 default_exception_section (void)
3664 {
3665   if (targetm.have_named_sections)
3666     {
3667       int flags;
3668 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3669       int tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3670
3671       flags = (! flag_pic
3672                || ((tt_format & 0x70) != DW_EH_PE_absptr
3673                    && (tt_format & 0x70) != DW_EH_PE_aligned))
3674               ? 0 : SECTION_WRITE;
3675 #else
3676       flags = SECTION_WRITE;
3677 #endif
3678       named_section_flags (".gcc_except_table", flags);
3679     }
3680   else if (flag_pic)
3681     data_section ();
3682   else
3683     readonly_data_section ();
3684 }
3685
3686 void
3687 output_function_exception_table (void)
3688 {
3689   int tt_format, cs_format, lp_format, i, n;
3690 #ifdef HAVE_AS_LEB128
3691   char ttype_label[32];
3692   char cs_after_size_label[32];
3693   char cs_end_label[32];
3694 #else
3695   int call_site_len;
3696 #endif
3697   int have_tt_data;
3698   int tt_format_size = 0;
3699
3700   /* Not all functions need anything.  */
3701   if (! cfun->uses_eh_lsda)
3702     return;
3703
3704 #ifdef IA64_UNWIND_INFO
3705   fputs ("\t.personality\t", asm_out_file);
3706   output_addr_const (asm_out_file, eh_personality_libfunc);
3707   fputs ("\n\t.handlerdata\n", asm_out_file);
3708   /* Note that varasm still thinks we're in the function's code section.
3709      The ".endp" directive that will immediately follow will take us back.  */
3710 #else
3711   (*targetm.asm_out.exception_section) ();
3712 #endif
3713
3714   have_tt_data = (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) > 0
3715                   || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3716
3717   /* Indicate the format of the @TType entries.  */
3718   if (! have_tt_data)
3719     tt_format = DW_EH_PE_omit;
3720   else
3721     {
3722       tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3723 #ifdef HAVE_AS_LEB128
3724       ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3725                                    current_function_funcdef_no);
3726 #endif
3727       tt_format_size = size_of_encoded_value (tt_format);
3728
3729       assemble_align (tt_format_size * BITS_PER_UNIT);
3730     }
3731
3732   (*targetm.asm_out.internal_label) (asm_out_file, "LLSDA",
3733                              current_function_funcdef_no);
3734
3735   /* The LSDA header.  */
3736
3737   /* Indicate the format of the landing pad start pointer.  An omitted
3738      field implies @LPStart == @Start.  */
3739   /* Currently we always put @LPStart == @Start.  This field would
3740      be most useful in moving the landing pads completely out of
3741      line to another section, but it could also be used to minimize
3742      the size of uleb128 landing pad offsets.  */
3743   lp_format = DW_EH_PE_omit;
3744   dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3745                        eh_data_format_name (lp_format));
3746
3747   /* @LPStart pointer would go here.  */
3748
3749   dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3750                        eh_data_format_name (tt_format));
3751
3752 #ifndef HAVE_AS_LEB128
3753   if (USING_SJLJ_EXCEPTIONS)
3754     call_site_len = sjlj_size_of_call_site_table ();
3755   else
3756     call_site_len = dw2_size_of_call_site_table ();
3757 #endif
3758
3759   /* A pc-relative 4-byte displacement to the @TType data.  */
3760   if (have_tt_data)
3761     {
3762 #ifdef HAVE_AS_LEB128
3763       char ttype_after_disp_label[32];
3764       ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3765                                    current_function_funcdef_no);
3766       dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3767                                     "@TType base offset");
3768       ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3769 #else
3770       /* Ug.  Alignment queers things.  */
3771       unsigned int before_disp, after_disp, last_disp, disp;
3772
3773       before_disp = 1 + 1;
3774       after_disp = (1 + size_of_uleb128 (call_site_len)
3775                     + call_site_len
3776                     + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3777                     + (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data)
3778                        * tt_format_size));
3779
3780       disp = after_disp;
3781       do
3782         {
3783           unsigned int disp_size, pad;
3784
3785           last_disp = disp;
3786           disp_size = size_of_uleb128 (disp);
3787           pad = before_disp + disp_size + after_disp;
3788           if (pad % tt_format_size)
3789             pad = tt_format_size - (pad % tt_format_size);
3790           else
3791             pad = 0;
3792           disp = after_disp + pad;
3793         }
3794       while (disp != last_disp);
3795
3796       dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3797 #endif
3798     }
3799
3800   /* Indicate the format of the call-site offsets.  */
3801 #ifdef HAVE_AS_LEB128
3802   cs_format = DW_EH_PE_uleb128;
3803 #else
3804   cs_format = DW_EH_PE_udata4;
3805 #endif
3806   dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3807                        eh_data_format_name (cs_format));
3808
3809 #ifdef HAVE_AS_LEB128
3810   ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3811                                current_function_funcdef_no);
3812   ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3813                                current_function_funcdef_no);
3814   dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3815                                 "Call-site table length");
3816   ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3817   if (USING_SJLJ_EXCEPTIONS)
3818     sjlj_output_call_site_table ();
3819   else
3820     dw2_output_call_site_table ();
3821   ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3822 #else
3823   dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3824   if (USING_SJLJ_EXCEPTIONS)
3825     sjlj_output_call_site_table ();
3826   else
3827     dw2_output_call_site_table ();
3828 #endif
3829
3830   /* ??? Decode and interpret the data for flag_debug_asm.  */
3831   n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3832   for (i = 0; i < n; ++i)
3833     dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3834                          (i ? NULL : "Action record table"));
3835
3836   if (have_tt_data)
3837     assemble_align (tt_format_size * BITS_PER_UNIT);
3838
3839   i = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data);
3840   while (i-- > 0)
3841     {
3842       tree type = VARRAY_TREE (cfun->eh->ttype_data, i);
3843       rtx value;
3844
3845       if (type == NULL_TREE)
3846         value = const0_rtx;
3847       else
3848         {
3849           struct cgraph_varpool_node *node;
3850
3851           type = lookup_type_for_runtime (type);
3852           value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3853
3854           /* Let cgraph know that the rtti decl is used.  Not all of the
3855              paths below go through assemble_integer, which would take
3856              care of this for us.  */
3857           if (TREE_CODE (type) == ADDR_EXPR)
3858             {
3859               type = TREE_OPERAND (type, 0);
3860               node = cgraph_varpool_node (type);
3861               if (node)
3862                 cgraph_varpool_mark_needed_node (node);
3863             }
3864           else if (TREE_CODE (type) != INTEGER_CST)
3865             abort ();
3866         }
3867
3868       if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3869         assemble_integer (value, tt_format_size,
3870                           tt_format_size * BITS_PER_UNIT, 1);
3871       else
3872         dw2_asm_output_encoded_addr_rtx (tt_format, value, NULL);
3873     }
3874
3875 #ifdef HAVE_AS_LEB128
3876   if (have_tt_data)
3877       ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3878 #endif
3879
3880   /* ??? Decode and interpret the data for flag_debug_asm.  */
3881   n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3882   for (i = 0; i < n; ++i)
3883     dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3884                          (i ? NULL : "Exception specification table"));
3885
3886   function_section (current_function_decl);
3887 }
3888
3889 #include "gt-except.h"