1 /* src/vm/finalizer.c - finalizer linked list and thread
3 Copyright (C) 1996-2005, 2006 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 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
31 $Id: finalizer.c 4357 2006-01-22 23:33:38Z twisti $
44 #include "mm/memory.h"
45 #include "native/jni.h"
46 #include "native/native.h"
48 #if defined(USE_THREADS)
49 # if defined(NATIVE_THREADS)
50 # include "threads/native/threads.h"
52 # include "threads/green/threads.h"
53 # include "threads/green/locks.h"
57 #include "vm/classcache.h"
58 #include "vm/exceptions.h"
59 #include "vm/finalizer.h"
60 #include "vm/global.h"
61 #include "vm/initialize.h"
62 #include "vm/options.h"
63 #include "vm/properties.h"
64 #include "vm/signallocal.h"
65 #include "vm/stringlocal.h"
68 #include "vm/jit/jit.h"
69 #include "vm/jit/asmpart.h"
70 #include "vm/jit/profile/profile.h"
71 #include "vm/rt-timing.h"
73 #if defined(ENABLE_JVMTI)
74 #include "native/jvmti/cacaodbg.h"
77 /* Invocation API variables ***************************************************/
79 _Jv_JavaVM *_Jv_jvm; /* denotes a Java VM */
80 _Jv_JNIEnv *_Jv_env; /* pointer to native method interface */
83 /* global variables ***********************************************************/
85 s4 vms = 0; /* number of VMs created */
87 bool vm_initializing = false;
88 bool vm_exiting = false;
90 #if defined(ENABLE_INTRP)
91 u1 *intrp_main_stack = NULL;
94 void **stackbottom = NULL;
96 char *mainstring = NULL;
97 classinfo *mainclass = NULL;
99 char *specificmethodname = NULL;
100 char *specificsignature = NULL;
105 /* define heap sizes **********************************************************/
107 #define HEAP_MAXSIZE 64 * 1024 * 1024 /* default 64MB */
108 #define HEAP_STARTSIZE 2 * 1024 * 1024 /* default 2MB */
109 #define STACK_SIZE 128 * 1024 /* default 128kB */
112 /* define command line options ************************************************/
134 /* Java non-standard options */
155 #if defined(ENABLE_STATISTICS)
168 #if defined(ENABLE_VERIFIER)
170 #if defined(TYPECHECK_VERBOSE)
173 #endif /* defined(ENABLE_VERIFIER) */
176 /* optimization options */
178 #if defined(ENABLE_LOOP)
182 #if defined(ENABLE_IFCONV)
186 #if defined(ENABLE_LSRA)
190 #if defined(ENABLE_INLINING)
194 #if defined(ENABLE_INTRP)
195 /* interpreter options */
218 opt_struct opts[] = {
221 { "jar", false, OPT_JAR },
223 { "d32", false, OPT_D32 },
224 { "d64", false, OPT_D64 },
225 { "client", false, OPT_IGNORE },
226 { "server", false, OPT_IGNORE },
227 { "hotspot", false, OPT_IGNORE },
229 { "classpath", true, OPT_CLASSPATH },
230 { "cp", true, OPT_CLASSPATH },
231 { "D", true, OPT_D },
232 { "version", false, OPT_VERSION },
233 { "showversion", false, OPT_SHOWVERSION },
234 { "fullversion", false, OPT_FULLVERSION },
235 { "help", false, OPT_HELP },
236 { "?", false, OPT_HELP },
237 { "X", false, OPT_X },
239 { "noasyncgc", false, OPT_IGNORE },
240 #if defined(ENABLE_VERIFIER)
241 { "noverify", false, OPT_NOVERIFY },
243 { "v", false, OPT_VERBOSE1 },
244 { "verbose:", true, OPT_VERBOSE },
246 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
247 { "verbosetc", false, OPT_VERBOSETC },
249 #if defined(__ALPHA__)
250 { "noieee", false, OPT_NOIEEE },
252 { "softnull", false, OPT_SOFTNULL },
253 #if defined(ENABLE_STATISTICS)
254 { "time", false, OPT_TIME },
255 { "stat", false, OPT_STAT },
257 { "log", true, OPT_LOG },
258 { "c", true, OPT_CHECK },
259 { "l", false, OPT_LOAD },
260 { "eager", false, OPT_EAGER },
261 { "sig", true, OPT_SIGNATURE },
262 { "all", false, OPT_ALL },
263 #if defined(ENABLE_LOOP)
264 { "oloop", false, OPT_OLOOP },
266 #if defined(ENABLE_IFCONV)
267 { "ifconv", false, OPT_IFCONV },
269 #if defined(ENABLE_LSRA)
270 { "lsra", false, OPT_LSRA },
273 #if defined(ENABLE_INTRP)
274 /* interpreter options */
276 { "trace", false, OPT_TRACE },
277 { "static-supers", true, OPT_STATIC_SUPERS },
278 { "no-dynamic", false, OPT_NO_DYNAMIC },
279 { "no-replication", false, OPT_NO_REPLICATION },
280 { "no-quicksuper", false, OPT_NO_QUICKSUPER },
283 /* JVMTI Agent Command Line Options */
285 { "agentlib:", true, OPT_AGENTLIB },
286 { "agentpath:", true, OPT_AGENTPATH },
289 /* Java non-standard options */
291 { "Xjit", false, OPT_JIT },
292 { "Xint", false, OPT_INTRP },
293 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
294 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
295 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
297 { "Xdebug", false, OPT_DEBUG },
298 { "Xnoagent", false, OPT_NOAGENT },
299 { "Xrunjdwp", true, OPT_XRUNJDWP },
301 { "Xms", true, OPT_MS },
302 { "ms", true, OPT_MS },
303 { "Xmx", true, OPT_MX },
304 { "mx", true, OPT_MX },
305 { "Xss", true, OPT_SS },
306 { "ss", true, OPT_SS },
307 { "Xprof:", true, OPT_PROF_OPTION },
308 { "Xprof", false, OPT_PROF },
310 /* keep these at the end of the list */
312 #if defined(ENABLE_INLINING)
313 { "i", true, OPT_INLINING },
315 { "m", true, OPT_METHOD },
316 { "s", true, OPT_SHOW },
322 /* usage ***********************************************************************
324 Prints the correct usage syntax to stdout.
326 *******************************************************************************/
330 puts("Usage: cacao [-options] classname [arguments]");
331 puts(" (to run a class file)");
332 puts(" or cacao [-options] -jar jarfile [arguments]");
333 puts(" (to run a standalone jar file)\n");
335 puts("Java options:");
336 puts(" -d32 use 32-bit data model if available");
337 puts(" -d64 use 64-bit data model if available");
338 puts(" -client compatibility (currently ignored)");
339 puts(" -server compatibility (currently ignored)");
340 puts(" -hotspot compatibility (currently ignored)\n");
342 puts(" -cp <path> specify a path to look for classes");
343 puts(" -classpath <path> specify a path to look for classes");
344 puts(" -D<name>=<value> add an entry to the property list");
345 puts(" -verbose[:class|gc|jni] enable specific verbose output");
346 puts(" -version print product version and exit");
347 puts(" -fullversion print jpackage-compatible product version and exit");
348 puts(" -showversion print product version and continue");
349 puts(" -help, -? print this help message");
350 puts(" -X print help on non-standard Java options\n");
353 puts(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent");
354 puts(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent");
357 puts("CACAO options:\n");
358 puts(" -v write state-information");
359 puts(" -verbose[:call|exception]enable specific verbose output");
360 #ifdef TYPECHECK_VERBOSE
361 puts(" -verbosetc write debug messages while typechecking");
363 #if defined(__ALPHA__)
364 puts(" -noieee don't use ieee compliant arithmetic");
366 #if defined(ENABLE_VERIFIER)
367 puts(" -noverify don't verify classfiles");
369 puts(" -softnull use software nullpointer check");
370 #if defined(ENABLE_STATISTICS)
371 puts(" -time measure the runtime");
372 puts(" -stat detailed compiler statistics");
374 puts(" -log logfile specify a name for the logfile");
375 puts(" -c(heck)b(ounds) don't check array bounds");
376 puts(" s(ync) don't check for synchronization");
377 #if defined(ENABLE_LOOP)
378 puts(" -oloop optimize array accesses in loops");
380 puts(" -l don't start the class after loading");
381 puts(" -eager perform eager class loading and linking");
382 puts(" -all compile all methods, no execution");
383 puts(" -m compile only a specific method");
384 puts(" -sig specify signature for a specific method");
385 puts(" -s(how)a(ssembler) show disassembled listing");
386 puts(" c(onstants) show the constant pool");
387 puts(" d(atasegment) show data segment listing");
388 puts(" e(xceptionstubs) show disassembled exception stubs (only with -sa)");
389 puts(" i(ntermediate) show intermediate representation");
390 puts(" m(ethods) show class fields and methods");
391 puts(" n(ative) show disassembled native stubs");
392 puts(" u(tf) show the utf - hash");
393 #if defined(ENABLE_INLINING)
394 puts(" -i n(line) activate inlining");
395 puts(" v(irtual) inline virtual methods (uses/turns rt option on)");
396 puts(" e(exception) inline methods with exceptions");
397 puts(" p(aramopt) optimize argument renaming");
398 puts(" o(utsiders) inline methods of foreign classes");
399 #endif /* defined(ENABLE_INLINING) */
400 #if defined(ENABLE_IFCONV)
401 puts(" -ifconv use if-conversion");
403 #if defined(ENABLE_LSRA)
404 puts(" -lsra use linear scan register allocation");
407 /* exit with error code */
413 static void Xusage(void)
415 #if defined(ENABLE_JIT)
416 puts(" -Xjit JIT mode execution (default)");
418 #if defined(ENABLE_INTRP)
419 puts(" -Xint interpreter mode execution");
421 puts(" -Xbootclasspath:<zip/jar files and directories separated by :>");
422 puts(" value is set as bootstrap class path");
423 puts(" -Xbootclasspath/a:<zip/jar files and directories separated by :>");
424 puts(" value is appended to the bootstrap class path");
425 puts(" -Xbootclasspath/p:<zip/jar files and directories separated by :>");
426 puts(" value is prepended to the bootstrap class path");
427 puts(" -Xms<size> set the initial size of the heap (default: 2MB)");
428 puts(" -Xmx<size> set the maximum size of the heap (default: 64MB)");
429 puts(" -Xss<size> set the thread stack size (default: 128kB)");
430 puts(" -Xprof[:bb] collect and print profiling data");
431 #if defined(ENABLE_JVMTI)
432 /* -Xdebug option depend on gnu classpath JDWP options. options:
433 transport=dt_socket,address=<hostname:port>,server=(y|n),suspend(y|n) */
434 puts(" -Xdebug enable remote debugging\n");
435 puts(" -Xrunjdwp transport=[dt_socket|...],address=<hostname:port>,server=[y|n],suspend=[y|n]\n");
436 puts(" enable remote debugging\n");
439 /* exit with error code */
445 /* version *********************************************************************
447 Only prints cacao version information.
449 *******************************************************************************/
451 static void version(void)
453 puts("java version \""JAVA_VERSION"\"");
454 puts("CACAO version "VERSION"");
456 puts("Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,");
457 puts("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,");
458 puts("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,");
459 puts("J. Wenninger, Institut f. Computersprachen - TU Wien\n");
461 puts("This program is free software; you can redistribute it and/or");
462 puts("modify it under the terms of the GNU General Public License as");
463 puts("published by the Free Software Foundation; either version 2, or (at");
464 puts("your option) any later version.\n");
466 puts("This program is distributed in the hope that it will be useful, but");
467 puts("WITHOUT ANY WARRANTY; without even the implied warranty of");
468 puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU");
469 puts("General Public License for more details.\n");
471 puts("Configure/Build options:\n");
472 puts(" ./configure: "VERSION_CONFIGURE_ARGS"");
473 #if defined(__VERSION__)
474 puts(" CC : "VERSION_CC" ("__VERSION__")");
476 puts(" CC : "VERSION_CC"");
478 puts(" CFLAGS : "VERSION_CFLAGS"");
482 /* fullversion *****************************************************************
484 Prints a Sun compatible version information (required e.g. by
485 jpackage, www.jpackage.org).
487 *******************************************************************************/
489 static void fullversion(void)
491 puts("java full version \"cacao-"JAVA_VERSION"\"");
499 /* vm_create *******************************************************************
501 Creates a JVM. Called by JNI_CreateJavaVM.
503 *******************************************************************************/
505 bool vm_create(JavaVMInitArgs *vm_args)
514 /* check the JNI version requested */
516 switch (vm_args->version) {
517 case JNI_VERSION_1_1:
519 case JNI_VERSION_1_2:
520 case JNI_VERSION_1_4:
526 /* we only support 1 JVM instance */
532 /* get stuff from the environment *****************************************/
534 #if defined(DISABLE_GC)
535 nogc_init(HEAP_MAXSIZE, HEAP_STARTSIZE);
539 /* set the bootclasspath */
541 cp = getenv("BOOTCLASSPATH");
544 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
545 strcpy(bootclasspath, cp);
548 cplen = strlen(CACAO_VM_ZIP_PATH) +
550 strlen(CLASSPATH_GLIBJ_ZIP_PATH) +
553 bootclasspath = MNEW(char, cplen);
554 strcat(bootclasspath, CACAO_VM_ZIP_PATH);
555 strcat(bootclasspath, ":");
556 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP_PATH);
560 /* set the classpath */
562 cp = getenv("CLASSPATH");
565 classpath = MNEW(char, strlen(cp) + strlen("0"));
566 strcat(classpath, cp);
569 classpath = MNEW(char, strlen(".") + strlen("0"));
570 strcpy(classpath, ".");
574 /* interpret the options **************************************************/
579 heapmaxsize = HEAP_MAXSIZE;
580 heapstartsize = HEAP_STARTSIZE;
581 opt_stacksize = STACK_SIZE;
584 #if defined(ENABLE_JVMTI)
585 /* initialize JVMTI related **********************************************/
589 jdwp = jvmti = dbgprocess = false;
592 /* initialize properties before commandline handling */
594 if (!properties_init())
595 throw_cacao_exception_exit(string_java_lang_InternalError,
596 "Unable to init properties");
598 /* iterate over all passed options */
600 while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
610 #if SIZEOF_VOID_P == 8
611 puts("Running a 32-bit JVM is not supported on this platform.");
617 #if SIZEOF_VOID_P == 4
618 puts("Running a 64-bit JVM is not supported on this platform.");
624 /* forget old classpath and set the argument as new classpath */
625 MFREE(classpath, char, strlen(classpath));
627 classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
628 strcpy(classpath, opt_arg);
632 for (j = 0; j < strlen(opt_arg); j++) {
633 if (opt_arg[j] == '=') {
635 properties_add(opt_arg, opt_arg + j + 1);
640 /* if no '=' is given, just create an empty property */
642 properties_add(opt_arg, "");
647 case OPT_BOOTCLASSPATH:
648 /* Forget default bootclasspath and set the argument as
649 new boot classpath. */
650 MFREE(bootclasspath, char, strlen(bootclasspath));
652 bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
653 strcpy(bootclasspath, opt_arg);
656 case OPT_BOOTCLASSPATH_A:
657 /* append to end of bootclasspath */
658 cplen = strlen(bootclasspath);
660 bootclasspath = MREALLOC(bootclasspath,
663 cplen + strlen(":") +
664 strlen(opt_arg) + strlen("0"));
666 strcat(bootclasspath, ":");
667 strcat(bootclasspath, opt_arg);
670 case OPT_BOOTCLASSPATH_P:
671 /* prepend in front of bootclasspath */
675 bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
676 cplen + strlen("0"));
678 strcpy(bootclasspath, opt_arg);
679 strcat(bootclasspath, ":");
680 strcat(bootclasspath, cp);
682 MFREE(cp, char, cplen);
685 #if defined(ENABLE_JVMTI)
690 /* I don't know yet what Xnoagent should do. This is only for
691 compatiblity with eclipse - motse */
696 while (transport[j]!='=') j++;
698 while (j<strlen(transport)) {
699 if (strncmp("suspend=",&transport[j],8)==0) {
700 if ((j+8)>=strlen(transport) ||
701 (transport[j+8]!= 'y' && transport[j+8]!= 'n')) {
702 printf("bad Xrunjdwp option: -Xrunjdwp%s\n",transport);
707 suspend = transport[j+8] == 'y';
711 while (transport[j]!=',') j++;
728 c = opt_arg[strlen(opt_arg) - 1];
730 if ((c == 'k') || (c == 'K')) {
731 j = atoi(opt_arg) * 1024;
733 } else if ((c == 'm') || (c == 'M')) {
734 j = atoi(opt_arg) * 1024 * 1024;
741 else if (opt == OPT_MS)
753 if (strcmp("class", opt_arg) == 0)
754 opt_verboseclass = true;
756 else if (strcmp("gc", opt_arg) == 0)
757 opt_verbosegc = true;
759 else if (strcmp("jni", opt_arg) == 0)
760 opt_verbosejni = true;
762 else if (strcmp("call", opt_arg) == 0)
763 opt_verbosecall = true;
765 else if (strcmp("jit", opt_arg) == 0) {
770 compileverbose = true;
772 else if (strcmp("exception", opt_arg) == 0)
773 opt_verboseexception = true;
776 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
778 opt_typecheckverbose = true;
787 case OPT_FULLVERSION:
791 case OPT_SHOWVERSION:
799 #if defined(ENABLE_VERIFIER)
809 #if defined(ENABLE_STATISTICS)
811 opt_getcompilingtime = true;
812 opt_getloadingtime = true;
825 for (j = 0; j < strlen(opt_arg); j++) {
826 switch (opt_arg[j]) {
841 makeinitializations = false;
850 opt_method = opt_arg;
851 makeinitializations = false;
855 opt_signature = opt_arg;
861 makeinitializations = false;
864 case OPT_SHOW: /* Display options */
865 for (j = 0; j < strlen(opt_arg); j++) {
866 switch (opt_arg[j]) {
868 opt_showdisassemble = true;
869 compileverbose = true;
872 showconstantpool = true;
875 opt_showddatasegment = true;
878 opt_showexceptionstubs = true;
881 opt_showintermediate = true;
882 compileverbose = true;
888 opt_shownativestub = true;
899 #if defined(ENABLE_LOOP)
905 #if defined(ENABLE_INLINING)
907 for (j = 0; j < strlen(opt_arg); j++) {
908 switch (opt_arg[j]) {
910 /* define in options.h; Used in main.c, jit.c
911 & inline.c inlining is currently
915 inlinevirtuals = true;
918 inlineexceptions = true;
921 inlineparamopt = true;
924 inlineoutsiders = true;
931 #endif /* defined(ENABLE_INLINING) */
933 #if defined(ENABLE_IFCONV)
939 #if defined(ENABLE_LSRA)
953 case OPT_PROF_OPTION:
954 /* use <= to get the last \0 too */
956 for (j = 0, k = 0; j <= strlen(opt_arg); j++) {
957 if (opt_arg[j] == ',')
960 if (opt_arg[j] == '\0') {
961 if (strcmp("bb", opt_arg + k) == 0)
965 printf("Unknown option: -Xprof:%s\n", opt_arg + k);
969 /* set k to next char */
981 #if defined(ENABLE_JIT)
984 printf("-Xjit option not enabled.\n");
990 #if defined(ENABLE_INTRP)
993 printf("-Xint option not enabled.\n");
998 #if defined(ENABLE_INTRP)
999 case OPT_STATIC_SUPERS:
1000 opt_static_supers = atoi(opt_arg);
1003 case OPT_NO_DYNAMIC:
1004 opt_no_dynamic = true;
1007 case OPT_NO_REPLICATION:
1008 opt_no_replication = true;
1011 case OPT_NO_QUICKSUPER:
1012 opt_no_quicksuper = true;
1021 printf("Unknown option: %s\n",
1022 vm_args->options[opt_index].optionString);
1028 /* get the main class *****************************************************/
1030 if (opt_index < vm_args->nOptions) {
1031 mainstring = vm_args->options[opt_index++].optionString;
1033 /* Put the jar file into the classpath (if any). */
1035 if (opt_jar == true) {
1036 /* free old classpath */
1038 MFREE(classpath, char, strlen(classpath));
1040 /* put jarfile into classpath */
1042 classpath = MNEW(char, strlen(mainstring) + strlen("0"));
1044 strcpy(classpath, mainstring);
1047 /* replace .'s with /'s in classname */
1049 for (i = strlen(mainstring) - 1; i >= 0; i--)
1050 if (mainstring[i] == '.')
1051 mainstring[i] = '/';
1055 #if defined(ENABLE_JVMTI)
1056 /* The fork has to occure before threads a created because threads are
1057 not forked correctly (see man pthread_atfork). Varibale dbgprocess
1058 stores information whether this is the debugger or debuggee process. */
1059 if (jvmti || jdwp) {
1060 set_jvmti_phase(JVMTI_PHASE_ONLOAD);
1061 dbgprocess = cacaodbgfork();
1064 if (dbgprocess && jvmti) { /* is this the parent/debugger process ? */
1065 agentload(agentarg);
1066 set_jvmti_phase(JVMTI_PHASE_PRIMORDIAL);
1071 /* initialize this JVM ****************************************************/
1073 vm_initializing = true;
1075 /* initialize the garbage collector */
1077 gc_init(heapmaxsize, heapstartsize);
1079 #if defined(ENABLE_INTRP)
1080 /* Allocate main thread stack on the Java heap. */
1083 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1084 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1088 #if defined(USE_THREADS)
1089 #if defined(NATIVE_THREADS)
1095 /* initialize the string hashtable stuff: lock (must be done
1096 _after_ threads_preinit) */
1099 throw_main_exception_exit();
1101 /* initialize the utf8 hashtable stuff: lock, often used utf8
1102 strings (must be done _after_ threads_preinit) */
1105 throw_main_exception_exit();
1107 /* initialize the classcache hashtable stuff: lock, hashtable
1108 (must be done _after_ threads_preinit) */
1110 if (!classcache_init())
1111 throw_main_exception_exit();
1113 /* initialize the loader with bootclasspath (must be done _after_
1117 throw_main_exception_exit();
1119 suck_add_from_property("java.endorsed.dirs");
1120 suck_add(bootclasspath);
1122 /* initialize the memory subsystem (must be done _after_
1126 throw_main_exception_exit();
1128 /* initialize the finalizer stuff (must be done _after_
1131 if (!finalizer_init())
1132 throw_main_exception_exit();
1134 /* install architecture dependent signal handler used for exceptions */
1138 /* initialize the codegen subsystems */
1142 /* initializes jit compiler */
1146 /* machine dependent initialization */
1148 #if defined(ENABLE_JIT)
1149 # if defined(ENABLE_INTRP)
1159 /* initialize the loader subsystems (must be done _after_
1162 if (!loader_init((u1 *) stackbottom))
1163 throw_main_exception_exit();
1166 throw_main_exception_exit();
1169 throw_main_exception_exit();
1171 if (!exceptions_init())
1172 throw_main_exception_exit();
1174 if (!builtin_init())
1175 throw_main_exception_exit();
1177 #if defined(USE_THREADS)
1178 if (!threads_init((u1 *) stackbottom))
1179 throw_main_exception_exit();
1182 /* That's important, otherwise we get into trouble, if the Runtime
1183 static initializer is called before (circular dependency. This
1184 is with classpath 0.09. Another important thing is, that this
1185 has to happen after initThreads!!! */
1187 if (!initialize_class(class_java_lang_System))
1188 throw_main_exception_exit();
1190 /* JNI init creates a Java object (this means running Java code) */
1193 throw_main_exception_exit();
1195 #if defined(ENABLE_PROFILING)
1196 /* initialize profiling */
1198 if (!profile_init())
1199 throw_main_exception_exit();
1202 #if defined(USE_THREADS)
1203 /* finally, start the finalizer thread */
1205 if (!finalizer_start_thread())
1206 throw_main_exception_exit();
1208 /* start the profile sampling thread */
1210 /* if (!profile_start_thread()) */
1211 /* throw_main_exception_exit(); */
1214 /* increment the number of VMs */
1218 /* initialization is done */
1220 vm_initializing = false;
1222 /* everything's ok */
1228 /* vm_destroy ******************************************************************
1230 Unloads a Java VM and reclaims its resources.
1232 *******************************************************************************/
1234 s4 vm_destroy(JavaVM *vm)
1236 #if defined(USE_THREADS)
1237 #if defined(NATIVE_THREADS)
1240 killThread(currentThread);
1244 /* everything's ok */
1250 /* vm_exit *********************************************************************
1252 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1254 *******************************************************************************/
1256 void vm_exit(s4 status)
1260 /* signal that we are exiting */
1264 assert(class_java_lang_System);
1265 assert(class_java_lang_System->state & CLASS_LOADED);
1267 #if defined(ENABLE_JVMTI)
1269 set_jvmti_phase(JVMTI_PHASE_DEAD);
1270 if (jvmti) agentunload();
1274 if (!link_class(class_java_lang_System))
1275 throw_main_exception_exit();
1277 /* call java.lang.System.exit(I)V */
1279 m = class_resolveclassmethod(class_java_lang_System,
1280 utf_new_char("exit"),
1282 class_java_lang_Object,
1286 throw_main_exception_exit();
1288 /* call the exit function with passed exit status */
1290 (void) vm_call_method(m, NULL, status);
1292 /* If we had an exception, just ignore the exception and exit with
1295 vm_shutdown(status);
1299 /* vm_shutdown *****************************************************************
1301 Terminates the system immediately without freeing memory explicitly
1302 (to be used only for abnormal termination).
1304 *******************************************************************************/
1306 void vm_shutdown(s4 status)
1308 #if defined(ENABLE_JVMTI)
1310 set_jvmti_phase(JVMTI_PHASE_DEAD);
1311 if (jvmti) agentunload();
1316 #if defined(ENABLE_STATISTICS)
1317 || opt_getcompilingtime || opt_stat
1321 log_text("CACAO terminated by shutdown");
1322 dolog("Exit status: %d\n", (s4) status);
1329 /* vm_exit_handler *************************************************************
1331 The exit_handler function is called upon program termination.
1333 ATTENTION: Don't free system resources here! Some threads may still
1334 be running as this is called from VMRuntime.exit(). The OS does the
1337 *******************************************************************************/
1339 void vm_exit_handler(void)
1341 #if defined(ENABLE_JVMTI)
1342 if (jvmti && jdwp) set_jvmti_phase(JVMTI_PHASE_DEAD);
1343 if (jvmti) agentunload();
1347 #if !defined(NDEBUG)
1349 class_showmethods(mainclass);
1351 if (showconstantpool)
1352 class_showconstantpool(mainclass);
1357 # if defined(ENABLE_PROFILING)
1359 profile_printstats();
1361 #endif /* !defined(NDEBUG) */
1363 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
1364 clear_thread_flags(); /* restores standard file descriptor
1368 #if defined(ENABLE_RT_TIMING)
1369 rt_timing_print_time_stats(stderr);
1372 #if defined(ENABLE_CYCLES_STATS)
1373 builtin_print_cycles_stats(stderr);
1374 stacktrace_print_cycles_stats(stderr);
1378 #if defined(ENABLE_STATISTICS)
1379 || opt_getcompilingtime || opt_stat
1383 log_text("CACAO terminated");
1385 #if defined(ENABLE_STATISTICS)
1388 #ifdef TYPECHECK_STATISTICS
1389 typecheck_print_statistics(get_logfile());
1395 if (opt_getcompilingtime)
1397 #endif /* defined(ENABLE_STATISTICS) */
1399 /* vm_print_profile(stderr);*/
1403 /* vm_vmargs_from_valist *******************************************************
1407 *******************************************************************************/
1409 static void vm_vmargs_from_valist(methodinfo *m, java_objectheader *o,
1410 vm_arg *vmargs, va_list ap)
1412 typedesc *paramtypes;
1415 paramtypes = m->parseddesc->paramtypes;
1417 /* if method is non-static fill first block and skip `this' pointer */
1422 /* the `this' pointer */
1423 vmargs[0].type = TYPE_ADR;
1424 vmargs[0].data.l = (u8) (ptrint) o;
1430 for (; i < m->parseddesc->paramcount; i++, paramtypes++) {
1431 switch (paramtypes->decltype) {
1432 /* primitive types */
1433 case PRIMITIVETYPE_BOOLEAN:
1434 case PRIMITIVETYPE_BYTE:
1435 case PRIMITIVETYPE_CHAR:
1436 case PRIMITIVETYPE_SHORT:
1437 case PRIMITIVETYPE_INT:
1438 vmargs[i].type = TYPE_INT;
1439 vmargs[i].data.l = (s8) va_arg(ap, s4);
1442 case PRIMITIVETYPE_LONG:
1443 vmargs[i].type = TYPE_LNG;
1444 vmargs[i].data.l = (s8) va_arg(ap, s8);
1447 case PRIMITIVETYPE_FLOAT:
1448 vmargs[i].type = TYPE_FLT;
1449 #if defined(__ALPHA__)
1450 /* this keeps the assembler function much simpler */
1452 vmargs[i].data.d = (jdouble) va_arg(ap, jdouble);
1454 vmargs[i].data.f = (jfloat) va_arg(ap, jdouble);
1458 case PRIMITIVETYPE_DOUBLE:
1459 vmargs[i].type = TYPE_DBL;
1460 vmargs[i].data.d = (jdouble) va_arg(ap, jdouble);
1464 vmargs[i].type = TYPE_ADR;
1465 vmargs[i].data.l = (u8) (ptrint) va_arg(ap, void*);
1472 /* vm_vmargs_from_jvalue *******************************************************
1476 *******************************************************************************/
1478 static void vm_vmargs_from_jvalue(methodinfo *m, java_objectheader *o,
1479 vm_arg *vmargs, jvalue *args)
1481 typedesc *paramtypes;
1485 paramtypes = m->parseddesc->paramtypes;
1487 /* if method is non-static fill first block and skip `this' pointer */
1492 /* the `this' pointer */
1493 vmargs[0].type = TYPE_ADR;
1494 vmargs[0].data.l = (u8) (ptrint) o;
1500 for (j = 0; i < m->parseddesc->paramcount; i++, j++, paramtypes++) {
1501 switch (paramtypes->decltype) {
1502 /* primitive types */
1503 case PRIMITIVETYPE_BOOLEAN:
1504 case PRIMITIVETYPE_BYTE:
1505 case PRIMITIVETYPE_CHAR:
1506 case PRIMITIVETYPE_SHORT:
1507 case PRIMITIVETYPE_INT:
1508 vmargs[i].type = TYPE_INT;
1509 vmargs[i].data.l = (s8) args[j].i;
1512 case PRIMITIVETYPE_LONG:
1513 vmargs[i].type = TYPE_LNG;
1514 vmargs[i].data.l = (s8) args[j].j;
1517 case PRIMITIVETYPE_FLOAT:
1518 vmargs[i].type = TYPE_FLT;
1519 #if defined(__ALPHA__)
1520 /* this keeps the assembler function much simpler */
1522 vmargs[i].data.d = (jdouble) args[j].f;
1524 vmargs[i].data.f = args[j].f;
1528 case PRIMITIVETYPE_DOUBLE:
1529 vmargs[i].type = TYPE_DBL;
1530 vmargs[i].data.d = args[j].d;
1534 vmargs[i].type = TYPE_ADR;
1535 vmargs[i].data.l = (u8) (ptrint) args[j].l;
1542 /* vm_call_method **************************************************************
1544 Calls a Java method with a variable number of arguments and returns
1547 *******************************************************************************/
1549 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
1552 java_objectheader *ro;
1555 ro = vm_call_method_valist(m, o, ap);
1562 /* vm_call_method_valist *******************************************************
1564 Calls a Java method with a variable number of arguments, passed via
1565 a va_list, and returns an address.
1567 *******************************************************************************/
1569 java_objectheader *vm_call_method_valist(methodinfo *m, java_objectheader *o,
1574 java_objectheader *ro;
1577 /* mark start of dump memory area */
1579 dumpsize = dump_size();
1581 /* get number of Java method arguments */
1583 vmargscount = m->parseddesc->paramcount;
1585 /* allocate vm_arg array */
1587 vmargs = DMNEW(vm_arg, vmargscount);
1589 /* fill the vm_arg array from a va_list */
1591 vm_vmargs_from_valist(m, o, vmargs, ap);
1593 /* call the Java method */
1595 ro = vm_call_method_vmarg(m, vmargscount, vmargs);
1597 /* release dump area */
1599 dump_release(dumpsize);
1605 /* vm_call_method_jvalue *******************************************************
1607 Calls a Java method with a variable number of arguments, passed via
1608 a jvalue array, and returns an address.
1610 *******************************************************************************/
1612 java_objectheader *vm_call_method_jvalue(methodinfo *m, java_objectheader *o,
1617 java_objectheader *ro;
1620 /* mark start of dump memory area */
1622 dumpsize = dump_size();
1624 /* get number of Java method arguments */
1626 vmargscount = m->parseddesc->paramcount;
1628 /* allocate vm_arg array */
1630 vmargs = DMNEW(vm_arg, vmargscount);
1632 /* fill the vm_arg array from a va_list */
1634 vm_vmargs_from_jvalue(m, o, vmargs, args);
1636 /* call the Java method */
1638 ro = vm_call_method_vmarg(m, vmargscount, vmargs);
1640 /* release dump area */
1642 dump_release(dumpsize);
1648 /* vm_call_method_vmarg ********************************************************
1650 Calls a Java method with a variable number of arguments, passed via
1651 a vm_arg array, and returns an address.
1653 *******************************************************************************/
1655 java_objectheader *vm_call_method_vmarg(methodinfo *m, s4 vmargscount,
1658 java_objectheader *o;
1660 #if defined(ENABLE_JIT)
1661 # if defined(ENABLE_INTRP)
1663 o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
1666 o = asm_vm_call_method(m, vmargscount, vmargs);
1668 o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
1675 /* vm_call_method_int **********************************************************
1677 Calls a Java method with a variable number of arguments and returns
1680 *******************************************************************************/
1682 s4 vm_call_method_int(methodinfo *m, java_objectheader *o, ...)
1688 i = vm_call_method_int_valist(m, o, ap);
1695 /* vm_call_method_int_valist ***************************************************
1697 Calls a Java method with a variable number of arguments, passed via
1698 a va_list, and returns an integer (s4).
1700 *******************************************************************************/
1702 s4 vm_call_method_int_valist(methodinfo *m, java_objectheader *o, va_list ap)
1709 /* mark start of dump memory area */
1711 dumpsize = dump_size();
1713 /* get number of Java method arguments */
1715 vmargscount = m->parseddesc->paramcount;
1717 /* allocate vm_arg array */
1719 vmargs = DMNEW(vm_arg, vmargscount);
1721 /* fill the vm_arg array from a va_list */
1723 vm_vmargs_from_valist(m, o, vmargs, ap);
1725 /* call the Java method */
1727 i = vm_call_method_int_vmarg(m, vmargscount, vmargs);
1729 /* release dump area */
1731 dump_release(dumpsize);
1737 /* vm_call_method_int_jvalue ***************************************************
1739 Calls a Java method with a variable number of arguments, passed via
1740 a jvalue array, and returns an integer (s4).
1742 *******************************************************************************/
1744 s4 vm_call_method_int_jvalue(methodinfo *m, java_objectheader *o, jvalue *args)
1751 /* mark start of dump memory area */
1753 dumpsize = dump_size();
1755 /* get number of Java method arguments */
1757 vmargscount = m->parseddesc->paramcount;
1759 /* allocate vm_arg array */
1761 vmargs = DMNEW(vm_arg, vmargscount);
1763 /* fill the vm_arg array from a va_list */
1765 vm_vmargs_from_jvalue(m, o, vmargs, args);
1767 /* call the Java method */
1769 i = vm_call_method_int_vmarg(m, vmargscount, vmargs);
1771 /* release dump area */
1773 dump_release(dumpsize);
1779 /* vm_call_method_int_vmarg ****************************************************
1781 Calls a Java method with a variable number of arguments, passed via
1782 a vm_arg array, and returns an integer (s4).
1784 *******************************************************************************/
1786 s4 vm_call_method_int_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
1790 #if defined(ENABLE_JIT)
1791 # if defined(ENABLE_INTRP)
1793 i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
1796 i = asm_vm_call_method_int(m, vmargscount, vmargs);
1798 i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
1805 /* vm_call_method_long *********************************************************
1807 Calls a Java method with a variable number of arguments and returns
1810 *******************************************************************************/
1812 s8 vm_call_method_long(methodinfo *m, java_objectheader *o, ...)
1818 l = vm_call_method_long_valist(m, o, ap);
1825 /* vm_call_method_long_valist **************************************************
1827 Calls a Java method with a variable number of arguments, passed via
1828 a va_list, and returns a long (s8).
1830 *******************************************************************************/
1832 s8 vm_call_method_long_valist(methodinfo *m, java_objectheader *o, va_list ap)
1839 /* mark start of dump memory area */
1841 dumpsize = dump_size();
1843 /* get number of Java method arguments */
1845 vmargscount = m->parseddesc->paramcount;
1847 /* allocate vm_arg array */
1849 vmargs = DMNEW(vm_arg, vmargscount);
1851 /* fill the vm_arg array from a va_list */
1853 vm_vmargs_from_valist(m, o, vmargs, ap);
1855 /* call the Java method */
1857 l = vm_call_method_long_vmarg(m, vmargscount, vmargs);
1859 /* release dump area */
1861 dump_release(dumpsize);
1867 /* vm_call_method_long_jvalue **************************************************
1869 Calls a Java method with a variable number of arguments, passed via
1870 a jvalue array, and returns a long (s8).
1872 *******************************************************************************/
1874 s8 vm_call_method_long_jvalue(methodinfo *m, java_objectheader *o, jvalue *args)
1881 /* mark start of dump memory area */
1883 dumpsize = dump_size();
1885 /* get number of Java method arguments */
1887 vmargscount = m->parseddesc->paramcount;
1889 /* allocate vm_arg array */
1891 vmargs = DMNEW(vm_arg, vmargscount);
1893 /* fill the vm_arg array from a va_list */
1895 vm_vmargs_from_jvalue(m, o, vmargs, args);
1897 /* call the Java method */
1899 l = vm_call_method_long_vmarg(m, vmargscount, vmargs);
1901 /* release dump area */
1903 dump_release(dumpsize);
1909 /* vm_call_method_long_vmarg ***************************************************
1911 Calls a Java method with a variable number of arguments, passed via
1912 a vm_arg array, and returns a long (s8).
1914 *******************************************************************************/
1916 s8 vm_call_method_long_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
1920 #if defined(ENABLE_JIT)
1921 # if defined(ENABLE_INTRP)
1923 l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
1926 l = asm_vm_call_method_long(m, vmargscount, vmargs);
1928 l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
1935 /* vm_call_method_float ********************************************************
1937 Calls a Java method with a variable number of arguments and returns
1940 *******************************************************************************/
1942 float vm_call_method_float(methodinfo *m, java_objectheader *o, ...)
1948 f = vm_call_method_float_valist(m, o, ap);
1955 /* vm_call_method_float_valist *************************************************
1957 Calls a Java method with a variable number of arguments, passed via
1958 a va_list, and returns a float.
1960 *******************************************************************************/
1962 float vm_call_method_float_valist(methodinfo *m, java_objectheader *o,
1970 /* mark start of dump memory area */
1972 dumpsize = dump_size();
1974 /* get number of Java method arguments */
1976 vmargscount = m->parseddesc->paramcount;
1978 /* allocate vm_arg array */
1980 vmargs = DMNEW(vm_arg, vmargscount);
1982 /* fill the vm_arg array from a va_list */
1984 vm_vmargs_from_valist(m, o, vmargs, ap);
1986 /* call the Java method */
1988 f = vm_call_method_float_vmarg(m, vmargscount, vmargs);
1990 /* release dump area */
1992 dump_release(dumpsize);
1998 /* vm_call_method_float_jvalue *************************************************
2000 Calls a Java method with a variable number of arguments, passed via
2001 a jvalue array, and returns a float.
2003 *******************************************************************************/
2005 float vm_call_method_float_jvalue(methodinfo *m, java_objectheader *o,
2013 /* mark start of dump memory area */
2015 dumpsize = dump_size();
2017 /* get number of Java method arguments */
2019 vmargscount = m->parseddesc->paramcount;
2021 /* allocate vm_arg array */
2023 vmargs = DMNEW(vm_arg, vmargscount);
2025 /* fill the vm_arg array from a va_list */
2027 vm_vmargs_from_jvalue(m, o, vmargs, args);
2029 /* call the Java method */
2031 f = vm_call_method_float_vmarg(m, vmargscount, vmargs);
2033 /* release dump area */
2035 dump_release(dumpsize);
2041 /* vm_call_method_float_vmarg **************************************************
2043 Calls a Java method with a variable number of arguments and returns
2046 *******************************************************************************/
2048 float vm_call_method_float_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
2052 #if defined(ENABLE_JIT)
2053 # if defined(ENABLE_INTRP)
2055 f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
2058 f = asm_vm_call_method_float(m, vmargscount, vmargs);
2060 f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
2067 /* vm_call_method_double *******************************************************
2069 Calls a Java method with a variable number of arguments and returns
2072 *******************************************************************************/
2074 double vm_call_method_double(methodinfo *m, java_objectheader *o, ...)
2080 d = vm_call_method_double_valist(m, o, ap);
2087 /* vm_call_method_double_valist ************************************************
2089 Calls a Java method with a variable number of arguments, passed via
2090 a va_list, and returns a double.
2092 *******************************************************************************/
2094 double vm_call_method_double_valist(methodinfo *m, java_objectheader *o,
2102 /* mark start of dump memory area */
2104 dumpsize = dump_size();
2106 /* get number of Java method arguments */
2108 vmargscount = m->parseddesc->paramcount;
2110 /* allocate vm_arg array */
2112 vmargs = DMNEW(vm_arg, vmargscount);
2114 /* fill the vm_arg array from a va_list */
2116 vm_vmargs_from_valist(m, o, vmargs, ap);
2118 /* call the Java method */
2120 d = vm_call_method_double_vmarg(m, vmargscount, vmargs);
2122 /* release dump area */
2124 dump_release(dumpsize);
2130 /* vm_call_method_double_jvalue ************************************************
2132 Calls a Java method with a variable number of arguments, passed via
2133 a jvalue array, and returns a double.
2135 *******************************************************************************/
2137 double vm_call_method_double_jvalue(methodinfo *m, java_objectheader *o,
2145 /* mark start of dump memory area */
2147 dumpsize = dump_size();
2149 /* get number of Java method arguments */
2151 vmargscount = m->parseddesc->paramcount;
2153 /* allocate vm_arg array */
2155 vmargs = DMNEW(vm_arg, vmargscount);
2157 /* fill the vm_arg array from a va_list */
2159 vm_vmargs_from_jvalue(m, o, vmargs, args);
2161 /* call the Java method */
2163 d = vm_call_method_double_vmarg(m, vmargscount, vmargs);
2165 /* release dump area */
2167 dump_release(dumpsize);
2173 /* vm_call_method_double_vmarg *************************************************
2175 Calls a Java method with a variable number of arguments and returns
2178 *******************************************************************************/
2180 double vm_call_method_double_vmarg(methodinfo *m, s4 vmargscount,
2185 #if defined(ENABLE_JIT)
2186 # if defined(ENABLE_INTRP)
2188 d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
2191 d = asm_vm_call_method_double(m, vmargscount, vmargs);
2193 d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
2201 * These are local overrides for various environment variables in Emacs.
2202 * Please do not remove this and leave it at the end of the file, where
2203 * Emacs will automagically detect them.
2204 * ---------------------------------------------------------------------
2207 * indent-tabs-mode: t