📄 except.c
字号:
/* Exception support for GNU CHILL. WARNING: Only works for native (needs setjmp.h)! FIXME! Copyright (C) 1992, 93, 1994, 1998 Free Software Foundation, Inc.This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe Free Software Foundation, 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA. */#include "config.h"#include "system.h"/* On Suns this can get you to the right definition if you set the right value for TARGET. */#include <setjmp.h>#ifdef sequent/* Can you believe they forgot this? */#ifndef _JBLEN#define _JBLEN 11#endif#endif#ifndef _JBLEN#define _JBLEN (sizeof(jmp_buf)/sizeof(int))#define _JBLEN_2 _JBLEN+20#else/* if we use i.e. posix threads, this buffer must be longer */#define _JBLEN_2 _JBLEN+20#endif/* On Linux setjmp is __setjmp FIXME: what is for CROSS */#ifndef SETJMP_LIBRARY_NAME#ifdef __linux__#define SETJMP_LIBRARY_NAME "__setjmp"#else#define SETJMP_LIBRARY_NAME "setjmp"#endif#endif#include "tree.h"#include "ch-tree.h"#include "rtl.h"#include "toplev.h"extern int expand_exit_needed;static tree link_handler_decl;static tree handler_link_pointer_type;static tree unlink_handler_decl;static int exceptions_initialized = 0;static void emit_setup_handler PROTO((void));static void initialize_exceptions PROTO((void));static tree char_pointer_type_for_handler;/* If this is 1, operations to push and pop on the __exceptionStack are inline. The default is is to use a function call, to allow for a per-thread exception stack. */static int inline_exception_stack_ops = 0;struct handler_state{ struct handler_state *next; /* Starts at 0, then incremented for every <on-alternative>. */ int prev_on_alternative; /* If > 0: handler number for ELSE handler. */ int else_handler; int action_number; char do_pushlevel; tree on_alt_list; tree setjmp_expr; /* A decl for the static handler array (used to map exception name to int).*/ tree handler_array_decl; rtx end_label; /* Used to pass a tree from emit_setup_handler to chill_start_on. */ tree handler_ref; tree unlink_cleanup; tree function; /* flag to indicate that we are currently compiling this handler. is_handled will need this to determine an unhandled exception */ int compiling;};/* This is incremented by one each time we start an action which might have an ON-handler. It is reset between passes. */static int action_number = 0;int action_nesting_level = 0;/* The global_handler_list is constructed in pass 1. It is not sorted. It contains one element for each action that actually had an ON-handler. An element's ACTION_NUMBER matches the action_number of that action. The global_handler_list is eaten up during pass 2. */#define ACTION_NUMBER(HANDLER) ((HANDLER)->action_number)struct handler_state *global_handler_list = NULL;/* This is a stack of handlers, one for each nested ON-handler. */static struct handler_state *current_handler = NULL;static struct handler_state *free_handlers = NULL; /* freelist */static tree handler_element_type;static tree handler_link_type;static tree BISJ;static tree jbuf_ident, prev_ident, handlers_ident;static tree exception_stack_decl = 0;/* Chain of cleanups assocated with exception handlers. The TREE_PURPOSE is an INTEGER_CST whose value is the DECL_ACTION_NESTING_LEVEL (when the handled actions was entered). The TREE_VALUE is an expression to expand when we exit that action. */static tree cleanup_chain = NULL_TREE;#if 0/* Merge the current sequence onto the tail of the previous one. */voidpop_sequence (){ rtx sequence_first = get_insns (); end_sequence (); emit_insns (sequence_first); }#endif/* Things we need to do at the beginning of pass 2. */voidexcept_init_pass_2 (){ /* First sort the global_handler_list on ACTION_NUMBER. This will already be in close to reverse order (the exception being nested ON-handlers), so insertion sort should essentially linear. */ register struct handler_state *old_list = global_handler_list; /* First add a dummy final element. */ if (free_handlers) global_handler_list = free_handlers; else global_handler_list = (struct handler_state*) permalloc (sizeof (struct handler_state)); /* Make the final dummy "larger" than any other element. */ ACTION_NUMBER (global_handler_list) = action_number + 1; /* Now move all the elements in old_list over to global_handler_list. */ while (old_list != NULL) { register struct handler_state **ptr = &global_handler_list; /* Unlink from old_list. */ register struct handler_state *current = old_list; old_list = old_list->next; while (ACTION_NUMBER (current) > ACTION_NUMBER (*ptr)) ptr = &(*ptr)->next; /* Link into proper place in global_handler_list (new list). */ current->next = *ptr; *ptr = current; } /* Don't forget to reset action_number. */ action_number = 0;}/* This function is called at the beginning of an action that might be followed by an ON-handler. Chill syntax doesn't let us know if we actually have an ON-handler until we see the ON, so we save away during pass 1 that information for use during pass 2. */voidpush_handler (){ register struct handler_state *hstate; action_number++; action_nesting_level++; if (pass == 1) { if (free_handlers) { hstate = free_handlers; free_handlers = hstate->next; } else { hstate = (struct handler_state*) permalloc (sizeof (struct handler_state)); } hstate->next = current_handler; current_handler = hstate; hstate->prev_on_alternative = 0; hstate->else_handler = 0; hstate->on_alt_list = NULL_TREE; hstate->compiling = 0; ACTION_NUMBER (hstate) = action_number; return; } if (ACTION_NUMBER (global_handler_list) != action_number) return; /* OK. This action actually has an ON-handler. Pop it from global_handler_list, and use it. */ hstate = global_handler_list; global_handler_list = hstate->next; /* Since this is pass 2, let's generate prologue code for that. */ hstate->next = current_handler; current_handler = hstate; hstate->prev_on_alternative = 0; hstate->function = current_function_decl; emit_setup_handler ();}static treestart_handler_array (){ tree handler_array_type, decl; push_obstacks_nochange (); end_temporary_allocation (); handler_array_type = build_array_type (handler_element_type, NULL_TREE); decl = build_lang_decl (VAR_DECL, get_unique_identifier ("handler_table"), handler_array_type);/* TREE_TYPE (decl) = handler_array_type;*/ TREE_READONLY (decl) = 1; TREE_STATIC (decl) = 1; DECL_INITIAL (decl) = error_mark_node; pushdecl (decl); make_decl_rtl (decl, NULL_PTR, 0); current_handler->handler_array_decl = decl; return decl;}static voidfinish_handler_array (){ tree decl = current_handler->handler_array_decl; tree t; tree handler_array_init = NULL_TREE; int handlers_count = 1; int nelts; /* Build the table mapping exceptions to handler(-number)s. This is done in reverse order. */ /* First push the end of the list. This is either the ELSE handler (current_handler->else_handler>0) or NULL handler to indicate the end of the list (if current_handler->else-handler == 0). The following works either way. */ handler_array_init = build_tree_list (NULL_TREE, chill_expand_tuple (handler_element_type, build_nt (CONSTRUCTOR, NULL_TREE, tree_cons (NULL_TREE, null_pointer_node, build_tree_list (NULL_TREE, build_int_2 (current_handler->else_handler, 0)))))); for (t = current_handler->on_alt_list; t != NULL_TREE; t = TREE_CHAIN (t)) { tree handler_number = TREE_PURPOSE(t); tree elist = TREE_VALUE (t); for ( ; elist != NULL_TREE; elist = TREE_CHAIN (elist)) { tree ex_decl = build_chill_exception_decl (IDENTIFIER_POINTER(TREE_VALUE(elist))); tree ex_addr = build1 (ADDR_EXPR, char_pointer_type_for_handler, ex_decl); tree el = build_nt (CONSTRUCTOR, NULL_TREE, tree_cons (NULL_TREE, ex_addr, build_tree_list (NULL_TREE, handler_number))); mark_addressable (ex_decl); TREE_CONSTANT (ex_addr) = 1; handler_array_init = tree_cons (NULL_TREE, chill_expand_tuple (handler_element_type, el), handler_array_init); handlers_count++; } }#if 1 nelts = list_length (handler_array_init); TYPE_DOMAIN (TREE_TYPE (decl)) = build_index_type (build_int_2 (nelts - 1, - (nelts == 0))); layout_type (TREE_TYPE (decl)); DECL_INITIAL (decl) = convert (TREE_TYPE (decl), build_nt (CONSTRUCTOR, NULL_TREE, handler_array_init)); /* Pop back to the obstack that is current for this binding level. This is because MAXINDEX, rtl, etc. to be made below must go in the permanent obstack. But don't discard the temporary data yet. */ pop_obstacks (); layout_decl (decl, 0); /* To prevent make_decl_rtl (called indiectly by rest_of_decl_compilation) throwing the existing RTL (which has already been used). */ PUT_MODE (DECL_RTL (decl), DECL_MODE (decl)); rest_of_decl_compilation (decl, (char*)0, 0, 0); expand_decl_init (decl);#else /* To prevent make_decl_rtl (called indirectly by finish_decl) altering the existing RTL. */ GET_MODE (DECL_RTL (current_handler->handler_array_decl)) = DECL_MODE (current_handler->handler_array_decl); finish_decl (current_handler->handler_array_decl, build_nt (CONSTRUCTOR, NULL_TREE, handler_array_init), NULL_TREE);#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -