Declare malloc, free, and atexit if inhibit_libc is defined.
[official-gcc.git] / gcc / except.c
blobc7257b31ef4557a32adb3118d45faae61cca98fb
1 /* Implements exception handling.
2 Copyright (C) 1989, 1992-1999 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)
10 any later version.
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 associated 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 There are two major codegen options for exception handling. The
47 flag -fsjlj-exceptions can be used to select the setjmp/longjmp
48 approach, which is the default. -fno-sjlj-exceptions can be used to
49 get the PC range table approach. While this is a compile time
50 flag, an entire application must be compiled with the same codegen
51 option. The first is a PC range table approach, the second is a
52 setjmp/longjmp based scheme. We will first discuss the PC range
53 table approach, after that, we will discuss the setjmp/longjmp
54 based approach.
56 It is appropriate to speak of the "context of a throw". This
57 context refers to the address where the exception is thrown from,
58 and is used to determine which exception region will handle the
59 exception.
61 Regions of code within a function can be marked such that if it
62 contains the context of a throw, control will be passed to a
63 designated "exception handler". These areas are known as "exception
64 regions". Exception regions cannot overlap, but they can be nested
65 to any arbitrary depth. Also, exception regions cannot cross
66 function boundaries.
68 Exception handlers can either be specified by the user (which we
69 will call a "user-defined handler") or generated by the compiler
70 (which we will designate as a "cleanup"). Cleanups are used to
71 perform tasks such as destruction of objects allocated on the
72 stack.
74 In the current implementation, cleanups are handled by allocating an
75 exception region for the area that the cleanup is designated for,
76 and the handler for the region performs the cleanup and then
77 rethrows the exception to the outer exception region. From the
78 standpoint of the current implementation, there is little
79 distinction made between a cleanup and a user-defined handler, and
80 the phrase "exception handler" can be used to refer to either one
81 equally well. (The section "Future Directions" below discusses how
82 this will change).
84 Each object file that is compiled with exception handling contains
85 a static array of exception handlers named __EXCEPTION_TABLE__.
86 Each entry contains the starting and ending addresses of the
87 exception region, and the address of the handler designated for
88 that region.
90 If the target does not use the DWARF 2 frame unwind information, at
91 program startup each object file invokes a function named
92 __register_exceptions with the address of its local
93 __EXCEPTION_TABLE__. __register_exceptions is defined in libgcc2.c, and
94 is responsible for recording all of the exception regions into one list
95 (which is kept in a static variable named exception_table_list).
97 On targets that support crtstuff.c, the unwind information
98 is stored in a section named .eh_frame and the information for the
99 entire shared object or program is registered with a call to
100 __register_frame_info. On other targets, the information for each
101 translation unit is registered from the file generated by collect2.
102 __register_frame_info is defined in frame.c, and is responsible for
103 recording all of the unwind regions into one list (which is kept in a
104 static variable named unwind_table_list).
106 The function __throw is actually responsible for doing the
107 throw. On machines that have unwind info support, __throw is generated
108 by code in libgcc2.c, otherwise __throw is generated on a
109 per-object-file basis for each source file compiled with
110 -fexceptions by the C++ frontend. Before __throw is invoked,
111 the current context of the throw needs to be placed in the global
112 variable __eh_pc.
114 __throw attempts to find the appropriate exception handler for the
115 PC value stored in __eh_pc by calling __find_first_exception_table_match
116 (which is defined in libgcc2.c). If __find_first_exception_table_match
117 finds a relevant handler, __throw transfers control directly to it.
119 If a handler for the context being thrown from can't be found, __throw
120 walks (see Walking the stack below) the stack up the dynamic call chain to
121 continue searching for an appropriate exception handler based upon the
122 caller of the function it last sought a exception handler for. It stops
123 then either an exception handler is found, or when the top of the
124 call chain is reached.
126 If no handler is found, an external library function named
127 __terminate is called. If a handler is found, then we restart
128 our search for a handler at the end of the call chain, and repeat
129 the search process, but instead of just walking up the call chain,
130 we unwind the call chain as we walk up it.
132 Internal implementation details:
134 To associate a user-defined handler with a block of statements, the
135 function expand_start_try_stmts is used to mark the start of the
136 block of statements with which the handler is to be associated
137 (which is known as a "try block"). All statements that appear
138 afterwards will be associated with the try block.
140 A call to expand_start_all_catch marks the end of the try block,
141 and also marks the start of the "catch block" (the user-defined
142 handler) associated with the try block.
144 This user-defined handler will be invoked for *every* exception
145 thrown with the context of the try block. It is up to the handler
146 to decide whether or not it wishes to handle any given exception,
147 as there is currently no mechanism in this implementation for doing
148 this. (There are plans for conditionally processing an exception
149 based on its "type", which will provide a language-independent
150 mechanism).
152 If the handler chooses not to process the exception (perhaps by
153 looking at an "exception type" or some other additional data
154 supplied with the exception), it can fall through to the end of the
155 handler. expand_end_all_catch and expand_leftover_cleanups
156 add additional code to the end of each handler to take care of
157 rethrowing to the outer exception handler.
159 The handler also has the option to continue with "normal flow of
160 code", or in other words to resume executing at the statement
161 immediately after the end of the exception region. The variable
162 caught_return_label_stack contains a stack of labels, and jumping
163 to the topmost entry's label via expand_goto will resume normal
164 flow to the statement immediately after the end of the exception
165 region. If the handler falls through to the end, the exception will
166 be rethrown to the outer exception region.
168 The instructions for the catch block are kept as a separate
169 sequence, and will be emitted at the end of the function along with
170 the handlers specified via expand_eh_region_end. The end of the
171 catch block is marked with expand_end_all_catch.
173 Any data associated with the exception must currently be handled by
174 some external mechanism maintained in the frontend. For example,
175 the C++ exception mechanism passes an arbitrary value along with
176 the exception, and this is handled in the C++ frontend by using a
177 global variable to hold the value. (This will be changing in the
178 future.)
180 The mechanism in C++ for handling data associated with the
181 exception is clearly not thread-safe. For a thread-based
182 environment, another mechanism must be used (possibly using a
183 per-thread allocation mechanism if the size of the area that needs
184 to be allocated isn't known at compile time.)
186 Internally-generated exception regions (cleanups) are marked by
187 calling expand_eh_region_start to mark the start of the region,
188 and expand_eh_region_end (handler) is used to both designate the
189 end of the region and to associate a specified handler/cleanup with
190 the region. The rtl code in HANDLER will be invoked whenever an
191 exception occurs in the region between the calls to
192 expand_eh_region_start and expand_eh_region_end. After HANDLER is
193 executed, additional code is emitted to handle rethrowing the
194 exception to the outer exception handler. The code for HANDLER will
195 be emitted at the end of the function.
197 TARGET_EXPRs can also be used to designate exception regions. A
198 TARGET_EXPR gives an unwind-protect style interface commonly used
199 in functional languages such as LISP. The associated expression is
200 evaluated, and whether or not it (or any of the functions that it
201 calls) throws an exception, the protect expression is always
202 invoked. This implementation takes care of the details of
203 associating an exception table entry with the expression and
204 generating the necessary code (it actually emits the protect
205 expression twice, once for normal flow and once for the exception
206 case). As for the other handlers, the code for the exception case
207 will be emitted at the end of the function.
209 Cleanups can also be specified by using add_partial_entry (handler)
210 and end_protect_partials. add_partial_entry creates the start of
211 a new exception region; HANDLER will be invoked if an exception is
212 thrown with the context of the region between the calls to
213 add_partial_entry and end_protect_partials. end_protect_partials is
214 used to mark the end of these regions. add_partial_entry can be
215 called as many times as needed before calling end_protect_partials.
216 However, end_protect_partials should only be invoked once for each
217 group of calls to add_partial_entry as the entries are queued
218 and all of the outstanding entries are processed simultaneously
219 when end_protect_partials is invoked. Similarly to the other
220 handlers, the code for HANDLER will be emitted at the end of the
221 function.
223 The generated RTL for an exception region includes
224 NOTE_INSN_EH_REGION_BEG and NOTE_INSN_EH_REGION_END notes that mark
225 the start and end of the exception region. A unique label is also
226 generated at the start of the exception region, which is available
227 by looking at the ehstack variable. The topmost entry corresponds
228 to the current region.
230 In the current implementation, an exception can only be thrown from
231 a function call (since the mechanism used to actually throw an
232 exception involves calling __throw). If an exception region is
233 created but no function calls occur within that region, the region
234 can be safely optimized away (along with its exception handlers)
235 since no exceptions can ever be caught in that region. This
236 optimization is performed unless -fasynchronous-exceptions is
237 given. If the user wishes to throw from a signal handler, or other
238 asynchronous place, -fasynchronous-exceptions should be used when
239 compiling for maximally correct code, at the cost of additional
240 exception regions. Using -fasynchronous-exceptions only produces
241 code that is reasonably safe in such situations, but a correct
242 program cannot rely upon this working. It can be used in failsafe
243 code, where trying to continue on, and proceeding with potentially
244 incorrect results is better than halting the program.
247 Walking the stack:
249 The stack is walked by starting with a pointer to the current
250 frame, and finding the pointer to the callers frame. The unwind info
251 tells __throw how to find it.
253 Unwinding the stack:
255 When we use the term unwinding the stack, we mean undoing the
256 effects of the function prologue in a controlled fashion so that we
257 still have the flow of control. Otherwise, we could just return
258 (jump to the normal end of function epilogue).
260 This is done in __throw in libgcc2.c when we know that a handler exists
261 in a frame higher up the call stack than its immediate caller.
263 To unwind, we find the unwind data associated with the frame, if any.
264 If we don't find any, we call the library routine __terminate. If we do
265 find it, we use the information to copy the saved register values from
266 that frame into the register save area in the frame for __throw, return
267 into a stub which updates the stack pointer, and jump to the handler.
268 The normal function epilogue for __throw handles restoring the saved
269 values into registers.
271 When unwinding, we use this method if we know it will
272 work (if DWARF2_UNWIND_INFO is defined). Otherwise, we know that
273 an inline unwinder will have been emitted for any function that
274 __unwind_function cannot unwind. The inline unwinder appears as a
275 normal exception handler for the entire function, for any function
276 that we know cannot be unwound by __unwind_function. We inform the
277 compiler of whether a function can be unwound with
278 __unwind_function by having DOESNT_NEED_UNWINDER evaluate to true
279 when the unwinder isn't needed. __unwind_function is used as an
280 action of last resort. If no other method can be used for
281 unwinding, __unwind_function is used. If it cannot unwind, it
282 should call __terminate.
284 By default, if the target-specific backend doesn't supply a definition
285 for __unwind_function and doesn't support DWARF2_UNWIND_INFO, inlined
286 unwinders will be used instead. The main tradeoff here is in text space
287 utilization. Obviously, if inline unwinders have to be generated
288 repeatedly, this uses much more space than if a single routine is used.
290 However, it is simply not possible on some platforms to write a
291 generalized routine for doing stack unwinding without having some
292 form of additional data associated with each function. The current
293 implementation can encode this data in the form of additional
294 machine instructions or as static data in tabular form. The later
295 is called the unwind data.
297 The backend macro DOESNT_NEED_UNWINDER is used to conditionalize whether
298 or not per-function unwinders are needed. If DOESNT_NEED_UNWINDER is
299 defined and has a non-zero value, a per-function unwinder is not emitted
300 for the current function. If the static unwind data is supported, then
301 a per-function unwinder is not emitted.
303 On some platforms it is possible that neither __unwind_function
304 nor inlined unwinders are available. For these platforms it is not
305 possible to throw through a function call, and abort will be
306 invoked instead of performing the throw.
308 The reason the unwind data may be needed is that on some platforms
309 the order and types of data stored on the stack can vary depending
310 on the type of function, its arguments and returned values, and the
311 compilation options used (optimization versus non-optimization,
312 -fomit-frame-pointer, processor variations, etc).
314 Unfortunately, this also means that throwing through functions that
315 aren't compiled with exception handling support will still not be
316 possible on some platforms. This problem is currently being
317 investigated, but no solutions have been found that do not imply
318 some unacceptable performance penalties.
320 Future directions:
322 Currently __throw makes no differentiation between cleanups and
323 user-defined exception regions. While this makes the implementation
324 simple, it also implies that it is impossible to determine if a
325 user-defined exception handler exists for a given exception without
326 completely unwinding the stack in the process. This is undesirable
327 from the standpoint of debugging, as ideally it would be possible
328 to trap unhandled exceptions in the debugger before the process of
329 unwinding has even started.
331 This problem can be solved by marking user-defined handlers in a
332 special way (probably by adding additional bits to exception_table_list).
333 A two-pass scheme could then be used by __throw to iterate
334 through the table. The first pass would search for a relevant
335 user-defined handler for the current context of the throw, and if
336 one is found, the second pass would then invoke all needed cleanups
337 before jumping to the user-defined handler.
339 Many languages (including C++ and Ada) make execution of a
340 user-defined handler conditional on the "type" of the exception
341 thrown. (The type of the exception is actually the type of the data
342 that is thrown with the exception.) It will thus be necessary for
343 __throw to be able to determine if a given user-defined
344 exception handler will actually be executed, given the type of
345 exception.
347 One scheme is to add additional information to exception_table_list
348 as to the types of exceptions accepted by each handler. __throw
349 can do the type comparisons and then determine if the handler is
350 actually going to be executed.
352 There is currently no significant level of debugging support
353 available, other than to place a breakpoint on __throw. While
354 this is sufficient in most cases, it would be helpful to be able to
355 know where a given exception was going to be thrown to before it is
356 actually thrown, and to be able to choose between stopping before
357 every exception region (including cleanups), or just user-defined
358 exception regions. This should be possible to do in the two-pass
359 scheme by adding additional labels to __throw for appropriate
360 breakpoints, and additional debugger commands could be added to
361 query various state variables to determine what actions are to be
362 performed next.
364 Another major problem that is being worked on is the issue with stack
365 unwinding on various platforms. Currently the only platforms that have
366 support for the generation of a generic unwinder are the SPARC and MIPS.
367 All other ports require per-function unwinders, which produce large
368 amounts of code bloat.
370 For setjmp/longjmp based exception handling, some of the details
371 are as above, but there are some additional details. This section
372 discusses the details.
374 We don't use NOTE_INSN_EH_REGION_{BEG,END} pairs. We don't
375 optimize EH regions yet. We don't have to worry about machine
376 specific issues with unwinding the stack, as we rely upon longjmp
377 for all the machine specific details. There is no variable context
378 of a throw, just the one implied by the dynamic handler stack
379 pointed to by the dynamic handler chain. There is no exception
380 table, and no calls to __register_exceptions. __sjthrow is used
381 instead of __throw, and it works by using the dynamic handler
382 chain, and longjmp. -fasynchronous-exceptions has no effect, as
383 the elimination of trivial exception regions is not yet performed.
385 A frontend can set protect_cleanup_actions_with_terminate when all
386 the cleanup actions should be protected with an EH region that
387 calls terminate when an unhandled exception is throw. C++ does
388 this, Ada does not. */
391 #include "config.h"
392 #include "defaults.h"
393 #include "eh-common.h"
394 #include "system.h"
395 #include "rtl.h"
396 #include "tree.h"
397 #include "flags.h"
398 #include "except.h"
399 #include "function.h"
400 #include "insn-flags.h"
401 #include "expr.h"
402 #include "insn-codes.h"
403 #include "regs.h"
404 #include "hard-reg-set.h"
405 #include "insn-config.h"
406 #include "recog.h"
407 #include "output.h"
408 #include "toplev.h"
409 #include "intl.h"
410 #include "obstack.h"
411 #include "ggc.h"
413 /* One to use setjmp/longjmp method of generating code for exception
414 handling. */
416 int exceptions_via_longjmp = 2;
418 /* One to enable asynchronous exception support. */
420 int asynchronous_exceptions = 0;
422 /* One to protect cleanup actions with a handler that calls
423 __terminate, zero otherwise. */
425 int protect_cleanup_actions_with_terminate;
427 /* A list of labels used for exception handlers. Created by
428 find_exception_handler_labels for the optimization passes. */
430 rtx exception_handler_labels;
432 /* Keeps track of the label used as the context of a throw to rethrow an
433 exception to the outer exception region. */
435 struct label_node *outer_context_label_stack = NULL;
437 /* Pseudos used to hold exception return data in the interim between
438 __builtin_eh_return and the end of the function. */
440 static rtx eh_return_context;
441 static rtx eh_return_stack_adjust;
442 static rtx eh_return_handler;
444 /* This is used for targets which can call rethrow with an offset instead
445 of an address. This is subtracted from the rethrow label we are
446 interested in. */
448 static rtx first_rethrow_symbol = NULL_RTX;
449 static rtx final_rethrow = NULL_RTX;
450 static rtx last_rethrow_symbol = NULL_RTX;
453 /* Prototypes for local functions. */
455 static void push_eh_entry PROTO((struct eh_stack *));
456 static struct eh_entry * pop_eh_entry PROTO((struct eh_stack *));
457 static void enqueue_eh_entry PROTO((struct eh_queue *, struct eh_entry *));
458 static struct eh_entry * dequeue_eh_entry PROTO((struct eh_queue *));
459 static rtx call_get_eh_context PROTO((void));
460 static void start_dynamic_cleanup PROTO((tree, tree));
461 static void start_dynamic_handler PROTO((void));
462 static void expand_rethrow PROTO((rtx));
463 static void output_exception_table_entry PROTO((FILE *, int));
464 static int can_throw PROTO((rtx));
465 static rtx scan_region PROTO((rtx, int, int *));
466 static void eh_regs PROTO((rtx *, rtx *, rtx *, int));
467 static void set_insn_eh_region PROTO((rtx *, int));
468 #ifdef DONT_USE_BUILTIN_SETJMP
469 static void jumpif_rtx PROTO((rtx, rtx));
470 #endif
471 static void mark_eh_node PROTO((struct eh_node *));
472 static void mark_eh_stack PROTO((struct eh_stack *));
473 static void mark_eh_queue PROTO((struct eh_queue *));
474 static void mark_tree_label_node PROTO ((struct label_node *));
475 static void mark_func_eh_entry PROTO ((void *));
476 static rtx create_rethrow_ref PROTO ((int));
477 static void push_entry PROTO ((struct eh_stack *, struct eh_entry*));
478 static void receive_exception_label PROTO ((rtx));
479 static int new_eh_region_entry PROTO ((int, rtx));
480 static int find_func_region PROTO ((int));
481 static void clear_function_eh_region PROTO ((void));
482 static void process_nestinfo PROTO ((int, eh_nesting_info *, int *));
484 rtx expand_builtin_return_addr PROTO((enum built_in_function, int, rtx));
486 /* Various support routines to manipulate the various data structures
487 used by the exception handling code. */
489 extern struct obstack permanent_obstack;
491 /* Generate a SYMBOL_REF for rethrow to use */
492 static rtx
493 create_rethrow_ref (region_num)
494 int region_num;
496 rtx def;
497 char *ptr;
498 char buf[60];
500 push_obstacks_nochange ();
501 end_temporary_allocation ();
503 ASM_GENERATE_INTERNAL_LABEL (buf, "LRTH", region_num);
504 ptr = ggc_alloc_string (buf, -1);
505 def = gen_rtx_SYMBOL_REF (Pmode, ptr);
506 SYMBOL_REF_NEED_ADJUST (def) = 1;
508 pop_obstacks ();
509 return def;
512 /* Push a label entry onto the given STACK. */
514 void
515 push_label_entry (stack, rlabel, tlabel)
516 struct label_node **stack;
517 rtx rlabel;
518 tree tlabel;
520 struct label_node *newnode
521 = (struct label_node *) xmalloc (sizeof (struct label_node));
523 if (rlabel)
524 newnode->u.rlabel = rlabel;
525 else
526 newnode->u.tlabel = tlabel;
527 newnode->chain = *stack;
528 *stack = newnode;
531 /* Pop a label entry from the given STACK. */
534 pop_label_entry (stack)
535 struct label_node **stack;
537 rtx label;
538 struct label_node *tempnode;
540 if (! *stack)
541 return NULL_RTX;
543 tempnode = *stack;
544 label = tempnode->u.rlabel;
545 *stack = (*stack)->chain;
546 free (tempnode);
548 return label;
551 /* Return the top element of the given STACK. */
553 tree
554 top_label_entry (stack)
555 struct label_node **stack;
557 if (! *stack)
558 return NULL_TREE;
560 return (*stack)->u.tlabel;
563 /* get an exception label. These must be on the permanent obstack */
566 gen_exception_label ()
568 rtx lab;
569 lab = gen_label_rtx ();
570 return lab;
573 /* Push a new eh_node entry onto STACK. */
575 static void
576 push_eh_entry (stack)
577 struct eh_stack *stack;
579 struct eh_node *node = (struct eh_node *) xmalloc (sizeof (struct eh_node));
580 struct eh_entry *entry = (struct eh_entry *) xmalloc (sizeof (struct eh_entry));
582 rtx rlab = gen_exception_label ();
583 entry->finalization = NULL_TREE;
584 entry->label_used = 0;
585 entry->exception_handler_label = rlab;
586 entry->false_label = NULL_RTX;
587 if (! flag_new_exceptions)
588 entry->outer_context = gen_label_rtx ();
589 else
590 entry->outer_context = create_rethrow_ref (CODE_LABEL_NUMBER (rlab));
591 entry->rethrow_label = entry->outer_context;
593 node->entry = entry;
594 node->chain = stack->top;
595 stack->top = node;
598 /* push an existing entry onto a stack. */
599 static void
600 push_entry (stack, entry)
601 struct eh_stack *stack;
602 struct eh_entry *entry;
604 struct eh_node *node = (struct eh_node *) xmalloc (sizeof (struct eh_node));
605 node->entry = entry;
606 node->chain = stack->top;
607 stack->top = node;
610 /* Pop an entry from the given STACK. */
612 static struct eh_entry *
613 pop_eh_entry (stack)
614 struct eh_stack *stack;
616 struct eh_node *tempnode;
617 struct eh_entry *tempentry;
619 tempnode = stack->top;
620 tempentry = tempnode->entry;
621 stack->top = stack->top->chain;
622 free (tempnode);
624 return tempentry;
627 /* Enqueue an ENTRY onto the given QUEUE. */
629 static void
630 enqueue_eh_entry (queue, entry)
631 struct eh_queue *queue;
632 struct eh_entry *entry;
634 struct eh_node *node = (struct eh_node *) xmalloc (sizeof (struct eh_node));
636 node->entry = entry;
637 node->chain = NULL;
639 if (queue->head == NULL)
641 queue->head = node;
643 else
645 queue->tail->chain = node;
647 queue->tail = node;
650 /* Dequeue an entry from the given QUEUE. */
652 static struct eh_entry *
653 dequeue_eh_entry (queue)
654 struct eh_queue *queue;
656 struct eh_node *tempnode;
657 struct eh_entry *tempentry;
659 if (queue->head == NULL)
660 return NULL;
662 tempnode = queue->head;
663 queue->head = queue->head->chain;
665 tempentry = tempnode->entry;
666 free (tempnode);
668 return tempentry;
671 static void
672 receive_exception_label (handler_label)
673 rtx handler_label;
675 emit_label (handler_label);
677 #ifdef HAVE_exception_receiver
678 if (! exceptions_via_longjmp)
679 if (HAVE_exception_receiver)
680 emit_insn (gen_exception_receiver ());
681 #endif
683 #ifdef HAVE_nonlocal_goto_receiver
684 if (! exceptions_via_longjmp)
685 if (HAVE_nonlocal_goto_receiver)
686 emit_insn (gen_nonlocal_goto_receiver ());
687 #endif
691 struct func_eh_entry
693 int range_number; /* EH region number from EH NOTE insn's. */
694 rtx rethrow_label; /* Label for rethrow. */
695 int rethrow_ref; /* Is rethrow referenced? */
696 struct handler_info *handlers;
700 /* table of function eh regions */
701 static struct func_eh_entry *function_eh_regions = NULL;
702 static int num_func_eh_entries = 0;
703 static int current_func_eh_entry = 0;
705 #define SIZE_FUNC_EH(X) (sizeof (struct func_eh_entry) * X)
707 /* Add a new eh_entry for this function, and base it off of the information
708 in the EH_ENTRY parameter. A NULL parameter is invalid.
709 OUTER_CONTEXT is a label which is used for rethrowing. The number
710 returned is an number which uniquely identifies this exception range. */
712 static int
713 new_eh_region_entry (note_eh_region, rethrow)
714 int note_eh_region;
715 rtx rethrow;
717 if (current_func_eh_entry == num_func_eh_entries)
719 if (num_func_eh_entries == 0)
721 function_eh_regions =
722 (struct func_eh_entry *) xmalloc (SIZE_FUNC_EH (50));
723 num_func_eh_entries = 50;
725 else
727 num_func_eh_entries = num_func_eh_entries * 3 / 2;
728 function_eh_regions = (struct func_eh_entry *)
729 xrealloc (function_eh_regions, SIZE_FUNC_EH (num_func_eh_entries));
732 function_eh_regions[current_func_eh_entry].range_number = note_eh_region;
733 if (rethrow == NULL_RTX)
734 function_eh_regions[current_func_eh_entry].rethrow_label =
735 create_rethrow_ref (note_eh_region);
736 else
737 function_eh_regions[current_func_eh_entry].rethrow_label = rethrow;
738 function_eh_regions[current_func_eh_entry].handlers = NULL;
740 return current_func_eh_entry++;
743 /* Add new handler information to an exception range. The first parameter
744 specifies the range number (returned from new_eh_entry()). The second
745 parameter specifies the handler. By default the handler is inserted at
746 the end of the list. A handler list may contain only ONE NULL_TREE
747 typeinfo entry. Regardless where it is positioned, a NULL_TREE entry
748 is always output as the LAST handler in the exception table for a region. */
750 void
751 add_new_handler (region, newhandler)
752 int region;
753 struct handler_info *newhandler;
755 struct handler_info *last;
757 newhandler->next = NULL;
758 last = function_eh_regions[region].handlers;
759 if (last == NULL)
760 function_eh_regions[region].handlers = newhandler;
761 else
763 for ( ; ; last = last->next)
765 if (last->type_info == CATCH_ALL_TYPE)
766 pedwarn ("additional handler after ...");
767 if (last->next == NULL)
768 break;
770 last->next = newhandler;
774 /* Remove a handler label. The handler label is being deleted, so all
775 regions which reference this handler should have it removed from their
776 list of possible handlers. Any region which has the final handler
777 removed can be deleted. */
779 void remove_handler (removing_label)
780 rtx removing_label;
782 struct handler_info *handler, *last;
783 int x;
784 for (x = 0 ; x < current_func_eh_entry; ++x)
786 last = NULL;
787 handler = function_eh_regions[x].handlers;
788 for ( ; handler; last = handler, handler = handler->next)
789 if (handler->handler_label == removing_label)
791 if (last)
793 last->next = handler->next;
794 handler = last;
796 else
797 function_eh_regions[x].handlers = handler->next;
802 /* This function will return a malloc'd pointer to an array of
803 void pointer representing the runtime match values that
804 currently exist in all regions. */
806 int
807 find_all_handler_type_matches (array)
808 void ***array;
810 struct handler_info *handler, *last;
811 int x,y;
812 void *val;
813 void **ptr;
814 int max_ptr;
815 int n_ptr = 0;
817 *array = NULL;
819 if (!doing_eh (0) || ! flag_new_exceptions)
820 return 0;
822 max_ptr = 100;
823 ptr = (void **) xmalloc (max_ptr * sizeof (void *));
825 for (x = 0 ; x < current_func_eh_entry; x++)
827 last = NULL;
828 handler = function_eh_regions[x].handlers;
829 for ( ; handler; last = handler, handler = handler->next)
831 val = handler->type_info;
832 if (val != NULL && val != CATCH_ALL_TYPE)
834 /* See if this match value has already been found. */
835 for (y = 0; y < n_ptr; y++)
836 if (ptr[y] == val)
837 break;
839 /* If we break early, we already found this value. */
840 if (y < n_ptr)
841 continue;
843 /* Do we need to allocate more space? */
844 if (n_ptr >= max_ptr)
846 max_ptr += max_ptr / 2;
847 ptr = (void **) xrealloc (ptr, max_ptr * sizeof (void *));
849 ptr[n_ptr] = val;
850 n_ptr++;
855 if (n_ptr == 0)
857 free (ptr);
858 ptr = NULL;
860 *array = ptr;
861 return n_ptr;
864 /* Create a new handler structure initialized with the handler label and
865 typeinfo fields passed in. */
867 struct handler_info *
868 get_new_handler (handler, typeinfo)
869 rtx handler;
870 void *typeinfo;
872 struct handler_info* ptr;
873 ptr = (struct handler_info *) xmalloc (sizeof (struct handler_info));
874 ptr->handler_label = handler;
875 ptr->handler_number = CODE_LABEL_NUMBER (handler);
876 ptr->type_info = typeinfo;
877 ptr->next = NULL;
879 return ptr;
884 /* Find the index in function_eh_regions associated with a NOTE region. If
885 the region cannot be found, a -1 is returned. This should never happen! */
887 static int
888 find_func_region (insn_region)
889 int insn_region;
891 int x;
892 for (x = 0; x < current_func_eh_entry; x++)
893 if (function_eh_regions[x].range_number == insn_region)
894 return x;
896 return -1;
899 /* Get a pointer to the first handler in an exception region's list. */
901 struct handler_info *
902 get_first_handler (region)
903 int region;
905 return function_eh_regions[find_func_region (region)].handlers;
908 /* Clean out the function_eh_region table and free all memory */
910 static void
911 clear_function_eh_region ()
913 int x;
914 struct handler_info *ptr, *next;
915 for (x = 0; x < current_func_eh_entry; x++)
916 for (ptr = function_eh_regions[x].handlers; ptr != NULL; ptr = next)
918 next = ptr->next;
919 free (ptr);
921 free (function_eh_regions);
922 num_func_eh_entries = 0;
923 current_func_eh_entry = 0;
926 /* Make a duplicate of an exception region by copying all the handlers
927 for an exception region. Return the new handler index. The final
928 parameter is a routine which maps old labels to new ones. */
930 int
931 duplicate_eh_handlers (old_note_eh_region, new_note_eh_region, map)
932 int old_note_eh_region, new_note_eh_region;
933 rtx (*map) PARAMS ((rtx));
935 struct handler_info *ptr, *new_ptr;
936 int new_region, region;
938 region = find_func_region (old_note_eh_region);
939 if (region == -1)
940 fatal ("Cannot duplicate non-existant exception region.");
942 /* duplicate_eh_handlers may have been called during a symbol remap. */
943 new_region = find_func_region (new_note_eh_region);
944 if (new_region != -1)
945 return (new_region);
947 new_region = new_eh_region_entry (new_note_eh_region, NULL_RTX);
949 ptr = function_eh_regions[region].handlers;
951 for ( ; ptr; ptr = ptr->next)
953 new_ptr = get_new_handler (map (ptr->handler_label), ptr->type_info);
954 add_new_handler (new_region, new_ptr);
957 return new_region;
961 /* Given a rethrow symbol, find the EH region number this is for. */
962 int
963 eh_region_from_symbol (sym)
964 rtx sym;
966 int x;
967 if (sym == last_rethrow_symbol)
968 return 1;
969 for (x = 0; x < current_func_eh_entry; x++)
970 if (function_eh_regions[x].rethrow_label == sym)
971 return function_eh_regions[x].range_number;
972 return -1;
976 /* When inlining/unrolling, we have to map the symbols passed to
977 __rethrow as well. This performs the remap. If a symbol isn't foiund,
978 the original one is returned. This is not an efficient routine,
979 so don't call it on everything!! */
980 rtx
981 rethrow_symbol_map (sym, map)
982 rtx sym;
983 rtx (*map) PARAMS ((rtx));
985 int x, y;
986 for (x = 0; x < current_func_eh_entry; x++)
987 if (function_eh_regions[x].rethrow_label == sym)
989 /* We've found the original region, now lets determine which region
990 this now maps to. */
991 rtx l1 = function_eh_regions[x].handlers->handler_label;
992 rtx l2 = map (l1);
993 y = CODE_LABEL_NUMBER (l2); /* This is the new region number */
994 x = find_func_region (y); /* Get the new permanent region */
995 if (x == -1) /* Hmm, Doesn't exist yet */
997 x = duplicate_eh_handlers (CODE_LABEL_NUMBER (l1), y, map);
998 /* Since we're mapping it, it must be used. */
999 function_eh_regions[x].rethrow_ref = 1;
1001 return function_eh_regions[x].rethrow_label;
1003 return sym;
1006 int
1007 rethrow_used (region)
1008 int region;
1010 if (flag_new_exceptions)
1012 int ret = function_eh_regions[find_func_region (region)].rethrow_ref;
1013 return ret;
1015 return 0;
1019 /* Routine to see if exception handling is turned on.
1020 DO_WARN is non-zero if we want to inform the user that exception
1021 handling is turned off.
1023 This is used to ensure that -fexceptions has been specified if the
1024 compiler tries to use any exception-specific functions. */
1027 doing_eh (do_warn)
1028 int do_warn;
1030 if (! flag_exceptions)
1032 static int warned = 0;
1033 if (! warned && do_warn)
1035 error ("exception handling disabled, use -fexceptions to enable");
1036 warned = 1;
1038 return 0;
1040 return 1;
1043 /* Given a return address in ADDR, determine the address we should use
1044 to find the corresponding EH region. */
1047 eh_outer_context (addr)
1048 rtx addr;
1050 /* First mask out any unwanted bits. */
1051 #ifdef MASK_RETURN_ADDR
1052 expand_and (addr, MASK_RETURN_ADDR, addr);
1053 #endif
1055 /* Then adjust to find the real return address. */
1056 #if defined (RETURN_ADDR_OFFSET)
1057 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
1058 #endif
1060 return addr;
1063 /* Start a new exception region for a region of code that has a
1064 cleanup action and push the HANDLER for the region onto
1065 protect_list. All of the regions created with add_partial_entry
1066 will be ended when end_protect_partials is invoked. */
1068 void
1069 add_partial_entry (handler)
1070 tree handler;
1072 expand_eh_region_start ();
1074 /* Make sure the entry is on the correct obstack. */
1075 push_obstacks_nochange ();
1076 resume_temporary_allocation ();
1078 /* Because this is a cleanup action, we may have to protect the handler
1079 with __terminate. */
1080 handler = protect_with_terminate (handler);
1082 protect_list = tree_cons (NULL_TREE, handler, protect_list);
1083 pop_obstacks ();
1086 /* Emit code to get EH context to current function. */
1088 static rtx
1089 call_get_eh_context ()
1091 static tree fn;
1092 tree expr;
1094 if (fn == NULL_TREE)
1096 tree fntype;
1097 fn = get_identifier ("__get_eh_context");
1098 push_obstacks_nochange ();
1099 end_temporary_allocation ();
1100 fntype = build_pointer_type (build_pointer_type
1101 (build_pointer_type (void_type_node)));
1102 fntype = build_function_type (fntype, NULL_TREE);
1103 fn = build_decl (FUNCTION_DECL, fn, fntype);
1104 DECL_EXTERNAL (fn) = 1;
1105 TREE_PUBLIC (fn) = 1;
1106 DECL_ARTIFICIAL (fn) = 1;
1107 TREE_READONLY (fn) = 1;
1108 make_decl_rtl (fn, NULL_PTR, 1);
1109 assemble_external (fn);
1110 pop_obstacks ();
1112 ggc_add_tree_root (&fn, 1);
1115 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
1116 expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
1117 expr, NULL_TREE, NULL_TREE);
1118 TREE_SIDE_EFFECTS (expr) = 1;
1120 return copy_to_reg (expand_expr (expr, NULL_RTX, VOIDmode, 0));
1123 /* Get a reference to the EH context.
1124 We will only generate a register for the current function EH context here,
1125 and emit a USE insn to mark that this is a EH context register.
1127 Later, emit_eh_context will emit needed call to __get_eh_context
1128 in libgcc2, and copy the value to the register we have generated. */
1131 get_eh_context ()
1133 if (current_function_ehc == 0)
1135 rtx insn;
1137 current_function_ehc = gen_reg_rtx (Pmode);
1139 insn = gen_rtx_USE (GET_MODE (current_function_ehc),
1140 current_function_ehc);
1141 insn = emit_insn_before (insn, get_first_nonparm_insn ());
1143 REG_NOTES (insn)
1144 = gen_rtx_EXPR_LIST (REG_EH_CONTEXT, current_function_ehc,
1145 REG_NOTES (insn));
1147 return current_function_ehc;
1150 /* Get a reference to the dynamic handler chain. It points to the
1151 pointer to the next element in the dynamic handler chain. It ends
1152 when there are no more elements in the dynamic handler chain, when
1153 the value is &top_elt from libgcc2.c. Immediately after the
1154 pointer, is an area suitable for setjmp/longjmp when
1155 DONT_USE_BUILTIN_SETJMP is defined, and an area suitable for
1156 __builtin_setjmp/__builtin_longjmp when DONT_USE_BUILTIN_SETJMP
1157 isn't defined. */
1160 get_dynamic_handler_chain ()
1162 rtx ehc, dhc, result;
1164 ehc = get_eh_context ();
1166 /* This is the offset of dynamic_handler_chain in the eh_context struct
1167 declared in eh-common.h. If its location is change, change this offset */
1168 dhc = plus_constant (ehc, POINTER_SIZE / BITS_PER_UNIT);
1170 result = copy_to_reg (dhc);
1172 /* We don't want a copy of the dcc, but rather, the single dcc. */
1173 return gen_rtx_MEM (Pmode, result);
1176 /* Get a reference to the dynamic cleanup chain. It points to the
1177 pointer to the next element in the dynamic cleanup chain.
1178 Immediately after the pointer, are two Pmode variables, one for a
1179 pointer to a function that performs the cleanup action, and the
1180 second, the argument to pass to that function. */
1183 get_dynamic_cleanup_chain ()
1185 rtx dhc, dcc, result;
1187 dhc = get_dynamic_handler_chain ();
1188 dcc = plus_constant (dhc, POINTER_SIZE / BITS_PER_UNIT);
1190 result = copy_to_reg (dcc);
1192 /* We don't want a copy of the dcc, but rather, the single dcc. */
1193 return gen_rtx_MEM (Pmode, result);
1196 #ifdef DONT_USE_BUILTIN_SETJMP
1197 /* Generate code to evaluate X and jump to LABEL if the value is nonzero.
1198 LABEL is an rtx of code CODE_LABEL, in this function. */
1200 static void
1201 jumpif_rtx (x, label)
1202 rtx x;
1203 rtx label;
1205 jumpif (make_tree (type_for_mode (GET_MODE (x), 0), x), label);
1207 #endif
1209 /* Start a dynamic cleanup on the EH runtime dynamic cleanup stack.
1210 We just need to create an element for the cleanup list, and push it
1211 into the chain.
1213 A dynamic cleanup is a cleanup action implied by the presence of an
1214 element on the EH runtime dynamic cleanup stack that is to be
1215 performed when an exception is thrown. The cleanup action is
1216 performed by __sjthrow when an exception is thrown. Only certain
1217 actions can be optimized into dynamic cleanup actions. For the
1218 restrictions on what actions can be performed using this routine,
1219 see expand_eh_region_start_tree. */
1221 static void
1222 start_dynamic_cleanup (func, arg)
1223 tree func;
1224 tree arg;
1226 rtx dcc;
1227 rtx new_func, new_arg;
1228 rtx x, buf;
1229 int size;
1231 /* We allocate enough room for a pointer to the function, and
1232 one argument. */
1233 size = 2;
1235 /* XXX, FIXME: The stack space allocated this way is too long lived,
1236 but there is no allocation routine that allocates at the level of
1237 the last binding contour. */
1238 buf = assign_stack_local (BLKmode,
1239 GET_MODE_SIZE (Pmode)*(size+1),
1242 buf = change_address (buf, Pmode, NULL_RTX);
1244 /* Store dcc into the first word of the newly allocated buffer. */
1246 dcc = get_dynamic_cleanup_chain ();
1247 emit_move_insn (buf, dcc);
1249 /* Store func and arg into the cleanup list element. */
1251 new_func = gen_rtx_MEM (Pmode, plus_constant (XEXP (buf, 0),
1252 GET_MODE_SIZE (Pmode)));
1253 new_arg = gen_rtx_MEM (Pmode, plus_constant (XEXP (buf, 0),
1254 GET_MODE_SIZE (Pmode)*2));
1255 x = expand_expr (func, new_func, Pmode, 0);
1256 if (x != new_func)
1257 emit_move_insn (new_func, x);
1259 x = expand_expr (arg, new_arg, Pmode, 0);
1260 if (x != new_arg)
1261 emit_move_insn (new_arg, x);
1263 /* Update the cleanup chain. */
1265 x = force_operand (XEXP (buf, 0), dcc);
1266 if (x != dcc)
1267 emit_move_insn (dcc, x);
1270 /* Emit RTL to start a dynamic handler on the EH runtime dynamic
1271 handler stack. This should only be used by expand_eh_region_start
1272 or expand_eh_region_start_tree. */
1274 static void
1275 start_dynamic_handler ()
1277 rtx dhc, dcc;
1278 rtx x, arg, buf;
1279 int size;
1281 #ifndef DONT_USE_BUILTIN_SETJMP
1282 /* The number of Pmode words for the setjmp buffer, when using the
1283 builtin setjmp/longjmp, see expand_builtin, case BUILT_IN_LONGJMP. */
1284 /* We use 2 words here before calling expand_builtin_setjmp.
1285 expand_builtin_setjmp uses 2 words, and then calls emit_stack_save.
1286 emit_stack_save needs space of size STACK_SAVEAREA_MODE (SAVE_NONLOCAL).
1287 Subtract one, because the assign_stack_local call below adds 1. */
1288 size = (2 + 2 + (GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL))
1289 / GET_MODE_SIZE (Pmode))
1290 - 1);
1291 #else
1292 #ifdef JMP_BUF_SIZE
1293 size = JMP_BUF_SIZE;
1294 #else
1295 /* Should be large enough for most systems, if it is not,
1296 JMP_BUF_SIZE should be defined with the proper value. It will
1297 also tend to be larger than necessary for most systems, a more
1298 optimal port will define JMP_BUF_SIZE. */
1299 size = FIRST_PSEUDO_REGISTER+2;
1300 #endif
1301 #endif
1302 /* XXX, FIXME: The stack space allocated this way is too long lived,
1303 but there is no allocation routine that allocates at the level of
1304 the last binding contour. */
1305 arg = assign_stack_local (BLKmode,
1306 GET_MODE_SIZE (Pmode)*(size+1),
1309 arg = change_address (arg, Pmode, NULL_RTX);
1311 /* Store dhc into the first word of the newly allocated buffer. */
1313 dhc = get_dynamic_handler_chain ();
1314 dcc = gen_rtx_MEM (Pmode, plus_constant (XEXP (arg, 0),
1315 GET_MODE_SIZE (Pmode)));
1316 emit_move_insn (arg, dhc);
1318 /* Zero out the start of the cleanup chain. */
1319 emit_move_insn (dcc, const0_rtx);
1321 /* The jmpbuf starts two words into the area allocated. */
1322 buf = plus_constant (XEXP (arg, 0), GET_MODE_SIZE (Pmode)*2);
1324 #ifdef DONT_USE_BUILTIN_SETJMP
1325 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, 1, SImode, 1,
1326 buf, Pmode);
1327 /* If we come back here for a catch, transfer control to the handler. */
1328 jumpif_rtx (x, ehstack.top->entry->exception_handler_label);
1329 #else
1331 /* A label to continue execution for the no exception case. */
1332 rtx noex = gen_label_rtx();
1333 x = expand_builtin_setjmp (buf, NULL_RTX, noex,
1334 ehstack.top->entry->exception_handler_label);
1335 emit_label (noex);
1337 #endif
1339 /* We are committed to this, so update the handler chain. */
1341 emit_move_insn (dhc, force_operand (XEXP (arg, 0), NULL_RTX));
1344 /* Start an exception handling region for the given cleanup action.
1345 All instructions emitted after this point are considered to be part
1346 of the region until expand_eh_region_end is invoked. CLEANUP is
1347 the cleanup action to perform. The return value is true if the
1348 exception region was optimized away. If that case,
1349 expand_eh_region_end does not need to be called for this cleanup,
1350 nor should it be.
1352 This routine notices one particular common case in C++ code
1353 generation, and optimizes it so as to not need the exception
1354 region. It works by creating a dynamic cleanup action, instead of
1355 a using an exception region. */
1358 expand_eh_region_start_tree (decl, cleanup)
1359 tree decl;
1360 tree cleanup;
1362 /* This is the old code. */
1363 if (! doing_eh (0))
1364 return 0;
1366 /* The optimization only applies to actions protected with
1367 terminate, and only applies if we are using the setjmp/longjmp
1368 codegen method. */
1369 if (exceptions_via_longjmp
1370 && protect_cleanup_actions_with_terminate)
1372 tree func, arg;
1373 tree args;
1375 /* Ignore any UNSAVE_EXPR. */
1376 if (TREE_CODE (cleanup) == UNSAVE_EXPR)
1377 cleanup = TREE_OPERAND (cleanup, 0);
1379 /* Further, it only applies if the action is a call, if there
1380 are 2 arguments, and if the second argument is 2. */
1382 if (TREE_CODE (cleanup) == CALL_EXPR
1383 && (args = TREE_OPERAND (cleanup, 1))
1384 && (func = TREE_OPERAND (cleanup, 0))
1385 && (arg = TREE_VALUE (args))
1386 && (args = TREE_CHAIN (args))
1388 /* is the second argument 2? */
1389 && TREE_CODE (TREE_VALUE (args)) == INTEGER_CST
1390 && TREE_INT_CST_LOW (TREE_VALUE (args)) == 2
1391 && TREE_INT_CST_HIGH (TREE_VALUE (args)) == 0
1393 /* Make sure there are no other arguments. */
1394 && TREE_CHAIN (args) == NULL_TREE)
1396 /* Arrange for returns and gotos to pop the entry we make on the
1397 dynamic cleanup stack. */
1398 expand_dcc_cleanup (decl);
1399 start_dynamic_cleanup (func, arg);
1400 return 1;
1404 expand_eh_region_start_for_decl (decl);
1405 ehstack.top->entry->finalization = cleanup;
1407 return 0;
1410 /* Just like expand_eh_region_start, except if a cleanup action is
1411 entered on the cleanup chain, the TREE_PURPOSE of the element put
1412 on the chain is DECL. DECL should be the associated VAR_DECL, if
1413 any, otherwise it should be NULL_TREE. */
1415 void
1416 expand_eh_region_start_for_decl (decl)
1417 tree decl;
1419 rtx note;
1421 /* This is the old code. */
1422 if (! doing_eh (0))
1423 return;
1425 /* We need a new block to record the start and end of the
1426 dynamic handler chain. We also want to prevent jumping into
1427 a try block. */
1428 expand_start_bindings (2);
1430 /* But we don't need or want a new temporary level. */
1431 pop_temp_slots ();
1433 /* Mark this block as created by expand_eh_region_start. This
1434 is so that we can pop the block with expand_end_bindings
1435 automatically. */
1436 mark_block_as_eh_region ();
1438 if (exceptions_via_longjmp)
1440 /* Arrange for returns and gotos to pop the entry we make on the
1441 dynamic handler stack. */
1442 expand_dhc_cleanup (decl);
1445 push_eh_entry (&ehstack);
1446 note = emit_note (NULL_PTR, NOTE_INSN_EH_REGION_BEG);
1447 NOTE_EH_HANDLER (note)
1448 = CODE_LABEL_NUMBER (ehstack.top->entry->exception_handler_label);
1449 if (exceptions_via_longjmp)
1450 start_dynamic_handler ();
1453 /* Start an exception handling region. All instructions emitted after
1454 this point are considered to be part of the region until
1455 expand_eh_region_end is invoked. */
1457 void
1458 expand_eh_region_start ()
1460 expand_eh_region_start_for_decl (NULL_TREE);
1463 /* End an exception handling region. The information about the region
1464 is found on the top of ehstack.
1466 HANDLER is either the cleanup for the exception region, or if we're
1467 marking the end of a try block, HANDLER is integer_zero_node.
1469 HANDLER will be transformed to rtl when expand_leftover_cleanups
1470 is invoked. */
1472 void
1473 expand_eh_region_end (handler)
1474 tree handler;
1476 struct eh_entry *entry;
1477 rtx note;
1478 int ret, r;
1480 if (! doing_eh (0))
1481 return;
1483 entry = pop_eh_entry (&ehstack);
1485 note = emit_note (NULL_PTR, NOTE_INSN_EH_REGION_END);
1486 ret = NOTE_EH_HANDLER (note)
1487 = CODE_LABEL_NUMBER (entry->exception_handler_label);
1488 if (exceptions_via_longjmp == 0 && ! flag_new_exceptions
1489 /* We share outer_context between regions; only emit it once. */
1490 && INSN_UID (entry->outer_context) == 0)
1492 rtx label;
1494 label = gen_label_rtx ();
1495 emit_jump (label);
1497 /* Emit a label marking the end of this exception region that
1498 is used for rethrowing into the outer context. */
1499 emit_label (entry->outer_context);
1500 expand_internal_throw ();
1502 emit_label (label);
1505 entry->finalization = handler;
1507 /* create region entry in final exception table */
1508 r = new_eh_region_entry (NOTE_EH_HANDLER (note), entry->rethrow_label);
1510 enqueue_eh_entry (&ehqueue, entry);
1512 /* If we have already started ending the bindings, don't recurse. */
1513 if (is_eh_region ())
1515 /* Because we don't need or want a new temporary level and
1516 because we didn't create one in expand_eh_region_start,
1517 create a fake one now to avoid removing one in
1518 expand_end_bindings. */
1519 push_temp_slots ();
1521 mark_block_as_not_eh_region ();
1523 expand_end_bindings (NULL_TREE, 0, 0);
1527 /* End the EH region for a goto fixup. We only need them in the region-based
1528 EH scheme. */
1530 void
1531 expand_fixup_region_start ()
1533 if (! doing_eh (0) || exceptions_via_longjmp)
1534 return;
1536 expand_eh_region_start ();
1539 /* End the EH region for a goto fixup. CLEANUP is the cleanup we just
1540 expanded; to avoid running it twice if it throws, we look through the
1541 ehqueue for a matching region and rethrow from its outer_context. */
1543 void
1544 expand_fixup_region_end (cleanup)
1545 tree cleanup;
1547 struct eh_node *node;
1548 int dont_issue;
1550 if (! doing_eh (0) || exceptions_via_longjmp)
1551 return;
1553 for (node = ehstack.top; node && node->entry->finalization != cleanup; )
1554 node = node->chain;
1555 if (node == 0)
1556 for (node = ehqueue.head; node && node->entry->finalization != cleanup; )
1557 node = node->chain;
1558 if (node == 0)
1559 abort ();
1561 /* If the outer context label has not been issued yet, we don't want
1562 to issue it as a part of this region, unless this is the
1563 correct region for the outer context. If we did, then the label for
1564 the outer context will be WITHIN the begin/end labels,
1565 and we could get an infinte loop when it tried to rethrow, or just
1566 generally incorrect execution following a throw. */
1568 if (flag_new_exceptions)
1569 dont_issue = 0;
1570 else
1571 dont_issue = ((INSN_UID (node->entry->outer_context) == 0)
1572 && (ehstack.top->entry != node->entry));
1574 ehstack.top->entry->outer_context = node->entry->outer_context;
1576 /* Since we are rethrowing to the OUTER region, we know we don't need
1577 a jump around sequence for this region, so we'll pretend the outer
1578 context label has been issued by setting INSN_UID to 1, then clearing
1579 it again afterwards. */
1581 if (dont_issue)
1582 INSN_UID (node->entry->outer_context) = 1;
1584 /* Just rethrow. size_zero_node is just a NOP. */
1585 expand_eh_region_end (size_zero_node);
1587 if (dont_issue)
1588 INSN_UID (node->entry->outer_context) = 0;
1591 /* If we are using the setjmp/longjmp EH codegen method, we emit a
1592 call to __sjthrow.
1594 Otherwise, we emit a call to __throw and note that we threw
1595 something, so we know we need to generate the necessary code for
1596 __throw.
1598 Before invoking throw, the __eh_pc variable must have been set up
1599 to contain the PC being thrown from. This address is used by
1600 __throw to determine which exception region (if any) is
1601 responsible for handling the exception. */
1603 void
1604 emit_throw ()
1606 if (exceptions_via_longjmp)
1608 emit_library_call (sjthrow_libfunc, 0, VOIDmode, 0);
1610 else
1612 #ifdef JUMP_TO_THROW
1613 emit_indirect_jump (throw_libfunc);
1614 #else
1615 emit_library_call (throw_libfunc, 0, VOIDmode, 0);
1616 #endif
1618 emit_barrier ();
1621 /* Throw the current exception. If appropriate, this is done by jumping
1622 to the next handler. */
1624 void
1625 expand_internal_throw ()
1627 emit_throw ();
1630 /* Called from expand_exception_blocks and expand_end_catch_block to
1631 emit any pending handlers/cleanups queued from expand_eh_region_end. */
1633 void
1634 expand_leftover_cleanups ()
1636 struct eh_entry *entry;
1638 while ((entry = dequeue_eh_entry (&ehqueue)) != 0)
1640 rtx prev;
1642 /* A leftover try block. Shouldn't be one here. */
1643 if (entry->finalization == integer_zero_node)
1644 abort ();
1646 /* Output the label for the start of the exception handler. */
1648 receive_exception_label (entry->exception_handler_label);
1650 /* register a handler for this cleanup region */
1651 add_new_handler (
1652 find_func_region (CODE_LABEL_NUMBER (entry->exception_handler_label)),
1653 get_new_handler (entry->exception_handler_label, NULL));
1655 /* And now generate the insns for the handler. */
1656 expand_expr (entry->finalization, const0_rtx, VOIDmode, 0);
1658 prev = get_last_insn ();
1659 if (prev == NULL || GET_CODE (prev) != BARRIER)
1660 /* Emit code to throw to the outer context if we fall off
1661 the end of the handler. */
1662 expand_rethrow (entry->outer_context);
1664 do_pending_stack_adjust ();
1665 free (entry);
1669 /* Called at the start of a block of try statements. */
1670 void
1671 expand_start_try_stmts ()
1673 if (! doing_eh (1))
1674 return;
1676 expand_eh_region_start ();
1679 /* Called to begin a catch clause. The parameter is the object which
1680 will be passed to the runtime type check routine. */
1681 void
1682 start_catch_handler (rtime)
1683 tree rtime;
1685 rtx handler_label;
1686 int insn_region_num;
1687 int eh_region_entry;
1689 if (! doing_eh (1))
1690 return;
1692 handler_label = catchstack.top->entry->exception_handler_label;
1693 insn_region_num = CODE_LABEL_NUMBER (handler_label);
1694 eh_region_entry = find_func_region (insn_region_num);
1696 /* If we've already issued this label, pick a new one */
1697 if (catchstack.top->entry->label_used)
1698 handler_label = gen_exception_label ();
1699 else
1700 catchstack.top->entry->label_used = 1;
1702 receive_exception_label (handler_label);
1704 add_new_handler (eh_region_entry, get_new_handler (handler_label, rtime));
1706 if (flag_new_exceptions && ! exceptions_via_longjmp)
1707 return;
1709 /* Under the old mechanism, as well as setjmp/longjmp, we need to
1710 issue code to compare 'rtime' to the value in eh_info, via the
1711 matching function in eh_info. If its is false, we branch around
1712 the handler we are about to issue. */
1714 if (rtime != NULL_TREE && rtime != CATCH_ALL_TYPE)
1716 rtx call_rtx, rtime_address;
1718 if (catchstack.top->entry->false_label != NULL_RTX)
1720 error ("Never issued previous false_label");
1721 abort ();
1723 catchstack.top->entry->false_label = gen_exception_label ();
1725 rtime_address = expand_expr (rtime, NULL_RTX, Pmode, EXPAND_INITIALIZER);
1726 #ifdef POINTERS_EXTEND_UNSIGNED
1727 rtime_address = convert_memory_address (Pmode, rtime_address);
1728 #endif
1729 rtime_address = force_reg (Pmode, rtime_address);
1731 /* Now issue the call, and branch around handler if needed */
1732 call_rtx = emit_library_call_value (eh_rtime_match_libfunc, NULL_RTX,
1733 0, SImode, 1, rtime_address, Pmode);
1735 /* Did the function return true? */
1736 emit_cmp_and_jump_insns (call_rtx, const0_rtx, EQ, NULL_RTX,
1737 GET_MODE (call_rtx), 0, 0,
1738 catchstack.top->entry->false_label);
1742 /* Called to end a catch clause. If we aren't using the new exception
1743 model tabel mechanism, we need to issue the branch-around label
1744 for the end of the catch block. */
1746 void
1747 end_catch_handler ()
1749 if (! doing_eh (1))
1750 return;
1752 if (flag_new_exceptions && ! exceptions_via_longjmp)
1754 emit_barrier ();
1755 return;
1758 /* A NULL label implies the catch clause was a catch all or cleanup */
1759 if (catchstack.top->entry->false_label == NULL_RTX)
1760 return;
1762 emit_label (catchstack.top->entry->false_label);
1763 catchstack.top->entry->false_label = NULL_RTX;
1766 /* Generate RTL for the start of a group of catch clauses.
1768 It is responsible for starting a new instruction sequence for the
1769 instructions in the catch block, and expanding the handlers for the
1770 internally-generated exception regions nested within the try block
1771 corresponding to this catch block. */
1773 void
1774 expand_start_all_catch ()
1776 struct eh_entry *entry;
1777 tree label;
1778 rtx outer_context;
1780 if (! doing_eh (1))
1781 return;
1783 outer_context = ehstack.top->entry->outer_context;
1785 /* End the try block. */
1786 expand_eh_region_end (integer_zero_node);
1788 emit_line_note (input_filename, lineno);
1789 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
1791 /* The label for the exception handling block that we will save.
1792 This is Lresume in the documentation. */
1793 expand_label (label);
1795 /* Push the label that points to where normal flow is resumed onto
1796 the top of the label stack. */
1797 push_label_entry (&caught_return_label_stack, NULL_RTX, label);
1799 /* Start a new sequence for all the catch blocks. We will add this
1800 to the global sequence catch_clauses when we have completed all
1801 the handlers in this handler-seq. */
1802 start_sequence ();
1804 entry = dequeue_eh_entry (&ehqueue);
1805 for ( ; entry->finalization != integer_zero_node;
1806 entry = dequeue_eh_entry (&ehqueue))
1808 rtx prev;
1810 /* Emit the label for the cleanup handler for this region, and
1811 expand the code for the handler.
1813 Note that a catch region is handled as a side-effect here;
1814 for a try block, entry->finalization will contain
1815 integer_zero_node, so no code will be generated in the
1816 expand_expr call below. But, the label for the handler will
1817 still be emitted, so any code emitted after this point will
1818 end up being the handler. */
1820 receive_exception_label (entry->exception_handler_label);
1822 /* register a handler for this cleanup region */
1823 add_new_handler (
1824 find_func_region (CODE_LABEL_NUMBER (entry->exception_handler_label)),
1825 get_new_handler (entry->exception_handler_label, NULL));
1827 /* And now generate the insns for the cleanup handler. */
1828 expand_expr (entry->finalization, const0_rtx, VOIDmode, 0);
1830 prev = get_last_insn ();
1831 if (prev == NULL || GET_CODE (prev) != BARRIER)
1832 /* Code to throw out to outer context when we fall off end
1833 of the handler. We can't do this here for catch blocks,
1834 so it's done in expand_end_all_catch instead. */
1835 expand_rethrow (entry->outer_context);
1837 do_pending_stack_adjust ();
1838 free (entry);
1841 /* At this point, all the cleanups are done, and the ehqueue now has
1842 the current exception region at its head. We dequeue it, and put it
1843 on the catch stack. */
1845 push_entry (&catchstack, entry);
1847 /* If we are not doing setjmp/longjmp EH, because we are reordered
1848 out of line, we arrange to rethrow in the outer context. We need to
1849 do this because we are not physically within the region, if any, that
1850 logically contains this catch block. */
1851 if (! exceptions_via_longjmp)
1853 expand_eh_region_start ();
1854 ehstack.top->entry->outer_context = outer_context;
1859 /* Finish up the catch block. At this point all the insns for the
1860 catch clauses have already been generated, so we only have to add
1861 them to the catch_clauses list. We also want to make sure that if
1862 we fall off the end of the catch clauses that we rethrow to the
1863 outer EH region. */
1865 void
1866 expand_end_all_catch ()
1868 rtx new_catch_clause;
1869 struct eh_entry *entry;
1871 if (! doing_eh (1))
1872 return;
1874 /* Dequeue the current catch clause region. */
1875 entry = pop_eh_entry (&catchstack);
1876 free (entry);
1878 if (! exceptions_via_longjmp)
1880 rtx outer_context = ehstack.top->entry->outer_context;
1882 /* Finish the rethrow region. size_zero_node is just a NOP. */
1883 expand_eh_region_end (size_zero_node);
1884 /* New exceptions handling models will never have a fall through
1885 of a catch clause */
1886 if (!flag_new_exceptions)
1887 expand_rethrow (outer_context);
1889 else
1890 expand_rethrow (NULL_RTX);
1892 /* Code to throw out to outer context, if we fall off end of catch
1893 handlers. This is rethrow (Lresume, same id, same obj) in the
1894 documentation. We use Lresume because we know that it will throw
1895 to the correct context.
1897 In other words, if the catch handler doesn't exit or return, we
1898 do a "throw" (using the address of Lresume as the point being
1899 thrown from) so that the outer EH region can then try to process
1900 the exception. */
1902 /* Now we have the complete catch sequence. */
1903 new_catch_clause = get_insns ();
1904 end_sequence ();
1906 /* This level of catch blocks is done, so set up the successful
1907 catch jump label for the next layer of catch blocks. */
1908 pop_label_entry (&caught_return_label_stack);
1909 pop_label_entry (&outer_context_label_stack);
1911 /* Add the new sequence of catches to the main one for this function. */
1912 push_to_sequence (catch_clauses);
1913 emit_insns (new_catch_clause);
1914 catch_clauses = get_insns ();
1915 end_sequence ();
1917 /* Here we fall through into the continuation code. */
1920 /* Rethrow from the outer context LABEL. */
1922 static void
1923 expand_rethrow (label)
1924 rtx label;
1926 if (exceptions_via_longjmp)
1927 emit_throw ();
1928 else
1929 if (flag_new_exceptions)
1931 rtx insn;
1932 int region;
1933 if (label == NULL_RTX)
1934 label = last_rethrow_symbol;
1935 emit_library_call (rethrow_libfunc, 0, VOIDmode, 1, label, Pmode);
1936 region = find_func_region (eh_region_from_symbol (label));
1937 function_eh_regions[region].rethrow_ref = 1;
1939 /* Search backwards for the actual call insn. */
1940 insn = get_last_insn ();
1941 while (GET_CODE (insn) != CALL_INSN)
1942 insn = PREV_INSN (insn);
1943 delete_insns_since (insn);
1945 /* Mark the label/symbol on the call. */
1946 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EH_RETHROW, label,
1947 REG_NOTES (insn));
1948 emit_barrier ();
1950 else
1951 emit_jump (label);
1954 /* End all the pending exception regions on protect_list. The handlers
1955 will be emitted when expand_leftover_cleanups is invoked. */
1957 void
1958 end_protect_partials ()
1960 while (protect_list)
1962 expand_eh_region_end (TREE_VALUE (protect_list));
1963 protect_list = TREE_CHAIN (protect_list);
1967 /* Arrange for __terminate to be called if there is an unhandled throw
1968 from within E. */
1970 tree
1971 protect_with_terminate (e)
1972 tree e;
1974 /* We only need to do this when using setjmp/longjmp EH and the
1975 language requires it, as otherwise we protect all of the handlers
1976 at once, if we need to. */
1977 if (exceptions_via_longjmp && protect_cleanup_actions_with_terminate)
1979 tree handler, result;
1981 /* All cleanups must be on the function_obstack. */
1982 push_obstacks_nochange ();
1983 resume_temporary_allocation ();
1985 handler = make_node (RTL_EXPR);
1986 TREE_TYPE (handler) = void_type_node;
1987 RTL_EXPR_RTL (handler) = const0_rtx;
1988 TREE_SIDE_EFFECTS (handler) = 1;
1989 start_sequence_for_rtl_expr (handler);
1991 emit_library_call (terminate_libfunc, 0, VOIDmode, 0);
1992 emit_barrier ();
1994 RTL_EXPR_SEQUENCE (handler) = get_insns ();
1995 end_sequence ();
1997 result = build (TRY_CATCH_EXPR, TREE_TYPE (e), e, handler);
1998 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
1999 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2000 TREE_READONLY (result) = TREE_READONLY (e);
2002 pop_obstacks ();
2004 e = result;
2007 return e;
2010 /* The exception table that we build that is used for looking up and
2011 dispatching exceptions, the current number of entries, and its
2012 maximum size before we have to extend it.
2014 The number in eh_table is the code label number of the exception
2015 handler for the region. This is added by add_eh_table_entry and
2016 used by output_exception_table_entry. */
2018 static int *eh_table = NULL;
2019 static int eh_table_size = 0;
2020 static int eh_table_max_size = 0;
2022 /* Note the need for an exception table entry for region N. If we
2023 don't need to output an explicit exception table, avoid all of the
2024 extra work.
2026 Called from final_scan_insn when a NOTE_INSN_EH_REGION_BEG is seen.
2027 (Or NOTE_INSN_EH_REGION_END sometimes)
2028 N is the NOTE_EH_HANDLER of the note, which comes from the code
2029 label number of the exception handler for the region. */
2031 void
2032 add_eh_table_entry (n)
2033 int n;
2035 #ifndef OMIT_EH_TABLE
2036 if (eh_table_size >= eh_table_max_size)
2038 if (eh_table)
2040 eh_table_max_size += eh_table_max_size>>1;
2042 if (eh_table_max_size < 0)
2043 abort ();
2045 eh_table = (int *) xrealloc (eh_table,
2046 eh_table_max_size * sizeof (int));
2048 else
2050 eh_table_max_size = 252;
2051 eh_table = (int *) xmalloc (eh_table_max_size * sizeof (int));
2054 eh_table[eh_table_size++] = n;
2055 #endif
2058 /* Return a non-zero value if we need to output an exception table.
2060 On some platforms, we don't have to output a table explicitly.
2061 This routine doesn't mean we don't have one. */
2064 exception_table_p ()
2066 if (eh_table)
2067 return 1;
2069 return 0;
2072 /* Output the entry of the exception table corresponding to the
2073 exception region numbered N to file FILE.
2075 N is the code label number corresponding to the handler of the
2076 region. */
2078 static void
2079 output_exception_table_entry (file, n)
2080 FILE *file;
2081 int n;
2083 char buf[256];
2084 rtx sym;
2085 struct handler_info *handler = get_first_handler (n);
2086 int index = find_func_region (n);
2087 rtx rethrow;
2089 /* form and emit the rethrow label, if needed */
2090 rethrow = function_eh_regions[index].rethrow_label;
2091 if (rethrow != NULL_RTX && !flag_new_exceptions)
2092 rethrow = NULL_RTX;
2093 if (rethrow != NULL_RTX && handler == NULL)
2094 if (! function_eh_regions[index].rethrow_ref)
2095 rethrow = NULL_RTX;
2098 for ( ; handler != NULL || rethrow != NULL_RTX; handler = handler->next)
2100 /* rethrow label should indicate the LAST entry for a region */
2101 if (rethrow != NULL_RTX && (handler == NULL || handler->next == NULL))
2103 ASM_GENERATE_INTERNAL_LABEL (buf, "LRTH", n);
2104 assemble_label(buf);
2105 rethrow = NULL_RTX;
2108 ASM_GENERATE_INTERNAL_LABEL (buf, "LEHB", n);
2109 sym = gen_rtx_SYMBOL_REF (Pmode, buf);
2110 assemble_integer (sym, POINTER_SIZE / BITS_PER_UNIT, 1);
2112 ASM_GENERATE_INTERNAL_LABEL (buf, "LEHE", n);
2113 sym = gen_rtx_SYMBOL_REF (Pmode, buf);
2114 assemble_integer (sym, POINTER_SIZE / BITS_PER_UNIT, 1);
2116 if (handler == NULL)
2117 assemble_integer (GEN_INT (0), POINTER_SIZE / BITS_PER_UNIT, 1);
2118 else
2120 ASM_GENERATE_INTERNAL_LABEL (buf, "L", handler->handler_number);
2121 sym = gen_rtx_SYMBOL_REF (Pmode, buf);
2122 assemble_integer (sym, POINTER_SIZE / BITS_PER_UNIT, 1);
2125 if (flag_new_exceptions)
2127 if (handler == NULL || handler->type_info == NULL)
2128 assemble_integer (const0_rtx, POINTER_SIZE / BITS_PER_UNIT, 1);
2129 else
2130 if (handler->type_info == CATCH_ALL_TYPE)
2131 assemble_integer (GEN_INT (CATCH_ALL_TYPE),
2132 POINTER_SIZE / BITS_PER_UNIT, 1);
2133 else
2134 output_constant ((tree)(handler->type_info),
2135 POINTER_SIZE / BITS_PER_UNIT);
2137 putc ('\n', file); /* blank line */
2138 /* We only output the first label under the old scheme */
2139 if (! flag_new_exceptions || handler == NULL)
2140 break;
2144 /* Output the exception table if we have and need one. */
2146 static short language_code = 0;
2147 static short version_code = 0;
2149 /* This routine will set the language code for exceptions. */
2150 void
2151 set_exception_lang_code (code)
2152 int code;
2154 language_code = code;
2157 /* This routine will set the language version code for exceptions. */
2158 void
2159 set_exception_version_code (code)
2160 int code;
2162 version_code = code;
2166 void
2167 output_exception_table ()
2169 int i;
2170 char buf[256];
2171 extern FILE *asm_out_file;
2173 if (! doing_eh (0) || ! eh_table)
2174 return;
2176 exception_section ();
2178 /* Beginning marker for table. */
2179 assemble_align (GET_MODE_ALIGNMENT (ptr_mode));
2180 assemble_label ("__EXCEPTION_TABLE__");
2182 if (flag_new_exceptions)
2184 assemble_integer (GEN_INT (NEW_EH_RUNTIME),
2185 POINTER_SIZE / BITS_PER_UNIT, 1);
2186 assemble_integer (GEN_INT (language_code), 2 , 1);
2187 assemble_integer (GEN_INT (version_code), 2 , 1);
2189 /* Add enough padding to make sure table aligns on a pointer boundry. */
2190 i = GET_MODE_ALIGNMENT (ptr_mode) / BITS_PER_UNIT - 4;
2191 for ( ; i < 0; i = i + GET_MODE_ALIGNMENT (ptr_mode) / BITS_PER_UNIT)
2193 if (i != 0)
2194 assemble_integer (const0_rtx, i , 1);
2196 /* Generate the label for offset calculations on rethrows */
2197 ASM_GENERATE_INTERNAL_LABEL (buf, "LRTH", 0);
2198 assemble_label(buf);
2201 for (i = 0; i < eh_table_size; ++i)
2202 output_exception_table_entry (asm_out_file, eh_table[i]);
2204 free (eh_table);
2205 clear_function_eh_region ();
2207 /* Ending marker for table. */
2208 /* Generate the label for end of table. */
2209 ASM_GENERATE_INTERNAL_LABEL (buf, "LRTH", CODE_LABEL_NUMBER (final_rethrow));
2210 assemble_label(buf);
2211 assemble_integer (constm1_rtx, POINTER_SIZE / BITS_PER_UNIT, 1);
2213 /* for binary compatability, the old __throw checked the second
2214 position for a -1, so we should output at least 2 -1's */
2215 if (! flag_new_exceptions)
2216 assemble_integer (constm1_rtx, POINTER_SIZE / BITS_PER_UNIT, 1);
2218 putc ('\n', asm_out_file); /* blank line */
2221 /* Emit code to get EH context.
2223 We have to scan thru the code to find possible EH context registers.
2224 Inlined functions may use it too, and thus we'll have to be able
2225 to change them too.
2227 This is done only if using exceptions_via_longjmp. */
2229 void
2230 emit_eh_context ()
2232 rtx insn;
2233 rtx ehc = 0;
2235 if (! doing_eh (0))
2236 return;
2238 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2239 if (GET_CODE (insn) == INSN
2240 && GET_CODE (PATTERN (insn)) == USE)
2242 rtx reg = find_reg_note (insn, REG_EH_CONTEXT, 0);
2243 if (reg)
2245 rtx insns;
2247 start_sequence ();
2249 /* If this is the first use insn, emit the call here. This
2250 will always be at the top of our function, because if
2251 expand_inline_function notices a REG_EH_CONTEXT note, it
2252 adds a use insn to this function as well. */
2253 if (ehc == 0)
2254 ehc = call_get_eh_context ();
2256 emit_move_insn (XEXP (reg, 0), ehc);
2257 insns = get_insns ();
2258 end_sequence ();
2260 emit_insns_before (insns, insn);
2262 /* At -O0, we must make the context register stay alive so
2263 that the stupid.c register allocator doesn't get confused. */
2264 if (obey_regdecls != 0)
2266 insns = gen_rtx_USE (GET_MODE (XEXP (reg,0)), XEXP (reg,0));
2267 emit_insn_before (insns, get_last_insn ());
2273 /* Scan the current insns and build a list of handler labels. The
2274 resulting list is placed in the global variable exception_handler_labels.
2276 It is called after the last exception handling region is added to
2277 the current function (when the rtl is almost all built for the
2278 current function) and before the jump optimization pass. */
2280 void
2281 find_exception_handler_labels ()
2283 rtx insn;
2285 exception_handler_labels = NULL_RTX;
2287 /* If we aren't doing exception handling, there isn't much to check. */
2288 if (! doing_eh (0))
2289 return;
2291 /* For each start of a region, add its label to the list. */
2293 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2295 struct handler_info* ptr;
2296 if (GET_CODE (insn) == NOTE
2297 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
2299 ptr = get_first_handler (NOTE_EH_HANDLER (insn));
2300 for ( ; ptr; ptr = ptr->next)
2302 /* make sure label isn't in the list already */
2303 rtx x;
2304 for (x = exception_handler_labels; x; x = XEXP (x, 1))
2305 if (XEXP (x, 0) == ptr->handler_label)
2306 break;
2307 if (! x)
2308 exception_handler_labels = gen_rtx_EXPR_LIST (VOIDmode,
2309 ptr->handler_label, exception_handler_labels);
2315 /* Return a value of 1 if the parameter label number is an exception handler
2316 label. Return 0 otherwise. */
2319 is_exception_handler_label (lab)
2320 int lab;
2322 rtx x;
2323 for (x = exception_handler_labels ; x ; x = XEXP (x, 1))
2324 if (lab == CODE_LABEL_NUMBER (XEXP (x, 0)))
2325 return 1;
2326 return 0;
2329 /* Perform sanity checking on the exception_handler_labels list.
2331 Can be called after find_exception_handler_labels is called to
2332 build the list of exception handlers for the current function and
2333 before we finish processing the current function. */
2335 void
2336 check_exception_handler_labels ()
2338 rtx insn, insn2;
2340 /* If we aren't doing exception handling, there isn't much to check. */
2341 if (! doing_eh (0))
2342 return;
2344 /* Make sure there is no more than 1 copy of a label */
2345 for (insn = exception_handler_labels; insn; insn = XEXP (insn, 1))
2347 int count = 0;
2348 for (insn2 = exception_handler_labels; insn2; insn2 = XEXP (insn2, 1))
2349 if (XEXP (insn, 0) == XEXP (insn2, 0))
2350 count++;
2351 if (count != 1)
2352 warning ("Counted %d copies of EH region %d in list.\n", count,
2353 CODE_LABEL_NUMBER (insn));
2358 /* Mark the children of NODE for GC. */
2360 static void
2361 mark_eh_node (node)
2362 struct eh_node *node;
2364 while (node)
2366 if (node->entry)
2368 ggc_mark_rtx (node->entry->outer_context);
2369 ggc_mark_rtx (node->entry->exception_handler_label);
2370 ggc_mark_tree (node->entry->finalization);
2371 ggc_mark_rtx (node->entry->false_label);
2372 ggc_mark_rtx (node->entry->rethrow_label);
2374 node = node ->chain;
2378 /* Mark S for GC. */
2380 static void
2381 mark_eh_stack (s)
2382 struct eh_stack *s;
2384 if (s)
2385 mark_eh_node (s->top);
2388 /* Mark Q for GC. */
2390 static void
2391 mark_eh_queue (q)
2392 struct eh_queue *q;
2394 if (q)
2395 mark_eh_node (q->head);
2398 /* Mark NODE for GC. A label_node contains a union containing either
2399 a tree or an rtx. This label_node will contain a tree. */
2401 static void
2402 mark_tree_label_node (node)
2403 struct label_node *node;
2405 while (node)
2407 ggc_mark_tree (node->u.tlabel);
2408 node = node->chain;
2412 /* Mark EH for GC. */
2414 void
2415 mark_eh_status (eh)
2416 struct eh_status *eh;
2418 if (eh == 0)
2419 return;
2421 mark_eh_stack (&eh->x_ehstack);
2422 mark_eh_stack (&eh->x_catchstack);
2423 mark_eh_queue (&eh->x_ehqueue);
2424 ggc_mark_rtx (eh->x_catch_clauses);
2426 lang_mark_false_label_stack (eh->x_false_label_stack);
2427 mark_tree_label_node (eh->x_caught_return_label_stack);
2429 ggc_mark_tree (eh->x_protect_list);
2430 ggc_mark_rtx (eh->ehc);
2431 ggc_mark_rtx (eh->x_eh_return_stub_label);
2434 /* Mark ARG (which is really a struct func_eh_entry**) for GC. */
2436 static void
2437 mark_func_eh_entry (arg)
2438 void *arg;
2440 struct func_eh_entry *fee;
2441 struct handler_info *h;
2442 int i;
2444 fee = *((struct func_eh_entry **) arg);
2446 for (i = 0; i < current_func_eh_entry; ++i)
2448 ggc_mark_rtx (fee->rethrow_label);
2449 for (h = fee->handlers; h; h = h->next)
2451 ggc_mark_rtx (h->handler_label);
2452 if (h->type_info != CATCH_ALL_TYPE)
2453 ggc_mark_tree ((tree) h->type_info);
2456 /* Skip to the next entry in the array. */
2457 ++fee;
2461 /* This group of functions initializes the exception handling data
2462 structures at the start of the compilation, initializes the data
2463 structures at the start of a function, and saves and restores the
2464 exception handling data structures for the start/end of a nested
2465 function. */
2467 /* Toplevel initialization for EH things. */
2469 void
2470 init_eh ()
2472 first_rethrow_symbol = create_rethrow_ref (0);
2473 final_rethrow = gen_exception_label ();
2474 last_rethrow_symbol = create_rethrow_ref (CODE_LABEL_NUMBER (final_rethrow));
2476 ggc_add_rtx_root (&exception_handler_labels, 1);
2477 ggc_add_rtx_root (&eh_return_context, 1);
2478 ggc_add_rtx_root (&eh_return_stack_adjust, 1);
2479 ggc_add_rtx_root (&eh_return_handler, 1);
2480 ggc_add_rtx_root (&first_rethrow_symbol, 1);
2481 ggc_add_rtx_root (&final_rethrow, 1);
2482 ggc_add_rtx_root (&last_rethrow_symbol, 1);
2483 ggc_add_root (&function_eh_regions, 1, sizeof (function_eh_regions),
2484 mark_func_eh_entry);
2487 /* Initialize the per-function EH information. */
2489 void
2490 init_eh_for_function ()
2492 current_function->eh
2493 = (struct eh_status *) xmalloc (sizeof (struct eh_status));
2495 ehstack.top = 0;
2496 catchstack.top = 0;
2497 ehqueue.head = ehqueue.tail = 0;
2498 catch_clauses = NULL_RTX;
2499 false_label_stack = 0;
2500 caught_return_label_stack = 0;
2501 protect_list = NULL_TREE;
2502 current_function_ehc = NULL_RTX;
2503 eh_return_context = NULL_RTX;
2504 eh_return_stack_adjust = NULL_RTX;
2505 eh_return_handler = NULL_RTX;
2506 eh_return_stub_label = NULL_RTX;
2509 void
2510 free_eh_status (f)
2511 struct function *f;
2513 free (f->eh);
2514 f->eh = NULL;
2517 /* This section is for the exception handling specific optimization
2518 pass. First are the internal routines, and then the main
2519 optimization pass. */
2521 /* Determine if the given INSN can throw an exception. */
2523 static int
2524 can_throw (insn)
2525 rtx insn;
2527 /* Calls can always potentially throw exceptions, unless they have
2528 a REG_EH_REGION note with a value of 0 or less. */
2529 if (GET_CODE (insn) == CALL_INSN)
2531 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2532 if (!note || XINT (XEXP (note, 0), 0) > 0)
2533 return 1;
2536 if (asynchronous_exceptions)
2538 /* If we wanted asynchronous exceptions, then everything but NOTEs
2539 and CODE_LABELs could throw. */
2540 if (GET_CODE (insn) != NOTE && GET_CODE (insn) != CODE_LABEL)
2541 return 1;
2544 return 0;
2547 /* Scan a exception region looking for the matching end and then
2548 remove it if possible. INSN is the start of the region, N is the
2549 region number, and DELETE_OUTER is to note if anything in this
2550 region can throw.
2552 Regions are removed if they cannot possibly catch an exception.
2553 This is determined by invoking can_throw on each insn within the
2554 region; if can_throw returns true for any of the instructions, the
2555 region can catch an exception, since there is an insn within the
2556 region that is capable of throwing an exception.
2558 Returns the NOTE_INSN_EH_REGION_END corresponding to this region, or
2559 calls abort if it can't find one.
2561 Can abort if INSN is not a NOTE_INSN_EH_REGION_BEGIN, or if N doesn't
2562 correspond to the region number, or if DELETE_OUTER is NULL. */
2564 static rtx
2565 scan_region (insn, n, delete_outer)
2566 rtx insn;
2567 int n;
2568 int *delete_outer;
2570 rtx start = insn;
2572 /* Assume we can delete the region. */
2573 int delete = 1;
2575 /* Can't delete something which is rethrown to. */
2576 if (rethrow_used (n))
2577 delete = 0;
2579 if (insn == NULL_RTX
2580 || GET_CODE (insn) != NOTE
2581 || NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
2582 || NOTE_EH_HANDLER (insn) != n
2583 || delete_outer == NULL)
2584 abort ();
2586 insn = NEXT_INSN (insn);
2588 /* Look for the matching end. */
2589 while (! (GET_CODE (insn) == NOTE
2590 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
2592 /* If anything can throw, we can't remove the region. */
2593 if (delete && can_throw (insn))
2595 delete = 0;
2598 /* Watch out for and handle nested regions. */
2599 if (GET_CODE (insn) == NOTE
2600 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
2602 insn = scan_region (insn, NOTE_EH_HANDLER (insn), &delete);
2605 insn = NEXT_INSN (insn);
2608 /* The _BEG/_END NOTEs must match and nest. */
2609 if (NOTE_EH_HANDLER (insn) != n)
2610 abort ();
2612 /* If anything in this exception region can throw, we can throw. */
2613 if (! delete)
2614 *delete_outer = 0;
2615 else
2617 /* Delete the start and end of the region. */
2618 delete_insn (start);
2619 delete_insn (insn);
2621 /* We no longer removed labels here, since flow will now remove any
2622 handler which cannot be called any more. */
2624 #if 0
2625 /* Only do this part if we have built the exception handler
2626 labels. */
2627 if (exception_handler_labels)
2629 rtx x, *prev = &exception_handler_labels;
2631 /* Find it in the list of handlers. */
2632 for (x = exception_handler_labels; x; x = XEXP (x, 1))
2634 rtx label = XEXP (x, 0);
2635 if (CODE_LABEL_NUMBER (label) == n)
2637 /* If we are the last reference to the handler,
2638 delete it. */
2639 if (--LABEL_NUSES (label) == 0)
2640 delete_insn (label);
2642 if (optimize)
2644 /* Remove it from the list of exception handler
2645 labels, if we are optimizing. If we are not, then
2646 leave it in the list, as we are not really going to
2647 remove the region. */
2648 *prev = XEXP (x, 1);
2649 XEXP (x, 1) = 0;
2650 XEXP (x, 0) = 0;
2653 break;
2655 prev = &XEXP (x, 1);
2658 #endif
2660 return insn;
2663 /* Perform various interesting optimizations for exception handling
2664 code.
2666 We look for empty exception regions and make them go (away). The
2667 jump optimization code will remove the handler if nothing else uses
2668 it. */
2670 void
2671 exception_optimize ()
2673 rtx insn;
2674 int n;
2676 /* Remove empty regions. */
2677 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2679 if (GET_CODE (insn) == NOTE
2680 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
2682 /* Since scan_region will return the NOTE_INSN_EH_REGION_END
2683 insn, we will indirectly skip through all the insns
2684 inbetween. We are also guaranteed that the value of insn
2685 returned will be valid, as otherwise scan_region won't
2686 return. */
2687 insn = scan_region (insn, NOTE_EH_HANDLER (insn), &n);
2692 /* This function determines whether any of the exception regions in the
2693 current function are targets of a rethrow or not, and set the
2694 reference flag according. */
2695 void
2696 update_rethrow_references ()
2698 rtx insn;
2699 int x, region;
2700 int *saw_region, *saw_rethrow;
2702 if (!flag_new_exceptions)
2703 return;
2705 saw_region = (int *) alloca (current_func_eh_entry * sizeof (int));
2706 saw_rethrow = (int *) alloca (current_func_eh_entry * sizeof (int));
2707 bzero ((char *) saw_region, (current_func_eh_entry * sizeof (int)));
2708 bzero ((char *) saw_rethrow, (current_func_eh_entry * sizeof (int)));
2710 /* Determine what regions exist, and whether there are any rethrows
2711 to those regions or not. */
2712 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2713 if (GET_CODE (insn) == CALL_INSN)
2715 rtx note = find_reg_note (insn, REG_EH_RETHROW, NULL_RTX);
2716 if (note)
2718 region = eh_region_from_symbol (XEXP (note, 0));
2719 region = find_func_region (region);
2720 saw_rethrow[region] = 1;
2723 else
2724 if (GET_CODE (insn) == NOTE)
2726 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
2728 region = find_func_region (NOTE_EH_HANDLER (insn));
2729 saw_region[region] = 1;
2733 /* For any regions we did see, set the referenced flag. */
2734 for (x = 0; x < current_func_eh_entry; x++)
2735 if (saw_region[x])
2736 function_eh_regions[x].rethrow_ref = saw_rethrow[x];
2739 /* Various hooks for the DWARF 2 __throw routine. */
2741 /* Do any necessary initialization to access arbitrary stack frames.
2742 On the SPARC, this means flushing the register windows. */
2744 void
2745 expand_builtin_unwind_init ()
2747 /* Set this so all the registers get saved in our frame; we need to be
2748 able to copy the saved values for any registers from frames we unwind. */
2749 current_function_has_nonlocal_label = 1;
2751 #ifdef SETUP_FRAME_ADDRESSES
2752 SETUP_FRAME_ADDRESSES ();
2753 #endif
2756 /* Given a value extracted from the return address register or stack slot,
2757 return the actual address encoded in that value. */
2760 expand_builtin_extract_return_addr (addr_tree)
2761 tree addr_tree;
2763 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
2764 return eh_outer_context (addr);
2767 /* Given an actual address in addr_tree, do any necessary encoding
2768 and return the value to be stored in the return address register or
2769 stack slot so the epilogue will return to that address. */
2772 expand_builtin_frob_return_addr (addr_tree)
2773 tree addr_tree;
2775 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
2776 #ifdef RETURN_ADDR_OFFSET
2777 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2778 #endif
2779 return addr;
2782 /* Choose three registers for communication between the main body of
2783 __throw and the epilogue (or eh stub) and the exception handler.
2784 We must do this with hard registers because the epilogue itself
2785 will be generated after reload, at which point we may not reference
2786 pseudos at all.
2788 The first passes the exception context to the handler. For this
2789 we use the return value register for a void*.
2791 The second holds the stack pointer value to be restored. For
2792 this we use the static chain register if it exists and is different
2793 from the previous, otherwise some arbitrary call-clobbered register.
2795 The third holds the address of the handler itself. Here we use
2796 some arbitrary call-clobbered register. */
2798 static void
2799 eh_regs (pcontext, psp, pra, outgoing)
2800 rtx *pcontext, *psp, *pra;
2801 int outgoing;
2803 rtx rcontext, rsp, rra;
2804 int i;
2806 #ifdef FUNCTION_OUTGOING_VALUE
2807 if (outgoing)
2808 rcontext = FUNCTION_OUTGOING_VALUE (build_pointer_type (void_type_node),
2809 current_function_decl);
2810 else
2811 #endif
2812 rcontext = FUNCTION_VALUE (build_pointer_type (void_type_node),
2813 current_function_decl);
2815 #ifdef STATIC_CHAIN_REGNUM
2816 if (outgoing)
2817 rsp = static_chain_incoming_rtx;
2818 else
2819 rsp = static_chain_rtx;
2820 if (REGNO (rsp) == REGNO (rcontext))
2821 #endif /* STATIC_CHAIN_REGNUM */
2822 rsp = NULL_RTX;
2824 if (rsp == NULL_RTX)
2826 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
2827 if (call_used_regs[i] && ! fixed_regs[i] && i != REGNO (rcontext))
2828 break;
2829 if (i == FIRST_PSEUDO_REGISTER)
2830 abort();
2832 rsp = gen_rtx_REG (Pmode, i);
2835 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
2836 if (call_used_regs[i] && ! fixed_regs[i]
2837 && i != REGNO (rcontext) && i != REGNO (rsp))
2838 break;
2839 if (i == FIRST_PSEUDO_REGISTER)
2840 abort();
2842 rra = gen_rtx_REG (Pmode, i);
2844 *pcontext = rcontext;
2845 *psp = rsp;
2846 *pra = rra;
2849 /* Retrieve the register which contains the pointer to the eh_context
2850 structure set the __throw. */
2852 #if 0
2853 rtx
2854 get_reg_for_handler ()
2856 rtx reg1;
2857 reg1 = FUNCTION_VALUE (build_pointer_type (void_type_node),
2858 current_function_decl);
2859 return reg1;
2861 #endif
2863 /* Set up the epilogue with the magic bits we'll need to return to the
2864 exception handler. */
2866 void
2867 expand_builtin_eh_return (context, stack, handler)
2868 tree context, stack, handler;
2870 if (eh_return_context)
2871 error("Duplicate call to __builtin_eh_return");
2873 eh_return_context
2874 = copy_to_reg (expand_expr (context, NULL_RTX, VOIDmode, 0));
2875 eh_return_stack_adjust
2876 = copy_to_reg (expand_expr (stack, NULL_RTX, VOIDmode, 0));
2877 eh_return_handler
2878 = copy_to_reg (expand_expr (handler, NULL_RTX, VOIDmode, 0));
2881 void
2882 expand_eh_return ()
2884 rtx reg1, reg2, reg3;
2885 rtx stub_start, after_stub;
2886 rtx ra, tmp;
2888 if (!eh_return_context)
2889 return;
2891 current_function_cannot_inline = N_("function uses __builtin_eh_return");
2893 eh_regs (&reg1, &reg2, &reg3, 1);
2894 #ifdef POINTERS_EXTEND_UNSIGNED
2895 eh_return_context = convert_memory_address (Pmode, eh_return_context);
2896 eh_return_stack_adjust =
2897 convert_memory_address (Pmode, eh_return_stack_adjust);
2898 eh_return_handler = convert_memory_address (Pmode, eh_return_handler);
2899 #endif
2900 emit_move_insn (reg1, eh_return_context);
2901 emit_move_insn (reg2, eh_return_stack_adjust);
2902 emit_move_insn (reg3, eh_return_handler);
2904 /* Talk directly to the target's epilogue code when possible. */
2906 #ifdef HAVE_eh_epilogue
2907 if (HAVE_eh_epilogue)
2909 emit_insn (gen_eh_epilogue (reg1, reg2, reg3));
2910 return;
2912 #endif
2914 /* Otherwise, use the same stub technique we had before. */
2916 eh_return_stub_label = stub_start = gen_label_rtx ();
2917 after_stub = gen_label_rtx ();
2919 /* Set the return address to the stub label. */
2921 ra = expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
2922 0, hard_frame_pointer_rtx);
2923 if (GET_CODE (ra) == REG && REGNO (ra) >= FIRST_PSEUDO_REGISTER)
2924 abort();
2926 tmp = memory_address (Pmode, gen_rtx_LABEL_REF (Pmode, stub_start));
2927 #ifdef RETURN_ADDR_OFFSET
2928 tmp = plus_constant (tmp, -RETURN_ADDR_OFFSET);
2929 #endif
2930 tmp = force_operand (tmp, ra);
2931 if (tmp != ra)
2932 emit_move_insn (ra, tmp);
2934 /* Indicate that the registers are in fact used. */
2935 emit_insn (gen_rtx_USE (VOIDmode, reg1));
2936 emit_insn (gen_rtx_USE (VOIDmode, reg2));
2937 emit_insn (gen_rtx_USE (VOIDmode, reg3));
2938 if (GET_CODE (ra) == REG)
2939 emit_insn (gen_rtx_USE (VOIDmode, ra));
2941 /* Generate the stub. */
2943 emit_jump (after_stub);
2944 emit_label (stub_start);
2946 eh_regs (&reg1, &reg2, &reg3, 0);
2947 adjust_stack (reg2);
2948 emit_indirect_jump (reg3);
2950 emit_label (after_stub);
2954 /* This contains the code required to verify whether arbitrary instructions
2955 are in the same exception region. */
2957 static int *insn_eh_region = (int *)0;
2958 static int maximum_uid;
2960 static void
2961 set_insn_eh_region (first, region_num)
2962 rtx *first;
2963 int region_num;
2965 rtx insn;
2966 int rnum;
2968 for (insn = *first; insn; insn = NEXT_INSN (insn))
2970 if ((GET_CODE (insn) == NOTE)
2971 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG))
2973 rnum = NOTE_EH_HANDLER (insn);
2974 insn_eh_region[INSN_UID (insn)] = rnum;
2975 insn = NEXT_INSN (insn);
2976 set_insn_eh_region (&insn, rnum);
2977 /* Upon return, insn points to the EH_REGION_END of nested region */
2978 continue;
2980 insn_eh_region[INSN_UID (insn)] = region_num;
2981 if ((GET_CODE (insn) == NOTE) &&
2982 (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
2983 break;
2985 *first = insn;
2988 /* Free the insn table, an make sure it cannot be used again. */
2990 void
2991 free_insn_eh_region ()
2993 if (!doing_eh (0))
2994 return;
2996 if (insn_eh_region)
2998 free (insn_eh_region);
2999 insn_eh_region = (int *)0;
3003 /* Initialize the table. max_uid must be calculated and handed into
3004 this routine. If it is unavailable, passing a value of 0 will
3005 cause this routine to calculate it as well. */
3007 void
3008 init_insn_eh_region (first, max_uid)
3009 rtx first;
3010 int max_uid;
3012 rtx insn;
3014 if (!doing_eh (0))
3015 return;
3017 if (insn_eh_region)
3018 free_insn_eh_region();
3020 if (max_uid == 0)
3021 for (insn = first; insn; insn = NEXT_INSN (insn))
3022 if (INSN_UID (insn) > max_uid) /* find largest UID */
3023 max_uid = INSN_UID (insn);
3025 maximum_uid = max_uid;
3026 insn_eh_region = (int *) xmalloc ((max_uid + 1) * sizeof (int));
3027 insn = first;
3028 set_insn_eh_region (&insn, 0);
3032 /* Check whether 2 instructions are within the same region. */
3034 int
3035 in_same_eh_region (insn1, insn2)
3036 rtx insn1, insn2;
3038 int ret, uid1, uid2;
3040 /* If no exceptions, instructions are always in same region. */
3041 if (!doing_eh (0))
3042 return 1;
3044 /* If the table isn't allocated, assume the worst. */
3045 if (!insn_eh_region)
3046 return 0;
3048 uid1 = INSN_UID (insn1);
3049 uid2 = INSN_UID (insn2);
3051 /* if instructions have been allocated beyond the end, either
3052 the table is out of date, or this is a late addition, or
3053 something... Assume the worst. */
3054 if (uid1 > maximum_uid || uid2 > maximum_uid)
3055 return 0;
3057 ret = (insn_eh_region[uid1] == insn_eh_region[uid2]);
3058 return ret;
3062 /* This function will initialize the handler list for a specified block.
3063 It may recursively call itself if the outer block hasn't been processed
3064 yet. At some point in the future we can trim out handlers which we
3065 know cannot be called. (ie, if a block has an INT type handler,
3066 control will never be passed to an outer INT type handler). */
3067 static void
3068 process_nestinfo (block, info, nested_eh_region)
3069 int block;
3070 eh_nesting_info *info;
3071 int *nested_eh_region;
3073 handler_info *ptr, *last_ptr = NULL;
3074 int x, y, count = 0;
3075 int extra = 0;
3076 handler_info **extra_handlers = 0;
3077 int index = info->region_index[block];
3079 /* If we've already processed this block, simply return. */
3080 if (info->num_handlers[index] > 0)
3081 return;
3083 for (ptr = get_first_handler (block); ptr; last_ptr = ptr, ptr = ptr->next)
3084 count++;
3086 /* pick up any information from the next outer region. It will already
3087 contain a summary of itself and all outer regions to it. */
3089 if (nested_eh_region [block] != 0)
3091 int nested_index = info->region_index[nested_eh_region[block]];
3092 process_nestinfo (nested_eh_region[block], info, nested_eh_region);
3093 extra = info->num_handlers[nested_index];
3094 extra_handlers = info->handlers[nested_index];
3095 info->outer_index[index] = nested_index;
3098 /* If the last handler is either a CATCH_ALL or a cleanup, then we
3099 won't use the outer ones since we know control will not go past the
3100 catch-all or cleanup. */
3102 if (last_ptr != NULL && (last_ptr->type_info == NULL
3103 || last_ptr->type_info == CATCH_ALL_TYPE))
3104 extra = 0;
3106 info->num_handlers[index] = count + extra;
3107 info->handlers[index] = (handler_info **) xmalloc ((count + extra)
3108 * sizeof (handler_info **));
3110 /* First put all our handlers into the list. */
3111 ptr = get_first_handler (block);
3112 for (x = 0; x < count; x++)
3114 info->handlers[index][x] = ptr;
3115 ptr = ptr->next;
3118 /* Now add all the outer region handlers, if they aren't they same as
3119 one of the types in the current block. We won't worry about
3120 derived types yet, we'll just look for the exact type. */
3121 for (y =0, x = 0; x < extra ; x++)
3123 int i, ok;
3124 ok = 1;
3125 /* Check to see if we have a type duplication. */
3126 for (i = 0; i < count; i++)
3127 if (info->handlers[index][i]->type_info == extra_handlers[x]->type_info)
3129 ok = 0;
3130 /* Record one less handler. */
3131 (info->num_handlers[index])--;
3132 break;
3134 if (ok)
3136 info->handlers[index][y + count] = extra_handlers[x];
3137 y++;
3142 /* This function will allocate and initialize an eh_nesting_info structure.
3143 It returns a pointer to the completed data structure. If there are
3144 no exception regions, a NULL value is returned. */
3145 eh_nesting_info *
3146 init_eh_nesting_info ()
3148 int *nested_eh_region;
3149 int region_count = 0;
3150 rtx eh_note = NULL_RTX;
3151 eh_nesting_info *info;
3152 rtx insn;
3153 int x;
3155 info = (eh_nesting_info *) xmalloc (sizeof (eh_nesting_info));
3156 info->region_index = (int *) xcalloc ((max_label_num () + 1), sizeof (int));
3158 nested_eh_region = (int *) alloca ((max_label_num () + 1) * sizeof (int));
3159 bzero ((char *) nested_eh_region, (max_label_num () + 1) * sizeof (int));
3161 /* Create the nested_eh_region list. If indexed with a block number, it
3162 returns the block number of the next outermost region, if any.
3163 We can count the number of regions and initialize the region_index
3164 vector at the same time. */
3165 for (insn = get_insns(); insn; insn = NEXT_INSN (insn))
3167 if (GET_CODE (insn) == NOTE)
3169 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
3171 int block = NOTE_EH_HANDLER (insn);
3172 region_count++;
3173 info->region_index[block] = region_count;
3174 if (eh_note)
3175 nested_eh_region [block] =
3176 NOTE_EH_HANDLER (XEXP (eh_note, 0));
3177 else
3178 nested_eh_region [block] = 0;
3179 eh_note = gen_rtx_EXPR_LIST (VOIDmode, insn, eh_note);
3181 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
3182 eh_note = XEXP (eh_note, 1);
3186 /* If there are no regions, wrap it up now. */
3187 if (region_count == 0)
3189 free (info->region_index);
3190 free (info);
3191 return NULL;
3194 region_count++;
3195 info->handlers = (handler_info ***) xcalloc (region_count,
3196 sizeof (handler_info ***));
3197 info->num_handlers = (int *) xcalloc (region_count, sizeof (int));
3198 info->outer_index = (int *) xcalloc (region_count, sizeof (int));
3200 /* Now initialize the handler lists for all exception blocks. */
3201 for (x = 0; x <= max_label_num (); x++)
3203 if (info->region_index[x] != 0)
3204 process_nestinfo (x, info, nested_eh_region);
3206 info->region_count = region_count;
3207 return info;
3211 /* This function is used to retreive the vector of handlers which
3212 can be reached by a given insn in a given exception region.
3213 BLOCK is the exception block the insn is in.
3214 INFO is the eh_nesting_info structure.
3215 INSN is the (optional) insn within the block. If insn is not NULL_RTX,
3216 it may contain reg notes which modify its throwing behavior, and
3217 these will be obeyed. If NULL_RTX is passed, then we simply return the
3218 handlers for block.
3219 HANDLERS is the address of a pointer to a vector of handler_info pointers.
3220 Upon return, this will have the handlers which can be reached by block.
3221 This function returns the number of elements in the handlers vector. */
3222 int
3223 reachable_handlers (block, info, insn, handlers)
3224 int block;
3225 eh_nesting_info *info;
3226 rtx insn ;
3227 handler_info ***handlers;
3229 int index = 0;
3230 *handlers = NULL;
3232 if (info == NULL)
3233 return 0;
3234 if (block > 0)
3235 index = info->region_index[block];
3237 if (insn && GET_CODE (insn) == CALL_INSN)
3239 /* RETHROWs specify a region number from which we are going to rethrow.
3240 This means we wont pass control to handlers in the specified
3241 region, but rather any region OUTSIDE the specified region.
3242 We accomplish this by setting block to the outer_index of the
3243 specified region. */
3244 rtx note = find_reg_note (insn, REG_EH_RETHROW, NULL_RTX);
3245 if (note)
3247 index = eh_region_from_symbol (XEXP (note, 0));
3248 index = info->region_index[index];
3249 if (index)
3250 index = info->outer_index[index];
3252 else
3254 /* If there is no rethrow, we look for a REG_EH_REGION, and
3255 we'll throw from that block. A value of 0 or less
3256 indicates that this insn cannot throw. */
3257 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3258 if (note)
3260 int b = XINT (XEXP (note, 0), 0);
3261 if (b <= 0)
3262 index = 0;
3263 else
3264 index = info->region_index[b];
3268 /* If we reach this point, and index is 0, there is no throw. */
3269 if (index == 0)
3270 return 0;
3272 *handlers = info->handlers[index];
3273 return info->num_handlers[index];
3277 /* This function will free all memory associated with the eh_nesting info. */
3279 void
3280 free_eh_nesting_info (info)
3281 eh_nesting_info *info;
3283 int x;
3284 if (info != NULL)
3286 if (info->region_index)
3287 free (info->region_index);
3288 if (info->num_handlers)
3289 free (info->num_handlers);
3290 if (info->outer_index)
3291 free (info->outer_index);
3292 if (info->handlers)
3294 for (x = 0; x < info->region_count; x++)
3295 if (info->handlers[x])
3296 free (info->handlers[x]);
3297 free (info->handlers);