(arm_comp_type_attributes): Simply and comment tests on type attributes.
[official-gcc.git] / gcc / ch / except.c
bloba59e6759a68d39554909cdbcad7c404fa276b87c
1 /* Exception support for GNU CHILL.
2 WARNING: Only works for native (needs setjmp.h)! FIXME!
3 Copyright (C) 1992, 93, 94, 98, 99, 2000 Free Software Foundation, Inc.
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. */
22 #include "config.h"
23 #include "system.h"
25 /* On Suns this can get you to the right definition if you
26 set the right value for TARGET. */
27 #include <setjmp.h>
28 #ifdef sequent
29 /* Can you believe they forgot this? */
30 #ifndef _JBLEN
31 #define _JBLEN 11
32 #endif
33 #endif
35 #ifndef _JBLEN
36 #define _JBLEN (sizeof(jmp_buf)/sizeof(int))
37 #define _JBLEN_2 _JBLEN+20
38 #else
39 /* if we use i.e. posix threads, this buffer must be longer */
40 #define _JBLEN_2 _JBLEN+20
41 #endif
43 /* On Linux setjmp is __setjmp FIXME: what is for CROSS */
44 #ifndef SETJMP_LIBRARY_NAME
45 #ifdef __linux__
46 #define SETJMP_LIBRARY_NAME "__setjmp"
47 #else
48 #define SETJMP_LIBRARY_NAME "setjmp"
49 #endif
50 #endif
52 #include "tree.h"
53 #include "ch-tree.h"
54 #include "rtl.h"
55 #include "toplev.h"
57 extern int expand_exit_needed;
59 static tree link_handler_decl;
60 static tree handler_link_pointer_type;
61 static tree unlink_handler_decl;
62 static int exceptions_initialized = 0;
63 static void emit_setup_handler PARAMS ((void));
64 static void initialize_exceptions PARAMS ((void));
65 static tree start_handler_array PARAMS ((void));
66 static void finish_handler_array PARAMS ((void));
67 static tree char_pointer_type_for_handler;
69 /* If this is 1, operations to push and pop on the __exceptionStack
70 are inline. The default is is to use a function call, to
71 allow for a per-thread exception stack. */
72 static int inline_exception_stack_ops = 0;
74 struct handler_state
76 struct handler_state *next;
78 /* Starts at 0, then incremented for every <on-alternative>. */
79 int prev_on_alternative;
81 /* If > 0: handler number for ELSE handler. */
82 int else_handler;
84 int action_number;
86 char do_pushlevel;
88 tree on_alt_list;
89 tree setjmp_expr;
91 /* A decl for the static handler array (used to map exception name to int).*/
92 tree handler_array_decl;
94 rtx end_label;
96 /* Used to pass a tree from emit_setup_handler to chill_start_on. */
97 tree handler_ref;
99 tree unlink_cleanup;
101 tree function;
103 /* flag to indicate that we are currently compiling this handler.
104 is_handled will need this to determine an unhandled exception */
105 int compiling;
108 /* This is incremented by one each time we start an action which
109 might have an ON-handler. It is reset between passes. */
110 static int action_number = 0;
112 int action_nesting_level = 0;
114 /* The global_handler_list is constructed in pass 1. It is not sorted.
115 It contains one element for each action that actually had an ON-handler.
116 An element's ACTION_NUMBER matches the action_number
117 of that action. The global_handler_list is eaten up during pass 2. */
118 #define ACTION_NUMBER(HANDLER) ((HANDLER)->action_number)
119 struct handler_state *global_handler_list = NULL;
121 /* This is a stack of handlers, one for each nested ON-handler. */
122 static struct handler_state *current_handler = NULL;
124 static struct handler_state *free_handlers = NULL; /* freelist */
126 static tree handler_element_type;
127 static tree handler_link_type;
128 static tree BISJ;
129 static tree jbuf_ident, prev_ident, handlers_ident;
130 static tree exception_stack_decl = 0;
132 /* Chain of cleanups assocated with exception handlers.
133 The TREE_PURPOSE is an INTEGER_CST whose value is the
134 DECL_ACTION_NESTING_LEVEL (when the handled actions was entered).
135 The TREE_VALUE is an expression to expand when we exit that action. */
137 static tree cleanup_chain = NULL_TREE;
139 #if 0
140 /* Merge the current sequence onto the tail of the previous one. */
142 void
143 pop_sequence ()
145 rtx sequence_first = get_insns ();
147 end_sequence ();
148 emit_insns (sequence_first);
151 #endif
153 /* Things we need to do at the beginning of pass 2. */
155 void
156 except_init_pass_2 ()
158 /* First sort the global_handler_list on ACTION_NUMBER.
159 This will already be in close to reverse order (the exception being
160 nested ON-handlers), so insertion sort should essentially linear. */
162 register struct handler_state *old_list = global_handler_list;
164 /* First add a dummy final element. */
165 if (free_handlers)
166 global_handler_list = free_handlers;
167 else
168 global_handler_list
169 = (struct handler_state*) permalloc (sizeof (struct handler_state));
170 /* Make the final dummy "larger" than any other element. */
171 ACTION_NUMBER (global_handler_list) = action_number + 1;
172 /* Now move all the elements in old_list over to global_handler_list. */
173 while (old_list != NULL)
175 register struct handler_state **ptr = &global_handler_list;
176 /* Unlink from old_list. */
177 register struct handler_state *current = old_list;
178 old_list = old_list->next;
180 while (ACTION_NUMBER (current) > ACTION_NUMBER (*ptr))
181 ptr = &(*ptr)->next;
182 /* Link into proper place in global_handler_list (new list). */
183 current->next = *ptr;
184 *ptr = current;
187 /* Don't forget to reset action_number. */
188 action_number = 0;
191 /* This function is called at the beginning of an action that might be
192 followed by an ON-handler. Chill syntax doesn't let us know if
193 we actually have an ON-handler until we see the ON, so we save
194 away during pass 1 that information for use during pass 2. */
196 void
197 push_handler ()
199 register struct handler_state *hstate;
201 action_number++;
202 action_nesting_level++;
204 if (pass == 1)
206 if (free_handlers)
208 hstate = free_handlers;
209 free_handlers = hstate->next;
211 else
213 hstate =
214 (struct handler_state*) permalloc (sizeof (struct handler_state));
217 hstate->next = current_handler;
218 current_handler = hstate;
219 hstate->prev_on_alternative = 0;
220 hstate->else_handler = 0;
221 hstate->on_alt_list = NULL_TREE;
222 hstate->compiling = 0;
224 ACTION_NUMBER (hstate) = action_number;
225 return;
228 if (ACTION_NUMBER (global_handler_list) != action_number)
229 return;
231 /* OK. This action actually has an ON-handler.
232 Pop it from global_handler_list, and use it. */
234 hstate = global_handler_list;
235 global_handler_list = hstate->next;
237 /* Since this is pass 2, let's generate prologue code for that. */
239 hstate->next = current_handler;
240 current_handler = hstate;
242 hstate->prev_on_alternative = 0;
243 hstate->function = current_function_decl;
245 emit_setup_handler ();
248 static tree
249 start_handler_array ()
251 tree handler_array_type, decl;
253 push_obstacks_nochange ();
254 end_temporary_allocation ();
255 handler_array_type = build_array_type (handler_element_type, NULL_TREE);
256 decl = build_lang_decl (VAR_DECL,
257 get_unique_identifier ("handler_table"),
258 handler_array_type);
260 /* TREE_TYPE (decl) = handler_array_type;*/
261 TREE_READONLY (decl) = 1;
262 TREE_STATIC (decl) = 1;
263 DECL_INITIAL (decl) = error_mark_node;
265 pushdecl (decl);
266 make_decl_rtl (decl, NULL_PTR, 0);
267 current_handler->handler_array_decl = decl;
268 return decl;
271 static void
272 finish_handler_array ()
274 tree decl = current_handler->handler_array_decl;
275 tree t;
276 tree handler_array_init = NULL_TREE;
277 int handlers_count = 1;
278 int nelts;
280 /* Build the table mapping exceptions to handler(-number)s.
281 This is done in reverse order. */
283 /* First push the end of the list. This is either the ELSE
284 handler (current_handler->else_handler>0) or NULL handler to indicate
285 the end of the list (if current_handler->else-handler == 0).
286 The following works either way. */
287 handler_array_init = build_tree_list
288 (NULL_TREE, chill_expand_tuple
289 (handler_element_type,
290 build_nt (CONSTRUCTOR, NULL_TREE,
291 tree_cons (NULL_TREE,
292 null_pointer_node,
293 build_tree_list (NULL_TREE,
294 build_int_2 (current_handler->else_handler,
295 0))))));
297 for (t = current_handler->on_alt_list; t != NULL_TREE; t = TREE_CHAIN (t))
298 { tree handler_number = TREE_PURPOSE(t);
299 tree elist = TREE_VALUE (t);
300 for ( ; elist != NULL_TREE; elist = TREE_CHAIN (elist))
302 tree ex_decl =
303 build_chill_exception_decl (IDENTIFIER_POINTER(TREE_VALUE(elist)));
304 tree ex_addr = build1 (ADDR_EXPR,
305 char_pointer_type_for_handler,
306 ex_decl);
307 tree el = build_nt (CONSTRUCTOR, NULL_TREE,
308 tree_cons (NULL_TREE,
309 ex_addr,
310 build_tree_list (NULL_TREE,
311 handler_number)));
312 mark_addressable (ex_decl);
313 TREE_CONSTANT (ex_addr) = 1;
314 handler_array_init =
315 tree_cons (NULL_TREE,
316 chill_expand_tuple (handler_element_type, el),
317 handler_array_init);
318 handlers_count++;
322 #if 1
323 nelts = list_length (handler_array_init);
324 TYPE_DOMAIN (TREE_TYPE (decl))
325 = build_index_type (build_int_2 (nelts - 1, - (nelts == 0)));
326 layout_type (TREE_TYPE (decl));
327 DECL_INITIAL (decl)
328 = convert (TREE_TYPE (decl),
329 build_nt (CONSTRUCTOR, NULL_TREE, handler_array_init));
331 /* Pop back to the obstack that is current for this binding level.
332 This is because MAXINDEX, rtl, etc. to be made below
333 must go in the permanent obstack. But don't discard the
334 temporary data yet. */
335 pop_obstacks ();
336 layout_decl (decl, 0);
337 /* To prevent make_decl_rtl (called indiectly by rest_of_decl_compilation)
338 throwing the existing RTL (which has already been used). */
339 PUT_MODE (DECL_RTL (decl), DECL_MODE (decl));
340 rest_of_decl_compilation (decl, (char*)0, 0, 0);
341 expand_decl_init (decl);
342 #else
343 /* To prevent make_decl_rtl (called indirectly by finish_decl)
344 altering the existing RTL. */
345 GET_MODE (DECL_RTL (current_handler->handler_array_decl)) =
346 DECL_MODE (current_handler->handler_array_decl);
348 finish_decl (current_handler->handler_array_decl,
349 build_nt (CONSTRUCTOR, NULL_TREE, handler_array_init),
350 NULL_TREE);
351 #endif
355 void
356 pop_handler (used)
357 int used;
359 action_nesting_level--;
360 if (pass == 1)
362 struct handler_state *old = current_handler;
363 if (old == NULL)
364 fatal ("internal error: on stack out of sync");
365 current_handler = old->next;
367 if (used)
368 { /* Push unto global_handler_list. */
369 old->next = global_handler_list;
370 global_handler_list = old;
372 else
374 /* Push onto free_handlers free list. */
375 old->next = free_handlers;
376 free_handlers = old;
379 else if (used)
381 current_handler = current_handler->next;
385 /* Emit code before an action that has an ON-handler. */
387 static void
388 emit_setup_handler ()
390 tree handler_decl, handler_addr, t;
392 /* Field references. */
393 tree jbuf_ref, handlers_ref,prev_ref;
394 if (!exceptions_initialized)
396 /* We temporarily reset the maximum_field_alignment to zero so the
397 compiler's exception data structures can be compatible with the
398 run-time system, even when we're compiling with -fpack. */
399 unsigned int save_maximum_field_alignment = maximum_field_alignment;
400 maximum_field_alignment = 0;
401 push_obstacks_nochange ();
402 end_temporary_allocation ();
403 initialize_exceptions ();
404 pop_obstacks ();
405 maximum_field_alignment = save_maximum_field_alignment;
408 push_momentary ();
410 handler_decl = build_lang_decl (VAR_DECL,
411 get_unique_identifier ("handler"),
412 handler_link_type);
413 push_obstacks_nochange ();
414 pushdecl(handler_decl);
415 expand_decl (handler_decl);
416 finish_decl (handler_decl);
418 jbuf_ref = build_component_ref (handler_decl, jbuf_ident);
419 jbuf_ref = build_chill_arrow_expr (jbuf_ref, 1);
420 handlers_ref = build_component_ref (handler_decl, handlers_ident);
421 prev_ref = build_component_ref (handler_decl, prev_ident);
423 /* Emit code to link in handler in __exceptionStack chain. */
424 mark_addressable (handler_decl);
425 handler_addr = build1 (ADDR_EXPR, handler_link_pointer_type, handler_decl);
426 if (inline_exception_stack_ops)
428 expand_expr_stmt (build_chill_modify_expr (prev_ref,
429 exception_stack_decl));
430 expand_expr_stmt (build_chill_modify_expr (exception_stack_decl,
431 handler_addr));
432 current_handler->handler_ref = prev_ref;
434 else
436 expand_expr_stmt (build_chill_function_call (link_handler_decl,
437 build_tree_list (NULL_TREE,
438 handler_addr)));
439 current_handler->handler_ref = handler_addr;
442 /* Expand: handler->__handlers = { <<array mapping names to ints } */
443 t = build1 (NOP_EXPR, build_pointer_type (handler_element_type),
444 build_chill_arrow_expr (start_handler_array (), 1));
445 expand_expr_stmt (build_chill_modify_expr (handlers_ref, t));
447 /* Emit code to unlink handler. */
448 if (inline_exception_stack_ops)
449 current_handler->unlink_cleanup
450 = build_chill_modify_expr (exception_stack_decl,
451 current_handler->handler_ref);
452 else
453 current_handler->unlink_cleanup
454 = build_chill_function_call (unlink_handler_decl,
455 build_tree_list(NULL_TREE,
456 current_handler->handler_ref));
457 cleanup_chain = tree_cons (build_int_2 (action_nesting_level, 0),
458 current_handler->unlink_cleanup,
459 cleanup_chain);
461 /* Emit code for setjmp. */
463 current_handler->setjmp_expr =
464 build_chill_function_call (BISJ, build_tree_list (NULL_TREE, jbuf_ref));
465 expand_start_case (1, current_handler->setjmp_expr,
466 integer_type_node, "on handler");
468 chill_handle_case_label (integer_zero_node, current_handler->setjmp_expr);
471 /* Start emitting code for: <actions> ON <handlers> END.
472 Assume we've parsed <actions>, and the setup needed for it. */
474 void
475 chill_start_on ()
477 expand_expr_stmt (current_handler->unlink_cleanup);
479 /* Emit code to jump past the handlers. */
480 current_handler->end_label = gen_label_rtx ();
481 current_handler->compiling = 1;
482 emit_jump (current_handler->end_label);
485 void
486 chill_finish_on ()
488 expand_end_case (current_handler->setjmp_expr);
490 finish_handler_array ();
492 emit_label (current_handler->end_label);
494 pop_momentary ();
496 cleanup_chain = TREE_CHAIN (cleanup_chain);
499 void
500 chill_handle_on_labels (labels)
501 tree labels;
503 int alternative = ++current_handler->prev_on_alternative;
504 if (pass == 1)
506 tree handler_number = build_int_2 (alternative, 0);
507 current_handler->on_alt_list =
508 tree_cons (handler_number, labels, current_handler->on_alt_list);
510 else
512 /* Find handler_number saved in pass 1. */
513 tree tmp = current_handler->on_alt_list;
514 while (TREE_INT_CST_LOW (TREE_PURPOSE (tmp)) != alternative)
515 tmp = TREE_CHAIN (tmp);
516 if (expand_exit_needed)
517 expand_exit_something (), expand_exit_needed = 0;
518 chill_handle_case_label (TREE_PURPOSE (tmp),
519 current_handler->setjmp_expr);
523 void
524 chill_start_default_handler ()
526 current_handler->else_handler = ++current_handler->prev_on_alternative;
527 if (!ignoring)
529 chill_handle_case_default ();
533 void
534 chill_check_no_handlers ()
536 if (current_handler != NULL)
537 fatal ("internal error: on stack not empty when done");
540 static void
541 initialize_exceptions ()
543 tree jmp_buf_type = build_array_type (integer_type_node,
544 build_index_type (build_int_2 (_JBLEN_2-1, 0)));
545 tree setjmp_fndecl, link_ftype;
546 tree parmtypes
547 = tree_cons (NULL_TREE, build_pointer_type (jmp_buf_type), void_list_node);
549 setjmp_fndecl = builtin_function ("setjmp",
550 build_function_type (integer_type_node,
551 parmtypes),
552 0, NOT_BUILT_IN,
553 SETJMP_LIBRARY_NAME);
554 BISJ = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (setjmp_fndecl)),
555 setjmp_fndecl);
557 char_pointer_type_for_handler
558 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
559 handler_element_type =
560 build_chill_struct_type (chainon
561 (build_decl (FIELD_DECL,
562 get_identifier("__exceptid"),
563 char_pointer_type_for_handler),
564 build_decl (FIELD_DECL,
565 get_identifier("__handlerno"),
566 integer_type_node)));
568 jbuf_ident = get_identifier("__jbuf");
569 prev_ident = get_identifier("__prev");
570 handlers_ident = get_identifier("__handlers");
572 handler_link_type =
573 build_chill_struct_type
574 (chainon
575 (build_decl (FIELD_DECL, prev_ident, ptr_type_node),
576 chainon
577 (build_decl (FIELD_DECL, handlers_ident,
578 build_pointer_type (handler_element_type)),
579 build_decl (FIELD_DECL, jbuf_ident, jmp_buf_type))));
581 handler_link_pointer_type = build_pointer_type (handler_link_type);
583 if (inline_exception_stack_ops)
585 exception_stack_decl =
586 build_lang_decl (VAR_DECL,
587 get_identifier("__exceptionStack"),
588 handler_link_pointer_type);
589 TREE_STATIC (exception_stack_decl) = 1;
590 TREE_PUBLIC (exception_stack_decl) = 1;
591 DECL_EXTERNAL (exception_stack_decl) = 1;
592 push_obstacks_nochange ();
593 pushdecl(exception_stack_decl);
594 make_decl_rtl (exception_stack_decl, NULL_PTR, 1);
595 finish_decl (exception_stack_decl);
598 link_ftype = build_function_type (void_type_node,
599 tree_cons (NULL_TREE,
600 handler_link_pointer_type,
601 void_list_node));
602 link_handler_decl = builtin_function ("__ch_link_handler", link_ftype,
603 0, NOT_BUILT_IN, NULL_PTR);
604 unlink_handler_decl = builtin_function ("__ch_unlink_handler", link_ftype,
605 0, NOT_BUILT_IN, NULL_PTR);
607 exceptions_initialized = 1;
610 /* Do the cleanup(s) needed for a GOTO label.
611 We only need to do the last of the cleanups. */
613 void
614 expand_goto_except_cleanup (label_level)
615 int label_level;
617 tree list = cleanup_chain;
618 tree last = NULL_TREE;
619 for ( ; list != NULL_TREE; list = TREE_CHAIN (list))
621 if (TREE_INT_CST_LOW (TREE_PURPOSE (list)) > label_level)
622 last = list;
623 else
624 break;
626 if (last)
627 expand_expr_stmt (TREE_VALUE (last));
630 /* Returns true if there is a valid handler for EXCEPT_NAME
631 in the current static scope.
632 0 ... no handler found
633 1 ... local handler available
634 2 ... function may propagate this exception
638 is_handled (except_name)
639 tree except_name;
641 tree t;
642 struct handler_state *h = current_handler;
644 /* if we are are currently compiling this handler
645 we have to start at the next level */
646 if (h && h->compiling)
647 h = h->next;
648 while (h != NULL)
650 if (h->function != current_function_decl)
651 break;
652 if (h->else_handler > 0)
653 return 1;
654 for (t = h->on_alt_list; t != NULL_TREE; t = TREE_CHAIN (t))
656 if (value_member (except_name, TREE_VALUE (t)))
657 return 1;
659 h = h->next;
662 t = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl));
664 if (value_member (except_name, t))
665 return 2;
666 return 0;
669 /* function generates code to reraise exceptions
670 for PROC's propagating exceptions. */
672 void
673 chill_reraise_exceptions (exceptions)
674 tree exceptions;
676 tree wrk;
678 if (exceptions == NULL_TREE)
679 return; /* just in case */
681 if (pass == 1)
683 for (wrk = exceptions; wrk != NULL_TREE; wrk = TREE_CHAIN (wrk))
684 chill_handle_on_labels (build_tree_list (NULL_TREE, TREE_VALUE (wrk)));
686 else /* pass == 2 */
688 chill_start_on ();
689 expand_exit_needed = 0;
691 for (wrk = exceptions; wrk != NULL_TREE; wrk = TREE_CHAIN (wrk))
693 chill_handle_on_labels (TREE_VALUE (wrk));
694 /* do a CAUSE exception */
695 expand_expr_stmt (build_cause_exception (TREE_VALUE (wrk), 0));
696 expand_exit_needed = 1;
698 chill_finish_on ();
700 pop_handler (1);