This commit introduces C++ support.
[cacao.git] / src / vm / vm.c
index 799c8421ecdbcb51ae3143e4b06f957215d4aa82..5fa1f57a1db303de45b7fe3d55459255e5654268 100644 (file)
@@ -49,7 +49,7 @@
 #include "native/include/java_lang_Object.h"             /* required by j.l.C */
 #include "native/include/java_lang_String.h"             /* required by j.l.C */
 
-#if defined(WITH_CLASSPATH_SUN)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
 # include "native/include/java_nio_ByteBuffer.h"        /* required by j.l.CL */
 # include "native/include/java_lang_ClassLoader.h"       /* required by j.l.C */
 #endif
 
 #include "native/vm/nativevm.h"
 
-#include "threads/threads-common.h"
+#include "threads/lock-common.h"
+#include "threads/mutex.h"
+#include "threads/threadlist.h"
+#include "threads/thread.h"
 
 #include "toolbox/logging.h"
 
@@ -73,7 +76,7 @@
 #include "vm/finalizer.h"
 #include "vm/global.h"
 #include "vm/initialize.h"
-#include "vm/package.h"
+#include "vm/package.hpp"
 #include "vm/primitive.h"
 #include "vm/properties.h"
 #include "vm/signallocal.h"
 
 #include "vm/jit/argument.h"
 #include "vm/jit/asmpart.h"
+#include "vm/jit/code.h"
 
 #if defined(ENABLE_DISASSEMBLER)
 # include "vm/jit/disass.h"
 #endif
 
 #include "vm/jit/jit.h"
+#include "vm/jit/methodtree.h"
 
 #if defined(ENABLE_PROFILING)
 # include "vm/jit/optimizing/profile.h"
 # include "vm/jit/python.h"
 #endif
 
+#include "vm/jit/trap.h"
+
 #include "vmcore/classcache.h"
 #include "vmcore/options.h"
 #include "vmcore/statistics.h"
@@ -125,10 +132,10 @@ _Jv_JNIEnv *_Jv_env;                    /* pointer to native method interface */
 s4 vms = 0;                             /* number of VMs created              */
 
 bool vm_initializing = false;
-bool vm_exiting = false;
+bool vm_created      = false;
+bool vm_exiting      = false;
 
-char      *mainstring = NULL;
-classinfo *mainclass = NULL;
+static classinfo *mainclass = NULL;
 
 #if defined(ENABLE_INTRP)
 u1 *intrp_main_stack = NULL;
@@ -627,16 +634,16 @@ static void vm_printconfig(void)
        printf("  initial heap size              : %d\n", HEAP_STARTSIZE);
        printf("  stack size                     : %d\n", STACK_SIZE);
 
-#if defined(WITH_JRE_LAYOUT)
+#if defined(ENABLE_JRE_LAYOUT)
        /* When we're building with JRE-layout, the default paths are the
           same as the runtime paths. */
 #else
-# if defined(WITH_CLASSPATH_GNU)
-       puts("  gnu.classpath.boot.library.path: "CLASSPATH_LIBDIR);
-       puts("  java.boot.class.path           : "CACAO_VM_ZIP":"CLASSPATH_CLASSES"");
-# elif defined(WITH_CLASSPATH_SUN)
-       puts("  sun.boot.library.path          : "CLASSPATH_LIBDIR);
-       puts("  java.boot.class.path           : "CLASSPATH_CLASSES);
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+       puts("  gnu.classpath.boot.library.path: "JAVA_RUNTIME_LIBRARY_LIBDIR);
+       puts("  java.boot.class.path           : "CACAO_VM_ZIP":"JAVA_RUNTIME_LIBRARY_CLASSES"");
+# elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
+       puts("  sun.boot.library.path          : "JAVA_RUNTIME_LIBRARY_LIBDIR);
+       puts("  java.boot.class.path           : "JAVA_RUNTIME_LIBRARY_CLASSES);
 # endif
 #endif
 
@@ -647,9 +654,9 @@ static void vm_printconfig(void)
        printf("  initial heap size              : %d\n", opt_heapstartsize);
        printf("  stack size                     : %d\n", opt_stacksize);
 
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
        printf("  gnu.classpath.boot.library.path: %s\n", properties_get("gnu.classpath.boot.library.path"));
-#elif defined(WITH_CLASSPATH_SUN)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
        printf("  sun.boot.library.path          : %s\n", properties_get("sun.boot.library.path"));
 #endif
 
