* src/native/jni.c (_Jv_JNI_FindClass): Renamed to jni_FindClass, call
[cacao.git] / src / vm / exceptions.c
index 3b2859d8b76a6e9151da7a420c6eeece18a83c88..2b3f71ba8eb52a82de096d86b3e03c9290824369 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/exceptions.c - exception related functions
 
-   Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
-   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
-   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
-   J. Wenninger, Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
@@ -22,8 +20,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: exceptions.c 8318 2007-08-16 10:05:34Z michi $
-
 */
 
 
@@ -33,7 +29,6 @@
 #include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <sys/mman.h>
 
 #include "vm/types.h"
 #include "native/native.h"
 
 #include "native/include/java_lang_String.h"
+#include "native/include/java_lang_Thread.h"
 #include "native/include/java_lang_Throwable.h"
 
 #include "threads/lock-common.h"
-#include "threads/threads-common.h"
+#include "threads/thread.h"
 
 #include "toolbox/util.h"
 
 #include "vm/jit/jit.h"
 #include "vm/jit/methodheader.h"
 #include "vm/jit/patcher-common.h"
+#include "vm/jit/show.h"
 #include "vm/jit/stacktrace.h"
+#include "vm/jit/trace.h"
 
 #include "vmcore/class.h"
 #include "vmcore/loader.h"
 #include "vmcore/method.h"
 #include "vmcore/options.h"
+#include "vmcore/system.h"
 
 #if defined(ENABLE_VMLOG)
 #include <vmlog_cacao.h>
@@ -83,13 +82,13 @@ java_object_t *_no_threads_exceptionptr = NULL;
 #endif
 
 
-/* init_system_exceptions ******************************************************
+/* exceptions_init *************************************************************
 
-   Load and link exceptions used in the system.
+   Initialize the exceptions subsystem.
 
 *******************************************************************************/
 
