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 8066 2007-06-12 09:08:12Z twisti $
36 #if defined(WITH_JRE_LAYOUT)
43 #include "mm/gc-common.h"
44 #include "mm/memory.h"
46 #include "native/jni.h"
47 #include "native/native.h"
48 #include "native/include/java_lang_String.h" /* required by java_lang_Class.h */
49 #include "native/include/java_lang_Class.h"
51 #include "threads/threads-common.h"
53 #include "toolbox/logging.h"
55 #include "vm/builtin.h"
56 #include "vm/exceptions.h"
57 #include "vm/finalizer.h"
58 #include "vm/global.h"
59 #include "vm/initialize.h"
60 #include "vm/properties.h"
61 #include "vm/signallocal.h"
62 #include "vm/stringlocal.h"
65 #include "vm/jit/jit.h"
66 #include "vm/jit/md.h"
67 #include "vm/jit/asmpart.h"
69 #if defined(ENABLE_PROFILING)
70 # include "vm/jit/optimizing/profile.h"
73 #include "vm/jit/optimizing/recompile.h"
75 #include "vmcore/classcache.h"
76 #include "vmcore/options.h"
77 #include "vmcore/primitive.h"
78 #include "vmcore/statistics.h"
79 #include "vmcore/suck.h"
81 #if defined(ENABLE_JVMTI)
82 # include "native/jvmti/cacaodbg.h"
85 #if defined(ENABLE_VMLOG)
86 #include <vmlog_cacao.h>
90 /* Invocation API variables ***************************************************/
92 _Jv_JavaVM *_Jv_jvm; /* denotes a Java VM */
93 _Jv_JNIEnv *_Jv_env; /* pointer to native method interface */
96 /* global variables ***********************************************************/
98 s4 vms = 0; /* number of VMs created */
100 bool vm_initializing = false;
101 bool vm_exiting = false;
103 char *cacao_prefix = NULL;
104 char *cacao_libjvm = NULL;
105 char *classpath_libdir = NULL;
107 char *_Jv_bootclasspath; /* contains the boot classpath */
108 char *_Jv_classpath; /* contains the classpath */
109 char *_Jv_java_library_path;
111 char *mainstring = NULL;
112 classinfo *mainclass = NULL;
114 char *specificmethodname = NULL;
115 char *specificsignature = NULL;
119 #if defined(ENABLE_INTRP)
120 u1 *intrp_main_stack = NULL;
124 /* define heap sizes **********************************************************/
126 #define HEAP_MAXSIZE 128 * 1024 * 1024 /* default 128MB */
127 #define HEAP_STARTSIZE 2 * 1024 * 1024 /* default 2MB */
128 #define STACK_SIZE 64 * 1024 /* default 64kB */
131 /* define command line options ************************************************/
162 /* Java non-standard options */
173 #if defined(ENABLE_PROFILING)
186 #if defined(ENABLE_STATISTICS)
203 #if defined(ENABLE_VERIFIER)
205 #if defined(TYPECHECK_VERBOSE)
208 #endif /* defined(ENABLE_VERIFIER) */
211 /* optimization options */
213 #if defined(ENABLE_LOOP)
217 #if defined(ENABLE_IFCONV)
221 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
225 #if defined(ENABLE_INLINING)
230 #if defined(ENABLE_INLINING_DEBUG)
231 OPT_INLINE_DEBUG_ALL,
232 OPT_INLINE_DEBUG_END,
233 OPT_INLINE_DEBUG_MIN,
234 OPT_INLINE_DEBUG_MAX,
235 OPT_INLINE_REPLACE_VERBOSE,
236 OPT_INLINE_REPLACE_VERBOSE2,
237 #endif /* defined(ENABLE_INLINING_DEBUG) */
238 #endif /* defined(ENABLE_INLINING) */
240 #if defined(ENABLE_INTRP)
241 /* interpreter options */
260 #if defined(ENABLE_DEBUG_FILTER)
261 OPT_FILTER_VERBOSECALL_INCLUDE,
262 OPT_FILTER_VERBOSECALL_EXCLUDE,
263 OPT_FILTER_SHOW_METHOD,
270 opt_struct opts[] = {
271 { "foo", false, OPT_FOO },
275 { "jar", false, OPT_JAR },
277 { "d32", false, OPT_D32 },
278 { "d64", false, OPT_D64 },
279 { "client", false, OPT_IGNORE },
280 { "server", false, OPT_IGNORE },
281 { "jvm", false, OPT_IGNORE },
282 { "hotspot", false, OPT_IGNORE },
284 { "classpath", true, OPT_CLASSPATH },
285 { "cp", true, OPT_CLASSPATH },
286 { "D", true, OPT_D },
287 { "version", false, OPT_VERSION },
288 { "showversion", false, OPT_SHOWVERSION },
289 { "fullversion", false, OPT_FULLVERSION },
290 { "help", false, OPT_HELP },
291 { "?", false, OPT_HELP },
292 { "X", false, OPT_X },
293 { "XX:", true, OPT_XX },
295 { "ea:", true, OPT_EA },
296 { "da:", true, OPT_DA },
297 { "ea", false, OPT_EA },
298 { "da", false, OPT_DA },
300 { "esa", false, OPT_ESA },
301 { "enablesystemassertions", false, OPT_ESA },
302 { "dsa", false, OPT_DSA },
303 { "disablesystemassertions", false, OPT_DSA },
305 { "noasyncgc", false, OPT_IGNORE },
306 #if defined(ENABLE_VERIFIER)
307 { "noverify", false, OPT_NOVERIFY },
309 { "v", false, OPT_VERBOSE1 },
310 { "verbose:", true, OPT_VERBOSE },
312 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
313 { "verbosetc", false, OPT_VERBOSETC },
315 #if defined(__ALPHA__)
316 { "noieee", false, OPT_NOIEEE },
318 #if defined(ENABLE_STATISTICS)
319 { "time", false, OPT_TIME },
320 { "stat", false, OPT_STAT },
322 { "log", true, OPT_LOG },
323 { "c", true, OPT_CHECK },
324 { "l", false, OPT_LOAD },
325 { "eager", false, OPT_EAGER },
328 { "all", false, OPT_ALL },
329 { "sig", true, OPT_SIGNATURE },
332 #if defined(ENABLE_LOOP)
333 { "oloop", false, OPT_OLOOP },
335 #if defined(ENABLE_IFCONV)
336 { "ifconv", false, OPT_IFCONV },
338 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
339 { "lsra", false, OPT_LSRA },
342 #if defined(ENABLE_INTRP)
343 /* interpreter options */
345 { "trace", false, OPT_TRACE },
346 { "static-supers", true, OPT_STATIC_SUPERS },
347 { "no-dynamic", false, OPT_NO_DYNAMIC },
348 { "no-replication", false, OPT_NO_REPLICATION },
349 { "no-quicksuper", false, OPT_NO_QUICKSUPER },
352 /* JVMTI Agent Command Line Options */
354 { "agentlib:", true, OPT_AGENTLIB },
355 { "agentpath:", true, OPT_AGENTPATH },
358 /* Java non-standard options */
360 { "Xjit", false, OPT_JIT },
361 { "Xint", false, OPT_INTRP },
362 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
363 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
364 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
365 { "Xbootclasspath/c:", true, OPT_BOOTCLASSPATH_C },
368 { "Xdebug", false, OPT_DEBUG },
369 { "Xnoagent", false, OPT_NOAGENT },
370 { "Xrunjdwp", true, OPT_XRUNJDWP },
373 { "Xms", true, OPT_MS },
374 { "ms", true, OPT_MS },
375 { "Xmx", true, OPT_MX },
376 { "mx", true, OPT_MX },
377 { "Xss", true, OPT_SS },
378 { "ss", true, OPT_SS },
380 #if defined(ENABLE_PROFILING)
381 { "Xprof:", true, OPT_PROF_OPTION },
382 { "Xprof", false, OPT_PROF },
385 /* inlining options */
387 #if defined(ENABLE_INLINING)
388 #if defined(ENABLE_INLINING_DEBUG)
389 { "ia", false, OPT_INLINE_DEBUG_ALL },
390 { "ii", true, OPT_INLINE_DEBUG_MIN },
391 { "im", true, OPT_INLINE_DEBUG_MAX },
392 { "ie", true, OPT_INLINE_DEBUG_END },
393 { "ir", false, OPT_INLINE_REPLACE_VERBOSE },
394 { "iR", false, OPT_INLINE_REPLACE_VERBOSE2 },
395 #endif /* defined(ENABLE_INLINING_DEBUG) */
397 { "il", false, OPT_INLINE_LOG },
399 { "i", false, OPT_INLINING },
400 #endif /* defined(ENABLE_INLINING) */
402 /* keep these at the end of the list */
405 { "m", true, OPT_METHOD },
408 { "s", true, OPT_SHOW },
409 { "debug-color", false, OPT_DEBUGCOLOR },
411 #if defined(ENABLE_DEBUG_FILTER)
412 { "XXfi", true, OPT_FILTER_VERBOSECALL_INCLUDE },
413 { "XXfx", true, OPT_FILTER_VERBOSECALL_EXCLUDE },
414 { "XXfm", true, OPT_FILTER_SHOW_METHOD },
421 /* usage ***********************************************************************
423 Prints the correct usage syntax to stdout.
425 *******************************************************************************/
429 puts("Usage: cacao [-options] classname [arguments]");
430 puts(" (to run a class file)");
431 puts(" or cacao [-options] -jar jarfile [arguments]");
432 puts(" (to run a standalone jar file)\n");
434 puts("where options include:");
435 puts(" -d32 use 32-bit data model if available");
436 puts(" -d64 use 64-bit data model if available");
437 puts(" -client compatibility (currently ignored)");
438 puts(" -server compatibility (currently ignored)");
439 puts(" -jvm compatibility (currently ignored)");
440 puts(" -hotspot compatibility (currently ignored)\n");
442 puts(" -cp <path> specify a path to look for classes");
443 puts(" -classpath <path> specify a path to look for classes");
444 puts(" -D<name>=<value> add an entry to the property list");
445 puts(" -verbose[:class|gc|jni] enable specific verbose output");
446 puts(" -version print product version and exit");
447 puts(" -fullversion print jpackage-compatible product version and exit");
448 puts(" -showversion print product version and continue");
449 puts(" -help, -? print this help message");
450 puts(" -X print help on non-standard Java options");
451 puts(" -XX print help on CACAO options");
452 puts(" -ea[:<packagename>...|:<classname>]");
453 puts(" -enableassertions[:<packagename>...|:<classname>]");
454 puts(" enable assertions with specified granularity");
455 puts(" -da[:<packagename>...|:<classname>]");
456 puts(" -disableassertions[:<packagename>...|:<classname>]");
457 puts(" disable assertions with specified granularity");
458 puts(" -esa | -enablesystemassertions");
459 puts(" enable system assertions");
460 puts(" -dsa | -disablesystemassertions");
461 puts(" disable system assertions");
464 puts(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent");
465 puts (" for jdwp help use: -agentlib:jdwp=help");
466 puts(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent");
469 /* exit with error code */
475 static void Xusage(void)
477 #if defined(ENABLE_JIT)
478 puts(" -Xjit JIT mode execution (default)");
480 #if defined(ENABLE_INTRP)
481 puts(" -Xint interpreter mode execution");
483 puts(" -Xbootclasspath:<zip/jar files and directories separated by :>");
484 puts(" value is set as bootstrap class path");
485 puts(" -Xbootclasspath/a:<zip/jar files and directories separated by :>");
486 puts(" value is appended to the bootstrap class path");
487 puts(" -Xbootclasspath/p:<zip/jar files and directories separated by :>");
488 puts(" value is prepended to the bootstrap class path");
489 puts(" -Xbootclasspath/c:<zip/jar files and directories separated by :>");
490 puts(" value is used as Java core library, but the");
491 puts(" hardcoded VM interface classes are prepended");
492 printf(" -Xms<size> set the initial size of the heap (default: %dMB)\n", HEAP_STARTSIZE / 1024 / 1024);
493 printf(" -Xmx<size> set the maximum size of the heap (default: %dMB)\n", HEAP_MAXSIZE / 1024 / 1024);
494 printf(" -Xss<size> set the thread stack size (default: %dkB)\n", STACK_SIZE / 1024);
496 #if defined(ENABLE_PROFILING)
497 puts(" -Xprof[:bb] collect and print profiling data");
500 #if defined(ENABLE_JVMTI)
501 /* -Xdebug option depend on gnu classpath JDWP options. options:
502 transport=dt_socket,address=<hostname:port>,server=(y|n),suspend(y|n) */
503 puts(" -Xdebug enable remote debugging\n");
504 puts(" -Xrunjdwp transport=[dt_socket|...],address=<hostname:port>,server=[y|n],suspend=[y|n]\n");
505 puts(" enable remote debugging\n");
508 /* exit with error code */
514 static void XXusage(void)
516 puts(" -v write state-information");
518 puts(" -verbose[:call|exception|jit|memory|threads]");
519 puts(" enable specific verbose output");
520 puts(" -debug-color colored output for ANSI terms");
522 #ifdef TYPECHECK_VERBOSE
523 puts(" -verbosetc write debug messages while typechecking");
525 #if defined(__ALPHA__)
526 puts(" -noieee don't use ieee compliant arithmetic");
528 #if defined(ENABLE_VERIFIER)
529 puts(" -noverify don't verify classfiles");
531 #if defined(ENABLE_STATISTICS)
532 puts(" -time measure the runtime");
533 puts(" -stat detailed compiler statistics");
535 puts(" -log logfile specify a name for the logfile");
536 puts(" -c(heck)b(ounds) don't check array bounds");
537 puts(" s(ync) don't check for synchronization");
538 #if defined(ENABLE_LOOP)
539 puts(" -oloop optimize array accesses in loops");
541 puts(" -l don't start the class after loading");
542 puts(" -eager perform eager class loading and linking");
544 puts(" -all compile all methods, no execution");
545 puts(" -m compile only a specific method");
546 puts(" -sig specify signature for a specific method");
549 puts(" -s... show...");
550 puts(" (c)onstants the constant pool");
551 puts(" (m)ethods class fields and methods");
552 puts(" (u)tf the utf - hash");
553 puts(" (i)ntermediate intermediate representation");
554 #if defined(ENABLE_DISASSEMBLER)
555 puts(" (a)ssembler disassembled listing");
556 puts(" n(o)ps show NOPs in disassembler output");
557 puts(" (e)xceptionstubs disassembled exception stubs (only with -sa)");
558 puts(" (n)ative disassembled native stubs");
560 puts(" (d)atasegment data segment listing");
562 #if defined(ENABLE_INLINING)
563 puts(" -i activate inlining");
565 puts(" -il log inlining");
567 #if defined(ENABLE_INLINING_DEBUG)
568 puts(" -ia use inlining for all methods");
569 puts(" -ii <size> set minimum size for inlined result");
570 puts(" -im <size> set maximum size for inlined result");
571 puts(" -ie <number> stop inlining after the given number of roots");
572 puts(" -ir log on-stack replacement");
573 puts(" -iR log on-stack replacement, more verbose");
574 #endif /* defined(ENABLE_INLINING_DEBUG) */
575 #endif /* defined(ENABLE_INLINING) */
577 #if defined(ENABLE_IFCONV)
578 puts(" -ifconv use if-conversion");
580 #if defined(ENABLE_LSRA)
581 puts(" -lsra use linear scan register allocation");
583 #if defined(ENABLE_SSA)
584 puts(" -lsra use linear scan register allocation (with SSA)");
586 #if defined(ENABLE_DEBUG_FILTER)
587 puts(" -XXfi <regex> begin of dynamic scope for verbosecall filter");
588 puts(" -XXfx <regex> end of dynamic scope for verbosecall filter");
589 puts(" -XXfm <regex> filter for show options");
591 /* exit with error code */
597 /* version *********************************************************************
599 Only prints cacao version information.
601 *******************************************************************************/
603 static void version(bool opt_exit)
605 puts("java version \""JAVA_VERSION"\"");
606 puts("CACAO version "VERSION"");
608 puts("Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,");
609 puts("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,");
610 puts("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,");
611 puts("J. Wenninger, Institut f. Computersprachen - TU Wien\n");
613 puts("This program is free software; you can redistribute it and/or");
614 puts("modify it under the terms of the GNU General Public License as");
615 puts("published by the Free Software Foundation; either version 2, or (at");
616 puts("your option) any later version.\n");
618 puts("This program is distributed in the hope that it will be useful, but");
619 puts("WITHOUT ANY WARRANTY; without even the implied warranty of");
620 puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU");
621 puts("General Public License for more details.\n");
623 puts("Configure/Build options:\n");
624 puts(" ./configure: "VERSION_CONFIGURE_ARGS"");
625 #if defined(__VERSION__)
626 puts(" CC : "VERSION_CC" ("__VERSION__")");
628 puts(" CC : "VERSION_CC"");
630 puts(" CFLAGS : "VERSION_CFLAGS"\n");
632 puts("Default variables:\n");
633 printf(" maximum heap size : %d\n", HEAP_MAXSIZE);
634 printf(" initial heap size : %d\n", HEAP_STARTSIZE);
635 printf(" stack size : %d\n", STACK_SIZE);
636 #if defined(WITH_CLASSPATH_GNU)
637 puts(" java.boot.class.path : "CACAO_VM_ZIP":"CLASSPATH_CLASSES"");
639 puts(" java.boot.class.path : "CLASSPATH_CLASSES"");
641 puts(" gnu.classpath.boot.library.path: "CLASSPATH_LIBDIR"/classpath\n");
643 puts("Runtime variables:\n");
644 printf(" maximum heap size : %d\n", opt_heapmaxsize);
645 printf(" initial heap size : %d\n", opt_heapstartsize);
646 printf(" stack size : %d\n", opt_stacksize);
647 printf(" libjvm.so : %s\n", cacao_libjvm);
648 printf(" java.boot.class.path : %s\n", _Jv_bootclasspath);
649 printf(" gnu.classpath.boot.library.path: %s\n", classpath_libdir);
650 printf(" java.class.path : %s\n", _Jv_classpath);
652 /* exit normally, if requested */
659 /* fullversion *****************************************************************
661 Prints a Sun compatible version information (required e.g. by
662 jpackage, www.jpackage.org).
664 *******************************************************************************/
666 static void fullversion(void)
668 puts("java full version \"cacao-"JAVA_VERSION"\"");
676 /* forward declarations *******************************************************/
678 static char *vm_get_mainclass_from_jar(char *mainstring);
680 static void vm_compile_all(void);
681 static void vm_compile_method(void);
685 /* vm_createjvm ****************************************************************
687 Implementation for JNI_CreateJavaVM.
689 *******************************************************************************/
691 bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
693 JavaVMInitArgs *_vm_args;
697 /* get the arguments for the new JVM */
699 _vm_args = (JavaVMInitArgs *) vm_args;
701 /* get the VM and Env tables (must be set before vm_create) */
703 env = NEW(_Jv_JNIEnv);
705 #if defined(ENABLE_JNI)
706 env->env = &_Jv_JNINativeInterface;
709 /* XXX Set the global variable. Maybe we should do that differently. */
713 /* create and fill a JavaVM structure */
715 vm = NEW(_Jv_JavaVM);
717 #if defined(ENABLE_JNI)
718 vm->functions = &_Jv_JNIInvokeInterface;
721 /* XXX Set the global variable. Maybe we should do that differently. */
722 /* XXX JVMTI Agents needs a JavaVM */
726 /* actually create the JVM */
728 if (!vm_create(_vm_args))
731 #if defined(ENABLE_JNI)
732 /* setup the local ref table (must be created after vm_create) */
734 if (!jni_init_localref_table())
738 /* now return the values */
740 *p_vm = (JavaVM *) vm;
741 *p_env = (void *) env;
746 /* release allocated memory */
748 FREE(env, _Jv_JNIEnv);
749 FREE(vm, _Jv_JavaVM);
755 /* vm_create *******************************************************************
757 Creates a JVM. Called by vm_createjvm.
759 *******************************************************************************/
761 bool vm_create(JavaVMInitArgs *vm_args)
770 #if defined(ENABLE_JVMTI)
772 char *libname, *agentarg;
773 bool jdwp,agentbypath;
774 jdwp = agentbypath = false;
777 #if defined(ENABLE_VMLOG)
778 vmlog_cacao_init(vm_args);
781 /* check the JNI version requested */
783 switch (vm_args->version) {
784 case JNI_VERSION_1_1:
786 case JNI_VERSION_1_2:
787 case JNI_VERSION_1_4:
793 /* we only support 1 JVM instance */
798 if (atexit(vm_exit_handler))
799 vm_abort("atexit failed: %s\n", strerror(errno));
802 log_text("CACAO started -------------------------------------------------------");
804 /* We need to check if the actual size of a java.lang.Class object
805 is smaller or equal than the assumption made in
806 src/vmcore/class.h. */
808 if (sizeof(java_lang_Class) > sizeof(dummy_java_lang_Class))
809 vm_abort("vm_create: java_lang_Class structure is bigger than classinfo.object (%d > %d)", sizeof(java_lang_Class), sizeof(dummy_java_lang_Class));
811 /* set the VM starttime */
813 _Jv_jvm->starttime = builtin_currenttimemillis();
815 /* get stuff from the environment *****************************************/
817 #if defined(WITH_JRE_LAYOUT)
818 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
820 cacao_prefix = MNEW(char, 4096);
822 if (readlink("/proc/self/exe", cacao_prefix, 4095) == -1)
823 vm_abort("readlink failed: %s\n", strerror(errno));
825 /* get the path of the current executable */
827 cacao_prefix = dirname(cacao_prefix);
829 if ((strlen(cacao_prefix) + strlen("/..") + strlen("0")) > 4096)
830 vm_abort("libjvm name to long for buffer\n");
832 /* concatenate the library name */
834 strcat(cacao_prefix, "/..");
836 /* now set path to libjvm.so */
838 len = strlen(cacao_prefix) + strlen("/lib/libjvm") + strlen("0");
840 cacao_libjvm = MNEW(char, len);
841 strcpy(cacao_libjvm, cacao_prefix);
842 strcat(cacao_libjvm, "/lib/libjvm");
844 /* and finally set the path to GNU Classpath libraries */
846 len = strlen(cacao_prefix) + strlen("/lib/classpath") + strlen("0");
848 classpath_libdir = MNEW(char, len);
849 strcpy(classpath_libdir, cacao_prefix);
850 strcat(classpath_libdir, "/lib/classpath");
852 cacao_prefix = CACAO_PREFIX;
853 cacao_libjvm = CACAO_LIBDIR"/libjvm";
854 classpath_libdir = CLASSPATH_LIBDIR"/classpath";
857 /* set the bootclasspath */
859 cp = getenv("BOOTCLASSPATH");
862 _Jv_bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
863 strcpy(_Jv_bootclasspath, cp);
866 #if defined(WITH_JRE_LAYOUT)
868 # if defined(WITH_CLASSPATH_GNU)
869 strlen(cacao_prefix) +
870 strlen("/share/cacao/vm.zip") +
873 strlen(cacao_prefix) +
874 strlen("/share/classpath/glibj.zip") +
877 _Jv_bootclasspath = MNEW(char, len);
878 # if defined(WITH_CLASSPATH_GNU)
879 strcat(_Jv_bootclasspath, cacao_prefix);
880 strcat(_Jv_bootclasspath, "/share/cacao/vm.zip");
881 strcat(_Jv_bootclasspath, ":");
883 strcat(_Jv_bootclasspath, cacao_prefix);
884 strcat(_Jv_bootclasspath, "/share/classpath/glibj.zip");
887 # if defined(WITH_CLASSPATH_GNU)
888 strlen(CACAO_VM_ZIP) +
891 strlen(CLASSPATH_CLASSES) +
894 _Jv_bootclasspath = MNEW(char, len);
895 # if defined(WITH_CLASSPATH_GNU)
896 strcat(_Jv_bootclasspath, CACAO_VM_ZIP);
897 strcat(_Jv_bootclasspath, ":");
899 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
903 /* set the classpath */
905 cp = getenv("CLASSPATH");
908 _Jv_classpath = MNEW(char, strlen(cp) + strlen("0"));
909 strcat(_Jv_classpath, cp);
912 _Jv_classpath = MNEW(char, strlen(".") + strlen("0"));
913 strcpy(_Jv_classpath, ".");
916 /* get and set java.library.path */
918 _Jv_java_library_path = getenv("LD_LIBRARY_PATH");
920 if (_Jv_java_library_path == NULL)
921 _Jv_java_library_path = "";
923 /* interpret the options **************************************************/
930 opt_heapmaxsize = HEAP_MAXSIZE;
931 opt_heapstartsize = HEAP_STARTSIZE;
932 opt_stacksize = STACK_SIZE;
935 #if defined(ENABLE_JVMTI)
936 /* initialize JVMTI related **********************************************/
940 /* initialize and fill properties before command-line handling */
942 if (!properties_init())
943 vm_abort("properties_init failed");
945 /* iterate over all passed options */
947 while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
961 #if SIZEOF_VOID_P == 8
962 puts("Running a 32-bit JVM is not supported on this platform.");
968 #if SIZEOF_VOID_P == 4
969 puts("Running a 64-bit JVM is not supported on this platform.");
975 /* forget old classpath and set the argument as new classpath */
976 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
978 _Jv_classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
979 strcpy(_Jv_classpath, opt_arg);
983 for (i = 0; i < strlen(opt_arg); i++) {
984 if (opt_arg[i] == '=') {
986 properties_add(opt_arg, opt_arg + i + 1);
991 /* if no '=' is given, just create an empty property */
993 properties_add(opt_arg, "");
998 case OPT_BOOTCLASSPATH:
999 /* Forget default bootclasspath and set the argument as
1000 new boot classpath. */
1002 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1004 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
1005 strcpy(_Jv_bootclasspath, opt_arg);
1008 case OPT_BOOTCLASSPATH_A:
1009 /* append to end of bootclasspath */
1011 len = strlen(_Jv_bootclasspath);
1013 _Jv_bootclasspath = MREALLOC(_Jv_bootclasspath,
1017 strlen(opt_arg) + strlen("0"));
1019 strcat(_Jv_bootclasspath, ":");
1020 strcat(_Jv_bootclasspath, opt_arg);
1023 case OPT_BOOTCLASSPATH_P:
1024 /* prepend in front of bootclasspath */
1026 cp = _Jv_bootclasspath;
1029 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
1032 strcpy(_Jv_bootclasspath, opt_arg);
1033 strcat(_Jv_bootclasspath, ":");
1034 strcat(_Jv_bootclasspath, cp);
1036 MFREE(cp, char, len);
1039 case OPT_BOOTCLASSPATH_C:
1040 /* use as Java core library, but prepend VM interface classes */
1042 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1044 len = strlen(CACAO_VM_ZIP) +
1049 _Jv_bootclasspath = MNEW(char, len);
1051 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
1052 strcat(_Jv_bootclasspath, ":");
1053 strcat(_Jv_bootclasspath, opt_arg);
1056 #if defined(ENABLE_JVMTI)
1058 /* this option exists only for compatibility reasons */
1062 /* I don't know yet what Xnoagent should do. This is only for
1063 compatiblity with eclipse - motse */
1072 strlen(CACAO_LIBDIR) +
1073 strlen("/libjdwp.so=") +
1077 agentarg = MNEW(char, len);
1079 strcpy(agentarg, CACAO_LIBDIR);
1080 strcat(agentarg, "/libjdwp.so=");
1081 strcat(agentarg, &opt_arg[1]);
1098 c = opt_arg[strlen(opt_arg) - 1];
1100 if ((c == 'k') || (c == 'K')) {
1101 j = atoi(opt_arg) * 1024;
1103 } else if ((c == 'm') || (c == 'M')) {
1104 j = atoi(opt_arg) * 1024 * 1024;
1110 opt_heapmaxsize = j;
1111 else if (opt == OPT_MS)
1112 opt_heapstartsize = j;
1123 if (strcmp("class", opt_arg) == 0) {
1124 opt_verboseclass = true;
1126 else if (strcmp("gc", opt_arg) == 0) {
1127 opt_verbosegc = true;
1129 else if (strcmp("jni", opt_arg) == 0) {
1130 opt_verbosejni = true;
1132 #if !defined(NDEBUG)
1133 else if (strcmp("call", opt_arg) == 0) {
1134 opt_verbosecall = true;
1136 else if (strcmp("exception", opt_arg) == 0) {
1137 opt_verboseexception = true;
1139 else if (strcmp("jit", opt_arg) == 0) {
1144 compileverbose = true;
1146 else if (strcmp("memory", opt_arg) == 0) {
1147 opt_verbosememory = true;
1149 # if defined(ENABLE_STATISTICS)
1150 /* we also need statistics */
1155 else if (strcmp("threads", opt_arg) == 0) {
1156 opt_verbosethreads = true;
1160 printf("Unknown -verbose option: %s\n", opt_arg);
1165 case OPT_DEBUGCOLOR:
1166 opt_debugcolor = true;
1169 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
1171 opt_typecheckverbose = true;
1180 case OPT_FULLVERSION:
1184 case OPT_SHOWVERSION:
1192 #if defined(ENABLE_VERIFIER)
1198 #if defined(ENABLE_STATISTICS)
1200 opt_getcompilingtime = true;
1201 opt_getloadingtime = true;
1214 for (i = 0; i < strlen(opt_arg); i++) {
1215 switch (opt_arg[i]) {
1217 checkbounds = false;
1230 makeinitializations = false;
1237 #if !defined(NDEBUG)
1241 makeinitializations = false;
1246 opt_method = opt_arg;
1247 makeinitializations = false;
1251 opt_signature = opt_arg;
1255 case OPT_SHOW: /* Display options */
1256 for (i = 0; i < strlen(opt_arg); i++) {
1257 switch (opt_arg[i]) {
1259 showconstantpool = true;
1271 opt_showintermediate = true;
1272 compileverbose = true;
1275 #if defined(ENABLE_DISASSEMBLER)
1277 opt_showdisassemble = true;
1278 compileverbose = true;
1282 opt_shownops = true;
1286 opt_showexceptionstubs = true;
1290 opt_shownativestub = true;
1295 opt_showddatasegment = true;
1304 #if defined(ENABLE_LOOP)
1310 #if defined(ENABLE_INLINING)
1311 #if defined(ENABLE_INLINING_DEBUG)
1312 case OPT_INLINE_DEBUG_ALL:
1313 opt_inline_debug_all = true;
1315 case OPT_INLINE_DEBUG_END:
1316 opt_inline_debug_end_counter = atoi(opt_arg);
1318 case OPT_INLINE_DEBUG_MIN:
1319 opt_inline_debug_min_size = atoi(opt_arg);
1321 case OPT_INLINE_DEBUG_MAX:
1322 opt_inline_debug_max_size = atoi(opt_arg);
1324 case OPT_INLINE_REPLACE_VERBOSE:
1325 opt_replace_verbose = 1;
1327 case OPT_INLINE_REPLACE_VERBOSE2:
1328 opt_replace_verbose = 2;
1330 #endif /* defined(ENABLE_INLINING_DEBUG) */
1331 #if !defined(NDEBUG)
1332 case OPT_INLINE_LOG:
1333 opt_inline_debug_log = true;
1335 #endif /* !defined(NDEBUG) */
1338 opt_inlining = true;
1340 #endif /* defined(ENABLE_INLINING) */
1342 #if defined(ENABLE_IFCONV)
1348 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1363 options_xx(opt_arg);
1367 /* currently ignored */
1371 /* currently ignored */
1375 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = true;
1379 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = false;
1382 #if defined(ENABLE_PROFILING)
1383 case OPT_PROF_OPTION:
1384 /* use <= to get the last \0 too */
1386 for (i = 0, j = 0; i <= strlen(opt_arg); i++) {
1387 if (opt_arg[i] == ',')
1390 if (opt_arg[i] == '\0') {
1391 if (strcmp("bb", opt_arg + j) == 0)
1395 printf("Unknown option: -Xprof:%s\n", opt_arg + j);
1399 /* set k to next char */
1412 #if defined(ENABLE_JIT)
1415 printf("-Xjit option not enabled.\n");
1421 #if defined(ENABLE_INTRP)
1424 printf("-Xint option not enabled.\n");
1429 #if defined(ENABLE_INTRP)
1430 case OPT_STATIC_SUPERS:
1431 opt_static_supers = atoi(opt_arg);
1434 case OPT_NO_DYNAMIC:
1435 opt_no_dynamic = true;
1438 case OPT_NO_REPLICATION:
1439 opt_no_replication = true;
1442 case OPT_NO_QUICKSUPER:
1443 opt_no_quicksuper = true;
1451 #if defined(ENABLE_DEBUG_FILTER)
1452 case OPT_FILTER_VERBOSECALL_INCLUDE:
1453 opt_filter_verbosecall_include = opt_arg;
1456 case OPT_FILTER_VERBOSECALL_EXCLUDE:
1457 opt_filter_verbosecall_exclude = opt_arg;
1460 case OPT_FILTER_SHOW_METHOD:
1461 opt_filter_show_method = opt_arg;
1466 printf("Unknown option: %s\n",
1467 vm_args->options[opt_index].optionString);
1472 /* get the main class *****************************************************/
1474 if (opt_index < vm_args->nOptions) {
1475 mainstring = vm_args->options[opt_index++].optionString;
1477 /* Put the jar file into the classpath (if any). */
1479 if (opt_jar == true) {
1480 /* free old classpath */
1482 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1484 /* put jarfile into classpath */
1486 _Jv_classpath = MNEW(char, strlen(mainstring) + strlen("0"));
1488 strcpy(_Jv_classpath, mainstring);
1491 /* replace .'s with /'s in classname */
1493 for (i = strlen(mainstring) - 1; i >= 0; i--)
1494 if (mainstring[i] == '.')
1495 mainstring[i] = '/';
1499 #if defined(ENABLE_JVMTI)
1501 jvmti_set_phase(JVMTI_PHASE_ONLOAD);
1502 jvmti_agentload(agentarg, agentbypath, &handle, &libname);
1505 MFREE(agentarg, char, strlen(agentarg));
1507 jvmti_set_phase(JVMTI_PHASE_PRIMORDIAL);
1511 /* initialize this JVM ****************************************************/
1513 vm_initializing = true;
1515 #if defined(ENABLE_THREADS)
1516 /* pre-initialize some core thread stuff, like the stopworldlock,
1517 thus this has to happen _before_ gc_init()!!! */
1522 /* initialize the garbage collector */
1524 gc_init(opt_heapmaxsize, opt_heapstartsize);
1526 /* install architecture dependent signal handlers */
1529 vm_abort("vm_create: signal_init failed");
1531 #if defined(ENABLE_INTRP)
1532 /* Allocate main thread stack on the Java heap. */
1535 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1536 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1540 /* AFTER: threads_preinit */
1543 vm_abort("vm_create: string_init failed");
1545 /* AFTER: threads_preinit */
1548 vm_abort("vm_create: utf8_init failed");
1550 /* AFTER: thread_preinit */
1553 vm_abort("vm_create: suck_init failed");
1555 suck_add_from_property("java.endorsed.dirs");
1557 /* Now we have all options handled and we can print the version
1560 AFTER: suck_add_from_property("java.endorsed.dirs"); */
1565 /* AFTER: utf8_init */
1567 suck_add(_Jv_bootclasspath);
1569 /* Now re-set some of the properties that may have changed. This
1570 must be done after _all_ environment variables have been
1571 processes (e.g. -jar handling).
1573 AFTER: suck_add_from_property, since it may change the
1574 _Jv_bootclasspath pointer. */
1576 if (!properties_postinit())
1577 vm_abort("vm_create: properties_postinit failed");
1579 /* initialize the classcache hashtable stuff: lock, hashtable
1580 (must be done _after_ threads_preinit) */
1582 if (!classcache_init())
1583 vm_abort("vm_create: classcache_init failed");
1585 /* initialize the memory subsystem (must be done _after_
1589 vm_abort("vm_create: memory_init failed");
1591 /* initialize the finalizer stuff (must be done _after_
1594 if (!finalizer_init())
1595 vm_abort("vm_create: finalizer_init failed");
1597 /* initialize the codegen subsystems */
1601 /* initializes jit compiler */
1605 /* machine dependent initialization */
1607 #if defined(ENABLE_JIT)
1608 # if defined(ENABLE_INTRP)
1618 /* initialize the loader subsystems (must be done _after_
1622 vm_abort("vm_create: loader_init failed");
1624 /* Link some important VM classes. */
1625 /* AFTER: utf8_init */
1628 vm_abort("vm_create: linker_init failed");
1630 if (!primitive_init())
1631 vm_abort("vm_create: primitive_init failed");
1633 /* Initialize the native subsystem. */
1636 vm_abort("vm_create: native_init failed");
1638 if (!exceptions_init())
1639 vm_abort("vm_create: exceptions_init failed");
1641 if (!builtin_init())
1642 vm_abort("vm_create: builtin_init failed");
1644 #if defined(ENABLE_JNI)
1645 /* Initialize the JNI subsystem (must be done _before_
1646 threads_init, as threads_init can call JNI methods
1647 (e.g. NewGlobalRef). */
1650 vm_abort("vm_create: jni_init failed");
1653 #if defined(ENABLE_THREADS)
1654 if (!threads_init())
1655 vm_abort("vm_create: threads_init failed");
1658 #if defined(ENABLE_PROFILING)
1659 /* initialize profiling */
1661 if (!profile_init())
1662 vm_abort("vm_create: profile_init failed");
1665 #if defined(ENABLE_THREADS)
1666 /* initialize recompilation */
1668 if (!recompile_init())
1669 vm_abort("vm_create: recompile_init failed");
1671 /* start the signal handler thread */
1673 #if defined(__LINUX__)
1674 /* XXX Remove for exact-GC. */
1675 if (threads_pthreads_implementation_nptl)
1677 if (!signal_start_thread())
1678 vm_abort("vm_create: signal_start_thread failed");
1680 /* finally, start the finalizer thread */
1682 if (!finalizer_start_thread())
1683 vm_abort("vm_create: finalizer_start_thread failed");
1685 # if !defined(NDEBUG)
1686 /* start the memory profiling thread */
1688 if (opt_verbosememory)
1689 if (!memory_start_thread())
1690 vm_abort("vm_create: memory_start_thread failed");
1693 /* start the recompilation thread (must be done before the
1694 profiling thread) */
1696 if (!recompile_start_thread())
1697 vm_abort("vm_create: recompile_start_thread failed");
1699 # if defined(ENABLE_PROFILING)
1700 /* start the profile sampling thread */
1703 /* if (!profile_start_thread()) */
1704 /* vm_abort("vm_create: profile_start_thread failed"); */
1708 #if defined(ENABLE_JVMTI)
1710 /* add agent library to native library hashtable */
1711 native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
1715 /* increment the number of VMs */
1719 /* initialization is done */
1721 vm_initializing = false;
1723 /* everything's ok */
1729 /* vm_run **********************************************************************
1731 Runs the main-method of the passed class.
1733 *******************************************************************************/
1735 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1738 classinfo *mainclass;
1739 java_objectheader *e;
1741 java_objectarray *oa;
1744 java_objectheader *s;
1748 #if !defined(NDEBUG)
1754 if (opt_method != NULL) {
1755 vm_compile_method();
1758 #endif /* !defined(NDEBUG) */
1760 /* should we run the main-method? */
1762 if (mainstring == NULL)
1765 /* set return value to OK */
1769 if (opt_jar == true) {
1770 /* open jar file with java.util.jar.JarFile */
1772 mainstring = vm_get_mainclass_from_jar(mainstring);
1774 if (mainstring == NULL)
1778 /* load the main class */
1780 mainutf = utf_new_char(mainstring);
1782 #if defined(ENABLE_JAVAME_CLDC1_1)
1783 mainclass = load_class_bootstrap(mainutf);
1785 mainclass = load_class_from_sysloader(mainutf);
1788 /* error loading class */
1790 e = exceptions_get_and_clear_exception();
1792 if ((e != NULL) || (mainclass == NULL)) {
1793 exceptions_throw_noclassdeffounderror_cause(e);
1794 exceptions_print_stacktrace();
1798 if (!link_class(mainclass)) {
1799 exceptions_print_stacktrace();
1803 /* find the `main' method of the main class */
1805 m = class_resolveclassmethod(mainclass,
1806 utf_new_char("main"),
1807 utf_new_char("([Ljava/lang/String;)V"),
1808 class_java_lang_Object,
1811 if (exceptions_get_exception()) {
1812 exceptions_print_stacktrace();
1816 /* there is no main method or it isn't static */
1818 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
1819 exceptions_clear_exception();
1820 exceptions_throw_nosuchmethoderror(mainclass,
1821 utf_new_char("main"),
1822 utf_new_char("([Ljava/lang/String;)V"));
1824 exceptions_print_stacktrace();
1828 /* build argument array */
1830 oalength = vm_args->nOptions - opt_index;
1832 oa = builtin_anewarray(oalength, class_java_lang_String);
1834 for (i = 0; i < oalength; i++) {
1835 u = utf_new_char(vm_args->options[opt_index + i].optionString);
1836 s = javastring_new(u);
1841 #ifdef TYPEINFO_DEBUG_TEST
1842 /* test the typeinfo system */
1845 /*class_showmethods(currentThread->group->header.vftbl->class); */
1847 #if defined(ENABLE_JVMTI)
1848 jvmti_set_phase(JVMTI_PHASE_LIVE);
1851 /* set ThreadMXBean variables */
1853 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
1854 _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
1856 if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
1857 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
1858 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
1859 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
1861 /* start the main thread */
1863 (void) vm_call_method(m, NULL, oa);
1865 /* exception occurred? */
1867 if (exceptions_get_exception()) {
1868 exceptions_print_stacktrace();
1872 /* unload the JavaVM */
1874 (void) vm_destroy(vm);
1882 /* vm_destroy ******************************************************************
1884 Unloads a Java VM and reclaims its resources.
1886 *******************************************************************************/
1888 s4 vm_destroy(JavaVM *vm)
1890 #if defined(ENABLE_THREADS)
1891 threads_join_all_threads();
1894 /* everything's ok */
1900 /* vm_exit *********************************************************************
1902 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1904 *******************************************************************************/
1906 void vm_exit(s4 status)
1910 /* signal that we are exiting */
1914 assert(class_java_lang_System);
1915 assert(class_java_lang_System->state & CLASS_LOADED);
1917 #if defined(ENABLE_JVMTI)
1918 if (jvmti || (dbgcom!=NULL)) {
1919 jvmti_set_phase(JVMTI_PHASE_DEAD);
1920 if (jvmti) jvmti_agentunload();
1924 if (!link_class(class_java_lang_System)) {
1925 exceptions_print_stacktrace();
1929 /* call java.lang.System.exit(I)V */
1931 m = class_resolveclassmethod(class_java_lang_System,
1932 utf_new_char("exit"),
1934 class_java_lang_Object,
1938 exceptions_print_stacktrace();
1942 /* call the exit function with passed exit status */
1944 (void) vm_call_method(m, NULL, status);
1946 /* If we had an exception, just ignore the exception and exit with
1949 vm_shutdown(status);
1953 /* vm_shutdown *****************************************************************
1955 Terminates the system immediately without freeing memory explicitly
1956 (to be used only for abnormal termination).
1958 *******************************************************************************/
1960 void vm_shutdown(s4 status)
1963 #if defined(ENABLE_STATISTICS)
1964 || opt_getcompilingtime || opt_stat
1968 log_text("CACAO terminated by shutdown");
1969 dolog("Exit status: %d\n", (s4) status);
1973 #if defined(ENABLE_JVMTI)
1974 /* terminate cacaodbgserver */
1976 pthread_mutex_lock(&dbgcomlock);
1978 pthread_mutex_unlock(&dbgcomlock);
1979 jvmti_cacaodbgserver_quit();
1987 /* vm_exit_handler *************************************************************
1989 The exit_handler function is called upon program termination.
1991 ATTENTION: Don't free system resources here! Some threads may still
1992 be running as this is called from VMRuntime.exit(). The OS does the
1995 *******************************************************************************/
1997 void vm_exit_handler(void)
1999 #if !defined(NDEBUG)
2001 class_showmethods(mainclass);
2003 if (showconstantpool)
2004 class_showconstantpool(mainclass);
2009 # if defined(ENABLE_PROFILING)
2011 profile_printstats();
2013 #endif /* !defined(NDEBUG) */
2015 #if defined(ENABLE_RT_TIMING)
2016 rt_timing_print_time_stats(stderr);
2019 #if defined(ENABLE_CYCLES_STATS)
2020 builtin_print_cycles_stats(stderr);
2021 stacktrace_print_cycles_stats(stderr);
2025 #if defined(ENABLE_STATISTICS)
2026 || opt_getcompilingtime || opt_stat
2030 log_text("CACAO terminated");
2032 #if defined(ENABLE_STATISTICS)
2035 #ifdef TYPECHECK_STATISTICS
2036 typecheck_print_statistics(get_logfile());
2040 if (opt_getcompilingtime)
2042 #endif /* defined(ENABLE_STATISTICS) */
2044 /* vm_print_profile(stderr);*/
2048 /* vm_abort ********************************************************************
2050 Prints an error message and aborts the VM.
2052 *******************************************************************************/
2054 void vm_abort(const char *text, ...)
2058 /* print the log message */
2063 log_vprint(text, ap);
2068 /* now abort the VM */
2074 /* vm_get_mainclass_from_jar ***************************************************
2076 Gets the name of the main class from a JAR's manifest file.
2078 *******************************************************************************/
2080 static char *vm_get_mainclass_from_jar(char *mainstring)
2083 java_objectheader *o;
2085 java_objectheader *s;
2087 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
2090 exceptions_print_stacktrace();
2094 /* create JarFile object */
2099 exceptions_print_stacktrace();
2103 m = class_resolveclassmethod(c,
2105 utf_java_lang_String__void,
2106 class_java_lang_Object,
2110 exceptions_print_stacktrace();
2114 s = javastring_new_from_ascii(mainstring);
2116 (void) vm_call_method(m, o, s);
2118 if (exceptions_get_exception()) {
2119 exceptions_print_stacktrace();
2123 /* get manifest object */
2125 m = class_resolveclassmethod(c,
2126 utf_new_char("getManifest"),
2127 utf_new_char("()Ljava/util/jar/Manifest;"),
2128 class_java_lang_Object,
2132 exceptions_print_stacktrace();
2136 o = vm_call_method(m, o);
2139 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
2144 /* get Main Attributes */
2146 m = class_resolveclassmethod(o->vftbl->class,
2147 utf_new_char("getMainAttributes"),
2148 utf_new_char("()Ljava/util/jar/Attributes;"),
2149 class_java_lang_Object,
2153 exceptions_print_stacktrace();
2157 o = vm_call_method(m, o);
2160 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
2165 /* get property Main-Class */
2167 m = class_resolveclassmethod(o->vftbl->class,
2168 utf_new_char("getValue"),
2169 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
2170 class_java_lang_Object,
2174 exceptions_print_stacktrace();
2178 s = javastring_new_from_ascii("Main-Class");
2180 o = vm_call_method(m, o, s);
2183 exceptions_print_stacktrace();
2187 return javastring_tochar(o);
2191 /* vm_compile_all **************************************************************
2193 Compile all methods found in the bootclasspath.
2195 *******************************************************************************/
2197 #if !defined(NDEBUG)
2198 static void vm_compile_all(void)
2203 classcache_name_entry *nmen;
2204 classcache_class_entry *clsen;
2207 /* create all classes found in the bootclasspath */
2208 /* XXX currently only works with zip/jar's */
2210 loader_load_all_classes();
2212 /* link all classes */
2214 for (slot = 0; slot < hashtable_classcache.size; slot++) {
2215 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
2217 for (; nmen; nmen = nmen->hashlink) {
2218 /* iterate over all class entries */
2220 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
2221 c = clsen->classobj;
2226 if (!(c->state & CLASS_LINKED)) {
2227 if (!link_class(c)) {
2228 fprintf(stderr, "Error linking: ");
2229 utf_fprint_printable_ascii_classname(stderr, c->name);
2230 fprintf(stderr, "\n");
2232 /* print out exception and cause */
2234 exceptions_print_current_exception();
2236 /* goto next class */
2242 /* compile all class methods */
2244 for (i = 0; i < c->methodscount; i++) {
2245 m = &(c->methods[i]);
2247 if (m->jcode != NULL) {
2248 if (!jit_compile(m)) {
2249 fprintf(stderr, "Error compiling: ");
2250 utf_fprint_printable_ascii_classname(stderr, c->name);
2251 fprintf(stderr, ".");
2252 utf_fprint_printable_ascii(stderr, m->name);
2253 utf_fprint_printable_ascii(stderr, m->descriptor);
2254 fprintf(stderr, "\n");
2256 /* print out exception and cause */
2258 exceptions_print_current_exception();
2266 #endif /* !defined(NDEBUG) */
2269 /* vm_compile_method ***********************************************************
2271 Compile a specific method.
2273 *******************************************************************************/
2275 #if !defined(NDEBUG)
2276 static void vm_compile_method(void)
2280 /* create, load and link the main class */
2282 mainclass = load_class_bootstrap(utf_new_char(mainstring));
2284 if (mainclass == NULL)
2285 exceptions_print_stacktrace();
2287 if (!link_class(mainclass))
2288 exceptions_print_stacktrace();
2290 if (opt_signature != NULL) {
2291 m = class_resolveclassmethod(mainclass,
2292 utf_new_char(opt_method),
2293 utf_new_char(opt_signature),
2298 m = class_resolveclassmethod(mainclass,
2299 utf_new_char(opt_method),
2306 vm_abort("vm_compile_method: java.lang.NoSuchMethodException: %s.%s",
2307 opt_method, opt_signature ? opt_signature : "");
2311 #endif /* !defined(NDEBUG) */
2314 /* vm_vmargs_from_valist *******************************************************
2318 *******************************************************************************/
2320 static void vm_vmargs_from_valist(methodinfo *m, java_objectheader *o,
2321 vm_arg *vmargs, va_list ap)
2323 typedesc *paramtypes;
2326 paramtypes = m->parseddesc->paramtypes;
2328 /* if method is non-static fill first block and skip `this' pointer */
2333 /* the `this' pointer */
2334 vmargs[0].type = TYPE_ADR;
2335 vmargs[0].data.l = (u8) (ptrint) o;
2341 for (; i < m->parseddesc->paramcount; i++, paramtypes++) {
2342 switch (paramtypes->type) {
2344 vmargs[i].type = TYPE_INT;
2345 vmargs[i].data.l = (s8) va_arg(ap, s4);
2349 vmargs[i].type = TYPE_LNG;
2350 vmargs[i].data.l = (s8) va_arg(ap, s8);
2354 vmargs[i].type = TYPE_FLT;
2355 #if defined(__ALPHA__)
2356 /* this keeps the assembler function much simpler */
2358 vmargs[i].data.d = (jdouble) va_arg(ap, jdouble);
2360 vmargs[i].data.f = (jfloat) va_arg(ap, jdouble);
2365 vmargs[i].type = TYPE_DBL;
2366 vmargs[i].data.d = (jdouble) va_arg(ap, jdouble);
2370 vmargs[i].type = TYPE_ADR;
2371 vmargs[i].data.l = (u8) (ptrint) va_arg(ap, void*);
2378 /* vm_vmargs_from_jvalue *******************************************************
2382 *******************************************************************************/
2384 static void vm_vmargs_from_jvalue(methodinfo *m, java_objectheader *o,
2385 vm_arg *vmargs, jvalue *args)
2387 typedesc *paramtypes;
2391 paramtypes = m->parseddesc->paramtypes;
2393 /* if method is non-static fill first block and skip `this' pointer */
2398 /* the `this' pointer */
2399 vmargs[0].type = TYPE_ADR;
2400 vmargs[0].data.l = (u8) (ptrint) o;
2406 for (j = 0; i < m->parseddesc->paramcount; i++, j++, paramtypes++) {
2407 switch (paramtypes->decltype) {
2409 vmargs[i].type = TYPE_INT;
2410 vmargs[i].data.l = (s8) args[j].i;
2414 vmargs[i].type = TYPE_LNG;
2415 vmargs[i].data.l = (s8) args[j].j;
2419 vmargs[i].type = TYPE_FLT;
2420 #if defined(__ALPHA__)
2421 /* this keeps the assembler function much simpler */
2423 vmargs[i].data.d = (jdouble) args[j].f;
2425 vmargs[i].data.f = args[j].f;
2430 vmargs[i].type = TYPE_DBL;
2431 vmargs[i].data.d = args[j].d;
2435 vmargs[i].type = TYPE_ADR;
2436 vmargs[i].data.l = (u8) (ptrint) args[j].l;
2443 /* vm_call_method **************************************************************
2445 Calls a Java method with a variable number of arguments and returns
2448 *******************************************************************************/
2450 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
2453 java_objectheader *ro;
2456 ro = vm_call_method_valist(m, o, ap);
2463 /* vm_call_method_valist *******************************************************
2465 Calls a Java method with a variable number of arguments, passed via
2466 a va_list, and returns an address.
2468 *******************************************************************************/
2470 java_objectheader *vm_call_method_valist(methodinfo *m, java_objectheader *o,
2475 java_objectheader *ro;
2478 /* mark start of dump memory area */
2480 dumpsize = dump_size();
2482 /* get number of Java method arguments */
2484 vmargscount = m->parseddesc->paramcount;
2486 /* allocate vm_arg array */
2488 vmargs = DMNEW(vm_arg, vmargscount);
2490 /* fill the vm_arg array from a va_list */
2492 vm_vmargs_from_valist(m, o, vmargs, ap);
2494 /* call the Java method */
2496 ro = vm_call_method_vmarg(m, vmargscount, vmargs);
2498 /* release dump area */
2500 dump_release(dumpsize);
2506 /* vm_call_method_jvalue *******************************************************
2508 Calls a Java method with a variable number of arguments, passed via
2509 a jvalue array, and returns an address.
2511 *******************************************************************************/
2513 java_objectheader *vm_call_method_jvalue(methodinfo *m, java_objectheader *o,
2518 java_objectheader *ro;
2521 /* mark start of dump memory area */
2523 dumpsize = dump_size();
2525 /* get number of Java method arguments */
2527 vmargscount = m->parseddesc->paramcount;
2529 /* allocate vm_arg array */
2531 vmargs = DMNEW(vm_arg, vmargscount);
2533 /* fill the vm_arg array from a va_list */
2535 vm_vmargs_from_jvalue(m, o, vmargs, args);
2537 /* call the Java method */
2539 ro = vm_call_method_vmarg(m, vmargscount, vmargs);
2541 /* release dump area */
2543 dump_release(dumpsize);
2549 /* vm_call_method_vmarg ********************************************************
2551 Calls a Java method with a variable number of arguments, passed via
2552 a vm_arg array, and returns an address.
2554 *******************************************************************************/
2556 java_objectheader *vm_call_method_vmarg(methodinfo *m, s4 vmargscount,
2559 java_objectheader *o;
2561 STATISTICS(count_calls_native_to_java++);
2563 #if defined(ENABLE_JIT)
2564 # if defined(ENABLE_INTRP)
2566 o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
2569 o = asm_vm_call_method(m, vmargscount, vmargs);
2571 o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
2578 /* vm_call_method_int **********************************************************
2580 Calls a Java method with a variable number of arguments and returns
2583 *******************************************************************************/
2585 s4 vm_call_method_int(methodinfo *m, java_objectheader *o, ...)
2591 i = vm_call_method_int_valist(m, o, ap);
2598 /* vm_call_method_int_valist ***************************************************
2600 Calls a Java method with a variable number of arguments, passed via
2601 a va_list, and returns an integer (s4).
2603 *******************************************************************************/
2605 s4 vm_call_method_int_valist(methodinfo *m, java_objectheader *o, va_list ap)
2612 /* mark start of dump memory area */
2614 dumpsize = dump_size();
2616 /* get number of Java method arguments */
2618 vmargscount = m->parseddesc->paramcount;
2620 /* allocate vm_arg array */
2622 vmargs = DMNEW(vm_arg, vmargscount);
2624 /* fill the vm_arg array from a va_list */
2626 vm_vmargs_from_valist(m, o, vmargs, ap);
2628 /* call the Java method */
2630 i = vm_call_method_int_vmarg(m, vmargscount, vmargs);
2632 /* release dump area */
2634 dump_release(dumpsize);
2640 /* vm_call_method_int_jvalue ***************************************************
2642 Calls a Java method with a variable number of arguments, passed via
2643 a jvalue array, and returns an integer (s4).
2645 *******************************************************************************/
2647 s4 vm_call_method_int_jvalue(methodinfo *m, java_objectheader *o, jvalue *args)
2654 /* mark start of dump memory area */
2656 dumpsize = dump_size();
2658 /* get number of Java method arguments */
2660 vmargscount = m->parseddesc->paramcount;
2662 /* allocate vm_arg array */
2664 vmargs = DMNEW(vm_arg, vmargscount);
2666 /* fill the vm_arg array from a va_list */
2668 vm_vmargs_from_jvalue(m, o, vmargs, args);
2670 /* call the Java method */
2672 i = vm_call_method_int_vmarg(m, vmargscount, vmargs);
2674 /* release dump area */
2676 dump_release(dumpsize);
2682 /* vm_call_method_int_vmarg ****************************************************
2684 Calls a Java method with a variable number of arguments, passed via
2685 a vm_arg array, and returns an integer (s4).
2687 *******************************************************************************/
2689 s4 vm_call_method_int_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
2693 STATISTICS(count_calls_native_to_java++);
2695 #if defined(ENABLE_JIT)
2696 # if defined(ENABLE_INTRP)
2698 i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
2701 i = asm_vm_call_method_int(m, vmargscount, vmargs);
2703 i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
2710 /* vm_call_method_long *********************************************************
2712 Calls a Java method with a variable number of arguments and returns
2715 *******************************************************************************/
2717 s8 vm_call_method_long(methodinfo *m, java_objectheader *o, ...)
2723 l = vm_call_method_long_valist(m, o, ap);
2730 /* vm_call_method_long_valist **************************************************
2732 Calls a Java method with a variable number of arguments, passed via
2733 a va_list, and returns a long (s8).
2735 *******************************************************************************/
2737 s8 vm_call_method_long_valist(methodinfo *m, java_objectheader *o, va_list ap)
2744 /* mark start of dump memory area */
2746 dumpsize = dump_size();
2748 /* get number of Java method arguments */
2750 vmargscount = m->parseddesc->paramcount;
2752 /* allocate vm_arg array */
2754 vmargs = DMNEW(vm_arg, vmargscount);
2756 /* fill the vm_arg array from a va_list */
2758 vm_vmargs_from_valist(m, o, vmargs, ap);
2760 /* call the Java method */
2762 l = vm_call_method_long_vmarg(m, vmargscount, vmargs);
2764 /* release dump area */
2766 dump_release(dumpsize);
2772 /* vm_call_method_long_jvalue **************************************************
2774 Calls a Java method with a variable number of arguments, passed via
2775 a jvalue array, and returns a long (s8).
2777 *******************************************************************************/
2779 s8 vm_call_method_long_jvalue(methodinfo *m, java_objectheader *o, jvalue *args)
2786 /* mark start of dump memory area */
2788 dumpsize = dump_size();
2790 /* get number of Java method arguments */
2792 vmargscount = m->parseddesc->paramcount;
2794 /* allocate vm_arg array */
2796 vmargs = DMNEW(vm_arg, vmargscount);
2798 /* fill the vm_arg array from a va_list */
2800 vm_vmargs_from_jvalue(m, o, vmargs, args);
2802 /* call the Java method */
2804 l = vm_call_method_long_vmarg(m, vmargscount, vmargs);
2806 /* release dump area */
2808 dump_release(dumpsize);
2814 /* vm_call_method_long_vmarg ***************************************************
2816 Calls a Java method with a variable number of arguments, passed via
2817 a vm_arg array, and returns a long (s8).
2819 *******************************************************************************/
2821 s8 vm_call_method_long_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
2825 STATISTICS(count_calls_native_to_java++);
2827 #if defined(ENABLE_JIT)
2828 # if defined(ENABLE_INTRP)
2830 l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
2833 l = asm_vm_call_method_long(m, vmargscount, vmargs);
2835 l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
2842 /* vm_call_method_float ********************************************************
2844 Calls a Java method with a variable number of arguments and returns
2847 *******************************************************************************/
2849 float vm_call_method_float(methodinfo *m, java_objectheader *o, ...)
2855 f = vm_call_method_float_valist(m, o, ap);
2862 /* vm_call_method_float_valist *************************************************
2864 Calls a Java method with a variable number of arguments, passed via
2865 a va_list, and returns a float.
2867 *******************************************************************************/
2869 float vm_call_method_float_valist(methodinfo *m, java_objectheader *o,
2877 /* mark start of dump memory area */
2879 dumpsize = dump_size();
2881 /* get number of Java method arguments */
2883 vmargscount = m->parseddesc->paramcount;
2885 /* allocate vm_arg array */
2887 vmargs = DMNEW(vm_arg, vmargscount);
2889 /* fill the vm_arg array from a va_list */
2891 vm_vmargs_from_valist(m, o, vmargs, ap);
2893 /* call the Java method */
2895 f = vm_call_method_float_vmarg(m, vmargscount, vmargs);
2897 /* release dump area */
2899 dump_release(dumpsize);
2905 /* vm_call_method_float_jvalue *************************************************
2907 Calls a Java method with a variable number of arguments, passed via
2908 a jvalue array, and returns a float.
2910 *******************************************************************************/
2912 float vm_call_method_float_jvalue(methodinfo *m, java_objectheader *o,
2920 /* mark start of dump memory area */
2922 dumpsize = dump_size();
2924 /* get number of Java method arguments */
2926 vmargscount = m->parseddesc->paramcount;
2928 /* allocate vm_arg array */
2930 vmargs = DMNEW(vm_arg, vmargscount);
2932 /* fill the vm_arg array from a va_list */
2934 vm_vmargs_from_jvalue(m, o, vmargs, args);
2936 /* call the Java method */
2938 f = vm_call_method_float_vmarg(m, vmargscount, vmargs);
2940 /* release dump area */
2942 dump_release(dumpsize);
2948 /* vm_call_method_float_vmarg **************************************************
2950 Calls a Java method with a variable number of arguments and returns
2953 *******************************************************************************/
2955 float vm_call_method_float_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
2959 STATISTICS(count_calls_native_to_java++);
2961 #if defined(ENABLE_JIT)
2962 # if defined(ENABLE_INTRP)
2964 f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
2967 f = asm_vm_call_method_float(m, vmargscount, vmargs);
2969 f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
2976 /* vm_call_method_double *******************************************************
2978 Calls a Java method with a variable number of arguments and returns
2981 *******************************************************************************/
2983 double vm_call_method_double(methodinfo *m, java_objectheader *o, ...)
2989 d = vm_call_method_double_valist(m, o, ap);
2996 /* vm_call_method_double_valist ************************************************
2998 Calls a Java method with a variable number of arguments, passed via
2999 a va_list, and returns a double.
3001 *******************************************************************************/
3003 double vm_call_method_double_valist(methodinfo *m, java_objectheader *o,
3011 /* mark start of dump memory area */
3013 dumpsize = dump_size();
3015 /* get number of Java method arguments */
3017 vmargscount = m->parseddesc->paramcount;
3019 /* allocate vm_arg array */
3021 vmargs = DMNEW(vm_arg, vmargscount);
3023 /* fill the vm_arg array from a va_list */
3025 vm_vmargs_from_valist(m, o, vmargs, ap);
3027 /* call the Java method */
3029 d = vm_call_method_double_vmarg(m, vmargscount, vmargs);
3031 /* release dump area */
3033 dump_release(dumpsize);
3039 /* vm_call_method_double_jvalue ************************************************
3041 Calls a Java method with a variable number of arguments, passed via
3042 a jvalue array, and returns a double.
3044 *******************************************************************************/
3046 double vm_call_method_double_jvalue(methodinfo *m, java_objectheader *o,
3054 /* mark start of dump memory area */
3056 dumpsize = dump_size();
3058 /* get number of Java method arguments */
3060 vmargscount = m->parseddesc->paramcount;
3062 /* allocate vm_arg array */
3064 vmargs = DMNEW(vm_arg, vmargscount);
3066 /* fill the vm_arg array from a va_list */
3068 vm_vmargs_from_jvalue(m, o, vmargs, args);
3070 /* call the Java method */
3072 d = vm_call_method_double_vmarg(m, vmargscount, vmargs);
3074 /* release dump area */
3076 dump_release(dumpsize);
3082 /* vm_call_method_double_vmarg *************************************************
3084 Calls a Java method with a variable number of arguments and returns
3087 *******************************************************************************/
3089 double vm_call_method_double_vmarg(methodinfo *m, s4 vmargscount,
3094 STATISTICS(count_calls_native_to_java++);
3096 #if defined(ENABLE_JIT)
3097 # if defined(ENABLE_INTRP)
3099 d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
3102 d = asm_vm_call_method_double(m, vmargscount, vmargs);
3104 d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
3112 * These are local overrides for various environment variables in Emacs.
3113 * Please do not remove this and leave it at the end of the file, where
3114 * Emacs will automagically detect them.
3115 * ---------------------------------------------------------------------
3118 * indent-tabs-mode: t