Merged revisions 7501-7598 via svnmerge from
[cacao.git] / src / vm / vm.c
index dc085294e217a6a515c714d87a44a772d8918cad..5d1cc1e49031606ff24df2ae3298a25e508baf82 100644 (file)
@@ -175,7 +175,6 @@ enum {
 
        OPT_VERBOSE1,
        OPT_NOIEEE,
-       OPT_SOFTNULL,
 
 #if defined(ENABLE_STATISTICS)
        OPT_TIME,
@@ -298,7 +297,6 @@ opt_struct opts[] = {
 #if defined(__ALPHA__)
        { "noieee",            false, OPT_NOIEEE },
 #endif
-       { "softnull",          false, OPT_SOFTNULL },
 #if defined(ENABLE_STATISTICS)
        { "time",              false, OPT_TIME },
        { "stat",              false, OPT_STAT },
@@ -500,7 +498,6 @@ static void XXusage(void)
 #if defined(ENABLE_VERIFIER)
        puts("    -noverify                don't verify classfiles");
 #endif
-       puts("    -softnull                use software nullpointer check");
 #if defined(ENABLE_STATISTICS)
        puts("    -time                    measure the runtime");
        puts("    -stat                    detailed compiler statistics");
@@ -670,7 +667,7 @@ bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
 
        env = NEW(_Jv_JNIEnv);
 
-#if defined(ENABLE_JAVASE)
+#if defined(ENABLE_JNI)
        env->env = &_Jv_JNINativeInterface;
 #endif
 
@@ -682,7 +679,7 @@ bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
 
        vm = NEW(_Jv_JavaVM);
 
-#if defined(ENABLE_JAVASE)
+#if defined(ENABLE_JNI)
        vm->functions = &_Jv_JNIInvokeInterface;
 #endif
 
@@ -696,7 +693,7 @@ bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
        if (!vm_create(_vm_args))
                goto error;
 
-#if defined(ENABLE_JAVASE)
+#if defined(ENABLE_JNI)
        /* setup the local ref table (must be created after vm_create) */
 
        if (!jni_init_localref_table())
@@ -889,7 +886,6 @@ bool vm_create(JavaVMInitArgs *vm_args)
        opt_version       = false;
        opt_exit          = false;
 
-       checknull         = false;
        opt_noieee        = false;
 
        opt_heapmaxsize   = HEAP_MAXSIZE;
@@ -1111,9 +1107,11 @@ bool vm_create(JavaVMInitArgs *vm_args)
                        else if (strcmp("memory", opt_arg) == 0) {
                                opt_verbosememory = true;
 
+# if defined(ENABLE_STATISTICS)
                                /* we also need statistics */
 
                                opt_stat = true;
+# endif
                        }
 #endif
                        break;
@@ -1150,10 +1148,6 @@ bool vm_create(JavaVMInitArgs *vm_args)
                        break;
 #endif
 
-               case OPT_SOFTNULL:
-                       checknull = true;
-                       break;
-
 #if defined(ENABLE_STATISTICS)
                case OPT_TIME:
                        opt_getcompilingtime = true;
@@ -1445,19 +1439,6 @@ bool vm_create(JavaVMInitArgs *vm_args)
        }
 #endif
 
-       /* Now re-set some of the properties that may have changed. This
-          must be done after _all_ environment variables have been
-          processes (e.g. -jar handling). */
-
-       if (!properties_postinit())
-               vm_abort("properties_postinit failed");
-
-       /* Now we have all options handled and we can print the version
-          information. */
-
-       if (opt_version)
-               version(opt_exit);
-
        /* initialize this JVM ****************************************************/
 
        vm_initializing = true;
@@ -1482,44 +1463,62 @@ bool vm_create(JavaVMInitArgs *vm_args)
        }
 #endif
 