-bool exceptions_init(void)
+void exceptions_init(void)
 {
 #if !(defined(__ARM__) && defined(__LINUX__))
        /* On arm-linux the first memory page can't be mmap'ed, as it
@@ -100,180 +99,244 @@ bool exceptions_init(void)
        /* mmap a memory page at address 0x0, so our hardware-exceptions
           work. */
 
-       pagesize = getpagesize();
+       pagesize = system_getpagesize();
 
-       (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
+       (void) system_mmap_anonymous(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
 #endif
 
+       TRACESUBSYSTEMINITIALIZATION("exceptions_init");
+
        /* check if we get into trouble with our hardware-exceptions */
 
        if (OFFSET(java_bytearray_t, data) <= EXCEPTION_HARDWARE_LARGEST)
                vm_abort("signal_init: array-data offset is less or equal the maximum hardware-exception displacement: %d <= %d", OFFSET(java_bytearray_t, data), EXCEPTION_HARDWARE_LARGEST);
+}
 
-       /* java/lang/Throwable */
 
-       if (!(class_java_lang_Throwable =
-                 load_class_bootstrap(utf_java_lang_Throwable)) ||
-               !link_class(class_java_lang_Throwable))
-               return false;
+/* exceptions_get_exception ****************************************************
 
-       /* java/lang/Error */
+   Returns the current exception pointer of the current thread.
 
-       if (!(class_java_lang_Error = load_class_bootstrap(utf_java_lang_Error)) ||
-               !link_class(class_java_lang_Error))
-               return false;
+*******************************************************************************/
 
-#if defined(ENABLE_JAVASE)
-       /* java/lang/LinkageError */
+java_handle_t *exceptions_get_exception(void)
+{
+       java_object_t *o;
+       java_handle_t *e;
+#if defined(ENABLE_THREADS)
+       threadobject  *t;
 
-       if (!(class_java_lang_LinkageError =
-                 load_class_bootstrap(utf_java_lang_LinkageError)) ||
-               !link_class(class_java_lang_LinkageError))
-               return false;
+       t = THREADOBJECT;
 #endif
 
-       /* java/lang/NoClassDefFoundError */
+       /* Get the exception. */
 
-       if (!(class_java_lang_NoClassDefFoundError =
-                 load_class_bootstrap(utf_java_lang_NoClassDefFoundError)) ||
-               !link_class(class_java_lang_NoClassDefFoundError))
-               return false;
+       LLNI_CRITICAL_START;
 
-       /* java/lang/OutOfMemoryError */
+#if defined(ENABLE_THREADS)
+       o = t->_exceptionptr;
+#else
+       o = _no_threads_exceptionptr;
+#endif
 
-       if (!(class_java_lang_OutOfMemoryError =
-                 load_class_bootstrap(utf_java_lang_OutOfMemoryError)) ||
-               !link_class(class_java_lang_OutOfMemoryError))
-               return false;
+       e = LLNI_WRAP(o);
 
-       /* java/lang/VirtualMachineError */
+       LLNI_CRITICAL_END;
 
-       if (!(class_java_lang_VirtualMachineError =
-                 load_class_bootstrap(utf_java_lang_VirtualMachineError)) ||
-               !link_class(class_java_lang_VirtualMachineError))
-               return false;
+       /* Return the exception. */
 
+       return e;
+}
 
-       /* java/lang/Exception */
 
-       if (!(class_java_lang_Exception =
-                 load_class_bootstrap(utf_java_lang_Exception)) ||
-               !link_class(class_java_lang_Exception))
-               return false;
+/* exceptions_set_exception ****************************************************
 
-       /* java/lang/ClassCastException */
+   Sets the exception pointer of the current thread.
 
-       if (!(class_java_lang_ClassCastException =
-                 load_class_bootstrap(utf_java_lang_ClassCastException)) ||
-               !link_class(class_java_lang_ClassCastException))
-               return false;
+*******************************************************************************/
 
-       /* java/lang/ClassNotFoundException */
+void exceptions_set_exception(java_handle_t *e)
+{
+       threadobject  *t;
+       java_object_t *o;
 
-       if (!(class_java_lang_ClassNotFoundException =
-                 load_class_bootstrap(utf_java_lang_ClassNotFoundException)) ||
-               !link_class(class_java_lang_ClassNotFoundException))
-               return false;
+#if defined(ENABLE_THREADS)
+       t = THREADOBJECT;
+#else
+       t = NULL;
+#endif
 
-       /* java/lang/NullPointerException */
+       /* Set the exception. */
 
-       if (!(class_java_lang_NullPointerException =
-                 load_class_bootstrap(utf_java_lang_NullPointerException)) ||
-               !link_class(class_java_lang_NullPointerException))
-               return false;
+       LLNI_CRITICAL_START;
 
+       o = LLNI_UNWRAP(e);
 
-#if defined(WITH_CLASSPATH_GNU)
-       /* java/lang/VMThrowable */
+#if !defined(NDEBUG)
+       if (opt_DebugExceptions) {
+               printf("[exceptions_set_exception  : t=%p, o=%p, class=",
+                          (void *) t, (void *) o);
+               class_print(o->vftbl->class);
+               printf("]\n");
+       }
+#endif
 
-       if (!(class_java_lang_VMThrowable =
-                 load_class_bootstrap(utf_java_lang_VMThrowable)) ||
-               !link_class(class_java_lang_VMThrowable))
-               return false;
+#if defined(ENABLE_THREADS)
+       t->_exceptionptr = o;
+#else
+       _no_threads_exceptionptr = o;
 #endif
 
-       return true;
+       LLNI_CRITICAL_END;
 }
 
 
-/* exceptions_get_exception ****************************************************
+/* exceptions_clear_exception **************************************************
 
-   Returns the current exception pointer of the current thread.
+   Clears the current exception pointer of the current thread.
 
 *******************************************************************************/
 
-java_handle_t *exceptions_get_exception(void)
+void exceptions_clear_exception(void)
 {
-       /* return the exception */
+       threadobject *t;
+
+#if defined(ENABLE_THREADS)
+       t = THREADOBJECT;
+#else
+       t = NULL;
+#endif
+
+       /* Set the exception. */
+
+#if !defined(NDEBUG)
+       if (opt_DebugExceptions) {
+               printf("[exceptions_clear_exception: t=%p]\n", (void *) t);
+       }
+#endif
 
-       return *exceptionptr;
+#if defined(ENABLE_THREADS)
+       t->_exceptionptr = NULL;
+#else
+       _no_threads_exceptionptr = NULL;
+#endif
 }
 
 
-/* exceptions_set_exception ****************************************************
+/* exceptions_get_and_clear_exception ******************************************
 
-   Sets the exception pointer of the current thread.
+   Gets the exception pointer of the current thread and clears it.
+   This function may return NULL.
 
 *******************************************************************************/
 
-void exceptions_set_exception(java_handle_t *o)
+java_handle_t *exceptions_get_and_clear_exception(void)
 {
-       /* set the exception */
+       java_handle_t *o;
+
+       /* Get the exception... */
+
+       o = exceptions_get_exception();
+
+       /* ...and clear the exception if it is set. */
+
+       if (o != NULL)
+               exceptions_clear_exception();
+
+       /* return the exception */
 
-       *exceptionptr = o;
+       return o;
 }
 
 
-/* exceptions_clear_exception **************************************************
+/* exceptions_abort ************************************************************
 
-   Clears the current exception pointer of the current thread.
+   Prints exception to be thrown and aborts.
+
+   IN:
+      classname....class name
+      message......exception message
 
 *******************************************************************************/
 
-void exceptions_clear_exception(void)
+static void exceptions_abort(utf *classname, utf *message)
 {
-       exceptions_set_exception(NULL);
+       log_println("exception thrown while VM is initializing: ");
+
+       log_start();
+       utf_display_printable_ascii_classname(classname);
+
+       if (message != NULL) {
+               log_print(": ");
+               utf_display_printable_ascii_classname(message);
+       }
+
+       log_finish();
+
+       vm_abort("Aborting...");
 }
 
 
-/* exceptions_get_and_clear_exception ******************************************
+/* exceptions_new_class_utf ****************************************************
 
-   Gets the exception pointer of the current thread and clears it.
-   This function may return NULL.
+   Creates an exception object with the given class and initalizes it
+   with the given utf message.
+
+   IN:
+      c ......... exception class
+         message ... the message as an utf *
+
+   RETURN VALUE:
+     an exception pointer (in any case -- either it is the newly
+     created exception, or an exception thrown while trying to create
+     it).
 
 *******************************************************************************/
 
-java_handle_t *exceptions_get_and_clear_exception(void)
+static java_handle_t *exceptions_new_class_utf(classinfo *c, utf *message)
 {
+       java_handle_t *s;
        java_handle_t *o;
 
-       /* get the exception */
+       if (vm_initializing)
+               exceptions_abort(c->name, message);
 
-       o = exceptions_get_exception();
+       s = javastring_new(message);
 
-       /* and clear the exception */
+       if (s == NULL)
+               return exceptions_get_exception();
 
-       exceptions_clear_exception();
+       o = native_new_and_init_string(c, s);
 
-       /* return the exception */
+       if (o == NULL)
+               return exceptions_get_exception();
 
        return o;
 }
 
 
-/* exceptions_new_class ********************************************************
+/* exceptions_new_utf **********************************************************
 
-   Creates an exception object from the given class and initalizes it.
+   Creates an exception object with the given name and initalizes it.
 
    IN:
-      class....class pointer
+      classname....class name in UTF-8
 
 *******************************************************************************/
 
-static java_handle_t *exceptions_new_class(classinfo *c)
+static java_handle_t *exceptions_new_utf(utf *classname)
 {
+       classinfo     *c;
        java_handle_t *o;
 
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
+
+       c = load_class_bootstrap(classname);
+
+       if (c == NULL)
+               return exceptions_get_exception();
+
        o = native_new_and_init(c);
 
        if (o == NULL)
@@ -283,49 +346,94 @@ static java_handle_t *exceptions_new_class(classinfo *c)
 }
 
 
-/* exceptions_new_utf **********************************************************
+/* exceptions_new_utf_javastring ***********************************************
 
-   Creates an exception object with the given name and initalizes it.
+   Creates an exception object with the given name and initalizes it
+   with the given java/lang/String message.
 
    IN:
       classname....class name in UTF-8
+         message......the message as a java.lang.String
+
+   RETURN VALUE:
+      an exception pointer (in any case -- either it is the newly created
+         exception, or an exception thrown while trying to create it).
 
 *******************************************************************************/
 
-static java_handle_t *exceptions_new_utf(utf *classname)
+static java_handle_t *exceptions_new_utf_javastring(utf *classname,
+                                                                                                       java_handle_t *message)
 {
-       classinfo     *c;
        java_handle_t *o;
+       classinfo     *c;
+   
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
 
        c = load_class_bootstrap(classname);
 
        if (c == NULL)
                return exceptions_get_exception();
 
-       o = exceptions_new_class(c);
+       o = native_new_and_init_string(c, message);
+
+       if (o == NULL)
+               return exceptions_get_exception();
 
        return o;
 }
 
 
-/* exceptions_throw_class ******************************************************
+/* exceptions_new_utf_utf ******************************************************
 
-   Creates an exception object from the given class, initalizes and
-   throws it.
+   Creates an exception object with the given name and initalizes it
+   with the given utf message.
 
    IN:
-      class....class pointer
+      classname....class name in UTF-8
+         message......the message as an utf *
+
+   RETURN VALUE:
+      an exception pointer (in any case -- either it is the newly created
+         exception, or an exception thrown while trying to create it).
 
 *******************************************************************************/
 
-static void exceptions_throw_class(classinfo *c)
+static java_handle_t *exceptions_new_utf_utf(utf *classname, utf *message)
 {
+       classinfo     *c;
        java_handle_t *o;
 
-       o = exceptions_new_class(c);
+       if (vm_initializing)
+               exceptions_abort(classname, message);
 
-       if (o == NULL)
-               return;
+       c = load_class_bootstrap(classname);
+
+       if (c == NULL)
+               return exceptions_get_exception();
+
+       o = exceptions_new_class_utf(c, message);
+
+       return o;
+}
+
+
+/* exceptions_throw_class_utf **************************************************
+
+   Creates an exception object with the given class, initalizes and
+   throws it with the given utf message.
+
+   IN:
+      c ......... exception class
+         message ... the message as an utf *
+
+*******************************************************************************/
+
+static void exceptions_throw_class_utf(classinfo *c, utf *message)
+{
+       java_handle_t *o;
+
+       o = exceptions_new_class_utf(c, message);
 
        exceptions_set_exception(o);
 }
@@ -343,14 +451,14 @@ static void exceptions_throw_class(classinfo *c)
 
 static void exceptions_throw_utf(utf *classname)
 {
-       classinfo *c;
+       java_handle_t *o;
 
-       c = load_class_bootstrap(classname);
+       o = exceptions_new_utf(classname);
 
-       if (c == NULL)
+       if (o == NULL)
                return;
 
-       exceptions_throw_class(c);
+       exceptions_set_exception(o);
 }
 
 
@@ -373,6 +481,9 @@ static void exceptions_throw_utf_throwable(utf *classname,
        methodinfo          *m;
        java_lang_Throwable *object;
 
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
+
        object = (java_lang_Throwable *) cause;
 
        c = load_class_bootstrap(classname);
@@ -422,6 +533,9 @@ static void exceptions_throw_utf_exception(utf *classname,
        java_handle_t *o;
        methodinfo    *m;
 
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
+
        c = load_class_bootstrap(classname);
 
        if (c == NULL)
@@ -470,6 +584,9 @@ static void exceptions_throw_utf_cause(utf *classname, java_handle_t *cause)
        java_lang_String    *s;
        java_lang_Throwable *object;
 
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
+
        object = (java_lang_Throwable *) cause;
 
        c = load_class_bootstrap(classname);
@@ -516,122 +633,6 @@ static void exceptions_throw_utf_cause(utf *classname, java_handle_t *cause)
 }
 
 
-/* exceptions_new_utf_javastring ***********************************************
-
-   Creates an exception object with the given name and initalizes it
-   with the given java/lang/String message.
-
-   IN:
-      classname....class name in UTF-8
-         message......the message as a java.lang.String
-
-   RETURN VALUE:
-      an exception pointer (in any case -- either it is the newly created
-         exception, or an exception thrown while trying to create it).
-
-*******************************************************************************/
-
-static java_handle_t *exceptions_new_utf_javastring(utf *classname,
-                                                                                                       java_handle_t *message)
-{
-       java_handle_t *o;
-       classinfo     *c;
-   
-       c = load_class_bootstrap(classname);
-
-       if (c == NULL)
-               return exceptions_get_exception();
-
-       o = native_new_and_init_string(c, message);
-
-       if (o == NULL)
-               return exceptions_get_exception();
-
-       return o;
-}
-
-
-/* exceptions_new_class_utf ****************************************************
-
-   Creates an exception object of the given class and initalizes it.
-
-   IN:
-      c..........class pointer
-      message....the message as UTF-8 string
-
-*******************************************************************************/
-
-static java_handle_t *exceptions_new_class_utf(classinfo *c, utf *message)
-{
-       java_handle_t *o;
-       java_handle_t *s;
-
-       s = javastring_new(message);
-
-       if (s == NULL)
-               return exceptions_get_exception();
-
-       o = native_new_and_init_string(c, s);
-
-       if (o == NULL)
-               return exceptions_get_exception();
-
-       return o;
-}
-
-
-/* exceptions_new_utf_utf ******************************************************
-
-   Creates an exception object with the given name and initalizes it
-   with the given utf message.
-
-   IN:
-      classname....class name in UTF-8
-         message......the message as an utf *
-
-   RETURN VALUE:
-      an exception pointer (in any case -- either it is the newly created
-         exception, or an exception thrown while trying to create it).
-
-*******************************************************************************/
-
-static java_handle_t *exceptions_new_utf_utf(utf *classname, utf *message)
-{
-       classinfo     *c;
-       java_handle_t *o;
-
-       c = load_class_bootstrap(classname);
-
-       if (c == NULL)
-               return exceptions_get_exception();
-
-       o = exceptions_new_class_utf(c, message);
-
-       return o;
-}
-
-
-/* exceptions_throw_class_utf **************************************************
-
-   Creates an exception object of the given class, initalizes and
-   throws it with the given utf message.
-
-   IN:
-      c..........class pointer
-         message....the message as an UTF-8
-
-*******************************************************************************/
-
-static void exceptions_throw_class_utf(classinfo *c, utf *message)
-{
-       java_handle_t *o;
-
-       o = exceptions_new_class_utf(c, message);
-
-       exceptions_set_exception(o);
-}
-
-
 /* exceptions_throw_utf_utf ****************************************************
 
    Creates an exception object with the given name, initalizes and
@@ -680,7 +681,7 @@ static java_handle_t *exceptions_new_error(utf *message)
 {
        java_handle_t *o;
 
-       o = exceptions_new_class_utf(class_java_lang_Error, message);
+       o = exceptions_new_utf_utf(utf_java_lang_Error, message);
 
        return o;
 }
@@ -696,12 +697,13 @@ static java_handle_t *exceptions_new_error(utf *message)
 
 java_object_t *exceptions_asm_new_abstractmethoderror(u1 *sp, u1 *ra)
 {
-       stackframeinfo  sfi;
-       java_handle_t  *e;
+       stackframeinfo_t  sfi;
+       java_handle_t    *e;
+       java_object_t    *o;
 
-       /* create the stackframeinfo (XPC is equal to RA) */
+       /* Fill and add a stackframeinfo (XPC is equal to RA). */
 
-       stacktrace_create_extern_stackframeinfo(&sfi, NULL, sp, ra, ra);
+       stacktrace_stackframeinfo_add(&sfi, NULL, sp, ra, ra);
 
        /* create the exception */
 
@@ -711,11 +713,16 @@ java_object_t *exceptions_asm_new_abstractmethoderror(u1 *sp, u1 *ra)
        e = exceptions_new_error(utf_java_lang_AbstractMethodError);
 #endif
 
-       /* remove the stackframeinfo */
+       /* Remove the stackframeinfo. */
 
-       stacktrace_remove_stackframeinfo(&sfi);
+       stacktrace_stackframeinfo_remove(&sfi);
 
-       return e;
+       /* unwrap the exception */
+       /* ATTENTION: do the this _after_ the stackframeinfo was removed */
+
+       o = LLNI_UNWRAP(e);
+
+       return o;
 }
 
 
@@ -837,9 +844,7 @@ void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
 *******************************************************************************/
 
 void exceptions_throw_classnotfoundexception(utf *name)
-{
-       /* we use class here, as this one is rather frequent */
-
+{      
        exceptions_throw_class_utf(class_java_lang_ClassNotFoundException, name);
 }
 
@@ -855,10 +860,7 @@ void exceptions_throw_classnotfoundexception(utf *name)
 
 void exceptions_throw_noclassdeffounderror(utf *name)
 {
-       if (vm_initializing)
-               vm_abort("java.lang.NoClassDefFoundError: %s", name->text);
-
-       exceptions_throw_class_utf(class_java_lang_NoClassDefFoundError, name);
+       exceptions_throw_utf_utf(utf_java_lang_NoClassDefFoundError, name);
 }
 
 
@@ -1034,39 +1036,35 @@ void exceptions_throw_internalerror(const char *message, ...)
 
 void exceptions_throw_linkageerror(const char *message, classinfo *c)
 {
-       java_handle_t *o;
-       char          *msg;
-       s4             msglen;
+       utf  *u;
+       char *msg;
+       int   len;
 
        /* calculate exception message length */
 
-       msglen = strlen(message) + 1;
+       len = strlen(message) + 1;
 
        if (c != NULL)
-               msglen += utf_bytes(c->name);
+               len += utf_bytes(c->name);
                
        /* allocate memory */
 
-       msg = MNEW(char, msglen);
+       msg = MNEW(char, len);
 
        /* generate message */
 
-       strcpy(msg,message);
+       strcpy(msg, message);
 
        if (c != NULL)
                utf_cat_classname(msg, c->name);
 
-       o = native_new_and_init_string(class_java_lang_LinkageError,
-                                                                  javastring_new_from_utf_string(msg));
+       u = utf_new_char(msg);
 
        /* free memory */
 
-       MFREE(msg, char, msglen);
-
-       if (o == NULL)
-               return;
+       MFREE(msg, char, len);
 
-       exceptions_set_exception(o);
+       exceptions_throw_utf_utf(utf_java_lang_LinkageError, u);
 }
 
 
@@ -1154,7 +1152,7 @@ void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
 #if defined(ENABLE_JAVASE)
        exceptions_throw_utf_utf(utf_java_lang_NoSuchMethodError, u);
 #else
-       exceptions_throw_class_utf(class_java_lang_Error, u);
+       exceptions_throw_utf_utf(utf_java_lang_Error, u);
 #endif
 }
 
@@ -1167,7 +1165,7 @@ void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
 
 void exceptions_throw_outofmemoryerror(void)
 {
-       exceptions_throw_class(class_java_lang_OutOfMemoryError);
+       exceptions_throw_utf(utf_java_lang_OutOfMemoryError);
 }
 
 
@@ -1186,7 +1184,7 @@ void exceptions_throw_unsatisfiedlinkerror(utf *name)
 #if defined(ENABLE_JAVASE)
        exceptions_throw_utf_utf(utf_java_lang_UnsatisfiedLinkError, name);
 #else
-       exceptions_throw_class_utf(class_java_lang_Error, name);
+       exceptions_throw_utf_utf(utf_java_lang_Error, name);
 #endif
 }
 
@@ -1457,7 +1455,6 @@ void exceptions_throw_arrayindexoutofboundsexception(void)
 void exceptions_throw_arraystoreexception(void)
 {
        exceptions_throw_utf(utf_java_lang_ArrayStoreException);
-/*     e = native_new_and_init(class_java_lang_ArrayStoreException); */
 }
 
 
@@ -1470,11 +1467,14 @@ void exceptions_throw_arraystoreexception(void)
 java_handle_t *exceptions_new_classcastexception(java_handle_t *o)
 {
        java_handle_t *e;
+       classinfo     *c;
        utf           *classname;
 
-       classname = o->vftbl->class->name;
+       LLNI_class_get(o, c);
 
-       e = exceptions_new_class_utf(class_java_lang_ClassCastException, classname);
+       classname = c->name;
+
+       e = exceptions_new_utf_utf(utf_java_lang_ClassCastException, classname);
 
        return e;
 }
@@ -1595,7 +1595,7 @@ java_handle_t *exceptions_new_nullpointerexception(void)
 {
        java_handle_t *o;
 
-       o = exceptions_new_class(class_java_lang_NullPointerException);
+       o = exceptions_new_utf(utf_java_lang_NullPointerException);
 
        return o;
 }
@@ -1610,7 +1610,7 @@ java_handle_t *exceptions_new_nullpointerexception(void)
 
 void exceptions_throw_nullpointerexception(void)
 {
-       exceptions_throw_class(class_java_lang_NullPointerException);
+       exceptions_throw_utf(utf_java_lang_NullPointerException);
 }
 
 
@@ -1640,51 +1640,6 @@ void exceptions_throw_stringindexoutofboundsexception(void)
 }
 
 
