* tree.c (unsave_expr_now): Handle NULL_TREE as input.
[official-gcc.git] / libjava / exception.cc
blob0dbf4aa1b54bc724d2d31083ca9a6558afedb3b5
1 // Functions for Exception Support for Java.
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include "exception"
14 #include <stddef.h>
16 #include <java/lang/Class.h>
17 #include <java/lang/NullPointerException.h>
18 #include <gcj/cni.h>
19 #include <jvm.h>
21 // eh-common.h needs gansidecl.h.
22 #include "gansidecl.h"
23 #include "eh-common.h"
25 typedef struct {
26 __eh_info eh_info;
27 void *value;
28 } java_eh_info;
31 /* Language-specific EH info pointer, throw routine, and language/version
32 info routines. All defined in libgcc2. */
34 extern "C" java_eh_info **__get_eh_info ();
35 extern "C" void __throw () __attribute__ ((__noreturn__));
36 extern "C" void __sjthrow () __attribute__ ((__noreturn__));
37 extern "C" short __get_eh_table_version (void *table);
38 extern "C" short __get_eh_table_language (void *table);
40 extern "C" void * malloc (size_t);
41 extern "C" void free (void *);
44 extern "C" void *
45 _Jv_type_matcher (java_eh_info *info, void* match_info,
46 void *exception_table)
48 #ifndef SJLJ_EXCEPTIONS
49 /* No exception table implies the old style mechanism, so don't check. */
50 if (exception_table != NULL
51 && __get_eh_table_language (exception_table) != EH_LANG_Java)
52 return NULL;
53 #endif
55 /* we don't worry about version info yet, there is only one version! */
57 if (match_info != NULL)
59 // The match_info is either a (java::lang::Class*) or
60 // match_info is one more than a (Utf8Const*).
61 if (sizeof(void*) != sizeof(size_t))
62 abort();
63 size_t mi = (size_t) match_info;
64 if ((mi & 1) != 0)
65 match_info = _Jv_FindClass ((Utf8Const*) (mi - 1), NULL);
66 if (! _Jv_IsInstanceOf ((jobject) info->value, (jclass) match_info))
67 return NULL;
70 return info->value;
73 /* Compiler hook to return a pointer to java exception object. The value
74 is cleared, so if the exception needs to be rethrown, it should be set
75 again */
77 extern "C" void *
78 _Jv_exception_info (void)
80 java_eh_info *info = *(__get_eh_info ());
81 void *ptr;
83 if (info == NULL)
84 abort ();
86 ptr = info->value;
88 /* clear the value so another throw is an error */
89 info->value = NULL;
91 return ptr;
96 /* Allocate an exception info structure for java. Called the first time
97 an exception is thrown. */
99 extern "C" void
100 _Jv_eh_alloc ()
102 /* FIXME: we should use _Jv_AllocBytes here. However, libgcc2
103 apparently can sometimes free() this value itself. */
104 java_eh_info *p = (java_eh_info *) malloc (sizeof (java_eh_info));
105 if (p == 0)
106 terminate ();
108 p->value = 0;
109 java_eh_info ** info_ptr = __get_eh_info ();
111 /* There should NOT be an exception info pointer already. */
112 if (*info_ptr != NULL)
113 abort ();
115 *info_ptr = p;
118 /* Deallocate the current exception info structure. Called at shutdown time. */
120 extern "C" void
121 _Jv_eh_free ()
123 java_eh_info ** info_ptr = __get_eh_info ();
124 if (*info_ptr == NULL)
125 abort ();
127 /* FIXME: ideally we should just let the GC handle this. */
128 free (*info_ptr);
129 *info_ptr = NULL;
132 /* Initialize an __eh_info structure with this libraries matching info. */
134 extern "C" void
135 _Jv_setup_eh_info (__eh_info *)
139 /* Perform a throw, Java style. Throw will unwind through this call,
140 so there better not be any handlers or exception thrown here. */
142 extern "C" void
143 _Jv_Throw (void *value)
145 if (value == NULL)
146 value = (void *) new java::lang::NullPointerException ();
147 java_eh_info *ehinfo = *(__get_eh_info ());
148 if (ehinfo == NULL)
150 _Jv_eh_alloc ();
151 ehinfo = *(__get_eh_info ());
153 ehinfo->eh_info.match_function = (__eh_matcher) _Jv_type_matcher;
154 ehinfo->eh_info.language = EH_LANG_Java;
155 ehinfo->eh_info.version = 1;
156 ehinfo->value = value;
158 /* We're happy with setjmp/longjmp exceptions or region-based
159 exception handlers: entry points are provided here for both. */
160 #ifdef SJLJ_EXCEPTIONS
161 __sjthrow ();
162 #else
163 __throw ();
164 #endif