@@ -663,7 +670,7 @@ static void vm_printconfig(void)
 static char *vm_get_mainclass_from_jar(char *mainstring);
 #if !defined(NDEBUG)
 static void  vm_compile_all(void);
-static void  vm_compile_method(void);
+static void  vm_compile_method(char* mainname);
 #endif
 
 
@@ -1355,37 +1362,6 @@ bool vm_create(JavaVMInitArgs *vm_args)
                }
        }
 
-       /* get the main class *****************************************************/
-
-       if (opt_index < vm_args->nOptions) {
-               mainstring = vm_args->options[opt_index++].optionString;
-
-               /* Put the jar file into the classpath (if any). */
-
-               if (opt_jar == true) {
-                       /* free old classpath */
-
-/*                     MFREE(_Jv_classpath, char, strlen(_Jv_classpath)); */
-
-                       /* put jarfile into classpath */
-
-                       p = MNEW(char, strlen(mainstring) + strlen("0"));
-
-                       strcpy(p, mainstring);
-
-#if defined(ENABLE_JAVASE)
-                       properties_add("java.class.path", p);
-#endif
-               }
-               else {
-                       /* replace .'s with /'s in classname */
-
-                       for (i = strlen(mainstring) - 1; i >= 0; i--)
-                               if (mainstring[i] == '.')
-                                       mainstring[i] = '/';
-               }
-       }
-
 #if defined(ENABLE_JVMTI)
        if (jvmti) {
                jvmti_set_phase(JVMTI_PHASE_ONLOAD);
@@ -1407,10 +1383,16 @@ bool vm_create(JavaVMInitArgs *vm_args)
        gc_init(opt_heapmaxsize, opt_heapstartsize);
 
 #if defined(ENABLE_THREADS)
+       /* BEFORE: threads_preinit */
+
+       threadlist_init();
+
        /* AFTER: gc_init (directly after, as this initializes the
           stopworldlock lock */
 
        threads_preinit();
+       lock_init();
+       critical_init();
 #endif
 
        /* install architecture dependent signal handlers */
@@ -1434,8 +1416,7 @@ bool vm_create(JavaVMInitArgs *vm_args)
 
        /* AFTER: threads_preinit */
 
-       if (!utf8_init())
-               vm_abort("vm_create: utf8_init failed");
+       utf8_init();
 
        /* AFTER: thread_preinit */
 
@@ -1474,9 +1455,11 @@ bool vm_create(JavaVMInitArgs *vm_args)
        if (!finalizer_init())
                vm_abort("vm_create: finalizer_init failed");
 
-       /* initializes jit compiler */
+       /* Initialize the JIT compiler. */
 
        jit_init();
+       code_init();
+       methodtree_init();
 
 #if defined(ENABLE_PYTHON)
        pythonpass_init();
@@ -1484,7 +1467,7 @@ bool vm_create(JavaVMInitArgs *vm_args)
 
        /* BEFORE: loader_preinit */
 
-       package_init();
+       Package_initialize();
 
        /* AFTER: utf8_init, classcache_init */
 
@@ -1501,8 +1484,11 @@ bool vm_create(JavaVMInitArgs *vm_args)
        /* AFTER: loader_init, linker_init */
 
        primitive_postinit();
+       method_init();
 
-       exceptions_init();
+#if defined(ENABLE_JIT)
+       trap_init();
+#endif
 
        if (!builtin_init())
                vm_abort("vm_create: builtin_init failed");
@@ -1516,8 +1502,7 @@ bool vm_create(JavaVMInitArgs *vm_args)
        /* Register the native methods implemented in the VM. */
        /* BEFORE: threads_init */
 
-       if (!nativevm_preinit())
-               vm_abort("vm_create: nativevm_preinit failed");
+       nativevm_preinit();
 
 #if defined(ENABLE_JNI)
        /* Initialize the JNI subsystem (must be done _before_
@@ -1536,16 +1521,19 @@ bool vm_create(JavaVMInitArgs *vm_args)
                vm_abort("vm_create: localref_table_init failed");
 #endif
 
+       /* Iinitialize some important system classes. */
+       /* BEFORE: threads_init */
+
+       initialize_init();
+
 #if defined(ENABLE_THREADS)
-       if (!threads_init())
-               vm_abort("vm_create: threads_init failed");
+       threads_init();
 #endif
 
        /* Initialize the native VM subsystem. */
        /* AFTER: threads_init (at least for SUN's classes) */
 
-       if (!nativevm_init())
-               vm_abort("vm_create: nativevm_init failed");
+       nativevm_init();
 
 #if defined(ENABLE_PROFILING)
        /* initialize profiling */
@@ -1608,21 +1596,20 @@ bool vm_create(JavaVMInitArgs *vm_args)
        }
 #endif
 
