1 /* Implements exception handling.
2 Copyright (C) 1989, 92-95, 1996 Free Software Foundation, Inc.
3 Contributed by Mike Stump <mrs@cygnus.com>.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* An exception is an event that can be signaled from within a
24 function. This event can then be "caught" or "trapped" by the
25 callers of this function. This potentially allows program flow to
26 be transferred to any arbitrary code assocated with a function call
27 several levels up the stack.
29 The intended use for this mechanism is for signaling "exceptional
30 events" in an out-of-band fashion, hence its name. The C++ language
31 (and many other OO-styled or functional languages) practically
32 requires such a mechanism, as otherwise it becomes very difficult
33 or even impossible to signal failure conditions in complex
34 situations. The traditional C++ example is when an error occurs in
35 the process of constructing an object; without such a mechanism, it
36 is impossible to signal that the error occurs without adding global
37 state variables and error checks around every object construction.
39 The act of causing this event to occur is referred to as "throwing
40 an exception". (Alternate terms include "raising an exception" or
41 "signaling an exception".) The term "throw" is used because control
42 is returned to the callers of the function that is signaling the
43 exception, and thus there is the concept of "throwing" the
44 exception up the call stack.
46 It is appropriate to speak of the "context of a throw". This
47 context refers to the address where the exception is thrown from,
48 and is used to determine which exception region will handle the
51 Regions of code within a function can be marked such that if it
52 contains the context of a throw, control will be passed to a
53 designated "exception handler". These areas are known as "exception
54 regions". Exception regions cannot overlap, but they can be nested
55 to any arbitrary depth. Also, exception regions cannot cross
58 Each object file that is compiled with exception handling contains a
59 static array of exception handlers named __EXCEPTION_TABLE__. Each entry
60 contains the starting and ending addresses of the exception region,
61 and the address of the handler designated for that region.
63 At program startup each object file invokes a function named
64 __register_exceptions with the address of its local
65 __EXCEPTION_TABLE__. __register_exceptions is defined in libgcc2.c,
66 and is responsible for recording all of the exception regions into
67 one list (which is kept in a static variable named exception_table_list).
69 The function __throw () is actually responsible for doing the
70 throw. In the C++ frontend, __throw () is generated on a
71 per-object-file basis for each source file compiled with
74 __throw () attempts to find the appropriate exception handler for the
75 PC value stored in __eh_pc by calling __find_first_exception_table_match
76 (which is defined in libgcc2.c). If an appropriate handler is
77 found, __throw jumps directly to it.
79 If a handler for the address being thrown from can't be found,
80 __throw is responsible for unwinding the stack, determining the
81 address of the caller of the current function (which will be used
82 as the new context to throw from), and then searching for a handler
83 for the new context. __throw may also call abort () if it is unable
84 to unwind the stack, and can also call an external library function
85 named __terminate if it reaches the top of the stack without
86 finding an appropriate handler.
88 Note that some of the regions and handlers are implicitly
89 generated. The handlers for these regions perform necessary
90 cleanups (in C++ these cleanups are responsible for invoking
91 necessary object destructors) before rethrowing the exception to
92 the outer exception region.
94 Internal implementation details:
96 The start of an exception region is indicated by calling
97 expand_eh_region_start (). expand_eh_region_end (handler) is
98 subsequently invoked to end the region and to associate a handler
99 with the region. This is used to create a region that has an
100 associated cleanup routine for performing tasks like object
103 To associate a user-defined handler with a block of statements, the
104 function expand_start_try_stmts () is used to mark the start of the
105 block of statements with which the handler is to be associated
106 (which is usually known as a "try block"). All statements that
107 appear afterwards will be associated with the try block.
109 A call to expand_start_all_catch () will mark the end of the try
110 block, and also marks the start of the "catch block" associated
111 with the try block. This catch block will only be invoked if an
112 exception is thrown through the try block. The instructions for the
113 catch block are kept as a separate sequence, and will be emitted at
114 the end of the function along with the handlers specified via
115 expand_eh_region_end (). The end of the catch block is marked with
116 expand_end_all_catch ().
118 Any data associated with the exception must currently be handled by
119 some external mechanism maintained in the frontend. For example,
120 the C++ exception mechanism passes an arbitrary value along with
121 the exception, and this is handled in the C++ frontend by using a
122 global variable to hold the value.
124 Internally-generated exception regions are marked by calling
125 expand_eh_region_start () to mark the start of the region, and
126 expand_eh_region_end () is used to both designate the end of the
127 region and to associate a handler/cleanup with the region. These
128 functions generate the appropriate RTL sequences to mark the start
129 and end of the exception regions and ensure that an appropriate
130 exception region entry will be added to the exception region table.
131 expand_eh_region_end () also queues the provided handler to be
132 emitted at the end of the current function.
134 TARGET_EXPRs can also be used to designate exception regions. A
135 TARGET_EXPR gives an unwind-protect style interface commonly used
136 in functional languages such as LISP. The associated expression is
137 evaluated, and if it (or any of the functions that it calls) throws
138 an exception it is caught by the associated cleanup. The backend
139 also takes care of the details of associating an exception table
140 entry with the expression and generating the necessary code.
142 The generated RTL for an exception region includes
143 NOTE_INSN_EH_REGION_BEG and NOTE_INSN_EH_REGION_END notes that mark
144 the start and end of the exception region. A unique label is also
145 generated at the start of the exception region.
147 In the current implementation, an exception can only be thrown from
148 a function call (since the mechanism used to actually throw an
149 exception involves calling __throw). If an exception region is
150 created but no function calls occur within that region, the region
151 can be safely optimized away since no exceptions can ever be caught
156 The details of unwinding the stack to the next frame can be rather
157 complex. While in many cases a generic __unwind_function () routine
158 can be used by the generated exception handling code to do this, it
159 is often necessary to generate inline code to do the unwinding.
161 Whether or not these inlined unwinders are necessary is
164 By default, if the target-specific backend doesn't supply a
165 definition for __unwind_function (), inlined unwinders will be used
166 instead. The main tradeoff here is in text space utilization.
167 Obviously, if inline unwinders have to be generated repeatedly,
168 this uses more space than if a single routine is used.
170 The backend macro DOESNT_NEED_UNWINDER is used to conditionalize
171 whether or not per-function unwinders are needed. If DOESNT_NEED_UNWINDER
172 is defined and has a non-zero value, a per-function unwinder is
173 not emitted for the current function.
175 On some platforms it is possible that neither __unwind_function ()
176 nor inlined unwinders are available. For these platforms it is not
177 possible to throw through a function call, and abort () will be
178 invoked instead of performing the throw. */
187 #include "function.h"
188 #include "insn-flags.h"
190 #include "insn-codes.h"
192 #include "hard-reg-set.h"
193 #include "insn-config.h"
198 /* A list of labels used for exception handlers. Created by
199 find_exception_handler_labels for the optimization passes. */
201 rtx exception_handler_labels
;
203 /* Nonzero means that __throw was invoked.
205 This is used by the C++ frontend to know if code needs to be emitted
206 for __throw or not. */
210 /* A stack used for keeping track of the currectly active exception
211 handling region. As each exception region is started, an entry
212 describing the region is pushed onto this stack. The current
213 region can be found by looking at the top of the stack, and as we
214 exit regions, the corresponding entries are popped.
216 Entries cannot overlap; they must be nested. So there is only one
217 entry at most that corresponds to the current instruction, and that
218 is the entry on the top of the stack. */
220 struct eh_stack ehstack
;
222 /* A queue used for tracking which exception regions have closed but
223 whose handlers have not yet been expanded. Regions are emitted in
224 groups in an attempt to improve paging performance.
226 As we exit a region, we enqueue a new entry. The entries are then
227 dequeued during expand_leftover_cleanups () and expand_start_all_catch (),
229 We should redo things so that we either take RTL for the handler,
230 or we expand the handler expressed as a tree immediately at region
233 struct eh_queue ehqueue
;
235 /* Insns for all of the exception handlers for the current function.
236 They are currently emitted by the frontend code. */
240 /* A TREE_CHAINed list of handlers for regions that are not yet
241 closed. The TREE_VALUE of each entry contains the handler for the
242 corresponding entry on the ehstack. */
244 static tree protect_list
;
246 /* Stacks to keep track of various labels. */
248 /* Keeps track of the label to resume to should one want to resume
249 normal control flow out of a handler (instead of, say, returning to
250 the caller of the current function or exiting the program). Also
251 used as the context of a throw to rethrow an exception to the outer
254 struct label_node
*caught_return_label_stack
= NULL
;
256 /* A random data area for the front end's own use. */
258 struct label_node
*false_label_stack
= NULL
;
260 /* The rtx and the tree for the saved PC value. */
265 rtx expand_builtin_return_addr
PROTO((enum built_in_function
, int, rtx
));
267 /* Various support routines to manipulate the various data structures
268 used by the exception handling code. */
270 /* Push a label entry onto the given STACK. */
273 push_label_entry (stack
, rlabel
, tlabel
)
274 struct label_node
**stack
;
278 struct label_node
*newnode
279 = (struct label_node
*) xmalloc (sizeof (struct label_node
));
282 newnode
->u
.rlabel
= rlabel
;
284 newnode
->u
.tlabel
= tlabel
;
285 newnode
->chain
= *stack
;
289 /* Pop a label entry from the given STACK. */
292 pop_label_entry (stack
)
293 struct label_node
**stack
;
296 struct label_node
*tempnode
;
302 label
= tempnode
->u
.rlabel
;
303 *stack
= (*stack
)->chain
;
309 /* Return the top element of the given STACK. */
312 top_label_entry (stack
)
313 struct label_node
**stack
;
318 return (*stack
)->u
.tlabel
;
321 /* Make a copy of ENTRY using xmalloc to allocate the space. */
323 static struct eh_entry
*
324 copy_eh_entry (entry
)
325 struct eh_entry
*entry
;
327 struct eh_entry
*newentry
;
329 newentry
= (struct eh_entry
*) xmalloc (sizeof (struct eh_entry
));
330 bcopy ((char *) entry
, (char *) newentry
, sizeof (struct eh_entry
));
335 /* Push a new eh_node entry onto STACK, and return the start label for
339 push_eh_entry (stack
)
340 struct eh_stack
*stack
;
342 struct eh_node
*node
= (struct eh_node
*) xmalloc (sizeof (struct eh_node
));
343 struct eh_entry
*entry
= (struct eh_entry
*) xmalloc (sizeof (struct eh_entry
));
345 entry
->start_label
= gen_label_rtx ();
346 entry
->end_label
= gen_label_rtx ();
347 entry
->exception_handler_label
= gen_label_rtx ();
348 entry
->finalization
= NULL_TREE
;
351 node
->chain
= stack
->top
;
354 return entry
->start_label
;
357 /* Pop an entry from the given STACK. */
359 static struct eh_entry
*
361 struct eh_stack
*stack
;
363 struct eh_node
*tempnode
;
364 struct eh_entry
*tempentry
;
366 tempnode
= stack
->top
;
367 tempentry
= tempnode
->entry
;
368 stack
->top
= stack
->top
->chain
;
374 /* Enqueue an ENTRY onto the given QUEUE. */
377 enqueue_eh_entry (queue
, entry
)
378 struct eh_queue
*queue
;
379 struct eh_entry
*entry
;
381 struct eh_node
*node
= (struct eh_node
*) xmalloc (sizeof (struct eh_node
));
386 if (queue
->head
== NULL
)
392 queue
->tail
->chain
= node
;
397 /* Dequeue an entry from the given QUEUE. */
399 static struct eh_entry
*
400 dequeue_eh_entry (queue
)
401 struct eh_queue
*queue
;
403 struct eh_node
*tempnode
;
404 struct eh_entry
*tempentry
;
406 if (queue
->head
== NULL
)
409 tempnode
= queue
->head
;
410 queue
->head
= queue
->head
->chain
;
412 tempentry
= tempnode
->entry
;
418 /* Routine to see if exception exception handling is turned on.
419 DO_WARN is non-zero if we want to inform the user that exception
420 handling is turned off.
422 This is used to ensure that -fexceptions has been specified if the
423 compiler tries to use any exception-specific functions. */
429 if (! flag_exceptions
)
431 static int warned
= 0;
432 if (! warned
&& do_warn
)
434 error ("exception handling disabled, use -fexceptions to enable");
442 /* Given a return address in ADDR, determine the address we should use
443 to find the corresponding EH region. */
446 eh_outer_context (addr
)
449 /* First mask out any unwanted bits. */
450 #ifdef MASK_RETURN_ADDR
451 emit_insn (gen_rtx (SET
, Pmode
,
454 addr
, MASK_RETURN_ADDR
)));
457 /* Then subtract out enough to get into the appropriate region. If
458 this is defined, assume we don't need to subtract anything as it
459 is already within the correct region. */
460 #if ! defined (RETURN_ADDR_OFFSET)
461 addr
= plus_constant (addr
, -1);
467 /* Start a new exception region and push the HANDLER for the region
468 onto protect_list. All of the regions created with add_partial_entry
469 will be ended when end_protect_partials () is invoked. */
472 add_partial_entry (handler
)
475 expand_eh_region_start ();
477 /* Make sure the entry is on the correct obstack. */
478 push_obstacks_nochange ();
479 resume_temporary_allocation ();
480 protect_list
= tree_cons (NULL_TREE
, handler
, protect_list
);
484 /* Output a note marking the start of an exception handling region.
485 All instructions emitted after this point are considered to be part
486 of the region until expand_eh_region_end () is invoked. */
489 expand_eh_region_start ()
493 /* This is the old code. */
498 /* Maybe do this to prevent jumping in and so on... */
502 note
= emit_note (NULL_PTR
, NOTE_INSN_EH_REGION_BEG
);
503 emit_label (push_eh_entry (&ehstack
));
504 NOTE_BLOCK_NUMBER (note
)
505 = CODE_LABEL_NUMBER (ehstack
.top
->entry
->exception_handler_label
);
508 /* Output a note marking the end of the exception handling region on
511 HANDLER is either the cleanup for the exception region, or if we're
512 marking the end of a try block, HANDLER is integer_zero_node.
514 HANDLER will be transformed to rtl when expand_leftover_cleanups ()
518 expand_eh_region_end (handler
)
523 struct eh_entry
*entry
;
528 entry
= pop_eh_entry (&ehstack
);
530 note
= emit_note (NULL_PTR
, NOTE_INSN_EH_REGION_END
);
531 NOTE_BLOCK_NUMBER (note
) = CODE_LABEL_NUMBER (entry
->exception_handler_label
);
533 /* Emit a label marking the end of this exception region. */
534 emit_label (entry
->end_label
);
536 /* Put in something that takes up space, as otherwise the end
537 address for this EH region could have the exact same address as
538 its outer region. This would cause us to miss the fact that
539 resuming exception handling with this PC value would be inside
541 emit_insn (gen_nop ());
543 entry
->finalization
= handler
;
545 enqueue_eh_entry (&ehqueue
, entry
);
548 /* Maybe do this to prevent jumping in and so on... */
553 /* Emit a call to __throw and note that we threw something, so we know
554 we need to generate the necessary code for __throw.
556 Before invoking throw, the __eh_pc variable must have been set up
557 to contain the PC being thrown from. This address is used by
558 __throw () to determine which exception region (if any) is
559 responsible for handling the exception. */
565 emit_indirect_jump (throw_libfunc
);
567 SYMBOL_REF_USED (throw_libfunc
) = 1;
568 emit_library_call (throw_libfunc
, 0, VOIDmode
, 0);
574 /* An internal throw with an indirect CONTEXT we want to throw from.
575 CONTEXT evaluates to the context of the throw. */
578 expand_internal_throw_indirect (context
)
581 assemble_external (eh_saved_pc
);
582 emit_move_insn (eh_saved_pc_rtx
, context
);
586 /* An internal throw with a direct CONTEXT we want to throw from.
587 CONTEXT must be a label; its address will be used as the context of
591 expand_internal_throw (context
)
594 expand_internal_throw_indirect (gen_rtx (LABEL_REF
, Pmode
, context
));
597 /* Called from expand_exception_blocks and expand_end_catch_block to
598 emit any pending handlers/cleanups queued from expand_eh_region_end (). */
601 expand_leftover_cleanups ()
603 struct eh_entry
*entry
;
605 while ((entry
= dequeue_eh_entry (&ehqueue
)) != 0)
609 /* A leftover try block. Shouldn't be one here. */
610 if (entry
->finalization
== integer_zero_node
)
613 /* Output the label for the start of the exception handler. */
614 emit_label (entry
->exception_handler_label
);
616 /* And now generate the insns for the handler. */
617 expand_expr (entry
->finalization
, const0_rtx
, VOIDmode
, 0);
619 prev
= get_last_insn ();
620 if (! (prev
&& GET_CODE (prev
) == BARRIER
))
622 /* The below can be optimized away, and we could just fall into the
623 next EH handler, if we are certain they are nested. */
624 /* Emit code to throw to the outer context if we fall off
625 the end of the handler. */
626 expand_internal_throw (entry
->end_label
);
633 /* Called at the start of a block of try statements. */
635 expand_start_try_stmts ()
640 expand_eh_region_start ();
643 /* Generate RTL for the start of a group of catch clauses.
645 It is responsible for starting a new instruction sequence for the
646 instructions in the catch block, and expanding the handlers for the
647 internally-generated exception regions nested within the try block
648 corresponding to this catch block. */
651 expand_start_all_catch ()
653 struct eh_entry
*entry
;
659 /* End the try block. */
660 expand_eh_region_end (integer_zero_node
);
662 emit_line_note (input_filename
, lineno
);
663 label
= build_decl (LABEL_DECL
, NULL_TREE
, NULL_TREE
);
665 /* The label for the exception handling block that we will save.
666 This is Lresume in the documention. */
667 expand_label (label
);
669 /* Put in something that takes up space, as otherwise the end
670 address for the EH region could have the exact same address as
671 the outer region, causing us to miss the fact that resuming
672 exception handling with this PC value would be inside the outer
674 emit_insn (gen_nop ());
676 /* Push the label that points to where normal flow is resumed onto
677 the top of the label stack. */
678 push_label_entry (&caught_return_label_stack
, NULL_RTX
, label
);
680 /* Start a new sequence for all the catch blocks. We will add this
681 to the global sequence catch_clauses when we have completed all
682 the handlers in this handler-seq. */
689 entry
= dequeue_eh_entry (&ehqueue
);
690 /* Emit the label for the exception handler for this region, and
691 expand the code for the handler.
693 Note that a catch region is handled as a side-effect here;
694 for a try block, entry->finalization will contain
695 integer_zero_node, so no code will be generated in the
696 expand_expr call below. But, the label for the handler will
697 still be emitted, so any code emitted after this point will
698 end up being the handler. */
699 emit_label (entry
->exception_handler_label
);
700 expand_expr (entry
->finalization
, const0_rtx
, VOIDmode
, 0);
702 /* When we get down to the matching entry for this try block, stop. */
703 if (entry
->finalization
== integer_zero_node
)
705 /* Don't forget to free this entry. */
710 prev
= get_last_insn ();
711 if (prev
== NULL
|| GET_CODE (prev
) != BARRIER
)
713 /* Code to throw out to outer context when we fall off end
714 of the handler. We can't do this here for catch blocks,
715 so it's done in expand_end_all_catch () instead.
717 The below can be optimized away (and we could just fall
718 into the next EH handler) if we are certain they are
721 expand_internal_throw (entry
->end_label
);
727 /* Finish up the catch block. At this point all the insns for the
728 catch clauses have already been generated, so we only have to add
729 them to the catch_clauses list. We also want to make sure that if
730 we fall off the end of the catch clauses that we rethrow to the
734 expand_end_all_catch ()
736 rtx new_catch_clause
;
741 /* Code to throw out to outer context, if we fall off end of catch
742 handlers. This is rethrow (Lresume, same id, same obj) in the
743 documentation. We use Lresume because we know that it will throw
744 to the correct context.
746 In other words, if the catch handler doesn't exit or return, we
747 do a "throw" (using the address of Lresume as the point being
748 thrown from) so that the outer EH region can then try to process
751 expand_internal_throw (DECL_RTL (top_label_entry (&caught_return_label_stack
)));
753 /* Now we have the complete catch sequence. */
754 new_catch_clause
= get_insns ();
757 /* This level of catch blocks is done, so set up the successful
758 catch jump label for the next layer of catch blocks. */
759 pop_label_entry (&caught_return_label_stack
);
761 /* Add the new sequence of catches to the main one for this function. */
762 push_to_sequence (catch_clauses
);
763 emit_insns (new_catch_clause
);
764 catch_clauses
= get_insns ();
767 /* Here we fall through into the continuation code. */
770 /* End all the pending exception regions on protect_list. The handlers
771 will be emitted when expand_leftover_cleanups () is invoked. */
774 end_protect_partials ()
778 expand_eh_region_end (TREE_VALUE (protect_list
));
779 protect_list
= TREE_CHAIN (protect_list
);
783 /* The exception table that we build that is used for looking up and
784 dispatching exceptions, the current number of entries, and its
785 maximum size before we have to extend it.
787 The number in eh_table is the code label number of the exception
788 handler for the region. This is added by add_eh_table_entry () and
789 used by output_exception_table_entry (). */
791 static int *eh_table
;
792 static int eh_table_size
;
793 static int eh_table_max_size
;
795 /* Note the need for an exception table entry for region N. If we
796 don't need to output an explicit exception table, avoid all of the
799 Called from final_scan_insn when a NOTE_INSN_EH_REGION_BEG is seen.
800 N is the NOTE_BLOCK_NUMBER of the note, which comes from the code
801 label number of the exception handler for the region. */
804 add_eh_table_entry (n
)
807 #ifndef OMIT_EH_TABLE
808 if (eh_table_size
>= eh_table_max_size
)
812 eh_table_max_size
+= eh_table_max_size
>>1;
814 if (eh_table_max_size
< 0)
817 if ((eh_table
= (int *) realloc (eh_table
,
818 eh_table_max_size
* sizeof (int)))
820 fatal ("virtual memory exhausted");
824 eh_table_max_size
= 252;
825 eh_table
= (int *) xmalloc (eh_table_max_size
* sizeof (int));
828 eh_table
[eh_table_size
++] = n
;
832 /* Return a non-zero value if we need to output an exception table.
834 On some platforms, we don't have to output a table explicitly.
835 This routine doesn't mean we don't have one. */
846 /* Output the entry of the exception table corresponding to to the
847 exception region numbered N to file FILE.
849 N is the code label number corresponding to the handler of the
853 output_exception_table_entry (file
, n
)
860 ASM_GENERATE_INTERNAL_LABEL (buf
, "LEHB", n
);
861 sym
= gen_rtx (SYMBOL_REF
, Pmode
, buf
);
862 assemble_integer (sym
, POINTER_SIZE
/ BITS_PER_UNIT
, 1);
864 ASM_GENERATE_INTERNAL_LABEL (buf
, "LEHE", n
);
865 sym
= gen_rtx (SYMBOL_REF
, Pmode
, buf
);
866 assemble_integer (sym
, POINTER_SIZE
/ BITS_PER_UNIT
, 1);
868 ASM_GENERATE_INTERNAL_LABEL (buf
, "L", n
);
869 sym
= gen_rtx (SYMBOL_REF
, Pmode
, buf
);
870 assemble_integer (sym
, POINTER_SIZE
/ BITS_PER_UNIT
, 1);
872 putc ('\n', file
); /* blank line */
875 /* Output the exception table if we have and need one. */
878 output_exception_table ()
881 extern FILE *asm_out_file
;
886 exception_section ();
888 /* Beginning marker for table. */
889 assemble_align (GET_MODE_ALIGNMENT (ptr_mode
));
890 assemble_label ("__EXCEPTION_TABLE__");
892 assemble_integer (const0_rtx
, POINTER_SIZE
/ BITS_PER_UNIT
, 1);
893 assemble_integer (const0_rtx
, POINTER_SIZE
/ BITS_PER_UNIT
, 1);
894 assemble_integer (const0_rtx
, POINTER_SIZE
/ BITS_PER_UNIT
, 1);
895 putc ('\n', asm_out_file
); /* blank line */
897 for (i
= 0; i
< eh_table_size
; ++i
)
898 output_exception_table_entry (asm_out_file
, eh_table
[i
]);
902 /* Ending marker for table. */
903 assemble_label ("__EXCEPTION_END__");
904 assemble_integer (constm1_rtx
, POINTER_SIZE
/ BITS_PER_UNIT
, 1);
905 assemble_integer (constm1_rtx
, POINTER_SIZE
/ BITS_PER_UNIT
, 1);
906 assemble_integer (constm1_rtx
, POINTER_SIZE
/ BITS_PER_UNIT
, 1);
907 putc ('\n', asm_out_file
); /* blank line */
910 /* Generate code to initialize the exception table at program startup
914 register_exception_table ()
916 emit_library_call (gen_rtx (SYMBOL_REF
, Pmode
, "__register_exceptions"), 0,
918 gen_rtx (SYMBOL_REF
, Pmode
, "__EXCEPTION_TABLE__"),
922 /* Emit the RTL for the start of the per-function unwinder for the
923 current function. See emit_unwinder () for further information.
925 DOESNT_NEED_UNWINDER is a target-specific macro that determines if
926 the current function actually needs a per-function unwinder or not.
927 By default, all functions need one. */
932 #ifdef DOESNT_NEED_UNWINDER
933 if (DOESNT_NEED_UNWINDER
)
937 expand_eh_region_start ();
940 /* Emit insns for the end of the per-function unwinder for the
947 rtx return_val_rtx
, ret_val
, label
, end
, insns
;
952 #ifdef DOESNT_NEED_UNWINDER
953 if (DOESNT_NEED_UNWINDER
)
957 assemble_external (eh_saved_pc
);
959 expr
= make_node (RTL_EXPR
);
960 TREE_TYPE (expr
) = void_type_node
;
961 RTL_EXPR_RTL (expr
) = const0_rtx
;
962 TREE_SIDE_EFFECTS (expr
) = 1;
963 start_sequence_for_rtl_expr (expr
);
965 /* ret_val will contain the address of the code where the call
966 to the current function occurred. */
967 ret_val
= expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS
,
968 0, hard_frame_pointer_rtx
);
969 return_val_rtx
= copy_to_reg (ret_val
);
971 /* Get the address we need to use to determine what exception
972 handler should be invoked, and store it in __eh_pc. */
973 return_val_rtx
= eh_outer_context (return_val_rtx
);
974 emit_move_insn (eh_saved_pc_rtx
, return_val_rtx
);
976 /* Either set things up so we do a return directly to __throw, or
977 we return here instead. */
979 emit_move_insn (ret_val
, throw_libfunc
);
981 label
= gen_label_rtx ();
982 emit_move_insn (ret_val
, gen_rtx (LABEL_REF
, Pmode
, label
));
985 #ifdef RETURN_ADDR_OFFSET
986 return_val_rtx
= plus_constant (ret_val
, -RETURN_ADDR_OFFSET
);
987 if (return_val_rtx
!= ret_val
)
988 emit_move_insn (ret_val
, return_val_rtx
);
991 end
= gen_label_rtx ();
994 RTL_EXPR_SEQUENCE (expr
) = get_insns ();
996 expand_eh_region_end (expr
);
1000 #ifndef JUMP_TO_THROW
1005 expand_leftover_cleanups ();
1010 /* If necessary, emit insns for the per function unwinder for the
1011 current function. Called after all the code that needs unwind
1012 protection is output.
1014 The unwinder takes care of catching any exceptions that have not
1015 been previously caught within the function, unwinding the stack to
1016 the next frame, and rethrowing using the address of the current
1017 function's caller as the context of the throw.
1019 On some platforms __throw can do this by itself (or with the help
1020 of __unwind_function) so the per-function unwinder is
1023 We cannot place the unwinder into the function until after we know
1024 we are done inlining, as we don't want to have more than one
1025 unwinder per non-inlined function. */
1033 start_eh_unwinder ();
1034 insns
= get_insns ();
1037 /* We place the start of the exception region associated with the
1038 per function unwinder at the top of the function. */
1040 emit_insns_after (insns
, get_insns ());
1044 insns
= get_insns ();
1047 /* And we place the end of the exception region before the USE and
1048 CLOBBER insns that may come at the end of the function. */
1052 insn
= get_last_insn ();
1053 while (GET_CODE (insn
) == NOTE
1054 || (GET_CODE (insn
) == INSN
1055 && (GET_CODE (PATTERN (insn
)) == USE
1056 || GET_CODE (PATTERN (insn
)) == CLOBBER
)))
1057 insn
= PREV_INSN (insn
);
1059 if (GET_CODE (insn
) == CODE_LABEL
1060 && GET_CODE (PREV_INSN (insn
)) == BARRIER
)
1062 insn
= PREV_INSN (insn
);
1066 rtx label
= gen_label_rtx ();
1067 emit_label_after (label
, insn
);
1068 insn
= emit_jump_insn_after (gen_jump (label
), insn
);
1069 insn
= emit_barrier_after (insn
);
1072 emit_insns_after (insns
, insn
);
1075 /* Scan the current insns and build a list of handler labels. The
1076 resulting list is placed in the global variable exception_handler_labels.
1078 It is called after the last exception handling region is added to
1079 the current function (when the rtl is almost all built for the
1080 current function) and before the jump optimization pass. */
1083 find_exception_handler_labels ()
1086 int max_labelno
= max_label_num ();
1087 int min_labelno
= get_first_label_num ();
1090 exception_handler_labels
= NULL_RTX
;
1092 /* If we aren't doing exception handling, there isn't much to check. */
1096 /* Generate a handy reference to each label. */
1098 labels
= (rtx
*) alloca ((max_labelno
- min_labelno
) * sizeof (rtx
));
1101 labels
-= min_labelno
;
1103 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1105 if (GET_CODE (insn
) == CODE_LABEL
)
1106 if (CODE_LABEL_NUMBER (insn
) >= min_labelno
1107 && CODE_LABEL_NUMBER (insn
) < max_labelno
)
1108 labels
[CODE_LABEL_NUMBER (insn
)] = insn
;
1111 /* For each start of a region, add its label to the list. */
1113 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1115 if (GET_CODE (insn
) == NOTE
1116 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EH_REGION_BEG
)
1118 rtx label
= NULL_RTX
;
1120 if (NOTE_BLOCK_NUMBER (insn
) >= min_labelno
1121 && NOTE_BLOCK_NUMBER (insn
) < max_labelno
)
1123 label
= labels
[NOTE_BLOCK_NUMBER (insn
)];
1126 exception_handler_labels
1127 = gen_rtx (EXPR_LIST
, VOIDmode
,
1128 label
, exception_handler_labels
);
1130 warning ("didn't find handler for EH region %d",
1131 NOTE_BLOCK_NUMBER (insn
));
1134 warning ("mismatched EH region %d", NOTE_BLOCK_NUMBER (insn
));
1139 /* Perform sanity checking on the exception_handler_labels list.
1141 Can be called after find_exception_handler_labels is called to
1142 build the list of exception handlers for the current function and
1143 before we finish processing the current function. */
1146 check_exception_handler_labels ()
1150 /* If we aren't doing exception handling, there isn't much to check. */
1154 /* Ensure that the CODE_LABEL_NUMBER for the CODE_LABEL entry point
1155 in each handler corresponds to the CODE_LABEL_NUMBER of the
1158 for (handler
= exception_handler_labels
;
1160 handler
= XEXP (handler
, 1))
1162 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1164 if (GET_CODE (insn
) == CODE_LABEL
)
1166 if (CODE_LABEL_NUMBER (insn
)
1167 == CODE_LABEL_NUMBER (XEXP (handler
, 0)))
1169 if (insn
!= XEXP (handler
, 0))
1170 warning ("mismatched handler %d",
1171 CODE_LABEL_NUMBER (insn
));
1176 if (insn
== NULL_RTX
)
1177 warning ("handler not found %d",
1178 CODE_LABEL_NUMBER (XEXP (handler
, 0)));
1181 /* Now go through and make sure that for each region there is a
1182 corresponding label. */
1183 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1185 if (GET_CODE (insn
) == NOTE
1186 && (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EH_REGION_BEG
||
1187 NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EH_REGION_END
))
1189 for (handler
= exception_handler_labels
;
1191 handler
= XEXP (handler
, 1))
1193 if (CODE_LABEL_NUMBER (XEXP (handler
, 0))
1194 == NOTE_BLOCK_NUMBER (insn
))
1197 if (handler
== NULL_RTX
)
1198 warning ("region exists, no handler %d",
1199 NOTE_BLOCK_NUMBER (insn
));
1204 /* This group of functions initializes the exception handling data
1205 structures at the start of the compilation, initializes the data
1206 structures at the start of a function, and saves and restores the
1207 exception handling data structures for the start/end of a nested
1210 /* Toplevel initialization for EH things. */
1215 /* Generate rtl to reference the variable in which the PC of the
1216 current context is saved. */
1217 tree type
= build_pointer_type (make_node (VOID_TYPE
));
1219 eh_saved_pc
= build_decl (VAR_DECL
, get_identifier ("__eh_pc"), type
);
1220 DECL_EXTERNAL (eh_saved_pc
) = 1;
1221 TREE_PUBLIC (eh_saved_pc
) = 1;
1222 make_decl_rtl (eh_saved_pc
, NULL_PTR
, 1);
1223 eh_saved_pc_rtx
= DECL_RTL (eh_saved_pc
);
1226 /* Initialize the per-function EH information. */
1229 init_eh_for_function ()
1232 ehqueue
.head
= ehqueue
.tail
= 0;
1233 catch_clauses
= NULL_RTX
;
1234 false_label_stack
= 0;
1235 caught_return_label_stack
= 0;
1236 protect_list
= NULL_TREE
;
1239 /* Save some of the per-function EH info into the save area denoted by
1242 This is currently called from save_stmt_status (). */
1250 p
->ehstack
= ehstack
;
1251 p
->ehqueue
= ehqueue
;
1252 p
->catch_clauses
= catch_clauses
;
1253 p
->false_label_stack
= false_label_stack
;
1254 p
->caught_return_label_stack
= caught_return_label_stack
;
1255 p
->protect_list
= protect_list
;
1260 /* Restore the per-function EH info saved into the area denoted by P.
1262 This is currently called from restore_stmt_status. */
1265 restore_eh_status (p
)
1270 protect_list
= p
->protect_list
;
1271 caught_return_label_stack
= p
->caught_return_label_stack
;
1272 false_label_stack
= p
->false_label_stack
;
1273 catch_clauses
= p
->catch_clauses
;
1274 ehqueue
= p
->ehqueue
;
1275 ehstack
= p
->ehstack
;
1278 /* This section is for the exception handling specific optimization
1279 pass. First are the internal routines, and then the main
1280 optimization pass. */
1282 /* Determine if the given INSN can throw an exception. */
1288 /* Calls can always potentially throw exceptions. */
1289 if (GET_CODE (insn
) == CALL_INSN
)
1292 #ifdef ASYNCH_EXCEPTIONS
1293 /* If we wanted asynchronous exceptions, then everything but NOTEs
1294 and CODE_LABELs could throw. */
1295 if (GET_CODE (insn
) != NOTE
&& GET_CODE (insn
) != CODE_LABEL
)
1302 /* Scan a exception region looking for the matching end and then
1303 remove it if possible. INSN is the start of the region, N is the
1304 region number, and DELETE_OUTER is to note if anything in this
1307 Regions are removed if they cannot possibly catch an exception.
1308 This is determined by invoking can_throw () on each insn within the
1309 region; if can_throw returns true for any of the instructions, the
1310 region can catch an exception, since there is an insn within the
1311 region that is capable of throwing an exception.
1313 Returns the NOTE_INSN_EH_REGION_END corresponding to this region, or
1314 calls abort () if it can't find one.
1316 Can abort if INSN is not a NOTE_INSN_EH_REGION_BEGIN, or if N doesn't
1317 correspond to the region number, or if DELETE_OUTER is NULL. */
1320 scan_region (insn
, n
, delete_outer
)
1327 /* Assume we can delete the region. */
1330 assert (insn
!= NULL_RTX
1331 && GET_CODE (insn
) == NOTE
1332 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EH_REGION_BEG
1333 && NOTE_BLOCK_NUMBER (insn
) == n
1334 && delete_outer
!= NULL
);
1336 insn
= NEXT_INSN (insn
);
1338 /* Look for the matching end. */
1339 while (! (GET_CODE (insn
) == NOTE
1340 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EH_REGION_END
))
1342 /* If anything can throw, we can't remove the region. */
1343 if (delete && can_throw (insn
))
1348 /* Watch out for and handle nested regions. */
1349 if (GET_CODE (insn
) == NOTE
1350 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EH_REGION_BEG
)
1352 insn
= scan_region (insn
, NOTE_BLOCK_NUMBER (insn
), &delete);
1355 insn
= NEXT_INSN (insn
);
1358 /* The _BEG/_END NOTEs must match and nest. */
1359 if (NOTE_BLOCK_NUMBER (insn
) != n
)
1362 /* If anything in this exception region can throw, we can throw. */
1367 /* Delete the start and end of the region. */
1368 delete_insn (start
);
1371 /* Only do this part if we have built the exception handler
1373 if (exception_handler_labels
)
1375 rtx x
, *prev
= &exception_handler_labels
;
1377 /* Find it in the list of handlers. */
1378 for (x
= exception_handler_labels
; x
; x
= XEXP (x
, 1))
1380 rtx label
= XEXP (x
, 0);
1381 if (CODE_LABEL_NUMBER (label
) == n
)
1383 /* If we are the last reference to the handler,
1385 if (--LABEL_NUSES (label
) == 0)
1386 delete_insn (label
);
1390 /* Remove it from the list of exception handler
1391 labels, if we are optimizing. If we are not, then
1392 leave it in the list, as we are not really going to
1393 remove the region. */
1394 *prev
= XEXP (x
, 1);
1401 prev
= &XEXP (x
, 1);
1408 /* Perform various interesting optimizations for exception handling
1411 We look for empty exception regions and make them go (away). The
1412 jump optimization code will remove the handler if nothing else uses
1416 exception_optimize ()
1418 rtx insn
, regions
= NULL_RTX
;
1421 /* Remove empty regions. */
1422 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1424 if (GET_CODE (insn
) == NOTE
1425 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EH_REGION_BEG
)
1427 /* Since scan_region () will return the NOTE_INSN_EH_REGION_END
1428 insn, we will indirectly skip through all the insns
1429 inbetween. We are also guaranteed that the value of insn
1430 returned will be valid, as otherwise scan_region () won't
1432 insn
= scan_region (insn
, NOTE_BLOCK_NUMBER (insn
), &n
);