1/* Exception Handling interface routines.
2   Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
3   Contributed by Mike Stump <mrs@cygnus.com>.
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING.  If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA.  */
21
22#if !defined(NULL_RTX) && !defined(rtx)
23typedef struct rtx_def *_except_rtx;
24#define rtx _except_rtx
25#endif
26
27/* The label generated by expand_builtin_eh_return.  */
28
29extern rtx eh_return_stub_label;
30
31#ifdef TREE_CODE
32
33/* A stack of labels. CHAIN points to the next entry in the stack.  */
34
35struct label_node {
36  union {
37    rtx rlabel;
38    tree tlabel;
39  } u;
40  struct label_node *chain;
41};
42
43/* An eh_entry is used to describe one exception handling region.
44
45   OUTER_CONTEXT is the label used for rethrowing into the outer context.
46
47   EXCEPTION_HANDLER_LABEL is the label corresponding to the handler
48   for this region.
49
50   LABEL_USED indicates whether a CATCH block has already used this
51   label or not. New ones are needed for additional catch blocks if
52   it has.
53
54   FALSE_LABEL is used when either setjmp/longjmp exceptions are in
55   use, or old style table exceptions. It contains the label for
56   branching to the next runtime type check as handlers are processed.
57
58   FINALIZATION is the tree codes for the handler, or is NULL_TREE if
59   one hasn't been generated yet, or is integer_zero_node to mark the
60   end of a group of try blocks.  */
61
62struct eh_entry {
63  rtx outer_context;
64  rtx exception_handler_label;
65  tree finalization;
66  int label_used;
67  rtx false_label;
68  rtx rethrow_label;
69};
70
71/* A list of EH_ENTRYs. ENTRY is the entry; CHAIN points to the next
72   entry in the list, or is NULL if this is the last entry.  */
73
74struct eh_node {
75  struct eh_entry *entry;
76  struct eh_node *chain;
77};
78
79/* A stack of EH_ENTRYs. TOP is the topmost entry on the stack. TOP is
80   NULL if the stack is empty.  */
81
82struct eh_stack {
83  struct eh_node *top;
84};
85
86/* A queue of EH_ENTRYs. HEAD is the front of the queue; TAIL is the
87   end (the latest entry). HEAD and TAIL are NULL if the queue is
88   empty.  */
89
90struct eh_queue {
91  struct eh_node *head;
92  struct eh_node *tail;
93};
94
95/* Start an exception handling region.  All instructions emitted after
96   this point are considered to be part of the region until
97   expand_eh_region_end () is invoked.  */
98
99extern void expand_eh_region_start		PROTO((void));
100
101/* Just like expand_eh_region_start, except if a cleanup action is
102   entered on the cleanup chain, the TREE_PURPOSE of the element put
103   on the chain is DECL.  DECL should be the associated VAR_DECL, if
104   any, otherwise it should be NULL_TREE.  */
105
106extern void expand_eh_region_start_for_decl	PROTO((tree));
107
108/* Start an exception handling region for the given cleanup action.
109   All instructions emitted after this point are considered to be part
110   of the region until expand_eh_region_end () is invoked.  CLEANUP is
111   the cleanup action to perform.  The return value is true if the
112   exception region was optimized away.  If that case,
113   expand_eh_region_end does not need to be called for this cleanup,
114   nor should it be.
115
116   This routine notices one particular common case in C++ code
117   generation, and optimizes it so as to not need the exception
118   region.  */
119
120extern int expand_eh_region_start_tree		PROTO((tree, tree));
121
122/* End an exception handling region.  The information about the region
123   is found on the top of ehstack.
124
125   HANDLER is either the cleanup for the exception region, or if we're
126   marking the end of a try block, HANDLER is integer_zero_node.
127
128   HANDLER will be transformed to rtl when expand_leftover_cleanups ()
129   is invoked.  */
130
131extern void expand_eh_region_end		PROTO((tree));
132
133/* Push RLABEL or TLABEL onto LABELSTACK. Only one of RLABEL or TLABEL
134   should be set; the other must be NULL.  */
135
136extern void push_label_entry			PROTO((struct label_node **labelstack, rtx rlabel, tree tlabel));
137
138/* Pop the topmost entry from LABELSTACK and return its value as an
139   rtx node. If LABELSTACK is empty, return NULL.  */
140
141extern rtx pop_label_entry			PROTO((struct label_node **labelstack));
142
143/* Return the topmost entry of LABELSTACK as a tree node, or return
144   NULL_TREE if LABELSTACK is empty.  */
145
146extern tree top_label_entry			PROTO((struct label_node **labelstack));
147
148/* A set of insns for the catch clauses in the current function. They
149   will be emitted at the end of the current function.  */
150
151extern rtx catch_clauses;
152
153#endif
154
155/* Test: is exception handling turned on? */
156
157extern int doing_eh				       PROTO ((int));
158
159/* Toplevel initialization for EH.  */
160
161void set_exception_lang_code                    PROTO((int));
162void set_exception_version_code                 PROTO((int));
163
164/* A list of handlers asocciated with an exception region. HANDLER_LABEL
165   is the the label that control should be transfered to if the data
166   in TYPE_INFO matches an exception. a value of NULL_TREE for TYPE_INFO
167   means This is a cleanup, and must always be called. A value of
168   CATCH_ALL_TYPE works like a cleanup, but a call to the runtime matcher
169   is still performed to avoid being caught by a different language
170   exception. NEXT is a pointer to the next handler for this region.
171   NULL means there are no more. */
172
173typedef struct handler_info
174{
175  rtx handler_label;
176  int handler_number;
177  void *type_info;
178  struct handler_info *next;
179} handler_info;
180
181
182/* Add new handler information to an exception range. The  first parameter
183   specifies the range number (returned from new_eh_entry()). The second
184   parameter specifies the handler.  By default the handler is inserted at
185   the end of the list. A handler list may contain only ONE NULL_TREE
186   typeinfo entry. Regardless where it is positioned, a NULL_TREE entry
187   is always output as the LAST handler in the exception table for a region. */
188
189void add_new_handler                       PROTO((int, struct handler_info *));
190
191/* Remove a handler label. The handler label is being deleted, so all
192   regions which reference this handler should have it removed from their
193   list of possible handlers. Any region which has the final handler
194   removed can be deleted. */
195
196void remove_handler                        PROTO((rtx));
197
198/* Create a new handler structure initialized with the handler label and
199   typeinfo fields passed in. */
200
201struct handler_info *get_new_handler            PROTO((rtx, void *));
202
203/* Make a duplicate of an exception region by copying all the handlers
204   for an exception region. Return the new handler index. */
205
206int duplicate_eh_handlers                       PROTO((int, int, rtx (*)(rtx)));
207
208/* map symbol refs for rethrow */
209
210rtx rethrow_symbol_map                          PROTO((rtx, rtx (*)(rtx)));
211
212/* Is the rethrow label for a region used? */
213
214int rethrow_used                                PROTO((int));
215
216/* Return the region number a this is the rethrow label for. */
217
218int eh_region_from_symbol                       PROTO((rtx));
219
220/* Get a pointer to the first handler in an exception region's list. */
221
222struct handler_info *get_first_handler          PROTO((int));
223
224/* Find all the runtime handlers type matches currently referenced */
225
226int find_all_handler_type_matches               PROTO((void ***));
227
228extern void init_eh				PROTO((void));
229
230/* Initialization for the per-function EH data.  */
231
232extern void init_eh_for_function		PROTO((void));
233
234/* Generate an exception label. Use instead of gen_label_rtx */
235
236extern rtx gen_exception_label                  PROTO((void));
237
238/* Adds an EH table entry for EH entry number N. Called from
239   final_scan_insn for NOTE_INSN_EH_REGION_BEG.  */
240
241extern void add_eh_table_entry			PROTO((int n));
242
243/* Start a catch clause, triggered by runtime value paramter. */
244
245#ifdef TREE_CODE
246extern void start_catch_handler                 PROTO((tree));
247#endif
248
249/* End an individual catch clause. */
250
251extern void end_catch_handler                   PROTO((void));
252
253/* Returns a non-zero value if we need to output an exception table.  */
254
255extern int exception_table_p			PROTO((void));
256
257/* Outputs the exception table if we have one.  */
258
259extern void output_exception_table		PROTO((void));
260
261/* Given a return address in ADDR, determine the address we should use
262   to find the corresponding EH region.  */
263
264extern rtx eh_outer_context			PROTO((rtx addr));
265
266/* Called at the start of a block of try statements for which there is
267   a supplied catch handler.  */
268
269extern void expand_start_try_stmts 		PROTO((void));
270
271/* Called at the start of a block of catch statements. It terminates the
272   previous set of try statements.  */
273
274extern void expand_start_all_catch		PROTO((void));
275
276/* Called at the end of a block of catch statements.  */
277
278extern void expand_end_all_catch		PROTO((void));
279
280#ifdef TREE_CODE
281/* Create a new exception region and add the handler for the region
282   onto a list. These regions will be ended (and their handlers
283   emitted) when end_protect_partials is invoked.  */
284
285extern void add_partial_entry			PROTO((tree handler));
286#endif
287
288/* End all of the pending exception regions that have handlers added with
289   push_protect_entry ().  */
290
291extern void end_protect_partials		PROTO((void));
292
293/* An internal throw.  */
294
295extern void expand_internal_throw		PROTO((void));
296
297/* Called from expand_exception_blocks and expand_end_catch_block to
298   expand and pending handlers.  */
299
300extern void expand_leftover_cleanups		PROTO((void));
301
302/* If necessary, emit insns to get EH context for the current
303   function. */
304
305extern void emit_eh_context			PROTO((void));
306
307/* Builds a list of handler labels and puts them in the global
308   variable exception_handler_labels.  */
309
310extern void find_exception_handler_labels	PROTO((void));
311
312/* Determine if an arbitrary label is an exception label */
313
314extern int is_exception_handler_label           PROTO((int));
315
316/* Performs sanity checking on the check_exception_handler_labels
317   list.  */
318
319extern void check_exception_handler_labels	PROTO((void));
320
321/* A stack used to keep track of the label used to resume normal program
322   flow out of the current exception handler region.  */
323
324extern struct label_node *caught_return_label_stack;
325
326/* Keeps track of the label used as the context of a throw to rethrow an
327   exception to the outer exception region.  */
328
329extern struct label_node *outer_context_label_stack;
330
331/* A random area used for purposes elsewhere.  */
332
333extern struct label_node *false_label_stack;
334
335/* A list of labels used for exception handlers. It is created by
336   find_exception_handler_labels for the optimization passes.  */
337
338extern rtx exception_handler_labels;
339
340/* Performs optimizations for exception handling, such as removing
341   unnecessary exception regions. Invoked from jump_optimize ().  */
342
343extern void exception_optimize			PROTO((void));
344
345/* Return EH context (and set it up once per fn).  */
346extern rtx get_eh_context			PROTO((void));
347
348/* Get the dynamic handler chain.  */
349extern rtx get_dynamic_handler_chain		PROTO((void));
350
351/* Get the dynamic cleanup chain.  */
352extern rtx get_dynamic_cleanup_chain		PROTO((void));
353
354/* Throw an exception.  */
355
356extern void emit_throw				PROTO((void));
357
358/* One to use setjmp/longjmp method of generating code.  */
359
360extern int exceptions_via_longjmp;
361
362/* One to enable asynchronous exception support.  */
363
364extern int asynchronous_exceptions;
365
366/* One to protect cleanup actions with a handler that calls
367   __terminate, zero otherwise.  */
368
369extern int protect_cleanup_actions_with_terminate;
370
371#ifdef TREE_CODE
372extern tree protect_with_terminate		PROTO((tree));
373#endif
374
375extern void expand_fixup_region_start	PROTO((void));
376#ifdef TREE_CODE
377extern void expand_fixup_region_end	PROTO((tree));
378#endif
379
380/* Various hooks for the DWARF 2 __throw routine.  */
381
382void expand_builtin_unwind_init		PROTO((void));
383rtx expand_builtin_dwarf_fp_regnum	PROTO((void));
384#ifdef TREE_CODE
385rtx expand_builtin_frob_return_addr	PROTO((tree));
386rtx expand_builtin_extract_return_addr	PROTO((tree));
387rtx expand_builtin_dwarf_reg_size	PROTO((tree, rtx));
388void expand_builtin_eh_return		PROTO((tree, tree, tree));
389#endif
390void expand_eh_return			PROTO((void));
391
392
393/* Checking whether 2 instructions are within the same exception region. */
394
395int in_same_eh_region                   PROTO((rtx, rtx));
396void free_insn_eh_region                PROTO((void));
397void init_insn_eh_region                PROTO((rtx, int));
398
399#ifdef rtx
400#undef rtx
401#endif
402