-       /* increment the number of VMs */
+       /* Increment the number of VMs. */
 
        vms++;
 
-       /* initialization is done */
+       /* Initialization is done, VM is created.. */
 
+       vm_created      = true;
        vm_initializing = false;
 
-#if !defined(NDEBUG)
        /* Print the VM configuration after all stuff is set and the VM is
           initialized. */
 
        if (opt_PrintConfig)
                vm_printconfig();
-#endif
 
        /* everything's ok */
 
@@ -1638,6 +1625,9 @@ bool vm_create(JavaVMInitArgs *vm_args)
 
 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
 {
+       char*                      option;
+       char*                      mainname;
+       char*                      p;
        utf                       *mainutf;
        classinfo                 *mainclass;
        java_handle_t             *e;
@@ -1646,26 +1636,78 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
        s4                         oalength;
        utf                       *u;
        java_handle_t             *s;
-       s4                         status;
-       s4                         i;
+       int                        status;
+       int                        i;
+
+       // Prevent compiler warnings.
+       oa = NULL;
 
 #if !defined(NDEBUG)
        if (compileall) {
                vm_compile_all();
                return;
        }
+#endif
 
-       if (opt_method != NULL) {
-               vm_compile_method();
-               return;
+       /* Get the main class plus it's arguments. */
+
+       mainname = NULL;
+
+       if (opt_index < vm_args->nOptions) {
+               /* Get main-class argument. */
+
+               mainname = vm_args->options[opt_index].optionString;
+
+               /* If the main class argument is a jar file, put it into the
+                  classpath. */
+
+               if (opt_jar == true) {
+                       p = MNEW(char, strlen(mainname) + strlen("0"));
+
+                       strcpy(p, mainname);
+
+#if defined(ENABLE_JAVASE)
+                       properties_add("java.class.path", p);
+#endif
+               }
+               else {
+                       /* Replace dots with slashes in the class name. */
+
+                       for (i = 0; i < strlen(mainname); i++)
+                               if (mainname[i] == '.')
+                                       mainname[i] = '/';
+               }
+
+               /* Build argument array.  Move index to first argument. */
+
+               opt_index++;
+
+               oalength = vm_args->nOptions - opt_index;
+
+               oa = builtin_anewarray(oalength, class_java_lang_String);
+
+               for (i = 0; i < oalength; i++) {
+                       option = vm_args->options[opt_index + i].optionString;
+
+                       u = utf_new_char(option);
+                       s = javastring_new(u);
+
+                       array_objectarray_element_set(oa, i, s);
+               }
        }
-#endif /* !defined(NDEBUG) */
 
-       /* should we run the main-method? */
+       /* Do we have a main-class argument? */
 
-       if (mainstring == NULL)
+       if (mainname == NULL)
                usage();
 
+#if !defined(NDEBUG)
+       if (opt_method != NULL) {
+               vm_compile_method(mainname);
+               return;
+       }
+#endif
+
        /* set return value to OK */
 
        status = 0;
@@ -1673,15 +1715,15 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
        if (opt_jar == true) {
                /* open jar file with java.util.jar.JarFile */
 
-               mainstring = vm_get_mainclass_from_jar(mainstring);
+               mainname = vm_get_mainclass_from_jar(mainname);
 
-               if (mainstring == NULL)
+               if (mainname == NULL)
                        vm_exit(1);
        }
 
        /* load the main class */
 
-       mainutf = utf_new_char(mainstring);
+       mainutf = utf_new_char(mainname);
 
 #if defined(ENABLE_JAVAME_CLDC1_1)
        mainclass = load_class_bootstrap(mainutf);
@@ -1729,24 +1771,10 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
                vm_exit(1);
        }
 
-       /* build argument array */
-
-       oalength = vm_args->nOptions - opt_index;
-
-       oa = builtin_anewarray(oalength, class_java_lang_String);
-
-       for (i = 0; i < oalength; i++) {
-               u = utf_new_char(vm_args->options[opt_index + i].optionString);
-               s = javastring_new(u);
-
-               array_objectarray_element_set(oa, i, s);
-       }
-
 #ifdef TYPEINFO_DEBUG_TEST
        /* test the typeinfo system */
        typeinfo_test();
 #endif
-       /*class_showmethods(currentThread->group->header.vftbl->class); */
 
 #if defined(ENABLE_JVMTI)
        jvmti_set_phase(JVMTI_PHASE_LIVE);
@@ -1773,11 +1801,19 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
                status = 1;
        }
 
