- created jitcache-arm-x86 branch
[cacao.git] / src / vm / vm.c
index 221d8a34d1e878e118ef85b423bca5229ab2f8af..84a9a7351b553a4f3f67a16d78660fbf1f33d0ca 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
@@ -59,8 +59,9 @@
 #include "native/vm/nativevm.h"
 
 #include "threads/lock-common.h"
+#include "threads/mutex.h"
 #include "threads/threadlist.h"
-#include "threads/threads-common.h"
+#include "threads/thread.h"
 
 #include "toolbox/logging.h"
 
@@ -75,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"
+#include "vm/jit/jitcache.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"
@@ -127,10 +133,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;
@@ -629,16 +635,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
 
@@ -649,9 +655,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
 
@@ -665,7 +671,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
 
 
@@ -1357,37 +1363,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);
@@ -1442,8 +1417,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 */
 
@@ -1482,9 +1456,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();
@@ -1492,7 +1468,7 @@ bool vm_create(JavaVMInitArgs *vm_args)
 
        /* BEFORE: loader_preinit */
 
-       package_init();
+       Package_initialize();
 
        /* AFTER: utf8_init, classcache_init */
 
@@ -1509,8 +1485,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");
@@ -1524,8 +1503,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_
@@ -1544,6 +1522,11 @@ 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)
        threads_init();
 #endif
@@ -1551,8 +1534,7 @@ bool vm_create(JavaVMInitArgs *vm_args)
        /* 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 */
@@ -1615,12 +1597,13 @@ 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;
 
        /* Print the VM configuration after all stuff is set and the VM is
@@ -1643,6 +1626,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;
@@ -1651,26 +1637,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;
@@ -1678,15 +1716,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);
@@ -1734,24 +1772,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);
@@ -1778,11 +1802,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);
 }
@@ -1794,13 +1826,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;
 }
@@ -1882,13 +1931,17 @@ 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
 
+#if defined (ENABLE_JITCACHE)
+       jitcache_quit();
+#endif
+
        exit(status);
 }
 
@@ -2092,7 +2145,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;
@@ -2126,7 +2179,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);
 
@@ -2151,7 +2204,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;
        }
 
@@ -2174,7 +2227,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;
        }
 
@@ -2199,7 +2252,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;
        }
 
@@ -2292,13 +2346,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();