-       /* initialize the string hashtable stuff: lock (must be done
-          _after_ threads_preinit) */
+       /* AFTER: threads_preinit */
 
        if (!string_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: string_init failed");
 
-       /* initialize the utf8 hashtable stuff: lock, often used utf8
-          strings (must be done _after_ threads_preinit) */
+       /* AFTER: threads_preinit */
 
        if (!utf8_init())
-               throw_main_exception_exit();
-
-       /* initialize the classcache hashtable stuff: lock, hashtable
-          (must be done _after_ threads_preinit) */
-
-       if (!classcache_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: utf8_init failed");
 
-       /* initialize the loader with bootclasspath (must be done _after_
-          thread_preinit) */
+       /* AFTER: thread_preinit */
 
        if (!suck_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: suck_init failed");
 
        suck_add_from_property("java.endorsed.dirs");
+
+       /* Now we have all options handled and we can print the version
+          information.
+
+          AFTER: suck_add_from_property("java.endorsed.dirs"); */
+
+       if (opt_version)
+               version(opt_exit);
+
+       /* AFTER: utf8_init */
+
        suck_add(_Jv_bootclasspath);
 
+       /* Now re-set some of the properties that may have changed. This
+          must be done after _all_ environment variables have been
+          processes (e.g. -jar handling).
+
+          AFTER: suck_add_from_property, since it may change the
+          _Jv_bootclasspath pointer. */
+
+       if (!properties_postinit())
+               vm_abort("vm_create: properties_postinit failed");
+
+       /* initialize the classcache hashtable stuff: lock, hashtable
+          (must be done _after_ threads_preinit) */
+
+       if (!classcache_init())
+               vm_abort("vm_create: classcache_init failed");
+
        /* initialize the memory subsystem (must be done _after_
           threads_preinit) */
 
        if (!memory_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: memory_init failed");
 
        /* initialize the finalizer stuff (must be done _after_
           threads_preinit) */
 
        if (!finalizer_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: finalizer_init failed");
 
        /* install architecture dependent signal handlers */
 
@@ -1550,81 +1549,85 @@ bool vm_create(JavaVMInitArgs *vm_args)
        classcache_init) */
 
        if (!loader_init())
-               vm_abort("loader_init failed");
+               vm_abort("vm_create: loader_init failed");
 
        if (!linker_init())
-               vm_abort("linker_init failed");
+               vm_abort("vm_create: linker_init failed");
 
        if (!native_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: native_init failed");
 
        if (!exceptions_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: exceptions_init failed");
 
        if (!builtin_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: builtin_init failed");
 
-#if defined(ENABLE_JAVASE)
+#if defined(ENABLE_JNI)
        /* Initialize the JNI subsystem (must be done _before_
           threads_init, as threads_init can call JNI methods
           (e.g. NewGlobalRef). */
 
        if (!jni_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: jni_init failed");
 #endif
 
 #if defined(ENABLE_THREADS)
        if (!threads_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: threads_init failed");
 #endif
 
 #if defined(ENABLE_PROFILING)
        /* initialize profiling */
 
        if (!profile_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: profile_init failed");
 #endif
 
 #if defined(ENABLE_THREADS)
        /* initialize recompilation */
 
        if (!recompile_init())
-               throw_main_exception_exit();
+               vm_abort("vm_create: recompile_init failed");
 
        /* start the signal handler thread */
 
-/*     if (!signal_start_thread()) */
-/*             throw_main_exception_exit(); */
+       if (!signal_start_thread())
+               vm_abort("vm_create: signal_start_thread failed");
 
        /* finally, start the finalizer thread */
 
        if (!finalizer_start_thread())
-               throw_main_exception_exit();
+               vm_abort("vm_create: finalizer_start_thread failed");
 
 # if !defined(NDEBUG)
        /* start the memory profiling thread */
 
        if (opt_verbosememory)
                if (!memory_start_thread())
-                       throw_main_exception_exit();
+                       vm_abort("vm_create: memory_start_thread failed");
 # endif
 
        /* start the recompilation thread (must be done before the
           profiling thread) */
 
        if (!recompile_start_thread())
-               throw_main_exception_exit();
+               vm_abort("vm_create: recompile_start_thread failed");
 
 # if defined(ENABLE_PROFILING)
        /* start the profile sampling thread */
 
 /*     if (opt_prof) */
 /*             if (!profile_start_thread()) */
-/*                     throw_main_exception_exit(); */
+/*                     exceptions_print_stacktrace(); */
 # endif
 #endif
 
 #if defined(ENABLE_JVMTI)
+# if defined(ENABLE_GC_CACAO)
+       /* XXX this will not work with the new indirection cells for classloaders!!! */
+       assert(0);
+# endif
        if (jvmti) {
                /* add agent library to native library hashtable */
                native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
@@ -1684,10 +1687,15 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
 
        status = 0;
 
-       if (opt_jar == true)
+       if (opt_jar == true) {
                /* open jar file with java.util.jar.JarFile */
+
                mainstring = vm_get_mainclass_from_jar(mainstring);
 
+               if (mainstring == NULL)
+                       vm_exit(1);
+       }
+
        /* load the main class */
 
        mainutf = utf_new_char(mainstring);
@@ -1700,11 +1708,15 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
 
        /* error loading class */
 
-       if ((exceptions_get_exception() != NULL) || (mainclass == NULL))
-               throw_main_exception_exit();
+       if ((exceptions_get_exception() != NULL) || (mainclass == NULL)) {
+               exceptions_print_stacktrace(); 
+               vm_exit(1);
+       }
 
-       if (!link_class(mainclass))
-               throw_main_exception_exit();
+       if (!link_class(mainclass)) {
+               exceptions_print_stacktrace();
+               vm_exit(1);
+       }
                        
        /* find the `main' method of the main class */
 
@@ -1714,8 +1726,9 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
                                                                 class_java_lang_Object,
                                                                 false);
 
-       if (*exceptionptr) {
-               throw_main_exception_exit();
+       if (exceptions_get_exception()) {
+               exceptions_print_stacktrace();
+               vm_exit(1);
        }
 
        /* there is no main method or it isn't static */
@@ -1726,7 +1739,8 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
                                                                                   utf_new_char("main"), 
                                                                                   utf_new_char("([Ljava/lang/String;)V"));
 
-               throw_main_exception_exit();
+               exceptions_print_stacktrace();
+               vm_exit(1);
        }
 
        /* build argument array */
@@ -1768,14 +1782,14 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
 
        /* exception occurred? */
 
-       if (*exceptionptr) {
-               throw_main_exception();
+       if (exceptions_get_exception()) {
+               exceptions_print_stacktrace();
                status = 1;
        }
 
        /* unload the JavaVM */
 
-       vm_destroy(vm);
+       (void) vm_destroy(vm);
 
        /* and exit */
 
@@ -1825,8 +1839,10 @@ void vm_exit(s4 status)
        }
 #endif
 
-       if (!link_class(class_java_lang_System))
-               throw_main_exception_exit();
+       if (!link_class(class_java_lang_System)) {
+               exceptions_print_stacktrace();
+               exit(1);
+       }
 
        /* call java.lang.System.exit(I)V */
 
@@ -1836,8 +1852,10 @@ void vm_exit(s4 status)
                                                                 class_java_lang_Object,
                                                                 true);
        
-       if (m == NULL)
-               throw_main_exception_exit();
+       if (m == NULL) {
+               exceptions_print_stacktrace();
+               exit(1);
+       }
 
        /* call the exit function with passed exit status */
 
@@ -1986,16 +2004,19 @@ static char *vm_get_mainclass_from_jar(char *mainstring)
 
        c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
 
-       if (c == NULL)
-               throw_main_exception_exit();
-       
+       if (c == NULL) {
+               exceptions_print_stacktrace();
+               return NULL;
+       }
+
        /* create JarFile object */
 
        o = builtin_new(c);
 
-       if (o == NULL)
-               throw_main_exception_exit();
-
+       if (o == NULL) {
+               exceptions_print_stacktrace();
+               return NULL;
+       }
 
        m = class_resolveclassmethod(c,
                                                                 utf_init, 
@@ -2003,15 +2024,19 @@ static char *vm_get_mainclass_from_jar(char *mainstring)
                                                                 class_java_lang_Object,
                                                                 true);
 
-       if (m == NULL)
-               throw_main_exception_exit();
+       if (m == NULL) {
+               exceptions_print_stacktrace();
+               return NULL;
+       }
 
        s = javastring_new_from_ascii(mainstring);
 
        (void) vm_call_method(m, o, s);
 
-       if (*exceptionptr)
-               throw_main_exception_exit();
+       if (exceptions_get_exception()) {
+               exceptions_print_stacktrace();
+               return NULL;
+       }
 
        /* get manifest object */
 
@@ -2021,14 +2046,16 @@ static char *vm_get_mainclass_from_jar(char *mainstring)
                                                                 class_java_lang_Object,
                                                                 true);
 
-       if (m == NULL)
-               throw_main_exception_exit();
+       if (m == NULL) {
+               exceptions_print_stacktrace();
+               return NULL;
+       }
 
        o = vm_call_method(m, o);
 
        if (o == NULL) {
                fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
-               vm_exit(1);
+               return NULL;
        }
 
 
@@ -2040,14 +2067,16 @@ static char *vm_get_mainclass_from_jar(char *mainstring)
                                                                 class_java_lang_Object,
                                                                 true);
 
-       if (m == NULL)
-               throw_main_exception_exit();
+       if (m == NULL) {
+               exceptions_print_stacktrace();
+               return NULL;
+       }
 
        o = vm_call_method(m, o);
 
        if (o == NULL) {
                fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
-               vm_exit(1);
+               return NULL;
        }
 
 
@@ -2059,15 +2088,19 @@ static char *vm_get_mainclass_from_jar(char *mainstring)
                                                                 class_java_lang_Object,
                                                                 true);
 
-       if (m == NULL)
-               throw_main_exception_exit();
+       if (m == NULL) {
+               exceptions_print_stacktrace();
+               return NULL;
+       }
 
        s = javastring_new_from_ascii("Main-Class");
 
        o = vm_call_method(m, o, s);
 
-       if (o == NULL)
-               throw_main_exception_exit();
+       if (o == NULL) {
+               exceptions_print_stacktrace();
+               return NULL;
+       }
 
        return javastring_tochar(o);
 }
@@ -2164,11 +2197,13 @@ static void vm_compile_method(void)
 
        /* create, load and link the main class */
 
-       if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
-               throw_main_exception_exit();
+       mainclass = load_class_bootstrap(utf_new_char(mainstring));
+
+       if (mainclass == NULL)
+               exceptions_print_stacktrace();
 
        if (!link_class(mainclass))
-               throw_main_exception_exit();
+               exceptions_print_stacktrace();
 
        if (opt_signature != NULL) {
                m = class_resolveclassmethod(mainclass,
@@ -2451,6 +2486,10 @@ java_objectheader *vm_call_method_vmarg(methodinfo *m, s4 vmargscount,
 {
        java_objectheader *o;
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE;
+#endif
+
 #if defined(ENABLE_JIT)
 # if defined(ENABLE_INTRP)
        if (opt_intrp)
@@ -2462,6 +2501,10 @@ java_objectheader *vm_call_method_vmarg(methodinfo *m, s4 vmargscount,
        o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
 #endif
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE;
+#endif
+
        return o;
 }
 
@@ -2581,6 +2624,10 @@ s4 vm_call_method_int_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
 {
        s4 i;
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE;
+#endif
+
 #if defined(ENABLE_JIT)
 # if defined(ENABLE_INTRP)
        if (opt_intrp)
@@ -2592,6 +2639,10 @@ s4 vm_call_method_int_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
        i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
 #endif
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE;
+#endif
+
        return i;
 }
 
@@ -2711,6 +2762,10 @@ s8 vm_call_method_long_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
 {
        s8 l;
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE;
+#endif
+
 #if defined(ENABLE_JIT)
 # if defined(ENABLE_INTRP)
        if (opt_intrp)
@@ -2722,6 +2777,10 @@ s8 vm_call_method_long_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
        l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
 #endif
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE;
+#endif
+
        return l;
 }
 
@@ -2843,6 +2902,10 @@ float vm_call_method_float_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
 {
        float f;
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE;
+#endif
+
 #if defined(ENABLE_JIT)
 # if defined(ENABLE_INTRP)
        if (opt_intrp)
@@ -2854,6 +2917,10 @@ float vm_call_method_float_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
        f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
 #endif
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE;
+#endif
+
        return f;
 }
 
@@ -2976,6 +3043,10 @@ double vm_call_method_double_vmarg(methodinfo *m, s4 vmargscount,
 {
        double d;
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE;
+#endif
+
 #if defined(ENABLE_JIT)
 # if defined(ENABLE_INTRP)
        if (opt_intrp)
@@ -2987,6 +3058,10 @@ double vm_call_method_double_vmarg(methodinfo *m, s4 vmargscount,
        d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
 #endif
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE;
+#endif
+
        return d;
 }