-       /* unload the JavaVM */
+#if defined(ENABLE_THREADS)
+    /* Detach the main thread so that it appears to have ended when
+          the application's main method exits. */
+
+       if (!thread_detach_current_thread())
+               vm_abort("vm_run: Could not detach main thread.");
+#endif
+
+       /* Destroy the JavaVM. */
 
        (void) vm_destroy(vm);
 
-       /* and exit */
+       /* And exit. */
 
        vm_exit(status);
 }
@@ -1789,13 +1825,30 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
 
 *******************************************************************************/
 
-s4 vm_destroy(JavaVM *vm)
+int vm_destroy(JavaVM *vm)
 {
 #if defined(ENABLE_THREADS)
+       /* Create a a trivial new Java waiter thread called
+          "DestroyJavaVM". */
+
+       JavaVMAttachArgs args;
+
+       args.name  = "DestroyJavaVM";
+       args.group = NULL;
+
+       if (!thread_attach_current_thread(&args, false))
+               return 1;
+
+       /* Wait until we are the last non-daemon thread. */
+
        threads_join_all_threads();
 #endif
 
-       /* everything's ok */
+       /* VM is gone. */
+
+       vm_created = false;
+
+       /* Everything is ok. */
 
        return 0;
 }
@@ -1877,9 +1930,9 @@ void vm_shutdown(s4 status)
 #if defined(ENABLE_JVMTI)
        /* terminate cacaodbgserver */
        if (dbgcom!=NULL) {
-               pthread_mutex_lock(&dbgcomlock);
+               mutex_lock(&dbgcomlock);
                dbgcom->running=1;
-               pthread_mutex_unlock(&dbgcomlock);
+               mutex_unlock(&dbgcomlock);
                jvmti_cacaodbgserver_quit();
        }       
 #endif
@@ -2087,7 +2140,7 @@ void vm_abort_disassemble(void *pc, int count, const char *text, ...)
 
 *******************************************************************************/
 
-static char *vm_get_mainclass_from_jar(char *mainstring)
+static char *vm_get_mainclass_from_jar(char *mainname)
 {
        classinfo     *c;
        java_handle_t *o;
@@ -2121,7 +2174,7 @@ static char *vm_get_mainclass_from_jar(char *mainstring)
                return NULL;
        }
 
-       s = javastring_new_from_ascii(mainstring);
+       s = javastring_new_from_ascii(mainname);
 
        (void) vm_call_method(m, o, s);
 
@@ -2146,7 +2199,7 @@ static char *vm_get_mainclass_from_jar(char *mainstring)
        o = vm_call_method(m, o);
 
        if (o == NULL) {
-               fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
+               fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainname);
                return NULL;
        }
 
@@ -2169,7 +2222,7 @@ static char *vm_get_mainclass_from_jar(char *mainstring)
        o = vm_call_method(m, o);
 
        if (o == NULL) {
-               fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
+               fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainname);
                return NULL;
        }
 
@@ -2194,7 +2247,8 @@ static char *vm_get_mainclass_from_jar(char *mainstring)
        o = vm_call_method(m, o, s);
 
        if (o == NULL) {
-               exceptions_print_stacktrace();
+               fprintf(stderr, "Failed to load Main-Class manifest attribute from\n");
+               fprintf(stderr, "%s\n", mainname);
                return NULL;
        }
 
@@ -2287,13 +2341,13 @@ static void vm_compile_all(void)
 *******************************************************************************/
 
 #if !defined(NDEBUG)
-static void vm_compile_method(void)
+static void vm_compile_method(char* mainname)
 {
        methodinfo *m;
 
        /* create, load and link the main class */
 
-       mainclass = load_class_bootstrap(utf_new_char(mainstring));
+       mainclass = load_class_bootstrap(utf_new_char(mainname));
 
        if (mainclass == NULL)
                exceptions_print_stacktrace();