-/* exceptions_classnotfoundexception_to_noclassdeffounderror *******************
-
-   Check the exception for a ClassNotFoundException. If it is one,
-   convert it to a NoClassDefFoundError.
-
-*******************************************************************************/
-
-void exceptions_classnotfoundexception_to_noclassdeffounderror(void)
-{
-       java_handle_t       *o;
-       java_handle_t       *cause;
-       java_lang_Throwable *object;
-       java_lang_String    *s;
-
-       /* get the cause */
-
-       cause = exceptions_get_exception();
-
-       /* convert ClassNotFoundException's to NoClassDefFoundError's */
-
-       if (builtin_instanceof(cause, class_java_lang_ClassNotFoundException)) {
-               /* clear exception, because we are calling jit code again */
-
-               exceptions_clear_exception();
-
-               /* create new error */
-
-               object = (java_lang_Throwable *) cause;
-               LLNI_field_get_ref(object, detailMessage, s);
-
-               o = exceptions_new_utf_javastring(utf_java_lang_NoClassDefFoundError,
-                                                                                 (java_handle_t *) s);
-
-               /* we had an exception while creating the error */
-
-               if (exceptions_get_exception())
-                       return;
-
-               /* set new exception */
-
-               exceptions_set_exception(o);
-       }
-}
-
-
 /* exceptions_fillinstacktrace *************************************************
 
    Calls the fillInStackTrace-method of the currently thrown
@@ -1695,6 +1650,7 @@ void exceptions_classnotfoundexception_to_noclassdeffounderror(void)
 java_handle_t *exceptions_fillinstacktrace(void)
 {
        java_handle_t *o;
+       classinfo     *c;
        methodinfo    *m;
 
        /* get exception */
