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 8318 2007-08-16 10:05:34Z 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 */
535 static void XXusage(void)
537 puts(" -v write state-information");
539 puts(" -verbose[:jit|threads]");
540 puts(" enable specific verbose output");
541 puts(" -debug-color colored output for ANSI terms");
543 #ifdef TYPECHECK_VERBOSE
544 puts(" -verbosetc write debug messages while typechecking");
546 #if defined(__ALPHA__)
547 puts(" -noieee don't use ieee compliant arithmetic");
549 #if defined(ENABLE_VERIFIER)
550 puts(" -noverify don't verify classfiles");
552 #if defined(ENABLE_STATISTICS)
553 puts(" -time measure the runtime");
554 puts(" -stat detailed compiler statistics");
556 puts(" -log logfile specify a name for the logfile");
557 puts(" -c(heck)b(ounds) don't check array bounds");
558 puts(" s(ync) don't check for synchronization");
559 #if defined(ENABLE_LOOP)
560 puts(" -oloop optimize array accesses in loops");
562 puts(" -l don't start the class after loading");
564 puts(" -all compile all methods, no execution");
565 puts(" -m compile only a specific method");
566 puts(" -sig specify signature for a specific method");
569 puts(" -s... show...");
570 puts(" (c)onstants the constant pool");
571 puts(" (m)ethods class fields and methods");
572 puts(" (u)tf the utf - hash");
573 puts(" (i)ntermediate intermediate representation");
574 #if defined(ENABLE_DISASSEMBLER)
575 puts(" (a)ssembler disassembled listing");
576 puts(" n(o)ps show NOPs in disassembler output");
577 puts(" (e)xceptionstubs disassembled exception stubs (only with -sa)");
578 puts(" (n)ative disassembled native stubs");
580 puts(" (d)atasegment data segment listing");
582 #if defined(ENABLE_INLINING)
583 puts(" -i activate inlining");
585 puts(" -il log inlining");
587 #if defined(ENABLE_INLINING_DEBUG)
588 puts(" -ia use inlining for all methods");
589 puts(" -ii <size> set minimum size for inlined result");
590 puts(" -im <size> set maximum size for inlined result");
591 puts(" -ie <number> stop inlining after the given number of roots");
592 #endif /* defined(ENABLE_INLINING_DEBUG) */
593 #endif /* defined(ENABLE_INLINING) */
595 #if defined(ENABLE_IFCONV)
596 puts(" -ifconv use if-conversion");
598 #if defined(ENABLE_LSRA)
599 puts(" -lsra use linear scan register allocation");
601 #if defined(ENABLE_SSA)
602 puts(" -lsra use linear scan register allocation (with SSA)");
604 #if defined(ENABLE_DEBUG_FILTER)
605 puts(" -XXfi <regex> begin of dynamic scope for verbosecall filter");
606 puts(" -XXfx <regex> end of dynamic scope for verbosecall filter");
607 puts(" -XXfm <regex> filter for show options");
609 /* exit with error code */
615 /* version *********************************************************************
617 Only prints cacao version information.
619 *******************************************************************************/
621 static void version(bool opt_exit)
623 puts("java version \""JAVA_VERSION"\"");
624 puts("CACAO version "VERSION"");
626 puts("Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,");
627 puts("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,");
628 puts("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,");
629 puts("J. Wenninger, Institut f. Computersprachen - TU Wien\n");
631 puts("This program is free software; you can redistribute it and/or");
632 puts("modify it under the terms of the GNU General Public License as");
633 puts("published by the Free Software Foundation; either version 2, or (at");
634 puts("your option) any later version.\n");
636 puts("This program is distributed in the hope that it will be useful, but");
637 puts("WITHOUT ANY WARRANTY; without even the implied warranty of");
638 puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU");
639 puts("General Public License for more details.");
641 /* exit normally, if requested */
648 /* fullversion *****************************************************************
650 Prints a Sun compatible version information (required e.g. by
651 jpackage, www.jpackage.org).
653 *******************************************************************************/
655 static void fullversion(void)
657 puts("java full version \"cacao-"JAVA_VERSION"\"");
665 void vm_printconfig(void)
667 puts("Configure/Build options:\n");
668 puts(" ./configure: "VERSION_CONFIGURE_ARGS"");
669 #if defined(__VERSION__)
670 puts(" CC : "VERSION_CC" ("__VERSION__")");
672 puts(" CC : "VERSION_CC"");
674 puts(" CFLAGS : "VERSION_CFLAGS"\n");
676 puts("Default variables:\n");
677 printf(" maximum heap size : %d\n", HEAP_MAXSIZE);
678 printf(" initial heap size : %d\n", HEAP_STARTSIZE);
679 printf(" stack size : %d\n", STACK_SIZE);
680 #if defined(WITH_CLASSPATH_GNU)
681 puts(" java.boot.class.path : "CACAO_VM_ZIP":"CLASSPATH_CLASSES"");
683 puts(" java.boot.class.path : "CLASSPATH_CLASSES"");
685 puts(" gnu.classpath.boot.library.path: "CLASSPATH_LIBDIR"/classpath\n");
687 puts("Runtime variables:\n");
688 printf(" maximum heap size : %d\n", opt_heapmaxsize);
689 printf(" initial heap size : %d\n", opt_heapstartsize);
690 printf(" stack size : %d\n", opt_stacksize);
691 printf(" libjvm.so : %s\n", cacao_libjvm);
692 printf(" java.boot.class.path : %s\n", _Jv_bootclasspath);
693 printf(" gnu.classpath.boot.library.path: %s\n", classpath_libdir);
694 printf(" java.class.path : %s\n", _Jv_classpath);
698 /* forward declarations *******************************************************/
700 static char *vm_get_mainclass_from_jar(char *mainstring);
702 static void vm_compile_all(void);
703 static void vm_compile_method(void);
707 /* vm_createjvm ****************************************************************
709 Implementation for JNI_CreateJavaVM.
711 *******************************************************************************/
713 bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
715 JavaVMInitArgs *_vm_args;
719 /* get the arguments for the new JVM */
721 _vm_args = (JavaVMInitArgs *) vm_args;
723 /* get the VM and Env tables (must be set before vm_create) */
725 env = NEW(_Jv_JNIEnv);
727 #if defined(ENABLE_JNI)
728 env->env = &_Jv_JNINativeInterface;
731 /* XXX Set the global variable. Maybe we should do that differently. */
735 /* create and fill a JavaVM structure */
737 vm = NEW(_Jv_JavaVM);
739 #if defined(ENABLE_JNI)
740 vm->functions = &_Jv_JNIInvokeInterface;
743 /* XXX Set the global variable. Maybe we should do that differently. */
744 /* XXX JVMTI Agents needs a JavaVM */
748 /* actually create the JVM */
750 if (!vm_create(_vm_args))
753 #if defined(ENABLE_JNI)
754 /* setup the local ref table (must be created after vm_create) */
756 if (!localref_table_init())
760 /* now return the values */
762 *p_vm = (JavaVM *) vm;
763 *p_env = (void *) env;
768 /* release allocated memory */
770 FREE(env, _Jv_JNIEnv);
771 FREE(vm, _Jv_JavaVM);
777 /* vm_create *******************************************************************
779 Creates a JVM. Called by vm_createjvm.
781 *******************************************************************************/
783 bool vm_create(JavaVMInitArgs *vm_args)
792 #if defined(ENABLE_JVMTI)
794 char *libname, *agentarg;
795 bool jdwp,agentbypath;
796 jdwp = agentbypath = false;
799 #if defined(ENABLE_VMLOG)
800 vmlog_cacao_init(vm_args);
803 /* check the JNI version requested */
805 switch (vm_args->version) {
806 case JNI_VERSION_1_1:
808 case JNI_VERSION_1_2:
809 case JNI_VERSION_1_4:
815 /* we only support 1 JVM instance */
820 if (atexit(vm_exit_handler))
821 vm_abort("atexit failed: %s\n", strerror(errno));
824 log_text("CACAO started -------------------------------------------------------");
826 /* We need to check if the actual size of a java.lang.Class object
827 is smaller or equal than the assumption made in
828 src/vmcore/class.h. */
830 if (sizeof(java_lang_Class) > sizeof(dummy_java_lang_Class))
831 vm_abort("vm_create: java_lang_Class structure is bigger than classinfo.object (%d > %d)", sizeof(java_lang_Class), sizeof(dummy_java_lang_Class));
833 /* set the VM starttime */
835 _Jv_jvm->starttime = builtin_currenttimemillis();
837 /* get stuff from the environment *****************************************/
839 #if defined(WITH_JRE_LAYOUT)
840 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
842 cacao_prefix = MNEW(char, 4096);
844 if (readlink("/proc/self/exe", cacao_prefix, 4095) == -1)
845 vm_abort("readlink failed: %s\n", strerror(errno));
847 /* get the path of the current executable */
849 cacao_prefix = dirname(cacao_prefix);
851 if ((strlen(cacao_prefix) + strlen("/..") + strlen("0")) > 4096)
852 vm_abort("libjvm name to long for buffer\n");
854 /* concatenate the library name */
856 strcat(cacao_prefix, "/..");
858 /* now set path to libjvm.so */
860 len = strlen(cacao_prefix) + strlen("/lib/libjvm") + strlen("0");
862 cacao_libjvm = MNEW(char, len);
863 strcpy(cacao_libjvm, cacao_prefix);
864 strcat(cacao_libjvm, "/lib/libjvm");
866 /* and finally set the path to GNU Classpath libraries */
868 len = strlen(cacao_prefix) + strlen("/lib/classpath") + strlen("0");
870 classpath_libdir = MNEW(char, len);
871 strcpy(classpath_libdir, cacao_prefix);
872 strcat(classpath_libdir, "/lib/classpath");
874 cacao_prefix = CACAO_PREFIX;
875 cacao_libjvm = CACAO_LIBDIR"/libjvm";
877 # if defined(WITH_CLASSPATH_GNU)
878 classpath_libdir = CLASSPATH_LIBDIR"/classpath";
880 classpath_libdir = CLASSPATH_LIBDIR;
884 /* set the bootclasspath */
886 cp = getenv("BOOTCLASSPATH");
889 _Jv_bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
890 strcpy(_Jv_bootclasspath, cp);
893 #if defined(WITH_JRE_LAYOUT)
895 # if defined(WITH_CLASSPATH_GNU)
896 strlen(cacao_prefix) +
897 strlen("/share/cacao/vm.zip") +
900 strlen(cacao_prefix) +
901 strlen("/share/classpath/glibj.zip") +
904 _Jv_bootclasspath = MNEW(char, len);
905 # if defined(WITH_CLASSPATH_GNU)
906 strcat(_Jv_bootclasspath, cacao_prefix);
907 strcat(_Jv_bootclasspath, "/share/cacao/vm.zip");
908 strcat(_Jv_bootclasspath, ":");
910 strcat(_Jv_bootclasspath, cacao_prefix);
911 strcat(_Jv_bootclasspath, "/share/classpath/glibj.zip");
913 # if defined(WITH_CLASSPATH_GNU)
915 strlen(CACAO_VM_ZIP) +
917 strlen(CLASSPATH_CLASSES) +
919 # elif defined(WITH_CLASSPATH_SUN)
920 /* This is the bootclasspath taken from HotSpot (see
921 hotspot/src/share/vm/runtime/os.cpp
922 (os::set_boot_path)). */
925 strlen(CLASSPATH_PREFIX"/lib/resources.jar:"
926 CLASSPATH_PREFIX"/lib/rt.jar:"
927 CLASSPATH_PREFIX"/lib/sunrsasign.jar:"
928 CLASSPATH_PREFIX"/lib/jsse.jar:"
929 CLASSPATH_PREFIX"/lib/jce.jar:"
930 CLASSPATH_PREFIX"/lib/charsets.jar:"
931 CLASSPATH_PREFIX"/classes") +
933 # elif defined(WITH_CLASSPATH_CLDC1_1)
935 strlen(CLASSPATH_CLASSES) +
938 # error unknown classpath configuration
941 _Jv_bootclasspath = MNEW(char, len);
943 # if defined(WITH_CLASSPATH_GNU)
944 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
945 strcat(_Jv_bootclasspath, ":");
946 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
947 # elif defined(WITH_CLASSPATH_SUN)
948 strcpy(_Jv_bootclasspath,
949 CLASSPATH_PREFIX"/lib/resources.jar:"
950 CLASSPATH_PREFIX"/lib/rt.jar:"
951 CLASSPATH_PREFIX"/lib/sunrsasign.jar:"
952 CLASSPATH_PREFIX"/lib/jsse.jar:"
953 CLASSPATH_PREFIX"/lib/jce.jar:"
954 CLASSPATH_PREFIX"/lib/charsets.jar:"
955 CLASSPATH_PREFIX"/classes");
956 # elif defined(WITH_CLASSPATH_CLDC1_1)
957 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
959 # error unknown classpath configuration
964 /* set the classpath */
966 cp = getenv("CLASSPATH");
969 _Jv_classpath = MNEW(char, strlen(cp) + strlen("0"));
970 strcat(_Jv_classpath, cp);
973 _Jv_classpath = MNEW(char, strlen(".") + strlen("0"));
974 strcpy(_Jv_classpath, ".");
977 /* Get and set java.library.path. */
979 _Jv_java_library_path = getenv("LD_LIBRARY_PATH");
981 if (_Jv_java_library_path == NULL)
982 _Jv_java_library_path = "";
984 /* interpret the options **************************************************/
991 opt_heapmaxsize = HEAP_MAXSIZE;
992 opt_heapstartsize = HEAP_STARTSIZE;
993 opt_stacksize = STACK_SIZE;
996 #if defined(ENABLE_JVMTI)
997 /* initialize JVMTI related **********************************************/
1001 /* Initialize and fill properties before command-line handling. */
1003 if (!properties_init())
1004 vm_abort("vm_create: properties_init failed");
1006 /* Set the classpath properties. */
1008 #if defined(ENABLE_JAVASE)
1009 properties_add("java.boot.class.path", _Jv_bootclasspath);
1010 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1011 properties_add("java.class.path", _Jv_classpath);
1014 /* iterate over all passed options */
1016 while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
1030 #if SIZEOF_VOID_P == 8
1031 puts("Running a 32-bit JVM is not supported on this platform.");
1037 #if SIZEOF_VOID_P == 4
1038 puts("Running a 64-bit JVM is not supported on this platform.");
1044 /* forget old classpath and set the argument as new classpath */
1045 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1047 _Jv_classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
1048 strcpy(_Jv_classpath, opt_arg);
1050 #if defined(ENABLE_JAVASE)
1051 properties_add("java.class.path", _Jv_classpath);
1056 for (i = 0; i < strlen(opt_arg); i++) {
1057 if (opt_arg[i] == '=') {
1059 properties_add(opt_arg, opt_arg + i + 1);
1064 /* if no '=' is given, just create an empty property */
1066 properties_add(opt_arg, "");
1071 case OPT_BOOTCLASSPATH:
1072 /* Forget default bootclasspath and set the argument as
1073 new boot classpath. */
1075 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1077 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
1078 strcpy(_Jv_bootclasspath, opt_arg);
1080 #if defined(ENABLE_JAVASE)
1081 properties_add("java.boot.class.path", _Jv_bootclasspath);
1082 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1086 case OPT_BOOTCLASSPATH_A:
1087 /* append to end of bootclasspath */
1089 len = strlen(_Jv_bootclasspath);
1091 _Jv_bootclasspath = MREALLOC(_Jv_bootclasspath,
1095 strlen(opt_arg) + strlen("0"));
1097 strcat(_Jv_bootclasspath, ":");
1098 strcat(_Jv_bootclasspath, opt_arg);
1100 #if defined(ENABLE_JAVASE)
1101 properties_add("java.boot.class.path", _Jv_bootclasspath);
1102 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1106 case OPT_BOOTCLASSPATH_P:
1107 /* prepend in front of bootclasspath */
1109 cp = _Jv_bootclasspath;
1112 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
1115 strcpy(_Jv_bootclasspath, opt_arg);
1116 strcat(_Jv_bootclasspath, ":");
1117 strcat(_Jv_bootclasspath, cp);
1119 MFREE(cp, char, len);
1121 #if defined(ENABLE_JAVASE)
1122 properties_add("java.boot.class.path", _Jv_bootclasspath);
1123 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1127 case OPT_BOOTCLASSPATH_C:
1128 /* use as Java core library, but prepend VM interface classes */
1130 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1132 len = strlen(CACAO_VM_ZIP) +
1137 _Jv_bootclasspath = MNEW(char, len);
1139 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
1140 strcat(_Jv_bootclasspath, ":");
1141 strcat(_Jv_bootclasspath, opt_arg);
1143 #if defined(ENABLE_JAVASE)
1144 properties_add("java.boot.class.path", _Jv_bootclasspath);
1145 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1149 #if defined(ENABLE_JVMTI)
1151 /* this option exists only for compatibility reasons */
1155 /* I don't know yet what Xnoagent should do. This is only for
1156 compatiblity with eclipse - motse */
1165 strlen(CACAO_LIBDIR) +
1166 strlen("/libjdwp.so=") +
1170 agentarg = MNEW(char, len);
1172 strcpy(agentarg, CACAO_LIBDIR);
1173 strcat(agentarg, "/libjdwp.so=");
1174 strcat(agentarg, &opt_arg[1]);
1191 c = opt_arg[strlen(opt_arg) - 1];
1193 if ((c == 'k') || (c == 'K')) {
1194 j = atoi(opt_arg) * 1024;
1196 } else if ((c == 'm') || (c == 'M')) {
1197 j = atoi(opt_arg) * 1024 * 1024;
1203 opt_heapmaxsize = j;
1204 else if (opt == OPT_MS)
1205 opt_heapstartsize = j;
1216 if (strcmp("class", opt_arg) == 0) {
1217 opt_verboseclass = true;
1219 else if (strcmp("gc", opt_arg) == 0) {
1220 opt_verbosegc = true;
1222 else if (strcmp("jni", opt_arg) == 0) {
1223 opt_verbosejni = true;
1225 #if !defined(NDEBUG)
1226 else if (strcmp("jit", opt_arg) == 0) {
1230 compileverbose = true;
1232 else if (strcmp("threads", opt_arg) == 0) {
1233 opt_verbosethreads = true;
1237 printf("Unknown -verbose option: %s\n", opt_arg);
1242 case OPT_DEBUGCOLOR:
1243 opt_debugcolor = true;
1246 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
1248 opt_typecheckverbose = true;
1257 case OPT_FULLVERSION:
1261 case OPT_SHOWVERSION:
1269 #if defined(ENABLE_VERIFIER)
1275 #if defined(ENABLE_STATISTICS)
1277 opt_getcompilingtime = true;
1278 opt_getloadingtime = true;
1291 for (i = 0; i < strlen(opt_arg); i++) {
1292 switch (opt_arg[i]) {
1294 checkbounds = false;
1307 makeinitializations = false;
1310 #if !defined(NDEBUG)
1314 makeinitializations = false;
1319 opt_method = opt_arg;
1320 makeinitializations = false;
1324 opt_signature = opt_arg;
1328 case OPT_SHOW: /* Display options */
1329 for (i = 0; i < strlen(opt_arg); i++) {
1330 switch (opt_arg[i]) {
1332 showconstantpool = true;
1344 opt_showintermediate = true;
1345 compileverbose = true;
1348 #if defined(ENABLE_DISASSEMBLER)
1350 opt_showdisassemble = true;
1351 compileverbose = true;
1355 opt_shownops = true;
1359 opt_showexceptionstubs = true;
1363 opt_shownativestub = true;
1368 opt_showddatasegment = true;
1377 #if defined(ENABLE_LOOP)
1383 #if defined(ENABLE_INLINING)
1384 #if defined(ENABLE_INLINING_DEBUG)
1385 case OPT_INLINE_DEBUG_ALL:
1386 opt_inline_debug_all = true;
1388 case OPT_INLINE_DEBUG_END:
1389 opt_inline_debug_end_counter = atoi(opt_arg);
1391 case OPT_INLINE_DEBUG_MIN:
1392 opt_inline_debug_min_size = atoi(opt_arg);
1394 case OPT_INLINE_DEBUG_MAX:
1395 opt_inline_debug_max_size = atoi(opt_arg);
1397 #endif /* defined(ENABLE_INLINING_DEBUG) */
1398 #if !defined(NDEBUG)
1399 case OPT_INLINE_LOG:
1400 opt_inline_debug_log = true;
1402 #endif /* !defined(NDEBUG) */
1405 opt_inlining = true;
1407 #endif /* defined(ENABLE_INLINING) */
1409 #if defined(ENABLE_IFCONV)
1415 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1430 options_xx(opt_arg);
1434 /* currently ignored */
1438 /* currently ignored */
1442 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = true;
1446 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = false;
1449 #if defined(ENABLE_PROFILING)
1450 case OPT_PROF_OPTION:
1451 /* use <= to get the last \0 too */
1453 for (i = 0, j = 0; i <= strlen(opt_arg); i++) {
1454 if (opt_arg[i] == ',')
1457 if (opt_arg[i] == '\0') {
1458 if (strcmp("bb", opt_arg + j) == 0)
1462 printf("Unknown option: -Xprof:%s\n", opt_arg + j);
1466 /* set k to next char */
1479 #if defined(ENABLE_JIT)
1482 printf("-Xjit option not enabled.\n");
1488 #if defined(ENABLE_INTRP)
1491 printf("-Xint option not enabled.\n");
1496 #if defined(ENABLE_INTRP)
1497 case OPT_STATIC_SUPERS:
1498 opt_static_supers = atoi(opt_arg);
1501 case OPT_NO_DYNAMIC:
1502 opt_no_dynamic = true;
1505 case OPT_NO_REPLICATION:
1506 opt_no_replication = true;
1509 case OPT_NO_QUICKSUPER:
1510 opt_no_quicksuper = true;
1518 #if defined(ENABLE_DEBUG_FILTER)
1519 case OPT_FILTER_VERBOSECALL_INCLUDE:
1520 opt_filter_verbosecall_include = opt_arg;
1523 case OPT_FILTER_VERBOSECALL_EXCLUDE:
1524 opt_filter_verbosecall_exclude = opt_arg;
1527 case OPT_FILTER_SHOW_METHOD:
1528 opt_filter_show_method = opt_arg;
1533 printf("Unknown option: %s\n",
1534 vm_args->options[opt_index].optionString);
1539 /* get the main class *****************************************************/
1541 if (opt_index < vm_args->nOptions) {
1542 mainstring = vm_args->options[opt_index++].optionString;
1544 /* Put the jar file into the classpath (if any). */
1546 if (opt_jar == true) {
1547 /* free old classpath */
1549 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1551 /* put jarfile into classpath */
1553 _Jv_classpath = MNEW(char, strlen(mainstring) + strlen("0"));
1555 strcpy(_Jv_classpath, mainstring);
1557 #if defined(ENABLE_JAVASE)
1558 properties_add("java.class.path", _Jv_classpath);
1562 /* replace .'s with /'s in classname */
1564 for (i = strlen(mainstring) - 1; i >= 0; i--)
1565 if (mainstring[i] == '.')
1566 mainstring[i] = '/';
1570 #if defined(ENABLE_JVMTI)
1572 jvmti_set_phase(JVMTI_PHASE_ONLOAD);
1573 jvmti_agentload(agentarg, agentbypath, &handle, &libname);
1576 MFREE(agentarg, char, strlen(agentarg));
1578 jvmti_set_phase(JVMTI_PHASE_PRIMORDIAL);
1582 /* initialize this JVM ****************************************************/
1584 vm_initializing = true;
1586 /* initialize the garbage collector */
1588 gc_init(opt_heapmaxsize, opt_heapstartsize);
1590 #if defined(ENABLE_THREADS)
1591 /* AFTER: gc_init (directly after, as this initializes the
1592 stopworldlock lock */
1597 /* install architecture dependent signal handlers */
1600 vm_abort("vm_create: signal_init failed");
1602 #if defined(ENABLE_INTRP)
1603 /* Allocate main thread stack on the Java heap. */
1606 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1607 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1611 /* AFTER: threads_preinit */
1614 vm_abort("vm_create: string_init failed");
1616 /* AFTER: threads_preinit */
1619 vm_abort("vm_create: utf8_init failed");
1621 /* AFTER: thread_preinit */
1624 vm_abort("vm_create: suck_init failed");
1626 suck_add_from_property("java.endorsed.dirs");
1628 /* Now we have all options handled and we can print the version
1631 AFTER: suck_add_from_property("java.endorsed.dirs"); */
1636 /* AFTER: utf8_init */
1638 suck_add(_Jv_bootclasspath);
1640 /* initialize the classcache hashtable stuff: lock, hashtable
1641 (must be done _after_ threads_preinit) */
1643 if (!classcache_init())
1644 vm_abort("vm_create: classcache_init failed");
1646 /* initialize the memory subsystem (must be done _after_
1650 vm_abort("vm_create: memory_init failed");
1652 /* initialize the finalizer stuff (must be done _after_
1655 if (!finalizer_init())
1656 vm_abort("vm_create: finalizer_init failed");
1658 /* initialize the codegen subsystems */
1662 /* initializes jit compiler */
1666 /* machine dependent initialization */
1668 #if defined(ENABLE_JIT)
1669 # if defined(ENABLE_INTRP)
1679 /* initialize the loader subsystems (must be done _after_
1683 vm_abort("vm_create: loader_init failed");
1685 /* Link some important VM classes. */
1686 /* AFTER: utf8_init */
1689 vm_abort("vm_create: linker_init failed");
1691 if (!primitive_init())
1692 vm_abort("vm_create: primitive_init failed");
1694 if (!exceptions_init())
1695 vm_abort("vm_create: exceptions_init failed");
1697 if (!builtin_init())
1698 vm_abort("vm_create: builtin_init failed");
1700 /* Initialize the native subsystem. */
1701 /* BEFORE: threads_init */
1704 vm_abort("vm_create: native_init failed");
1706 /* Register the native methods implemented in the VM. */
1707 /* BEFORE: threads_init */
1709 if (!nativevm_preinit())
1710 vm_abort("vm_create: nativevm_preinit failed");
1712 #if defined(ENABLE_JNI)
1713 /* Initialize the JNI subsystem (must be done _before_
1714 threads_init, as threads_init can call JNI methods
1715 (e.g. NewGlobalRef). */
1718 vm_abort("vm_create: jni_init failed");
1721 #if defined(ENABLE_THREADS)
1722 if (!threads_init())
1723 vm_abort("vm_create: threads_init failed");
1726 /* Initialize the native VM subsystem. */
1727 /* AFTER: threads_init (at least for SUN's classes) */
1729 if (!nativevm_init())
1730 vm_abort("vm_create: nativevm_init failed");
1732 #if defined(ENABLE_PROFILING)
1733 /* initialize profiling */
1735 if (!profile_init())
1736 vm_abort("vm_create: profile_init failed");
1739 #if defined(ENABLE_THREADS)
1740 /* initialize recompilation */
1742 if (!recompile_init())
1743 vm_abort("vm_create: recompile_init failed");
1745 /* start the signal handler thread */
1747 #if defined(__LINUX__)
1748 /* XXX Remove for exact-GC. */
1749 if (threads_pthreads_implementation_nptl)
1751 if (!signal_start_thread())
1752 vm_abort("vm_create: signal_start_thread failed");
1754 /* finally, start the finalizer thread */
1756 if (!finalizer_start_thread())
1757 vm_abort("vm_create: finalizer_start_thread failed");
1759 # if !defined(NDEBUG)
1760 /* start the memory profiling thread */
1762 if (opt_ProfileMemoryUsage || opt_ProfileGCMemoryUsage)
1763 if (!memory_start_thread())
1764 vm_abort("vm_create: memory_start_thread failed");
1767 /* start the recompilation thread (must be done before the
1768 profiling thread) */
1770 if (!recompile_start_thread())
1771 vm_abort("vm_create: recompile_start_thread failed");
1773 # if defined(ENABLE_PROFILING)
1774 /* start the profile sampling thread */
1777 /* if (!profile_start_thread()) */
1778 /* vm_abort("vm_create: profile_start_thread failed"); */
1782 #if defined(ENABLE_JVMTI)
1784 /* add agent library to native library hashtable */
1785 native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
1789 /* increment the number of VMs */
1793 /* initialization is done */
1795 vm_initializing = false;
1797 /* everything's ok */
1803 /* vm_run **********************************************************************
1805 Runs the main-method of the passed class.
1807 *******************************************************************************/
1809 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1812 classinfo *mainclass;
1815 java_handle_objectarray_t *oa;
1822 #if !defined(NDEBUG)
1828 if (opt_method != NULL) {
1829 vm_compile_method();
1832 #endif /* !defined(NDEBUG) */
1834 /* should we run the main-method? */
1836 if (mainstring == NULL)
1839 /* set return value to OK */
1843 if (opt_jar == true) {
1844 /* open jar file with java.util.jar.JarFile */
1846 mainstring = vm_get_mainclass_from_jar(mainstring);
1848 if (mainstring == NULL)
1852 /* load the main class */
1854 mainutf = utf_new_char(mainstring);
1856 #if defined(ENABLE_JAVAME_CLDC1_1)
1857 mainclass = load_class_bootstrap(mainutf);
1859 mainclass = load_class_from_sysloader(mainutf);
1862 /* error loading class */
1864 e = exceptions_get_and_clear_exception();
1866 if ((e != NULL) || (mainclass == NULL)) {
1867 exceptions_throw_noclassdeffounderror_cause(e);
1868 exceptions_print_stacktrace();
1872 if (!link_class(mainclass)) {
1873 exceptions_print_stacktrace();
1877 /* find the `main' method of the main class */
1879 m = class_resolveclassmethod(mainclass,
1880 utf_new_char("main"),
1881 utf_new_char("([Ljava/lang/String;)V"),
1882 class_java_lang_Object,
1885 if (exceptions_get_exception()) {
1886 exceptions_print_stacktrace();
1890 /* there is no main method or it isn't static */
1892 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
1893 exceptions_clear_exception();
1894 exceptions_throw_nosuchmethoderror(mainclass,
1895 utf_new_char("main"),
1896 utf_new_char("([Ljava/lang/String;)V"));
1898 exceptions_print_stacktrace();
1902 /* build argument array */
1904 oalength = vm_args->nOptions - opt_index;
1906 oa = builtin_anewarray(oalength, class_java_lang_String);
1908 for (i = 0; i < oalength; i++) {
1909 u = utf_new_char(vm_args->options[opt_index + i].optionString);
1910 s = javastring_new(u);
1912 LLNI_objectarray_element_set(oa, i, s);
1915 #ifdef TYPEINFO_DEBUG_TEST
1916 /* test the typeinfo system */
1919 /*class_showmethods(currentThread->group->header.vftbl->class); */
1921 #if defined(ENABLE_JVMTI)
1922 jvmti_set_phase(JVMTI_PHASE_LIVE);
1925 /* set ThreadMXBean variables */
1927 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
1928 _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
1930 if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
1931 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
1932 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
1933 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
1935 /* start the main thread */
1937 (void) vm_call_method(m, NULL, oa);
1939 /* exception occurred? */
1941 if (exceptions_get_exception()) {
1942 exceptions_print_stacktrace();
1946 /* unload the JavaVM */
1948 (void) vm_destroy(vm);
1956 /* vm_destroy ******************************************************************
1958 Unloads a Java VM and reclaims its resources.
1960 *******************************************************************************/
1962 s4 vm_destroy(JavaVM *vm)
1964 #if defined(ENABLE_THREADS)
1965 threads_join_all_threads();
1968 /* everything's ok */
1974 /* vm_exit *********************************************************************
1976 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1978 *******************************************************************************/
1980 void vm_exit(s4 status)
1984 /* signal that we are exiting */
1988 assert(class_java_lang_System);
1989 assert(class_java_lang_System->state & CLASS_LOADED);
1991 #if defined(ENABLE_JVMTI)
1992 if (jvmti || (dbgcom!=NULL)) {
1993 jvmti_set_phase(JVMTI_PHASE_DEAD);
1994 if (jvmti) jvmti_agentunload();
1998 if (!link_class(class_java_lang_System)) {
1999 exceptions_print_stacktrace();
2003 /* call java.lang.System.exit(I)V */
2005 m = class_resolveclassmethod(class_java_lang_System,
2006 utf_new_char("exit"),
2008 class_java_lang_Object,
2012 exceptions_print_stacktrace();
2016 /* call the exit function with passed exit status */
2018 (void) vm_call_method(m, NULL, status);
2020 /* If we had an exception, just ignore the exception and exit with
2023 vm_shutdown(status);
2027 /* vm_shutdown *****************************************************************
2029 Terminates the system immediately without freeing memory explicitly
2030 (to be used only for abnormal termination).
2032 *******************************************************************************/
2034 void vm_shutdown(s4 status)
2037 #if defined(ENABLE_STATISTICS)
2038 || opt_getcompilingtime || opt_stat
2042 log_text("CACAO terminated by shutdown");
2043 dolog("Exit status: %d\n", (s4) status);
2047 #if defined(ENABLE_JVMTI)
2048 /* terminate cacaodbgserver */
2050 pthread_mutex_lock(&dbgcomlock);
2052 pthread_mutex_unlock(&dbgcomlock);
2053 jvmti_cacaodbgserver_quit();
2061 /* vm_exit_handler *************************************************************
2063 The exit_handler function is called upon program termination.
2065 ATTENTION: Don't free system resources here! Some threads may still
2066 be running as this is called from VMRuntime.exit(). The OS does the
2069 *******************************************************************************/
2071 void vm_exit_handler(void)
2073 #if !defined(NDEBUG)
2075 class_showmethods(mainclass);
2077 if (showconstantpool)
2078 class_showconstantpool(mainclass);
2083 # if defined(ENABLE_PROFILING)
2085 profile_printstats();
2087 #endif /* !defined(NDEBUG) */
2089 #if defined(ENABLE_RT_TIMING)
2090 rt_timing_print_time_stats(stderr);
2093 #if defined(ENABLE_CYCLES_STATS)
2094 builtin_print_cycles_stats(stderr);
2095 stacktrace_print_cycles_stats(stderr);
2099 #if defined(ENABLE_STATISTICS)
2100 || opt_getcompilingtime || opt_stat
2104 log_text("CACAO terminated");
2106 #if defined(ENABLE_STATISTICS)
2109 #ifdef TYPECHECK_STATISTICS
2110 typecheck_print_statistics(get_logfile());
2114 if (opt_getcompilingtime)
2116 #endif /* defined(ENABLE_STATISTICS) */
2118 /* vm_print_profile(stderr);*/
2122 /* vm_abort ********************************************************************
2124 Prints an error message and aborts the VM.
2126 *******************************************************************************/
2128 void vm_abort(const char *text, ...)
2132 /* print the log message */
2137 log_vprint(text, ap);
2142 /* now abort the VM */
2148 /* vm_get_mainclass_from_jar ***************************************************
2150 Gets the name of the main class from a JAR's manifest file.
2152 *******************************************************************************/
2154 static char *vm_get_mainclass_from_jar(char *mainstring)
2161 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
2164 exceptions_print_stacktrace();
2168 /* create JarFile object */
2173 exceptions_print_stacktrace();
2177 m = class_resolveclassmethod(c,
2179 utf_java_lang_String__void,
2180 class_java_lang_Object,
2184 exceptions_print_stacktrace();
2188 s = javastring_new_from_ascii(mainstring);
2190 (void) vm_call_method(m, o, s);
2192 if (exceptions_get_exception()) {
2193 exceptions_print_stacktrace();
2197 /* get manifest object */
2199 m = class_resolveclassmethod(c,
2200 utf_new_char("getManifest"),
2201 utf_new_char("()Ljava/util/jar/Manifest;"),
2202 class_java_lang_Object,
2206 exceptions_print_stacktrace();
2210 o = vm_call_method(m, o);
2213 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
2218 /* get Main Attributes */
2220 m = class_resolveclassmethod(o->vftbl->class,
2221 utf_new_char("getMainAttributes"),
2222 utf_new_char("()Ljava/util/jar/Attributes;"),
2223 class_java_lang_Object,
2227 exceptions_print_stacktrace();
2231 o = vm_call_method(m, o);
2234 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
2239 /* get property Main-Class */
2241 m = class_resolveclassmethod(o->vftbl->class,
2242 utf_new_char("getValue"),
2243 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
2244 class_java_lang_Object,
2248 exceptions_print_stacktrace();
2252 s = javastring_new_from_ascii("Main-Class");
2254 o = vm_call_method(m, o, s);
2257 exceptions_print_stacktrace();
2261 return javastring_tochar(o);
2265 /* vm_compile_all **************************************************************
2267 Compile all methods found in the bootclasspath.
2269 *******************************************************************************/
2271 #if !defined(NDEBUG)
2272 static void vm_compile_all(void)
2277 classcache_name_entry *nmen;
2278 classcache_class_entry *clsen;
2281 /* create all classes found in the bootclasspath */
2282 /* XXX currently only works with zip/jar's */
2284 loader_load_all_classes();
2286 /* link all classes */
2288 for (slot = 0; slot < hashtable_classcache.size; slot++) {
2289 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
2291 for (; nmen; nmen = nmen->hashlink) {
2292 /* iterate over all class entries */
2294 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
2295 c = clsen->classobj;
2300 if (!(c->state & CLASS_LINKED)) {
2301 if (!link_class(c)) {
2302 fprintf(stderr, "Error linking: ");
2303 utf_fprint_printable_ascii_classname(stderr, c->name);
2304 fprintf(stderr, "\n");
2306 /* print out exception and cause */
2308 exceptions_print_current_exception();
2310 /* goto next class */
2316 /* compile all class methods */
2318 for (i = 0; i < c->methodscount; i++) {
2319 m = &(c->methods[i]);
2321 if (m->jcode != NULL) {
2322 if (!jit_compile(m)) {
2323 fprintf(stderr, "Error compiling: ");
2324 utf_fprint_printable_ascii_classname(stderr, c->name);
2325 fprintf(stderr, ".");
2326 utf_fprint_printable_ascii(stderr, m->name);
2327 utf_fprint_printable_ascii(stderr, m->descriptor);
2328 fprintf(stderr, "\n");
2330 /* print out exception and cause */
2332 exceptions_print_current_exception();
2340 #endif /* !defined(NDEBUG) */
2343 /* vm_compile_method ***********************************************************
2345 Compile a specific method.
2347 *******************************************************************************/
2349 #if !defined(NDEBUG)
2350 static void vm_compile_method(void)
2354 /* create, load and link the main class */
2356 mainclass = load_class_bootstrap(utf_new_char(mainstring));
2358 if (mainclass == NULL)
2359 exceptions_print_stacktrace();
2361 if (!link_class(mainclass))
2362 exceptions_print_stacktrace();
2364 if (opt_signature != NULL) {
2365 m = class_resolveclassmethod(mainclass,
2366 utf_new_char(opt_method),
2367 utf_new_char(opt_signature),
2372 m = class_resolveclassmethod(mainclass,
2373 utf_new_char(opt_method),
2380 vm_abort("vm_compile_method: java.lang.NoSuchMethodException: %s.%s",
2381 opt_method, opt_signature ? opt_signature : "");
2385 #endif /* !defined(NDEBUG) */
2388 /* vm_array_store_int **********************************************************
2390 Helper function to store an integer into the argument array, taking
2391 care of architecture specific issues.
2393 *******************************************************************************/
2395 static void vm_array_store_int(uint64_t *array, paramdesc *pd, int32_t value)
2399 if (!pd->inmemory) {
2401 array[index] = (int64_t) value;
2404 index = ARG_CNT + pd->index;
2405 #if SIZEOF_VOID_P == 8
2406 array[index] = (int64_t) value;
2408 # if WORDS_BIGENDIAN == 1
2409 array[index] = ((int64_t) value) << 32;
2411 array[index] = (int64_t) value;
2418 /* vm_array_store_lng **********************************************************
2420 Helper function to store a long into the argument array, taking
2421 care of architecture specific issues.
2423 *******************************************************************************/
2425 static void vm_array_store_lng(uint64_t *array, paramdesc *pd, int64_t value)
2429 #if SIZEOF_VOID_P == 8
2433 index = ARG_CNT + pd->index;
2435 array[index] = value;
2437 if (!pd->inmemory) {
2438 /* move low and high 32-bits into it's own argument slot */
2440 index = GET_LOW_REG(pd->index);
2441 array[index] = value & 0x00000000ffffffff;
2443 index = GET_HIGH_REG(pd->index);
2444 array[index] = value >> 32;
2447 index = ARG_CNT + pd->index;
2448 array[index] = value;
2454 /* vm_array_store_flt **********************************************************
2456 Helper function to store a float into the argument array, taking
2457 care of architecture specific issues.
2459 *******************************************************************************/
2461 static void vm_array_store_flt(uint64_t *array, paramdesc *pd, uint64_t value)
2465 if (!pd->inmemory) {
2466 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2469 index = INT_ARG_CNT + pd->index;
2471 #if WORDS_BIGENDIAN == 1 && !defined(__POWERPC__) && !defined(__POWERPC64__) && !defined(__S390__)
2472 array[index] = value >> 32;
2474 array[index] = value;
2478 index = ARG_CNT + pd->index;
2479 #if defined(__SPARC_64__)
2480 array[index] = value >> 32;
2482 array[index] = value;
2488 /* vm_array_store_dbl **********************************************************
2490 Helper function to store a double into the argument array, taking
2491 care of architecture specific issues.
2493 *******************************************************************************/
2495 static void vm_array_store_dbl(uint64_t *array, paramdesc *pd, uint64_t value)
2499 if (!pd->inmemory) {
2500 #if SIZEOF_VOID_P != 8 && defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2501 index = GET_LOW_REG(pd->index);
2502 array[index] = value & 0x00000000ffffffff;
2504 index = GET_HIGH_REG(pd->index);
2505 array[index] = value >> 32;
2507 index = INT_ARG_CNT + pd->index;
2508 array[index] = value;
2512 index = ARG_CNT + pd->index;
2513 array[index] = value;
2518 /* vm_array_store_adr **********************************************************
2520 Helper function to store an address into the argument array, taking
2521 care of architecture specific issues.
2523 *******************************************************************************/
2525 static void vm_array_store_adr(uint64_t *array, paramdesc *pd, void *value)
2529 if (!pd->inmemory) {
2530 #if defined(HAS_ADDRESS_REGISTER_FILE)
2531 /* When the architecture has address registers, place them
2532 after integer and float registers. */
2534 index = INT_ARG_CNT + FLT_ARG_CNT + pd->index;
2538 array[index] = (uint64_t) (intptr_t) value;
2541 index = ARG_CNT + pd->index;
2542 #if SIZEOF_VOID_P == 8
2543 array[index] = (uint64_t) (intptr_t) value;
2545 # if WORDS_BIGENDIAN == 1
2546 array[index] = ((uint64_t) (intptr_t) value) << 32;
2548 array[index] = (uint64_t) (intptr_t) value;
2555 /* vm_array_from_valist ********************************************************
2559 *******************************************************************************/
2561 uint64_t *vm_array_from_valist(methodinfo *m, java_object_t *o, va_list ap)
2570 /* get the descriptors */
2574 td = md->paramtypes;
2576 /* allocate argument array */
2578 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2580 /* if method is non-static fill first block and skip `this' pointer */
2585 /* the `this' pointer */
2586 vm_array_store_adr(array, pd, o);
2593 for (; i < md->paramcount; i++, pd++, td++) {
2596 value.i = va_arg(ap, int32_t);
2597 vm_array_store_int(array, pd, value.i);
2601 value.l = va_arg(ap, int64_t);
2602 vm_array_store_lng(array, pd, value.l);
2606 #if defined(__ALPHA__) || defined(__POWERPC__) || defined(__POWERPC64__)
2607 /* This is required to load the correct float value in
2610 value.d = (double) va_arg(ap, double);
2612 value.f = (float) va_arg(ap, double);
2614 vm_array_store_flt(array, pd, value.l);
2618 value.d = va_arg(ap, double);
2619 vm_array_store_dbl(array, pd, value.l);
2623 value.a = va_arg(ap, void*);
2624 vm_array_store_adr(array, pd, value.a);
2633 /* vm_array_from_jvalue ********************************************************
2637 *******************************************************************************/
2639 static uint64_t *vm_array_from_jvalue(methodinfo *m, java_object_t *o,
2649 /* get the descriptors */
2653 td = md->paramtypes;
2655 /* allocate argument array */
2657 #if defined(HAS_ADDRESS_REGISTER_FILE)
2658 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + ADR_ARG_CNT + md->memuse);
2660 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2663 /* if method is non-static fill first block and skip `this' pointer */
2668 /* the `this' pointer */
2669 vm_array_store_adr(array, pd, o);
2676 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2677 switch (td->decltype) {
2679 vm_array_store_int(array, pd, args[j].i);
2683 vm_array_store_lng(array, pd, args[j].j);
2687 vm_array_store_flt(array, pd, args[j].j);
2691 vm_array_store_dbl(array, pd, args[j].j);
2695 vm_array_store_adr(array, pd, args[j].l);
2704 /* vm_array_from_objectarray ***************************************************
2708 *******************************************************************************/
2710 uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o,
2711 java_handle_objectarray_t *params)
2717 java_handle_t *param;
2723 /* get the descriptors */
2727 td = md->paramtypes;
2729 /* allocate argument array */
2731 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2733 /* if method is non-static fill first block and skip `this' pointer */
2739 vm_array_store_adr(array, pd, o);
2746 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2747 LLNI_objectarray_element_get(params, j, param);
2754 /* convert the value according to its declared type */
2756 c = param->vftbl->class;
2758 switch (td->decltype) {
2759 case PRIMITIVETYPE_BOOLEAN:
2760 if (c == class_java_lang_Boolean)
2761 LLNI_field_get_val((java_lang_Boolean *) param, value, value.i);
2766 case PRIMITIVETYPE_BYTE:
2767 if (c == class_java_lang_Byte)
2768 LLNI_field_get_val((java_lang_Byte *) param, value, value.i);
2773 case PRIMITIVETYPE_CHAR:
2774 if (c == class_java_lang_Character)
2775 LLNI_field_get_val((java_lang_Character *) param, value, value.i);
2780 case PRIMITIVETYPE_SHORT:
2781 if (c == class_java_lang_Short)
2782 LLNI_field_get_val((java_lang_Short *) param, value, value.i);
2783 else if (c == class_java_lang_Byte)
2784 LLNI_field_get_val((java_lang_Byte *) param, value, value.i);
2789 case PRIMITIVETYPE_INT:
2790 if (c == class_java_lang_Integer)
2791 LLNI_field_get_val((java_lang_Integer *) param, value, value.i);
2792 else if (c == class_java_lang_Short)
2793 LLNI_field_get_val((java_lang_Short *) param, value, value.i);
2794 else if (c == class_java_lang_Byte)
2795 LLNI_field_get_val((java_lang_Byte *) param, value, value.i);
2804 vm_array_store_int(array, pd, value.i);
2811 /* convert the value according to its declared type */
2813 c = param->vftbl->class;
2815 switch (td->decltype) {
2816 case PRIMITIVETYPE_LONG:
2817 if (c == class_java_lang_Long)
2818 LLNI_field_get_val((java_lang_Long *) param, value, value.l);
2819 else if (c == class_java_lang_Integer)
2820 value.l = (int64_t) LLNI_field_direct(((java_lang_Integer *) param), value);
2821 else if (c == class_java_lang_Short)
2822 value.l = (int64_t) LLNI_field_direct(((java_lang_Short *) param), value);
2823 else if (c == class_java_lang_Byte)
2824 value.l = (int64_t) LLNI_field_direct(((java_lang_Byte *) param), value);
2833 vm_array_store_lng(array, pd, value.l);
2840 /* convert the value according to its declared type */
2842 c = param->vftbl->class;
2844 switch (td->decltype) {
2845 case PRIMITIVETYPE_FLOAT:
2846 if (c == class_java_lang_Float)
2847 LLNI_field_get_val((java_lang_Float *) param, value, value.f);
2856 vm_array_store_flt(array, pd, value.l);
2863 /* convert the value according to its declared type */
2865 c = param->vftbl->class;
2867 switch (td->decltype) {
2868 case PRIMITIVETYPE_DOUBLE:
2869 if (c == class_java_lang_Double)
2870 LLNI_field_get_val((java_lang_Double *) param, value, value.d);
2871 else if (c == class_java_lang_Float)
2872 LLNI_field_get_val((java_lang_Float *) param, value, value.f);
2881 vm_array_store_dbl(array, pd, value.l);
2885 if (!resolve_class_from_typedesc(td, true, true, &c))
2888 if (param != NULL) {
2889 if (td->arraydim > 0) {
2890 if (!builtin_arrayinstanceof(param, c))
2894 if (!builtin_instanceof(param, c))
2899 vm_array_store_adr(array, pd, param);
2910 exceptions_throw_illegalargumentexception();
2915 /* vm_call_method **************************************************************
2917 Calls a Java method with a variable number of arguments.
2919 *******************************************************************************/
2921 #define VM_CALL_METHOD(name, type) \
2922 type vm_call_method##name(methodinfo *m, java_handle_t *o, ...) \
2928 value = vm_call_method##name##_valist(m, o, ap); \
2934 VM_CALL_METHOD(, java_handle_t *)
2935 VM_CALL_METHOD(_int, int32_t)
2936 VM_CALL_METHOD(_long, int64_t)
2937 VM_CALL_METHOD(_float, float)
2938 VM_CALL_METHOD(_double, double)
2941 /* vm_call_method_valist *******************************************************
2943 Calls a Java method with a variable number of arguments, passed via
2946 *******************************************************************************/
2948 #define VM_CALL_METHOD_VALIST(name, type) \
2949 type vm_call_method##name##_valist(methodinfo *m, java_handle_t *o, \
2956 dumpsize = dump_size(); \
2957 array = vm_array_from_valist(m, o, ap); \
2958 value = vm_call##name##_array(m, array); \
2959 dump_release(dumpsize); \
2964 VM_CALL_METHOD_VALIST(, java_handle_t *)
2965 VM_CALL_METHOD_VALIST(_int, int32_t)
2966 VM_CALL_METHOD_VALIST(_long, int64_t)
2967 VM_CALL_METHOD_VALIST(_float, float)
2968 VM_CALL_METHOD_VALIST(_double, double)
2971 /* vm_call_method_jvalue *******************************************************
2973 Calls a Java method with a variable number of arguments, passed via
2976 *******************************************************************************/
2978 #define VM_CALL_METHOD_JVALUE(name, type) \
2979 type vm_call_method##name##_jvalue(methodinfo *m, java_handle_t *o, \
2980 const jvalue *args) \
2986 dumpsize = dump_size(); \
2987 array = vm_array_from_jvalue(m, o, args); \
2988 value = vm_call##name##_array(m, array); \
2989 dump_release(dumpsize); \
2994 VM_CALL_METHOD_JVALUE(, java_handle_t *)
2995 VM_CALL_METHOD_JVALUE(_int, int32_t)
2996 VM_CALL_METHOD_JVALUE(_long, int64_t)
2997 VM_CALL_METHOD_JVALUE(_float, float)
2998 VM_CALL_METHOD_JVALUE(_double, double)
3001 /* vm_call_array ***************************************************************
3003 Calls a Java method with a variable number of arguments, passed via
3006 *******************************************************************************/
3008 #define VM_CALL_ARRAY(name, type) \
3009 type vm_call##name##_array(methodinfo *m, uint64_t *array) \
3015 md = m->parseddesc; \
3017 if (m->code == NULL) \
3018 if (!jit_compile(m)) \
3021 pv = m->code->entrypoint; \
3023 STATISTICS(count_calls_native_to_java++); \
3025 value = asm_vm_call_method##name(pv, array, md->memuse); \
3030 VM_CALL_ARRAY(, java_handle_t *)
3031 VM_CALL_ARRAY(_int, int32_t)
3032 VM_CALL_ARRAY(_long, int64_t)
3033 VM_CALL_ARRAY(_float, float)
3034 VM_CALL_ARRAY(_double, double)
3038 * These are local overrides for various environment variables in Emacs.
3039 * Please do not remove this and leave it at the end of the file, where
3040 * Emacs will automagically detect them.
3041 * ---------------------------------------------------------------------
3044 * indent-tabs-mode: t