1 /* src/vm/vm.c - VM startup and shutdown functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: vm.c 8362 2007-08-20 18:35:26Z michi $
37 #if defined(WITH_JRE_LAYOUT)
47 #include "vm/jit/abi-asm.h"
49 #include "mm/gc-common.h"
50 #include "mm/memory.h"
52 #include "native/jni.h"
53 #include "native/llni.h"
54 #include "native/native.h"
56 #include "native/include/java_lang_Object.h" /* required by j.l.C */
57 #include "native/include/java_lang_String.h" /* required by j.l.C */
59 #if defined(WITH_CLASSPATH_SUN)
60 # include "native/include/java_nio_ByteBuffer.h" /* required by j.l.CL */
61 # include "native/include/java_lang_ClassLoader.h" /* required by j.l.C */
64 #include "native/include/java_lang_Class.h"
66 #include "native/include/java_lang_Byte.h"
67 #include "native/include/java_lang_Character.h"
68 #include "native/include/java_lang_Short.h"
69 #include "native/include/java_lang_Integer.h"
70 #include "native/include/java_lang_Boolean.h"
71 #include "native/include/java_lang_Long.h"
72 #include "native/include/java_lang_Float.h"
73 #include "native/include/java_lang_Double.h"
75 #include "native/vm/nativevm.h"
77 #include "threads/threads-common.h"
79 #include "toolbox/logging.h"
81 #include "vm/builtin.h"
82 #include "vm/exceptions.h"
83 #include "vm/finalizer.h"
84 #include "vm/global.h"
85 #include "vm/initialize.h"
86 #include "vm/primitive.h"
87 #include "vm/properties.h"
88 #include "vm/signallocal.h"
89 #include "vm/stringlocal.h"
92 #include "vm/jit/jit.h"
93 #include "vm/jit/md.h"
94 #include "vm/jit/asmpart.h"
96 #if defined(ENABLE_PROFILING)
97 # include "vm/jit/optimizing/profile.h"
100 #include "vm/jit/optimizing/recompile.h"
102 #include "vmcore/classcache.h"
103 #include "vmcore/options.h"
104 #include "vmcore/statistics.h"
105 #include "vmcore/suck.h"
107 #if defined(ENABLE_JVMTI)
108 # include "native/jvmti/cacaodbg.h"
111 #if defined(ENABLE_VMLOG)
112 #include <vmlog_cacao.h>
116 /* Invocation API variables ***************************************************/
118 _Jv_JavaVM *_Jv_jvm; /* denotes a Java VM */
119 _Jv_JNIEnv *_Jv_env; /* pointer to native method interface */
122 /* global variables ***********************************************************/
124 s4 vms = 0; /* number of VMs created */
126 bool vm_initializing = false;
127 bool vm_exiting = false;
129 char *cacao_prefix = NULL;
130 char *cacao_libjvm = NULL;
131 char *classpath_libdir = NULL;
133 char *_Jv_bootclasspath; /* contains the boot classpath */
134 char *_Jv_classpath; /* contains the classpath */
135 char *_Jv_java_library_path;
137 char *mainstring = NULL;
138 classinfo *mainclass = NULL;
140 char *specificmethodname = NULL;
141 char *specificsignature = NULL;
145 #if defined(ENABLE_INTRP)
146 u1 *intrp_main_stack = NULL;
150 /* define heap sizes **********************************************************/
152 #define HEAP_MAXSIZE 128 * 1024 * 1024 /* default 128MB */
153 #define HEAP_STARTSIZE 2 * 1024 * 1024 /* default 2MB */
154 #define STACK_SIZE 64 * 1024 /* default 64kB */
157 /* define command line options ************************************************/
188 /* Java non-standard options */
199 #if defined(ENABLE_PROFILING)
212 #if defined(ENABLE_STATISTICS)
229 #if defined(ENABLE_VERIFIER)
231 #if defined(TYPECHECK_VERBOSE)
234 #endif /* defined(ENABLE_VERIFIER) */
236 /* optimization options */
238 #if defined(ENABLE_LOOP)
242 #if defined(ENABLE_IFCONV)
246 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
250 #if defined(ENABLE_INLINING)
255 #if defined(ENABLE_INLINING_DEBUG)
256 OPT_INLINE_DEBUG_ALL,
257 OPT_INLINE_DEBUG_END,
258 OPT_INLINE_DEBUG_MIN,
259 OPT_INLINE_DEBUG_MAX,
260 #endif /* defined(ENABLE_INLINING_DEBUG) */
261 #endif /* defined(ENABLE_INLINING) */
263 #if defined(ENABLE_INTRP)
264 /* interpreter options */
283 #if defined(ENABLE_DEBUG_FILTER)
284 OPT_FILTER_VERBOSECALL_INCLUDE,
285 OPT_FILTER_VERBOSECALL_EXCLUDE,
286 OPT_FILTER_SHOW_METHOD,
293 opt_struct opts[] = {
294 { "foo", false, OPT_FOO },
298 { "jar", false, OPT_JAR },
300 { "d32", false, OPT_D32 },
301 { "d64", false, OPT_D64 },
302 { "client", false, OPT_IGNORE },
303 { "server", false, OPT_IGNORE },
304 { "jvm", false, OPT_IGNORE },
305 { "hotspot", false, OPT_IGNORE },
307 { "classpath", true, OPT_CLASSPATH },
308 { "cp", true, OPT_CLASSPATH },
309 { "D", true, OPT_D },
310 { "version", false, OPT_VERSION },
311 { "showversion", false, OPT_SHOWVERSION },
312 { "fullversion", false, OPT_FULLVERSION },
313 { "help", false, OPT_HELP },
314 { "?", false, OPT_HELP },
315 { "X", false, OPT_X },
316 { "XX:", true, OPT_XX },
317 { "XX", false, OPT_XX },
319 { "ea:", true, OPT_EA },
320 { "da:", true, OPT_DA },
321 { "ea", false, OPT_EA },
322 { "da", false, OPT_DA },
324 { "esa", false, OPT_ESA },
325 { "enablesystemassertions", false, OPT_ESA },
326 { "dsa", false, OPT_DSA },
327 { "disablesystemassertions", false, OPT_DSA },
329 { "noasyncgc", false, OPT_IGNORE },
330 #if defined(ENABLE_VERIFIER)
331 { "noverify", false, OPT_NOVERIFY },
333 { "v", false, OPT_VERBOSE1 },
334 { "verbose:", true, OPT_VERBOSE },
336 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
337 { "verbosetc", false, OPT_VERBOSETC },
339 #if defined(__ALPHA__)
340 { "noieee", false, OPT_NOIEEE },
342 #if defined(ENABLE_STATISTICS)
343 { "time", false, OPT_TIME },
344 { "stat", false, OPT_STAT },
346 { "log", true, OPT_LOG },
347 { "c", true, OPT_CHECK },
348 { "l", false, OPT_LOAD },
351 { "all", false, OPT_ALL },
352 { "sig", true, OPT_SIGNATURE },
355 #if defined(ENABLE_LOOP)
356 { "oloop", false, OPT_OLOOP },
358 #if defined(ENABLE_IFCONV)
359 { "ifconv", false, OPT_IFCONV },
361 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
362 { "lsra", false, OPT_LSRA },
365 #if defined(ENABLE_INTRP)
366 /* interpreter options */
368 { "trace", false, OPT_TRACE },
369 { "static-supers", true, OPT_STATIC_SUPERS },
370 { "no-dynamic", false, OPT_NO_DYNAMIC },
371 { "no-replication", false, OPT_NO_REPLICATION },
372 { "no-quicksuper", false, OPT_NO_QUICKSUPER },
375 /* JVMTI Agent Command Line Options */
377 { "agentlib:", true, OPT_AGENTLIB },
378 { "agentpath:", true, OPT_AGENTPATH },
381 /* Java non-standard options */
383 { "Xjit", false, OPT_JIT },
384 { "Xint", false, OPT_INTRP },
385 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
386 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
387 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
388 { "Xbootclasspath/c:", true, OPT_BOOTCLASSPATH_C },
391 { "Xdebug", false, OPT_DEBUG },
392 { "Xnoagent", false, OPT_NOAGENT },
393 { "Xrunjdwp", true, OPT_XRUNJDWP },
396 { "Xms", true, OPT_MS },
397 { "ms", true, OPT_MS },
398 { "Xmx", true, OPT_MX },
399 { "mx", true, OPT_MX },
400 { "Xss", true, OPT_SS },
401 { "ss", true, OPT_SS },
403 #if defined(ENABLE_PROFILING)
404 { "Xprof:", true, OPT_PROF_OPTION },
405 { "Xprof", false, OPT_PROF },
408 /* inlining options */
410 #if defined(ENABLE_INLINING)
411 #if defined(ENABLE_INLINING_DEBUG)
412 { "ia", false, OPT_INLINE_DEBUG_ALL },
413 { "ii", true, OPT_INLINE_DEBUG_MIN },
414 { "im", true, OPT_INLINE_DEBUG_MAX },
415 { "ie", true, OPT_INLINE_DEBUG_END },
416 #endif /* defined(ENABLE_INLINING_DEBUG) */
418 { "il", false, OPT_INLINE_LOG },
420 { "i", false, OPT_INLINING },
421 #endif /* defined(ENABLE_INLINING) */
423 /* keep these at the end of the list */
426 { "m", true, OPT_METHOD },
429 { "s", true, OPT_SHOW },
430 { "debug-color", false, OPT_DEBUGCOLOR },
432 #if defined(ENABLE_DEBUG_FILTER)
433 { "XXfi", true, OPT_FILTER_VERBOSECALL_INCLUDE },
434 { "XXfx", true, OPT_FILTER_VERBOSECALL_EXCLUDE },
435 { "XXfm", true, OPT_FILTER_SHOW_METHOD },
442 /* usage ***********************************************************************
444 Prints the correct usage syntax to stdout.
446 *******************************************************************************/
450 puts("Usage: cacao [-options] classname [arguments]");
451 puts(" (to run a class file)");
452 puts(" or cacao [-options] -jar jarfile [arguments]");
453 puts(" (to run a standalone jar file)\n");
455 puts("where options include:");
456 puts(" -d32 use 32-bit data model if available");
457 puts(" -d64 use 64-bit data model if available");
458 puts(" -client compatibility (currently ignored)");
459 puts(" -server compatibility (currently ignored)");
460 puts(" -jvm compatibility (currently ignored)");
461 puts(" -hotspot compatibility (currently ignored)\n");
463 puts(" -cp <path> specify a path to look for classes");
464 puts(" -classpath <path> specify a path to look for classes");
465 puts(" -D<name>=<value> add an entry to the property list");
466 puts(" -verbose[:class|gc|jni] enable specific verbose output");
467 puts(" -version print product version and exit");
468 puts(" -fullversion print jpackage-compatible product version and exit");
469 puts(" -showversion print product version and continue");
470 puts(" -help, -? print this help message");
471 puts(" -X print help on non-standard Java options");
472 puts(" -XX print help on debugging options");
473 puts(" -ea[:<packagename>...|:<classname>]");
474 puts(" -enableassertions[:<packagename>...|:<classname>]");
475 puts(" enable assertions with specified granularity");
476 puts(" -da[:<packagename>...|:<classname>]");
477 puts(" -disableassertions[:<packagename>...|:<classname>]");
478 puts(" disable assertions with specified granularity");
479 puts(" -esa | -enablesystemassertions");
480 puts(" enable system assertions");
481 puts(" -dsa | -disablesystemassertions");
482 puts(" disable system assertions");
485 puts(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent");
486 puts (" for jdwp help use: -agentlib:jdwp=help");
487 puts(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent");
490 /* exit with error code */
496 static void Xusage(void)
498 #if defined(ENABLE_JIT)
499 puts(" -Xjit JIT mode execution (default)");
501 #if defined(ENABLE_INTRP)
502 puts(" -Xint interpreter mode execution");
504 puts(" -Xbootclasspath:<zip/jar files and directories separated by :>");
505 puts(" value is set as bootstrap class path");
506 puts(" -Xbootclasspath/a:<zip/jar files and directories separated by :>");
507 puts(" value is appended to the bootstrap class path");
508 puts(" -Xbootclasspath/p:<zip/jar files and directories separated by :>");
509 puts(" value is prepended to the bootstrap class path");
510 puts(" -Xbootclasspath/c:<zip/jar files and directories separated by :>");
511 puts(" value is used as Java core library, but the");
512 puts(" hardcoded VM interface classes are prepended");
513 printf(" -Xms<size> set the initial size of the heap (default: %dMB)\n", HEAP_STARTSIZE / 1024 / 1024);
514 printf(" -Xmx<size> set the maximum size of the heap (default: %dMB)\n", HEAP_MAXSIZE / 1024 / 1024);
515 printf(" -Xss<size> set the thread stack size (default: %dkB)\n", STACK_SIZE / 1024);
517 #if defined(ENABLE_PROFILING)
518 puts(" -Xprof[:bb] collect and print profiling data");
521 #if defined(ENABLE_JVMTI)
522 /* -Xdebug option depend on gnu classpath JDWP options. options:
523 transport=dt_socket,address=<hostname:port>,server=(y|n),suspend(y|n) */
524 puts(" -Xdebug enable remote debugging\n");
525 puts(" -Xrunjdwp transport=[dt_socket|...],address=<hostname:port>,server=[y|n],suspend=[y|n]\n");
526 puts(" enable remote debugging\n");
529 /* exit with error code */
536 static void XXusage(void)
538 puts(" -v write state-information");
540 puts(" -verbose[:jit|threads]");
541 puts(" enable specific verbose output");
542 puts(" -debug-color colored output for ANSI terms");
544 #ifdef TYPECHECK_VERBOSE
545 puts(" -verbosetc write debug messages while typechecking");
547 #if defined(__ALPHA__)
548 puts(" -noieee don't use ieee compliant arithmetic");
550 #if defined(ENABLE_VERIFIER)
551 puts(" -noverify don't verify classfiles");
553 #if defined(ENABLE_STATISTICS)
554 puts(" -time measure the runtime");
555 puts(" -stat detailed compiler statistics");
557 puts(" -log logfile specify a name for the logfile");
558 puts(" -c(heck)b(ounds) don't check array bounds");
559 puts(" s(ync) don't check for synchronization");
560 #if defined(ENABLE_LOOP)
561 puts(" -oloop optimize array accesses in loops");
563 puts(" -l don't start the class after loading");
565 puts(" -all compile all methods, no execution");
566 puts(" -m compile only a specific method");
567 puts(" -sig specify signature for a specific method");
570 puts(" -s... show...");
571 puts(" (c)onstants the constant pool");
572 puts(" (m)ethods class fields and methods");
573 puts(" (u)tf the utf - hash");
574 puts(" (i)ntermediate intermediate representation");
575 #if defined(ENABLE_DISASSEMBLER)
576 puts(" (a)ssembler disassembled listing");
577 puts(" n(o)ps show NOPs in disassembler output");
578 puts(" (e)xceptionstubs disassembled exception stubs (only with -sa)");
579 puts(" (n)ative disassembled native stubs");
581 puts(" (d)atasegment data segment listing");
583 #if defined(ENABLE_INLINING)
584 puts(" -i activate inlining");
586 puts(" -il log inlining");
588 #if defined(ENABLE_INLINING_DEBUG)
589 puts(" -ia use inlining for all methods");
590 puts(" -ii <size> set minimum size for inlined result");
591 puts(" -im <size> set maximum size for inlined result");
592 puts(" -ie <number> stop inlining after the given number of roots");
593 #endif /* defined(ENABLE_INLINING_DEBUG) */
594 #endif /* defined(ENABLE_INLINING) */
596 #if defined(ENABLE_IFCONV)
597 puts(" -ifconv use if-conversion");
599 #if defined(ENABLE_LSRA)
600 puts(" -lsra use linear scan register allocation");
602 #if defined(ENABLE_SSA)
603 puts(" -lsra use linear scan register allocation (with SSA)");
605 #if defined(ENABLE_DEBUG_FILTER)
606 puts(" -XXfi <regex> begin of dynamic scope for verbosecall filter");
607 puts(" -XXfx <regex> end of dynamic scope for verbosecall filter");
608 puts(" -XXfm <regex> filter for show options");
610 /* exit with error code */
617 /* version *********************************************************************
619 Only prints cacao version information.
621 *******************************************************************************/
623 static void version(bool opt_exit)
625 puts("java version \""JAVA_VERSION"\"");
626 puts("CACAO version "VERSION"");
628 puts("Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,");
629 puts("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,");
630 puts("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,");
631 puts("J. Wenninger, Institut f. Computersprachen - TU Wien\n");
633 puts("This program is free software; you can redistribute it and/or");
634 puts("modify it under the terms of the GNU General Public License as");
635 puts("published by the Free Software Foundation; either version 2, or (at");
636 puts("your option) any later version.\n");
638 puts("This program is distributed in the hope that it will be useful, but");
639 puts("WITHOUT ANY WARRANTY; without even the implied warranty of");
640 puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU");
641 puts("General Public License for more details.");
643 /* exit normally, if requested */
650 /* fullversion *****************************************************************
652 Prints a Sun compatible version information (required e.g. by
653 jpackage, www.jpackage.org).
655 *******************************************************************************/
657 static void fullversion(void)
659 puts("java full version \"cacao-"JAVA_VERSION"\"");
667 void vm_printconfig(void)
669 puts("Configure/Build options:\n");
670 puts(" ./configure: "VERSION_CONFIGURE_ARGS"");
671 #if defined(__VERSION__)
672 puts(" CC : "VERSION_CC" ("__VERSION__")");
674 puts(" CC : "VERSION_CC"");
676 puts(" CFLAGS : "VERSION_CFLAGS"\n");
678 puts("Default variables:\n");
679 printf(" maximum heap size : %d\n", HEAP_MAXSIZE);
680 printf(" initial heap size : %d\n", HEAP_STARTSIZE);
681 printf(" stack size : %d\n", STACK_SIZE);
682 #if defined(WITH_CLASSPATH_GNU)
683 puts(" java.boot.class.path : "CACAO_VM_ZIP":"CLASSPATH_CLASSES"");
685 puts(" java.boot.class.path : "CLASSPATH_CLASSES"");
687 puts(" gnu.classpath.boot.library.path: "CLASSPATH_LIBDIR"/classpath\n");
689 puts("Runtime variables:\n");
690 printf(" maximum heap size : %d\n", opt_heapmaxsize);
691 printf(" initial heap size : %d\n", opt_heapstartsize);
692 printf(" stack size : %d\n", opt_stacksize);
693 printf(" libjvm.so : %s\n", cacao_libjvm);
694 printf(" java.boot.class.path : %s\n", _Jv_bootclasspath);
695 printf(" gnu.classpath.boot.library.path: %s\n", classpath_libdir);
696 printf(" java.class.path : %s\n", _Jv_classpath);
700 /* forward declarations *******************************************************/
702 static char *vm_get_mainclass_from_jar(char *mainstring);
704 static void vm_compile_all(void);
705 static void vm_compile_method(void);
709 /* vm_createjvm ****************************************************************
711 Implementation for JNI_CreateJavaVM.
713 *******************************************************************************/
715 bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
717 JavaVMInitArgs *_vm_args;
721 /* get the arguments for the new JVM */
723 _vm_args = (JavaVMInitArgs *) vm_args;
725 /* get the VM and Env tables (must be set before vm_create) */
727 env = NEW(_Jv_JNIEnv);
729 #if defined(ENABLE_JNI)
730 env->env = &_Jv_JNINativeInterface;
733 /* XXX Set the global variable. Maybe we should do that differently. */
737 /* create and fill a JavaVM structure */
739 vm = NEW(_Jv_JavaVM);
741 #if defined(ENABLE_JNI)
742 vm->functions = &_Jv_JNIInvokeInterface;
745 /* XXX Set the global variable. Maybe we should do that differently. */
746 /* XXX JVMTI Agents needs a JavaVM */
750 /* actually create the JVM */
752 if (!vm_create(_vm_args))
755 #if defined(ENABLE_JNI)
756 /* setup the local ref table (must be created after vm_create) */
758 /* XXX this one will never get freed for the main thread;
759 call localref_table_destroy() if you want to do it! */
761 if (!localref_table_init())
765 /* now return the values */
767 *p_vm = (JavaVM *) vm;
768 *p_env = (void *) env;
773 /* release allocated memory */
775 FREE(env, _Jv_JNIEnv);
776 FREE(vm, _Jv_JavaVM);
782 /* vm_create *******************************************************************
784 Creates a JVM. Called by vm_createjvm.
786 *******************************************************************************/
788 bool vm_create(JavaVMInitArgs *vm_args)
797 #if defined(ENABLE_JVMTI)
799 char *libname, *agentarg;
800 bool jdwp,agentbypath;
801 jdwp = agentbypath = false;
804 #if defined(ENABLE_VMLOG)
805 vmlog_cacao_init(vm_args);
808 /* check the JNI version requested */
810 switch (vm_args->version) {
811 case JNI_VERSION_1_1:
813 case JNI_VERSION_1_2:
814 case JNI_VERSION_1_4:
820 /* we only support 1 JVM instance */
825 if (atexit(vm_exit_handler))
826 vm_abort("atexit failed: %s\n", strerror(errno));
829 log_text("CACAO started -------------------------------------------------------");
831 /* We need to check if the actual size of a java.lang.Class object
832 is smaller or equal than the assumption made in
833 src/vmcore/class.h. */
835 if (sizeof(java_lang_Class) > sizeof(dummy_java_lang_Class))
836 vm_abort("vm_create: java_lang_Class structure is bigger than classinfo.object (%d > %d)", sizeof(java_lang_Class), sizeof(dummy_java_lang_Class));
838 /* set the VM starttime */
840 _Jv_jvm->starttime = builtin_currenttimemillis();
842 /* get stuff from the environment *****************************************/
844 #if defined(WITH_JRE_LAYOUT)
845 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
847 cacao_prefix = MNEW(char, 4096);
849 if (readlink("/proc/self/exe", cacao_prefix, 4095) == -1)
850 vm_abort("readlink failed: %s\n", strerror(errno));
852 /* get the path of the current executable */
854 cacao_prefix = dirname(cacao_prefix);
856 if ((strlen(cacao_prefix) + strlen("/..") + strlen("0")) > 4096)
857 vm_abort("libjvm name to long for buffer\n");
859 /* concatenate the library name */
861 strcat(cacao_prefix, "/..");
863 /* now set path to libjvm.so */
865 len = strlen(cacao_prefix) + strlen("/lib/libjvm") + strlen("0");
867 cacao_libjvm = MNEW(char, len);
868 strcpy(cacao_libjvm, cacao_prefix);
869 strcat(cacao_libjvm, "/lib/libjvm");
871 /* and finally set the path to GNU Classpath libraries */
873 len = strlen(cacao_prefix) + strlen("/lib/classpath") + strlen("0");
875 classpath_libdir = MNEW(char, len);
876 strcpy(classpath_libdir, cacao_prefix);
877 strcat(classpath_libdir, "/lib/classpath");
879 cacao_prefix = CACAO_PREFIX;
880 cacao_libjvm = CACAO_LIBDIR"/libjvm";
882 # if defined(WITH_CLASSPATH_GNU)
883 classpath_libdir = CLASSPATH_LIBDIR"/classpath";
885 classpath_libdir = CLASSPATH_LIBDIR;
889 /* set the bootclasspath */
891 cp = getenv("BOOTCLASSPATH");
894 _Jv_bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
895 strcpy(_Jv_bootclasspath, cp);
898 #if defined(WITH_JRE_LAYOUT)
900 # if defined(WITH_CLASSPATH_GNU)
901 strlen(cacao_prefix) +
902 strlen("/share/cacao/vm.zip") +
905 strlen(cacao_prefix) +
906 strlen("/share/classpath/glibj.zip") +
909 _Jv_bootclasspath = MNEW(char, len);
910 # if defined(WITH_CLASSPATH_GNU)
911 strcat(_Jv_bootclasspath, cacao_prefix);
912 strcat(_Jv_bootclasspath, "/share/cacao/vm.zip");
913 strcat(_Jv_bootclasspath, ":");
915 strcat(_Jv_bootclasspath, cacao_prefix);
916 strcat(_Jv_bootclasspath, "/share/classpath/glibj.zip");
918 # if defined(WITH_CLASSPATH_GNU)
920 strlen(CACAO_VM_ZIP) +
922 strlen(CLASSPATH_CLASSES) +
924 # elif defined(WITH_CLASSPATH_SUN)
925 /* This is the bootclasspath taken from HotSpot (see
926 hotspot/src/share/vm/runtime/os.cpp
927 (os::set_boot_path)). */
930 strlen(CLASSPATH_PREFIX"/lib/resources.jar:"
931 CLASSPATH_PREFIX"/lib/rt.jar:"
932 CLASSPATH_PREFIX"/lib/sunrsasign.jar:"
933 CLASSPATH_PREFIX"/lib/jsse.jar:"
934 CLASSPATH_PREFIX"/lib/jce.jar:"
935 CLASSPATH_PREFIX"/lib/charsets.jar:"
936 CLASSPATH_PREFIX"/classes") +
938 # elif defined(WITH_CLASSPATH_CLDC1_1)
940 strlen(CLASSPATH_CLASSES) +
943 # error unknown classpath configuration
946 _Jv_bootclasspath = MNEW(char, len);
948 # if defined(WITH_CLASSPATH_GNU)
949 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
950 strcat(_Jv_bootclasspath, ":");
951 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
952 # elif defined(WITH_CLASSPATH_SUN)
953 strcpy(_Jv_bootclasspath,
954 CLASSPATH_PREFIX"/lib/resources.jar:"
955 CLASSPATH_PREFIX"/lib/rt.jar:"
956 CLASSPATH_PREFIX"/lib/sunrsasign.jar:"
957 CLASSPATH_PREFIX"/lib/jsse.jar:"
958 CLASSPATH_PREFIX"/lib/jce.jar:"
959 CLASSPATH_PREFIX"/lib/charsets.jar:"
960 CLASSPATH_PREFIX"/classes");
961 # elif defined(WITH_CLASSPATH_CLDC1_1)
962 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
964 # error unknown classpath configuration
969 /* set the classpath */
971 cp = getenv("CLASSPATH");
974 _Jv_classpath = MNEW(char, strlen(cp) + strlen("0"));
975 strcat(_Jv_classpath, cp);
978 _Jv_classpath = MNEW(char, strlen(".") + strlen("0"));
979 strcpy(_Jv_classpath, ".");
982 /* Get and set java.library.path. */
984 _Jv_java_library_path = getenv("LD_LIBRARY_PATH");
986 if (_Jv_java_library_path == NULL)
987 _Jv_java_library_path = "";
989 /* interpret the options **************************************************/
996 opt_heapmaxsize = HEAP_MAXSIZE;
997 opt_heapstartsize = HEAP_STARTSIZE;
998 opt_stacksize = STACK_SIZE;
1001 #if defined(ENABLE_JVMTI)
1002 /* initialize JVMTI related **********************************************/
1006 /* Initialize and fill properties before command-line handling. */
1008 if (!properties_init())
1009 vm_abort("vm_create: properties_init failed");
1011 /* Set the classpath properties. */
1013 #if defined(ENABLE_JAVASE)
1014 properties_add("java.boot.class.path", _Jv_bootclasspath);
1015 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1016 properties_add("java.class.path", _Jv_classpath);
1019 /* iterate over all passed options */
1021 while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
1035 #if SIZEOF_VOID_P == 8
1036 puts("Running a 32-bit JVM is not supported on this platform.");
1042 #if SIZEOF_VOID_P == 4
1043 puts("Running a 64-bit JVM is not supported on this platform.");
1049 /* forget old classpath and set the argument as new classpath */
1050 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1052 _Jv_classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
1053 strcpy(_Jv_classpath, opt_arg);
1055 #if defined(ENABLE_JAVASE)
1056 properties_add("java.class.path", _Jv_classpath);
1061 for (i = 0; i < strlen(opt_arg); i++) {
1062 if (opt_arg[i] == '=') {
1064 properties_add(opt_arg, opt_arg + i + 1);
1069 /* if no '=' is given, just create an empty property */
1071 properties_add(opt_arg, "");
1076 case OPT_BOOTCLASSPATH:
1077 /* Forget default bootclasspath and set the argument as
1078 new boot classpath. */
1080 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1082 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
1083 strcpy(_Jv_bootclasspath, opt_arg);
1085 #if defined(ENABLE_JAVASE)
1086 properties_add("java.boot.class.path", _Jv_bootclasspath);
1087 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1091 case OPT_BOOTCLASSPATH_A:
1092 /* append to end of bootclasspath */
1094 len = strlen(_Jv_bootclasspath);
1096 _Jv_bootclasspath = MREALLOC(_Jv_bootclasspath,
1100 strlen(opt_arg) + strlen("0"));
1102 strcat(_Jv_bootclasspath, ":");
1103 strcat(_Jv_bootclasspath, opt_arg);
1105 #if defined(ENABLE_JAVASE)
1106 properties_add("java.boot.class.path", _Jv_bootclasspath);
1107 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1111 case OPT_BOOTCLASSPATH_P:
1112 /* prepend in front of bootclasspath */
1114 cp = _Jv_bootclasspath;
1117 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
1120 strcpy(_Jv_bootclasspath, opt_arg);
1121 strcat(_Jv_bootclasspath, ":");
1122 strcat(_Jv_bootclasspath, cp);
1124 MFREE(cp, char, len);
1126 #if defined(ENABLE_JAVASE)
1127 properties_add("java.boot.class.path", _Jv_bootclasspath);
1128 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1132 case OPT_BOOTCLASSPATH_C:
1133 /* use as Java core library, but prepend VM interface classes */
1135 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1137 len = strlen(CACAO_VM_ZIP) +
1142 _Jv_bootclasspath = MNEW(char, len);
1144 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
1145 strcat(_Jv_bootclasspath, ":");
1146 strcat(_Jv_bootclasspath, opt_arg);
1148 #if defined(ENABLE_JAVASE)
1149 properties_add("java.boot.class.path", _Jv_bootclasspath);
1150 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1154 #if defined(ENABLE_JVMTI)
1156 /* this option exists only for compatibility reasons */
1160 /* I don't know yet what Xnoagent should do. This is only for
1161 compatiblity with eclipse - motse */
1170 strlen(CACAO_LIBDIR) +
1171 strlen("/libjdwp.so=") +
1175 agentarg = MNEW(char, len);
1177 strcpy(agentarg, CACAO_LIBDIR);
1178 strcat(agentarg, "/libjdwp.so=");
1179 strcat(agentarg, &opt_arg[1]);
1196 c = opt_arg[strlen(opt_arg) - 1];
1198 if ((c == 'k') || (c == 'K')) {
1199 j = atoi(opt_arg) * 1024;
1201 } else if ((c == 'm') || (c == 'M')) {
1202 j = atoi(opt_arg) * 1024 * 1024;
1208 opt_heapmaxsize = j;
1209 else if (opt == OPT_MS)
1210 opt_heapstartsize = j;
1221 if (strcmp("class", opt_arg) == 0) {
1222 opt_verboseclass = true;
1224 else if (strcmp("gc", opt_arg) == 0) {
1225 opt_verbosegc = true;
1227 else if (strcmp("jni", opt_arg) == 0) {
1228 opt_verbosejni = true;
1230 #if !defined(NDEBUG)
1231 else if (strcmp("jit", opt_arg) == 0) {
1235 compileverbose = true;
1237 else if (strcmp("threads", opt_arg) == 0) {
1238 opt_verbosethreads = true;
1242 printf("Unknown -verbose option: %s\n", opt_arg);
1247 case OPT_DEBUGCOLOR:
1248 opt_debugcolor = true;
1251 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
1253 opt_typecheckverbose = true;
1262 case OPT_FULLVERSION:
1266 case OPT_SHOWVERSION:
1274 #if defined(ENABLE_VERIFIER)
1280 #if defined(ENABLE_STATISTICS)
1282 opt_getcompilingtime = true;
1283 opt_getloadingtime = true;
1296 for (i = 0; i < strlen(opt_arg); i++) {
1297 switch (opt_arg[i]) {
1299 checkbounds = false;
1312 makeinitializations = false;
1315 #if !defined(NDEBUG)
1319 makeinitializations = false;
1324 opt_method = opt_arg;
1325 makeinitializations = false;
1329 opt_signature = opt_arg;
1333 case OPT_SHOW: /* Display options */
1334 for (i = 0; i < strlen(opt_arg); i++) {
1335 switch (opt_arg[i]) {
1337 showconstantpool = true;
1349 opt_showintermediate = true;
1350 compileverbose = true;
1353 #if defined(ENABLE_DISASSEMBLER)
1355 opt_showdisassemble = true;
1356 compileverbose = true;
1360 opt_shownops = true;
1364 opt_showexceptionstubs = true;
1368 opt_shownativestub = true;
1373 opt_showddatasegment = true;
1382 #if defined(ENABLE_LOOP)
1388 #if defined(ENABLE_INLINING)
1389 #if defined(ENABLE_INLINING_DEBUG)
1390 case OPT_INLINE_DEBUG_ALL:
1391 opt_inline_debug_all = true;
1393 case OPT_INLINE_DEBUG_END:
1394 opt_inline_debug_end_counter = atoi(opt_arg);
1396 case OPT_INLINE_DEBUG_MIN:
1397 opt_inline_debug_min_size = atoi(opt_arg);
1399 case OPT_INLINE_DEBUG_MAX:
1400 opt_inline_debug_max_size = atoi(opt_arg);
1402 #endif /* defined(ENABLE_INLINING_DEBUG) */
1403 #if !defined(NDEBUG)
1404 case OPT_INLINE_LOG:
1405 opt_inline_debug_log = true;
1407 #endif /* !defined(NDEBUG) */
1410 opt_inlining = true;
1412 #endif /* defined(ENABLE_INLINING) */
1414 #if defined(ENABLE_IFCONV)
1420 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1435 options_xx(opt_arg);
1439 /* currently ignored */
1443 /* currently ignored */
1447 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = true;
1451 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = false;
1454 #if defined(ENABLE_PROFILING)
1455 case OPT_PROF_OPTION:
1456 /* use <= to get the last \0 too */
1458 for (i = 0, j = 0; i <= strlen(opt_arg); i++) {
1459 if (opt_arg[i] == ',')
1462 if (opt_arg[i] == '\0') {
1463 if (strcmp("bb", opt_arg + j) == 0)
1467 printf("Unknown option: -Xprof:%s\n", opt_arg + j);
1471 /* set k to next char */
1484 #if defined(ENABLE_JIT)
1487 printf("-Xjit option not enabled.\n");
1493 #if defined(ENABLE_INTRP)
1496 printf("-Xint option not enabled.\n");
1501 #if defined(ENABLE_INTRP)
1502 case OPT_STATIC_SUPERS:
1503 opt_static_supers = atoi(opt_arg);
1506 case OPT_NO_DYNAMIC:
1507 opt_no_dynamic = true;
1510 case OPT_NO_REPLICATION:
1511 opt_no_replication = true;
1514 case OPT_NO_QUICKSUPER:
1515 opt_no_quicksuper = true;
1523 #if defined(ENABLE_DEBUG_FILTER)
1524 case OPT_FILTER_VERBOSECALL_INCLUDE:
1525 opt_filter_verbosecall_include = opt_arg;
1528 case OPT_FILTER_VERBOSECALL_EXCLUDE:
1529 opt_filter_verbosecall_exclude = opt_arg;
1532 case OPT_FILTER_SHOW_METHOD:
1533 opt_filter_show_method = opt_arg;
1538 printf("Unknown option: %s\n",
1539 vm_args->options[opt_index].optionString);
1544 /* get the main class *****************************************************/
1546 if (opt_index < vm_args->nOptions) {
1547 mainstring = vm_args->options[opt_index++].optionString;
1549 /* Put the jar file into the classpath (if any). */
1551 if (opt_jar == true) {
1552 /* free old classpath */
1554 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1556 /* put jarfile into classpath */
1558 _Jv_classpath = MNEW(char, strlen(mainstring) + strlen("0"));
1560 strcpy(_Jv_classpath, mainstring);
1562 #if defined(ENABLE_JAVASE)
1563 properties_add("java.class.path", _Jv_classpath);
1567 /* replace .'s with /'s in classname */
1569 for (i = strlen(mainstring) - 1; i >= 0; i--)
1570 if (mainstring[i] == '.')
1571 mainstring[i] = '/';
1575 #if defined(ENABLE_JVMTI)
1577 jvmti_set_phase(JVMTI_PHASE_ONLOAD);
1578 jvmti_agentload(agentarg, agentbypath, &handle, &libname);
1581 MFREE(agentarg, char, strlen(agentarg));
1583 jvmti_set_phase(JVMTI_PHASE_PRIMORDIAL);
1587 /* initialize this JVM ****************************************************/
1589 vm_initializing = true;
1591 /* initialize the garbage collector */
1593 gc_init(opt_heapmaxsize, opt_heapstartsize);
1595 #if defined(ENABLE_THREADS)
1596 /* AFTER: gc_init (directly after, as this initializes the
1597 stopworldlock lock */
1602 /* install architecture dependent signal handlers */
1605 vm_abort("vm_create: signal_init failed");
1607 #if defined(ENABLE_INTRP)
1608 /* Allocate main thread stack on the Java heap. */
1611 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1612 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1616 /* AFTER: threads_preinit */
1619 vm_abort("vm_create: string_init failed");
1621 /* AFTER: threads_preinit */
1624 vm_abort("vm_create: utf8_init failed");
1626 /* AFTER: thread_preinit */
1629 vm_abort("vm_create: suck_init failed");
1631 suck_add_from_property("java.endorsed.dirs");
1633 /* Now we have all options handled and we can print the version
1636 AFTER: suck_add_from_property("java.endorsed.dirs"); */
1641 /* AFTER: utf8_init */
1643 suck_add(_Jv_bootclasspath);
1645 /* initialize the classcache hashtable stuff: lock, hashtable
1646 (must be done _after_ threads_preinit) */
1648 if (!classcache_init())
1649 vm_abort("vm_create: classcache_init failed");
1651 /* initialize the memory subsystem (must be done _after_
1655 vm_abort("vm_create: memory_init failed");
1657 /* initialize the finalizer stuff (must be done _after_
1660 if (!finalizer_init())
1661 vm_abort("vm_create: finalizer_init failed");
1663 /* initialize the codegen subsystems */
1667 /* initializes jit compiler */
1671 /* machine dependent initialization */
1673 #if defined(ENABLE_JIT)
1674 # if defined(ENABLE_INTRP)
1684 /* initialize the loader subsystems (must be done _after_
1688 vm_abort("vm_create: loader_init failed");
1690 /* Link some important VM classes. */
1691 /* AFTER: utf8_init */
1694 vm_abort("vm_create: linker_init failed");
1696 if (!primitive_init())
1697 vm_abort("vm_create: primitive_init failed");
1699 if (!exceptions_init())
1700 vm_abort("vm_create: exceptions_init failed");
1702 if (!builtin_init())
1703 vm_abort("vm_create: builtin_init failed");
1705 /* Initialize the native subsystem. */
1706 /* BEFORE: threads_init */
1709 vm_abort("vm_create: native_init failed");
1711 /* Register the native methods implemented in the VM. */
1712 /* BEFORE: threads_init */
1714 if (!nativevm_preinit())
1715 vm_abort("vm_create: nativevm_preinit failed");
1717 #if defined(ENABLE_JNI)
1718 /* Initialize the JNI subsystem (must be done _before_
1719 threads_init, as threads_init can call JNI methods
1720 (e.g. NewGlobalRef). */
1723 vm_abort("vm_create: jni_init failed");
1726 #if defined(ENABLE_THREADS)
1727 if (!threads_init())
1728 vm_abort("vm_create: threads_init failed");
1731 /* Initialize the native VM subsystem. */
1732 /* AFTER: threads_init (at least for SUN's classes) */
1734 if (!nativevm_init())
1735 vm_abort("vm_create: nativevm_init failed");
1737 #if defined(ENABLE_PROFILING)
1738 /* initialize profiling */
1740 if (!profile_init())
1741 vm_abort("vm_create: profile_init failed");
1744 #if defined(ENABLE_THREADS)
1745 /* initialize recompilation */
1747 if (!recompile_init())
1748 vm_abort("vm_create: recompile_init failed");
1750 /* start the signal handler thread */
1752 #if defined(__LINUX__)
1753 /* XXX Remove for exact-GC. */
1754 if (threads_pthreads_implementation_nptl)
1756 if (!signal_start_thread())
1757 vm_abort("vm_create: signal_start_thread failed");
1759 /* finally, start the finalizer thread */
1761 if (!finalizer_start_thread())
1762 vm_abort("vm_create: finalizer_start_thread failed");
1764 # if !defined(NDEBUG)
1765 /* start the memory profiling thread */
1767 if (opt_ProfileMemoryUsage || opt_ProfileGCMemoryUsage)
1768 if (!memory_start_thread())
1769 vm_abort("vm_create: memory_start_thread failed");
1772 /* start the recompilation thread (must be done before the
1773 profiling thread) */
1775 if (!recompile_start_thread())
1776 vm_abort("vm_create: recompile_start_thread failed");
1778 # if defined(ENABLE_PROFILING)
1779 /* start the profile sampling thread */
1782 /* if (!profile_start_thread()) */
1783 /* vm_abort("vm_create: profile_start_thread failed"); */
1787 #if defined(ENABLE_JVMTI)
1788 # if defined(ENABLE_GC_CACAO)
1789 /* XXX this will not work with the new indirection cells for classloaders!!! */
1793 /* add agent library to native library hashtable */
1794 native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
1798 /* increment the number of VMs */
1802 /* initialization is done */
1804 vm_initializing = false;
1806 /* everything's ok */
1812 /* vm_run **********************************************************************
1814 Runs the main-method of the passed class.
1816 *******************************************************************************/
1818 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1821 classinfo *mainclass;
1824 java_handle_objectarray_t *oa;
1831 #if !defined(NDEBUG)
1837 if (opt_method != NULL) {
1838 vm_compile_method();
1841 #endif /* !defined(NDEBUG) */
1843 /* should we run the main-method? */
1845 if (mainstring == NULL)
1848 /* set return value to OK */
1852 if (opt_jar == true) {
1853 /* open jar file with java.util.jar.JarFile */
1855 mainstring = vm_get_mainclass_from_jar(mainstring);
1857 if (mainstring == NULL)
1861 /* load the main class */
1863 mainutf = utf_new_char(mainstring);
1865 #if defined(ENABLE_JAVAME_CLDC1_1)
1866 mainclass = load_class_bootstrap(mainutf);
1868 mainclass = load_class_from_sysloader(mainutf);
1871 /* error loading class */
1873 e = exceptions_get_and_clear_exception();
1875 if ((e != NULL) || (mainclass == NULL)) {
1876 exceptions_throw_noclassdeffounderror_cause(e);
1877 exceptions_print_stacktrace();
1881 if (!link_class(mainclass)) {
1882 exceptions_print_stacktrace();
1886 /* find the `main' method of the main class */
1888 m = class_resolveclassmethod(mainclass,
1889 utf_new_char("main"),
1890 utf_new_char("([Ljava/lang/String;)V"),
1891 class_java_lang_Object,
1894 if (exceptions_get_exception()) {
1895 exceptions_print_stacktrace();
1899 /* there is no main method or it isn't static */
1901 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
1902 exceptions_clear_exception();
1903 exceptions_throw_nosuchmethoderror(mainclass,
1904 utf_new_char("main"),
1905 utf_new_char("([Ljava/lang/String;)V"));
1907 exceptions_print_stacktrace();
1911 /* build argument array */
1913 oalength = vm_args->nOptions - opt_index;
1915 oa = builtin_anewarray(oalength, class_java_lang_String);
1917 for (i = 0; i < oalength; i++) {
1918 u = utf_new_char(vm_args->options[opt_index + i].optionString);
1919 s = javastring_new(u);
1921 LLNI_objectarray_element_set(oa, i, s);
1924 #ifdef TYPEINFO_DEBUG_TEST
1925 /* test the typeinfo system */
1928 /*class_showmethods(currentThread->group->header.vftbl->class); */
1930 #if defined(ENABLE_JVMTI)
1931 jvmti_set_phase(JVMTI_PHASE_LIVE);
1934 /* set ThreadMXBean variables */
1936 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
1937 _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
1939 if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
1940 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
1941 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
1942 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
1944 /* start the main thread */
1946 (void) vm_call_method(m, NULL, oa);
1948 /* exception occurred? */
1950 if (exceptions_get_exception()) {
1951 exceptions_print_stacktrace();
1955 /* unload the JavaVM */
1957 (void) vm_destroy(vm);
1965 /* vm_destroy ******************************************************************
1967 Unloads a Java VM and reclaims its resources.
1969 *******************************************************************************/
1971 s4 vm_destroy(JavaVM *vm)
1973 #if defined(ENABLE_THREADS)
1974 threads_join_all_threads();
1977 /* everything's ok */
1983 /* vm_exit *********************************************************************
1985 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1987 *******************************************************************************/
1989 void vm_exit(s4 status)
1993 /* signal that we are exiting */
1997 assert(class_java_lang_System);
1998 assert(class_java_lang_System->state & CLASS_LOADED);
2000 #if defined(ENABLE_JVMTI)
2001 if (jvmti || (dbgcom!=NULL)) {
2002 jvmti_set_phase(JVMTI_PHASE_DEAD);
2003 if (jvmti) jvmti_agentunload();
2007 if (!link_class(class_java_lang_System)) {
2008 exceptions_print_stacktrace();
2012 /* call java.lang.System.exit(I)V */
2014 m = class_resolveclassmethod(class_java_lang_System,
2015 utf_new_char("exit"),
2017 class_java_lang_Object,
2021 exceptions_print_stacktrace();
2025 /* call the exit function with passed exit status */
2027 (void) vm_call_method(m, NULL, status);
2029 /* If we had an exception, just ignore the exception and exit with
2032 vm_shutdown(status);
2036 /* vm_shutdown *****************************************************************
2038 Terminates the system immediately without freeing memory explicitly
2039 (to be used only for abnormal termination).
2041 *******************************************************************************/
2043 void vm_shutdown(s4 status)
2046 #if defined(ENABLE_STATISTICS)
2047 || opt_getcompilingtime || opt_stat
2051 log_text("CACAO terminated by shutdown");
2052 dolog("Exit status: %d\n", (s4) status);
2056 #if defined(ENABLE_JVMTI)
2057 /* terminate cacaodbgserver */
2059 pthread_mutex_lock(&dbgcomlock);
2061 pthread_mutex_unlock(&dbgcomlock);
2062 jvmti_cacaodbgserver_quit();
2070 /* vm_exit_handler *************************************************************
2072 The exit_handler function is called upon program termination.
2074 ATTENTION: Don't free system resources here! Some threads may still
2075 be running as this is called from VMRuntime.exit(). The OS does the
2078 *******************************************************************************/
2080 void vm_exit_handler(void)
2082 #if !defined(NDEBUG)
2084 class_showmethods(mainclass);
2086 if (showconstantpool)
2087 class_showconstantpool(mainclass);
2092 # if defined(ENABLE_PROFILING)
2094 profile_printstats();
2096 #endif /* !defined(NDEBUG) */
2098 #if defined(ENABLE_RT_TIMING)
2099 rt_timing_print_time_stats(stderr);
2102 #if defined(ENABLE_CYCLES_STATS)
2103 builtin_print_cycles_stats(stderr);
2104 stacktrace_print_cycles_stats(stderr);
2108 #if defined(ENABLE_STATISTICS)
2109 || opt_getcompilingtime || opt_stat
2113 log_text("CACAO terminated");
2115 #if defined(ENABLE_STATISTICS)
2118 #ifdef TYPECHECK_STATISTICS
2119 typecheck_print_statistics(get_logfile());
2123 if (opt_getcompilingtime)
2125 #endif /* defined(ENABLE_STATISTICS) */
2127 /* vm_print_profile(stderr);*/
2131 /* vm_abort ********************************************************************
2133 Prints an error message and aborts the VM.
2135 *******************************************************************************/
2137 void vm_abort(const char *text, ...)
2141 /* print the log message */
2146 log_vprint(text, ap);
2151 /* now abort the VM */
2157 /* vm_get_mainclass_from_jar ***************************************************
2159 Gets the name of the main class from a JAR's manifest file.
2161 *******************************************************************************/
2163 static char *vm_get_mainclass_from_jar(char *mainstring)
2170 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
2173 exceptions_print_stacktrace();
2177 /* create JarFile object */
2182 exceptions_print_stacktrace();
2186 m = class_resolveclassmethod(c,
2188 utf_java_lang_String__void,
2189 class_java_lang_Object,
2193 exceptions_print_stacktrace();
2197 s = javastring_new_from_ascii(mainstring);
2199 (void) vm_call_method(m, o, s);
2201 if (exceptions_get_exception()) {
2202 exceptions_print_stacktrace();
2206 /* get manifest object */
2208 m = class_resolveclassmethod(c,
2209 utf_new_char("getManifest"),
2210 utf_new_char("()Ljava/util/jar/Manifest;"),
2211 class_java_lang_Object,
2215 exceptions_print_stacktrace();
2219 o = vm_call_method(m, o);
2222 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
2227 /* get Main Attributes */
2229 m = class_resolveclassmethod(o->vftbl->class,
2230 utf_new_char("getMainAttributes"),
2231 utf_new_char("()Ljava/util/jar/Attributes;"),
2232 class_java_lang_Object,
2236 exceptions_print_stacktrace();
2240 o = vm_call_method(m, o);
2243 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
2248 /* get property Main-Class */
2250 m = class_resolveclassmethod(o->vftbl->class,
2251 utf_new_char("getValue"),
2252 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
2253 class_java_lang_Object,
2257 exceptions_print_stacktrace();
2261 s = javastring_new_from_ascii("Main-Class");
2263 o = vm_call_method(m, o, s);
2266 exceptions_print_stacktrace();
2270 return javastring_tochar(o);
2274 /* vm_compile_all **************************************************************
2276 Compile all methods found in the bootclasspath.
2278 *******************************************************************************/
2280 #if !defined(NDEBUG)
2281 static void vm_compile_all(void)
2286 classcache_name_entry *nmen;
2287 classcache_class_entry *clsen;
2290 /* create all classes found in the bootclasspath */
2291 /* XXX currently only works with zip/jar's */
2293 loader_load_all_classes();
2295 /* link all classes */
2297 for (slot = 0; slot < hashtable_classcache.size; slot++) {
2298 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
2300 for (; nmen; nmen = nmen->hashlink) {
2301 /* iterate over all class entries */
2303 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
2304 c = clsen->classobj;
2309 if (!(c->state & CLASS_LINKED)) {
2310 if (!link_class(c)) {
2311 fprintf(stderr, "Error linking: ");
2312 utf_fprint_printable_ascii_classname(stderr, c->name);
2313 fprintf(stderr, "\n");
2315 /* print out exception and cause */
2317 exceptions_print_current_exception();
2319 /* goto next class */
2325 /* compile all class methods */
2327 for (i = 0; i < c->methodscount; i++) {
2328 m = &(c->methods[i]);
2330 if (m->jcode != NULL) {
2331 if (!jit_compile(m)) {
2332 fprintf(stderr, "Error compiling: ");
2333 utf_fprint_printable_ascii_classname(stderr, c->name);
2334 fprintf(stderr, ".");
2335 utf_fprint_printable_ascii(stderr, m->name);
2336 utf_fprint_printable_ascii(stderr, m->descriptor);
2337 fprintf(stderr, "\n");
2339 /* print out exception and cause */
2341 exceptions_print_current_exception();
2349 #endif /* !defined(NDEBUG) */
2352 /* vm_compile_method ***********************************************************
2354 Compile a specific method.
2356 *******************************************************************************/
2358 #if !defined(NDEBUG)
2359 static void vm_compile_method(void)
2363 /* create, load and link the main class */
2365 mainclass = load_class_bootstrap(utf_new_char(mainstring));
2367 if (mainclass == NULL)
2368 exceptions_print_stacktrace();
2370 if (!link_class(mainclass))
2371 exceptions_print_stacktrace();
2373 if (opt_signature != NULL) {
2374 m = class_resolveclassmethod(mainclass,
2375 utf_new_char(opt_method),
2376 utf_new_char(opt_signature),
2381 m = class_resolveclassmethod(mainclass,
2382 utf_new_char(opt_method),
2389 vm_abort("vm_compile_method: java.lang.NoSuchMethodException: %s.%s",
2390 opt_method, opt_signature ? opt_signature : "");
2394 #endif /* !defined(NDEBUG) */
2397 /* vm_array_store_int **********************************************************
2399 Helper function to store an integer into the argument array, taking
2400 care of architecture specific issues.
2402 *******************************************************************************/
2404 static void vm_array_store_int(uint64_t *array, paramdesc *pd, int32_t value)
2408 if (!pd->inmemory) {
2410 array[index] = (int64_t) value;
2413 index = ARG_CNT + pd->index;
2414 #if SIZEOF_VOID_P == 8
2415 array[index] = (int64_t) value;
2417 # if WORDS_BIGENDIAN == 1
2418 array[index] = ((int64_t) value) << 32;
2420 array[index] = (int64_t) value;
2427 /* vm_array_store_lng **********************************************************
2429 Helper function to store a long into the argument array, taking
2430 care of architecture specific issues.
2432 *******************************************************************************/
2434 static void vm_array_store_lng(uint64_t *array, paramdesc *pd, int64_t value)
2438 #if SIZEOF_VOID_P == 8
2442 index = ARG_CNT + pd->index;
2444 array[index] = value;
2446 if (!pd->inmemory) {
2447 /* move low and high 32-bits into it's own argument slot */
2449 index = GET_LOW_REG(pd->index);
2450 array[index] = value & 0x00000000ffffffff;
2452 index = GET_HIGH_REG(pd->index);
2453 array[index] = value >> 32;
2456 index = ARG_CNT + pd->index;
2457 array[index] = value;
2463 /* vm_array_store_flt **********************************************************
2465 Helper function to store a float into the argument array, taking
2466 care of architecture specific issues.
2468 *******************************************************************************/
2470 static void vm_array_store_flt(uint64_t *array, paramdesc *pd, uint64_t value)
2474 if (!pd->inmemory) {
2475 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2478 index = INT_ARG_CNT + pd->index;
2480 #if WORDS_BIGENDIAN == 1 && !defined(__POWERPC__) && !defined(__POWERPC64__) && !defined(__S390__)
2481 array[index] = value >> 32;
2483 array[index] = value;
2487 index = ARG_CNT + pd->index;
2488 #if defined(__SPARC_64__)
2489 array[index] = value >> 32;
2491 array[index] = value;
2497 /* vm_array_store_dbl **********************************************************
2499 Helper function to store a double into the argument array, taking
2500 care of architecture specific issues.
2502 *******************************************************************************/
2504 static void vm_array_store_dbl(uint64_t *array, paramdesc *pd, uint64_t value)
2508 if (!pd->inmemory) {
2509 #if SIZEOF_VOID_P != 8 && defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2510 index = GET_LOW_REG(pd->index);
2511 array[index] = value & 0x00000000ffffffff;
2513 index = GET_HIGH_REG(pd->index);
2514 array[index] = value >> 32;
2516 index = INT_ARG_CNT + pd->index;
2517 array[index] = value;
2521 index = ARG_CNT + pd->index;
2522 array[index] = value;
2527 /* vm_array_store_adr **********************************************************
2529 Helper function to store an address into the argument array, taking
2530 care of architecture specific issues.
2532 *******************************************************************************/
2534 static void vm_array_store_adr(uint64_t *array, paramdesc *pd, void *value)
2538 if (!pd->inmemory) {
2539 #if defined(HAS_ADDRESS_REGISTER_FILE)
2540 /* When the architecture has address registers, place them
2541 after integer and float registers. */
2543 index = INT_ARG_CNT + FLT_ARG_CNT + pd->index;
2547 array[index] = (uint64_t) (intptr_t) value;
2550 index = ARG_CNT + pd->index;
2551 #if SIZEOF_VOID_P == 8
2552 array[index] = (uint64_t) (intptr_t) value;
2554 # if WORDS_BIGENDIAN == 1
2555 array[index] = ((uint64_t) (intptr_t) value) << 32;
2557 array[index] = (uint64_t) (intptr_t) value;
2564 /* vm_array_from_valist ********************************************************
2568 *******************************************************************************/
2570 uint64_t *vm_array_from_valist(methodinfo *m, java_object_t *o, va_list ap)
2579 /* get the descriptors */
2583 td = md->paramtypes;
2585 /* allocate argument array */
2587 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2589 /* if method is non-static fill first block and skip `this' pointer */
2594 /* the `this' pointer */
2595 vm_array_store_adr(array, pd, o);
2602 for (; i < md->paramcount; i++, pd++, td++) {
2605 value.i = va_arg(ap, int32_t);
2606 vm_array_store_int(array, pd, value.i);
2610 value.l = va_arg(ap, int64_t);
2611 vm_array_store_lng(array, pd, value.l);
2615 #if defined(__ALPHA__) || defined(__POWERPC__) || defined(__POWERPC64__)
2616 /* This is required to load the correct float value in
2619 value.d = (double) va_arg(ap, double);
2621 value.f = (float) va_arg(ap, double);
2623 vm_array_store_flt(array, pd, value.l);
2627 value.d = va_arg(ap, double);
2628 vm_array_store_dbl(array, pd, value.l);
2632 value.a = va_arg(ap, void*);
2633 vm_array_store_adr(array, pd, value.a);
2642 /* vm_array_from_jvalue ********************************************************
2646 *******************************************************************************/
2648 static uint64_t *vm_array_from_jvalue(methodinfo *m, java_object_t *o,
2658 /* get the descriptors */
2662 td = md->paramtypes;
2664 /* allocate argument array */
2666 #if defined(HAS_ADDRESS_REGISTER_FILE)
2667 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + ADR_ARG_CNT + md->memuse);
2669 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2672 /* if method is non-static fill first block and skip `this' pointer */
2677 /* the `this' pointer */
2678 vm_array_store_adr(array, pd, o);
2685 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2686 switch (td->decltype) {
2688 vm_array_store_int(array, pd, args[j].i);
2692 vm_array_store_lng(array, pd, args[j].j);
2696 vm_array_store_flt(array, pd, args[j].j);
2700 vm_array_store_dbl(array, pd, args[j].j);
2704 vm_array_store_adr(array, pd, args[j].l);
2713 /* vm_array_from_objectarray ***************************************************
2717 *******************************************************************************/
2719 uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o,
2720 java_handle_objectarray_t *params)
2726 java_handle_t *param;
2733 /* get the descriptors */
2737 td = md->paramtypes;
2739 /* allocate argument array */
2741 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2743 /* if method is non-static fill first block and skip `this' pointer */
2749 vm_array_store_adr(array, pd, o);
2756 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2757 LLNI_objectarray_element_get(params, j, param);
2764 /* convert the value according to its declared type */
2766 c = param->vftbl->class;
2767 type = primitive_type_get_by_wrapperclass(c);
2769 switch (td->decltype) {
2770 case PRIMITIVETYPE_BOOLEAN:
2772 case PRIMITIVETYPE_BOOLEAN:
2773 /* This type is OK. */
2780 case PRIMITIVETYPE_BYTE:
2782 case PRIMITIVETYPE_BYTE:
2783 /* This type is OK. */
2790 case PRIMITIVETYPE_CHAR:
2792 case PRIMITIVETYPE_CHAR:
2793 /* This type is OK. */
2800 case PRIMITIVETYPE_SHORT:
2802 case PRIMITIVETYPE_BYTE:
2803 case PRIMITIVETYPE_SHORT:
2804 /* These types are OK. */
2811 case PRIMITIVETYPE_INT:
2813 case PRIMITIVETYPE_BYTE:
2814 case PRIMITIVETYPE_SHORT:
2815 case PRIMITIVETYPE_INT:
2816 /* These types are OK. */
2824 vm_abort("vm_array_from_objectarray: invalid type %d",
2828 value = primitive_unbox(param);
2829 vm_array_store_int(array, pd, value.i);
2836 c = param->vftbl->class;
2837 type = primitive_type_get_by_wrapperclass(c);
2839 assert(td->decltype == PRIMITIVETYPE_LONG);
2842 case PRIMITIVETYPE_BYTE:
2843 case PRIMITIVETYPE_SHORT:
2844 case PRIMITIVETYPE_INT:
2845 case PRIMITIVETYPE_LONG:
2846 /* These types are OK. */
2852 value = primitive_unbox(param);
2853 vm_array_store_lng(array, pd, value.l);
2860 c = param->vftbl->class;
2861 type = primitive_type_get_by_wrapperclass(c);
2863 assert(td->decltype == PRIMITIVETYPE_FLOAT);
2866 case PRIMITIVETYPE_FLOAT:
2867 /* This type is OK. */
2873 value = primitive_unbox(param);
2874 vm_array_store_flt(array, pd, value.l);
2881 c = param->vftbl->class;
2882 type = primitive_type_get_by_wrapperclass(c);
2884 assert(td->decltype == PRIMITIVETYPE_DOUBLE);
2887 case PRIMITIVETYPE_FLOAT:
2888 case PRIMITIVETYPE_DOUBLE:
2889 /* These types are OK. */
2895 value = primitive_unbox(param);
2896 vm_array_store_dbl(array, pd, value.l);
2900 if (!resolve_class_from_typedesc(td, true, true, &c))
2903 if (param != NULL) {
2904 if (td->arraydim > 0) {
2905 if (!builtin_arrayinstanceof(param, c))
2909 if (!builtin_instanceof(param, c))
2914 vm_array_store_adr(array, pd, param);
2918 vm_abort("vm_array_from_objectarray: invalid type %d", td->type);
2925 exceptions_throw_illegalargumentexception();
2930 /* vm_call_method **************************************************************
2932 Calls a Java method with a variable number of arguments.
2934 *******************************************************************************/
2936 #define VM_CALL_METHOD(name, type) \
2937 type vm_call_method##name(methodinfo *m, java_handle_t *o, ...) \
2943 value = vm_call_method##name##_valist(m, o, ap); \
2949 VM_CALL_METHOD(, java_handle_t *)
2950 VM_CALL_METHOD(_int, int32_t)
2951 VM_CALL_METHOD(_long, int64_t)
2952 VM_CALL_METHOD(_float, float)
2953 VM_CALL_METHOD(_double, double)
2956 /* vm_call_method_valist *******************************************************
2958 Calls a Java method with a variable number of arguments, passed via
2961 *******************************************************************************/
2963 #define VM_CALL_METHOD_VALIST(name, type) \
2964 type vm_call_method##name##_valist(methodinfo *m, java_handle_t *o, \
2971 dumpsize = dump_size(); \
2972 array = vm_array_from_valist(m, o, ap); \
2973 value = vm_call##name##_array(m, array); \
2974 dump_release(dumpsize); \
2979 VM_CALL_METHOD_VALIST(, java_handle_t *)
2980 VM_CALL_METHOD_VALIST(_int, int32_t)
2981 VM_CALL_METHOD_VALIST(_long, int64_t)
2982 VM_CALL_METHOD_VALIST(_float, float)
2983 VM_CALL_METHOD_VALIST(_double, double)
2986 /* vm_call_method_jvalue *******************************************************
2988 Calls a Java method with a variable number of arguments, passed via
2991 *******************************************************************************/
2993 #define VM_CALL_METHOD_JVALUE(name, type) \
2994 type vm_call_method##name##_jvalue(methodinfo *m, java_handle_t *o, \
2995 const jvalue *args) \
3001 dumpsize = dump_size(); \
3002 array = vm_array_from_jvalue(m, o, args); \
3003 value = vm_call##name##_array(m, array); \
3004 dump_release(dumpsize); \
3009 VM_CALL_METHOD_JVALUE(, java_handle_t *)
3010 VM_CALL_METHOD_JVALUE(_int, int32_t)
3011 VM_CALL_METHOD_JVALUE(_long, int64_t)
3012 VM_CALL_METHOD_JVALUE(_float, float)
3013 VM_CALL_METHOD_JVALUE(_double, double)
3016 /* vm_call_array ***************************************************************
3018 Calls a Java method with a variable number of arguments, passed via
3021 *******************************************************************************/
3023 #define VM_CALL_ARRAY(name, type) \
3024 type vm_call##name##_array(methodinfo *m, uint64_t *array) \
3030 md = m->parseddesc; \
3032 if (m->code == NULL) \
3033 if (!jit_compile(m)) \
3036 pv = m->code->entrypoint; \
3038 STATISTICS(count_calls_native_to_java++); \
3040 value = asm_vm_call_method##name(pv, array, md->memuse); \
3045 VM_CALL_ARRAY(, java_handle_t *)
3046 VM_CALL_ARRAY(_int, int32_t)
3047 VM_CALL_ARRAY(_long, int64_t)
3048 VM_CALL_ARRAY(_float, float)
3049 VM_CALL_ARRAY(_double, double)
3053 * These are local overrides for various environment variables in Emacs.
3054 * Please do not remove this and leave it at the end of the file, where
3055 * Emacs will automagically detect them.
3056 * ---------------------------------------------------------------------
3059 * indent-tabs-mode: t