@@ -1705,12 +1661,14 @@ java_handle_t *exceptions_fillinstacktrace(void)
 
        /* resolve methodinfo pointer from exception object */
 
+       LLNI_class_get(o, c);
+
 #if defined(ENABLE_JAVASE)
-       m = class_resolvemethod(o->vftbl->class,
+       m = class_resolvemethod(c,
                                                        utf_fillInStackTrace,
                                                        utf_void__java_lang_Throwable);
 #elif defined(ENABLE_JAVAME_CLDC1_1)
-       m = class_resolvemethod(o->vftbl->class,
+       m = class_resolvemethod(c,
                                                        utf_fillInStackTrace,
                                                        utf_void__void);
 #else
@@ -1746,69 +1704,78 @@ java_handle_t *exceptions_fillinstacktrace(void)
 *******************************************************************************/
 
 #if defined(ENABLE_JIT)
-u1 *exceptions_handle_exception(java_object_t *xptr, u1 *xpc, u1 *pv, u1 *sp)
+void *exceptions_handle_exception(java_object_t *xptro, void *xpc, void *pv, void *sp)
 {
-       methodinfo            *m;
-       codeinfo              *code;
-       s4                     issync;
-       dseg_exception_entry  *ex;
-       s4                     exceptiontablelength;
-       s4                     i;
-       classref_or_classinfo  cr;
-       classinfo             *c;
+       stackframeinfo_t        sfi;
+       java_handle_t          *xptr;
+       methodinfo             *m;
+       codeinfo               *code;
+       exceptiontable_t       *et;
+       exceptiontable_entry_t *ete;
+       s4                      i;
+       classref_or_classinfo   cr;
+       classinfo              *c;
 #if defined(ENABLE_THREADS)
-       java_object_t         *o;
+       java_object_t          *o;
 #endif
+       void                   *result;
 
 #ifdef __S390__
        /* Addresses are 31 bit integers */
-#      define ADDR_MASK(x) (u1 *)((u4)(x) & 0x7FFFFFFF)
+#      define ADDR_MASK(x) (void *) ((uintptr_t) (x) & 0x7FFFFFFF)
 #else
 #      define ADDR_MASK(x) (x)
 #endif
 
-       xpc = ADDR_MASK(xpc);
+       xptr = LLNI_WRAP(xptro);
+       xpc  = ADDR_MASK(xpc);
+
+       /* Fill and add a stackframeinfo (XPC is equal to RA). */
 
-       /* get info from the method header */
+       stacktrace_stackframeinfo_add(&sfi, pv, sp, xpc, xpc);
 
-       code                 = *((codeinfo **)            (pv + CodeinfoPointer));
-       issync               = *((s4 *)                   (pv + IsSync));
-       ex                   =   (dseg_exception_entry *) (pv + ExTableStart);
-       exceptiontablelength = *((s4 *)                   (pv + ExTableSize));
+       result = NULL;
+
+       /* Get the codeinfo for the current method. */
+
+       code = code_get_codeinfo_for_pv(pv);
 
        /* Get the methodinfo pointer from the codeinfo pointer. For
-          asm_vm_call_method the codeinfo pointer is NULL. */
+          asm_vm_call_method the codeinfo pointer is NULL and we simply
+          can return the proper exception handler. */
+
+       if (code == NULL) {
+               result = (void *) (uintptr_t) &asm_vm_call_method_exception_handler;
+               goto exceptions_handle_exception_return;
+       }
 
-       m = (code == NULL) ? NULL : code->m;
+       m = code->m;
 
 #if !defined(NDEBUG)
        /* print exception trace */
 
-       if (opt_verbose || opt_verbosecall || opt_TraceExceptions)
-               builtin_trace_exception(xptr, m, xpc, 1);
-#endif
+       if (opt_TraceExceptions)
+               trace_exception(LLNI_DIRECT(xptr), m, xpc);
 
-#if defined(ENABLE_VMLOG)
+# if defined(ENABLE_VMLOG)
        vmlog_cacao_throw(xptr);
+# endif
 #endif
 
-       for (i = 0; i < exceptiontablelength; i++) {
-               /* ATTENTION: keep this here, as we need to decrement the
-           pointer before the loop executes! */
+       /* Get the exception table. */
 
-               ex--;
+       et = code->exceptiontable;
 
-               /* If the start and end PC is NULL, this means we have the
-                  special case of asm_vm_call_method.  So, just return the
-                  proper exception handler. */
+       if (et != NULL) {
+       /* Iterate over all exception table entries. */
 
-               if ((ex->startpc == NULL) && (ex->endpc == NULL))
-                       return (u1 *) (ptrint) &asm_vm_call_method_exception_handler;
+       ete = et->entries;
 
+       for (i = 0; i < et->length; i++, ete++) {
                /* is the xpc is the current catch range */
 
-               if ((ADDR_MASK(ex->startpc) <= xpc) && (xpc < ADDR_MASK(ex->endpc))) {
-                       cr = ex->catchtype;
+               if ((ADDR_MASK(ete->startpc) <= xpc) && (xpc < ADDR_MASK(ete->endpc))) {
+                       cr = ete->catchtype;
 
                        /* NULL catches everything */
 
@@ -1816,44 +1783,47 @@ u1 *exceptions_handle_exception(java_object_t *xptr, u1 *xpc, u1 *pv, u1 *sp)
 #if !defined(NDEBUG)
                                /* Print stacktrace of exception when caught. */
 
-#if defined(ENABLE_VMLOG)
+# if defined(ENABLE_VMLOG)
                                vmlog_cacao_catch(xptr);
-#endif
+# endif
 
                                if (opt_TraceExceptions) {
                                        exceptions_print_exception(xptr);
-                                       stacktrace_print_trace(xptr);
+                                       stacktrace_print_exception(xptr);
                                }
 #endif
 
-                               return ex->handlerpc;
+                               result = ete->handlerpc;
+                               goto exceptions_handle_exception_return;
                        }
 
                        /* resolve or load/link the exception class */
 
                        if (IS_CLASSREF(cr)) {
                                /* The exception class reference is unresolved. */
-                               /* We have to do _eager_ resolving here. While the class of */
-                               /* the exception object is guaranteed to be loaded, it may  */
-                               /* well have been loaded by a different loader than the     */
-                               /* defining loader of m's class, which is the one we must   */
-                               /* use to resolve the catch class. Thus lazy resolving      */
-                               /* might fail, even if the result of the resolution would   */
-                               /* be an already loaded class.                              */
+                               /* We have to do _eager_ resolving here. While the
+                                  class of the exception object is guaranteed to be
+                                  loaded, it may well have been loaded by a different
+                                  loader than the defining loader of m's class, which
+                                  is the one we must use to resolve the catch
+                                  class. Thus lazy resolving might fail, even if the
+                                  result of the resolution would be an already loaded
+                                  class. */
 
                                c = resolve_classref_eager(cr.ref);
 
                                if (c == NULL) {
                                        /* Exception resolving the exception class, argh! */
-                                       return NULL;
+                                       goto exceptions_handle_exception_return;
                                }
 
-                               /* Ok, we resolved it. Enter it in the table, so we don't */
-                               /* have to do this again.                                 */
-                               /* XXX this write should be atomic. Is it?                */
+                               /* Ok, we resolved it. Enter it in the table, so we
+                                  don't have to do this again. */
+                               /* XXX this write should be atomic. Is it? */
 
-                               ex->catchtype.cls = c;
-                       } else {
+                               ete->catchtype.cls = c;
+                       }
+                       else {
                                c = cr.cls;
 
                                /* XXX I don't think this case can ever happen. -Edwin */
@@ -1861,14 +1831,14 @@ u1 *exceptions_handle_exception(java_object_t *xptr, u1 *xpc, u1 *pv, u1 *sp)
                                        /* use the methods' classloader */
                                        if (!load_class_from_classloader(c->name,
                                                                                                         m->class->classloader))
-                                               return NULL;
+                                               goto exceptions_handle_exception_return;
 
-                               /* XXX I think, if it is not linked, we can be sure that     */
-                               /* the exception object is no (indirect) instance of it, no? */
-                               /* -Edwin                                                    */
+                               /* XXX I think, if it is not linked, we can be sure
+                                  that the exception object is no (indirect) instance
+                                  of it, no?  -Edwin  */
                                if (!(c->state & CLASS_LINKED))
                                        if (!link_class(c))
-                                               return NULL;
+                                               goto exceptions_handle_exception_return;
                        }
 
                        /* is the thrown exception an instance of the catch class? */
@@ -1877,47 +1847,71 @@ u1 *exceptions_handle_exception(java_object_t *xptr, u1 *xpc, u1 *pv, u1 *sp)
 #if !defined(NDEBUG)
                                /* Print stacktrace of exception when caught. */
 
-#if defined(ENABLE_VMLOG)
+# if defined(ENABLE_VMLOG)
                                vmlog_cacao_catch(xptr);
-#endif
+# endif
 
                                if (opt_TraceExceptions) {
                                        exceptions_print_exception(xptr);
-                                       stacktrace_print_trace(xptr);
+                                       stacktrace_print_exception(xptr);
                                }
 #endif
 
-                               return ex->handlerpc;
+                               result = ete->handlerpc;
+                               goto exceptions_handle_exception_return;
                        }
                }
        }
+       }
 
 #if defined(ENABLE_THREADS)
-       /* is this method synchronized? */
+       /* Is this method realization synchronized? */
 
-       if (issync) {
-               /* get synchronization object */
+       if (code_is_synchronized(code)) {
+               /* Get synchronization object. */
 
-# if (defined(__MIPS__) && (SIZEOF_VOID_P == 4)) || defined(__I386__) || defined(__S390__) || defined(__POWERPC__)
-               /* XXX change this if we ever want to use 4-byte stackslots */
-               o = *((java_object_t **) (sp + issync - 8));
-# else
-               o = *((java_object_t **) (sp + issync - SIZEOF_VOID_P));
-# endif
+               o = *((java_object_t **) (((uintptr_t) sp) + code->synchronizedoffset));
 
                assert(o != NULL);
 
-               lock_monitor_exit(o);
+               lock_monitor_exit(LLNI_QUICKWRAP(o));
        }
 #endif
 
        /* none of the exceptions catch this one */
 
-#if defined(ENABLE_VMLOG)
+#if !defined(NDEBUG)
+# if defined(ENABLE_VMLOG)
        vmlog_cacao_unwnd_method(m);
-#endif
+# endif
+
+# if defined(ENABLE_DEBUG_FILTER)
+       if (show_filters_test_verbosecall_exit(m)) {
+# endif
+
+       /* outdent the log message */
+
+       if (opt_verbosecall) {
+               if (TRACEJAVACALLINDENT)
+                       TRACEJAVACALLINDENT--;
+               else
+                       log_text("exceptions_handle_exception: WARNING: unmatched unindent");
+       }
+
+# if defined(ENABLE_DEBUG_FILTER)
+       }
+# endif
+#endif /* !defined(NDEBUG) */
+
+       result = NULL;
+
+exceptions_handle_exception_return:
 
-       return NULL;
+       /* Remove the stackframeinfo. */
+
+       stacktrace_stackframeinfo_remove(&sfi);
+
+       return result;
 }
 #endif /* defined(ENABLE_JIT) */
 
@@ -2019,63 +2013,100 @@ void exceptions_print_current_exception(void)
 
 void exceptions_print_stacktrace(void)
 {
-       java_handle_t *oxptr;
-       java_handle_t *xptr;
-       classinfo     *c;
-       methodinfo    *m;
+       java_handle_t    *e;
+       java_handle_t    *ne;
+       classinfo        *c;
+       methodinfo       *m;
 
-       /* get original exception */
+#if defined(ENABLE_THREADS)
+       threadobject     *t;
+       java_lang_Thread *to;
+#endif
 
-       oxptr = exceptions_get_and_clear_exception();
+       /* Get and clear exception because we are calling Java code
+          again. */
 
-       if (oxptr == NULL)
-               vm_abort("exceptions_print_stacktrace: no exception thrown");
+       e = exceptions_get_and_clear_exception();
 
-       /* clear exception, because we are calling jit code again */
+       if (e == NULL)
+               return;
 
-       c = oxptr->vftbl->class;
+#if 0
+       /* FIXME Enable me. */
+       if (builtin_instanceof(e, class_java_lang_ThreadDeath)) {
+               /* Don't print anything if we are being killed. */
+       }
+       else
+#endif
+       {
+               /* Get the exception class. */
 
-       /* find the printStackTrace() method */
+               LLNI_class_get(e, c);
 
-       m = class_resolveclassmethod(c,
-                                                                utf_printStackTrace,
-                                                                utf_void__void,
-                                                                class_java_lang_Object,
-                                                                false);
+               /* Find the printStackTrace() method. */
 
-       if (m == NULL)
-               vm_abort("exceptions_print_stacktrace: printStackTrace()V not found");
+               m = class_resolveclassmethod(c,
+                                                                        utf_printStackTrace,
+                                                                        utf_void__void,
+                                                                        class_java_lang_Object,
+                                                                        false);
 
-       /* print compatibility message */
+               if (m == NULL)
+                       vm_abort("exceptions_print_stacktrace: printStackTrace()V not found");
 
-       fprintf(stderr, "Exception in thread \"main\" ");
+               /* Print message. */
 
-       /* print the stacktrace */
+               fprintf(stderr, "Exception ");
 
-       (void) vm_call_method(m, oxptr);
+#if defined(ENABLE_THREADS)
+               /* Print thread name.  We get the thread here explicitly as we
+                  need it afterwards. */
+
+               t  = thread_get_current();
+               to = (java_lang_Thread *) thread_get_object(t);
+
+               if (to != NULL) {
+                       fprintf(stderr, "in thread \"");
+                       thread_fprint_name(t, stderr);
+                       fprintf(stderr, "\" ");
+               }
+#endif
 
-       /* This normally means, we are EXTREMLY out of memory or
-          have a serious problem while printStackTrace. But may
-          be another exception, so print it. */
+               /* Print the stacktrace. */
 
-       xptr = exceptions_get_exception();
+               if (builtin_instanceof(e, class_java_lang_Throwable)) {
+                       (void) vm_call_method(m, e);
 
-       if (xptr != NULL) {
-               fprintf(stderr, "Exception while printStackTrace(): ");
+                       /* If this happens we are EXTREMLY out of memory or have a
+                          serious problem while printStackTrace.  But may be
+                          another exception, so print it. */
 
-               /* now print original exception */
+                       ne = exceptions_get_exception();
 
-               exceptions_print_exception(xptr);
-               stacktrace_print_trace(xptr);
+                       if (ne != NULL) {
+                               fprintf(stderr, "Exception while printStackTrace(): ");
 
-               /* now print original exception */
+                               /* Print the current exception. */
 
-               fprintf(stderr, "Original exception was: ");
-               exceptions_print_exception(oxptr);
-               stacktrace_print_trace(oxptr);
-       }
+                               exceptions_print_exception(ne);
+                               stacktrace_print_exception(ne);
 
-       fflush(stderr);
+                               /* Now print the original exception. */
+
+                               fprintf(stderr, "Original exception was: ");
+                               exceptions_print_exception(e);
+                               stacktrace_print_exception(e);
+                       }
+               }
+               else {
+                       fprintf(stderr, ". Uncaught exception of type ");
+                       /* FIXME This prints to stdout. */
+                       class_print(c);
+                       fprintf(stderr, ".");
+               }
+
+               fflush(stderr);
+       }
 }