1 /* src/cacao/cacao.c - contains main() of cacao
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
34 This module does the following tasks:
35 - Command line option handling
36 - Calling initialization routines
37 - Calling the class loader
38 - Running the main method
40 $Id: cacao.c 4085 2006-01-03 23:44:38Z twisti $
50 #include "cacao/cacao.h"
52 #include "mm/memory.h"
53 #include "native/jni.h"
54 #include "native/native.h"
56 #if defined(ENABLE_JVMTI)
57 #include "native/jvmti/jvmti.h"
58 #include "native/jvmti/dbg.h"
59 #include <sys/types.h>
64 #include "toolbox/logging.h"
65 #include "vm/classcache.h"
66 #include "vm/exceptions.h"
67 #include "vm/finalizer.h"
68 #include "vm/global.h"
69 #include "vm/initialize.h"
70 #include "vm/loader.h"
71 #include "vm/options.h"
72 #include "vm/properties.h"
73 #include "vm/signallocal.h"
74 #include "vm/statistics.h"
75 #include "vm/stringlocal.h"
77 #include "vm/jit/asmpart.h"
78 #include "vm/jit/jit.h"
80 #ifdef TYPEINFO_DEBUG_TEST
81 #include "vm/jit/verify/typeinfo.h"
85 /* define heap sizes **********************************************************/
87 #define HEAP_MAXSIZE 64 * 1024 * 1024 /* default 64MB */
88 #define HEAP_STARTSIZE 2 * 1024 * 1024 /* default 2MB */
89 #define STACK_SIZE 128 * 1024 /* default 128kB */
91 #if defined(ENABLE_INTRP)
96 /* CACAO related stuff ********************************************************/
98 bool cacao_initializing;
102 /* Invocation API variables ***************************************************/
104 JavaVM *jvm; /* denotes a Java VM */
105 JNIEnv *env; /* pointer to native method interface */
107 JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization arguments */
111 static classinfo *mainclass;
113 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
114 void **stackbottom = 0;
118 /* define command line options ************************************************/
133 #if defined(ENABLE_STATISTICS)
147 #define STATIC_ANALYSIS
148 #if defined(STATIC_ANALYSIS)
157 OPT_VERBOSEEXCEPTION,
160 #if defined(ENABLE_LSRA)
178 #if defined(ENABLE_INTRP)
179 /* interpreter options */
200 opt_struct opts[] = {
201 { "classpath", true, OPT_CLASSPATH },
202 { "cp", true, OPT_CLASSPATH },
203 { "D", true, OPT_D },
204 { "noasyncgc", false, OPT_IGNORE },
205 { "noverify", false, OPT_NOVERIFY },
206 { "liberalutf", false, OPT_LIBERALUTF },
207 { "v", false, OPT_VERBOSE1 },
208 { "verbose", false, OPT_VERBOSE },
209 { "verbose:", true, OPT_VERBOSESPECIFIC },
210 { "verbosecall", false, OPT_VERBOSECALL },
211 { "verboseexception", false, OPT_VERBOSEEXCEPTION },
212 #ifdef TYPECHECK_VERBOSE
213 { "verbosetc", false, OPT_VERBOSETC },
215 #if defined(__ALPHA__)
216 { "noieee", false, OPT_NOIEEE },
218 { "softnull", false, OPT_SOFTNULL },
219 { "time", false, OPT_TIME },
220 #if defined(ENABLE_STATISTICS)
221 { "stat", false, OPT_STAT },
223 { "log", true, OPT_LOG },
224 { "c", true, OPT_CHECK },
225 { "l", false, OPT_LOAD },
226 { "eager", false, OPT_EAGER },
227 { "sig", true, OPT_SIGNATURE },
228 { "all", false, OPT_ALL },
229 { "oloop", false, OPT_OLOOP },
230 #ifdef STATIC_ANALYSIS
231 { "rt", false, OPT_RT },
232 { "xta", false, OPT_XTA },
233 { "vta", false, OPT_VTA },
235 #if defined(ENABLE_LSRA)
236 { "lsra", false, OPT_LSRA },
238 { "jar", false, OPT_JAR },
239 { "version", false, OPT_VERSION },
240 { "showversion", false, OPT_SHOWVERSION },
241 { "fullversion", false, OPT_FULLVERSION },
242 { "help", false, OPT_HELP },
243 { "?", false, OPT_HELP },
245 #if defined(ENABLE_INTRP)
246 /* interpreter options */
248 { "trace", false, OPT_TRACE },
249 { "static-supers", true, OPT_STATIC_SUPERS },
250 { "no-dynamic", false, OPT_NO_DYNAMIC },
251 { "no-replication", false, OPT_NO_REPLICATION },
252 { "no-quicksuper", false, OPT_NO_QUICKSUPER },
255 /* JVMTI Agent Command Line Options */
257 { "agentlib:", true, OPT_AGENTLIB },
258 { "agentpath:", true, OPT_AGENTPATH },
263 { "X", false, OPT_X },
264 { "Xjit", false, OPT_JIT },
265 { "Xint", false, OPT_INTRP },
266 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
267 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
268 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
270 { "Xdebug", false, OPT_DEBUG },
272 { "Xms", true, OPT_MS },
273 { "Xmx", true, OPT_MX },
274 { "Xss", true, OPT_SS },
275 { "ms", true, OPT_MS },
276 { "mx", true, OPT_MX },
277 { "ss", true, OPT_SS },
279 /* keep these at the end of the list */
281 { "i", true, OPT_INLINING },
282 { "m", true, OPT_METHOD },
283 { "s", true, OPT_SHOW },
289 /* usage ***********************************************************************
291 Prints the correct usage syntax to stdout.
293 *******************************************************************************/
295 static void usage(void)
297 printf("Usage: cacao [-options] classname [arguments]\n");
298 printf(" (to run a class file)\n");
299 printf(" cacao [-options] -jar jarfile [arguments]\n");
300 printf(" (to run a standalone jar file)\n\n");
302 printf("Java options:\n");
303 printf(" -cp <path> specify a path to look for classes\n");
304 printf(" -classpath <path> specify a path to look for classes\n");
305 printf(" -D<name>=<value> add an entry to the property list\n");
306 printf(" -verbose[:class|gc|jni] enable specific verbose output\n");
307 printf(" -version print product version and exit\n");
308 printf(" -fullversion print jpackage-compatible product version and exit\n");
309 printf(" -showversion print product version and continue\n");
310 printf(" -help, -? print this help message\n");
311 printf(" -X print help on non-standard Java options\n\n");
314 printf(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent\n");
315 printf(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent\n");
318 printf("CACAO options:\n");
319 printf(" -v write state-information\n");
320 printf(" -verbose write more information\n");
321 printf(" -verbosegc write message for each GC\n");
322 printf(" -verbosecall write message for each call\n");
323 printf(" -verboseexception write message for each step of stack unwinding\n");
324 #ifdef TYPECHECK_VERBOSE
325 printf(" -verbosetc write debug messages while typechecking\n");
327 #if defined(__ALPHA__)
328 printf(" -noieee don't use ieee compliant arithmetic\n");
330 printf(" -noverify don't verify classfiles\n");
331 printf(" -liberalutf don't warn about overlong UTF-8 sequences\n");
332 printf(" -softnull use software nullpointer check\n");
333 printf(" -time measure the runtime\n");
334 #if defined(ENABLE_STATISTICS)
335 printf(" -stat detailed compiler statistics\n");
337 printf(" -log logfile specify a name for the logfile\n");
338 printf(" -c(heck)b(ounds) don't check array bounds\n");
339 printf(" s(ync) don't check for synchronization\n");
340 printf(" -oloop optimize array accesses in loops\n");
341 printf(" -l don't start the class after loading\n");
342 printf(" -eager perform eager class loading and linking\n");
343 printf(" -all compile all methods, no execution\n");
344 printf(" -m compile only a specific method\n");
345 printf(" -sig specify signature for a specific method\n");
346 printf(" -s(how)a(ssembler) show disassembled listing\n");
347 printf(" c(onstants) show the constant pool\n");
348 printf(" d(atasegment) show data segment listing\n");
349 printf(" e(xceptionstubs) show disassembled exception stubs (only with -sa)\n");
350 printf(" i(ntermediate) show intermediate representation\n");
351 printf(" m(ethods) show class fields and methods\n");
352 printf(" n(ative) show disassembled native stubs\n");
353 printf(" u(tf) show the utf - hash\n");
354 printf(" -i n(line) activate inlining\n");
355 printf(" v(irtual) inline virtual methods (uses/turns rt option on)\n");
356 printf(" e(exception) inline methods with exceptions\n");
357 printf(" p(aramopt) optimize argument renaming\n");
358 printf(" o(utsiders) inline methods of foreign classes\n");
359 #ifdef STATIC_ANALYSIS
360 printf(" -rt use rapid type analysis\n");
361 printf(" -xta use x type analysis\n");
362 printf(" -vta use variable type analysis\n");
364 #if defined(ENABLE_LSRA)
365 printf(" -lsra use linear scan register allocation\n");
368 /* exit with error code */
374 static void Xusage(void)
376 #if defined(ENABLE_JIT)
377 printf(" -Xjit JIT mode execution (default)\n");
379 #if defined(ENABLE_INTRP)
380 printf(" -Xint interpreter mode execution\n");
382 printf(" -Xbootclasspath:<zip/jar files and directories separated by :>\n");
383 printf(" value is set as bootstrap class path\n");
384 printf(" -Xbootclasspath/a:<zip/jar files and directories separated by :>\n");
385 printf(" value is appended to the bootstrap class path\n");
386 printf(" -Xbootclasspath/p:<zip/jar files and directories separated by :>\n");
387 printf(" value is prepended to the bootstrap class path\n");
388 printf(" -Xms<size> set the initial size of the heap (default: 2MB)\n");
389 printf(" -Xmx<size> set the maximum size of the heap (default: 64MB)\n");
390 printf(" -Xss<size> set the thread stack size (default: 128kB)\n");
391 #if defined(ENABLE_JVMTI)
392 printf(" -Xdebug<transport> enable remote debugging\n");
395 /* exit with error code */
401 /* version *********************************************************************
403 Only prints cacao version information.
405 *******************************************************************************/
407 static void version(void)
409 printf("java version \""JAVA_VERSION"\"\n");
410 printf("CACAO version "VERSION"\n");
412 printf("Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,\n");
413 printf("R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,\n");
414 printf("C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,\n");
415 printf("Institut f. Computersprachen - TU Wien\n\n");
417 printf("This program is free software; you can redistribute it and/or\n");
418 printf("modify it under the terms of the GNU General Public License as\n");
419 printf("published by the Free Software Foundation; either version 2, or (at\n");
420 printf("your option) any later version.\n\n");
422 printf("This program is distributed in the hope that it will be useful, but\n");
423 printf("WITHOUT ANY WARRANTY; without even the implied warranty of\n");
424 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n");
425 printf("General Public License for more details.\n");
429 /* fullversion *****************************************************************
431 Prints a Sun compatible version information (required e.g. by
432 jpackage, www.jpackage.org).
434 *******************************************************************************/
436 static void fullversion(void)
438 printf("java full version \"cacao-"JAVA_VERSION"\"\n");
446 #ifdef TYPECHECK_STATISTICS
447 void typecheck_print_statistics(FILE *file);
450 /* setup_debugger_process *****************************************************
452 Helper function to start JDWP threads
454 *******************************************************************************/
455 #if defined(ENABLE_JVMTI)
457 static void setup_debugger_process(char* transport) {
458 java_objectheader *o;
462 /* new gnu.classpath.jdwp.Jdwp() */
464 load_class_from_sysloader(utf_new_char("gnu.classpath.jdwp.Jdwp"));
466 throw_main_exception_exit();
468 o = builtin_new(mainclass);
471 throw_main_exception_exit();
473 m = class_resolveclassmethod(mainclass,
475 utf_java_lang_String__void,
476 class_java_lang_Object,
479 throw_main_exception_exit();
481 asm_calljavafunction(m, o, NULL, NULL, NULL);
483 /* configure(transport,NULL) */
484 m = class_resolveclassmethod(
485 mainclass, utf_new_char("configure"),
486 utf_new_char("(Ljava/lang/String;Ljava/lang/Thread;)V"),
487 class_java_lang_Object,
491 s = javastring_new_char(transport);
492 asm_calljavafunction(m, o, s, NULL, NULL);
494 throw_main_exception_exit();
496 /* _doInitialization */
497 m = class_resolveclassmethod(mainclass,
498 utf_new_char("_doInitialization"),
504 throw_main_exception_exit();
506 asm_calljavafunction(m, o, NULL, NULL, NULL);
511 /* getmainclassfromjar *********************************************************
513 Gets the name of the main class form a JAR's manifest file.
515 *******************************************************************************/
517 static char *getmainclassnamefromjar(char *mainstring)
520 java_objectheader *o;
524 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
527 throw_main_exception_exit();
529 /* create JarFile object */
534 throw_main_exception_exit();
537 m = class_resolveclassmethod(c,
539 utf_java_lang_String__void,
540 class_java_lang_Object,
544 throw_main_exception_exit();
546 s = javastring_new_char(mainstring);
548 asm_calljavafunction(m, o, s, NULL, NULL);
551 throw_main_exception_exit();
553 /* get manifest object */
555 m = class_resolveclassmethod(c,
556 utf_new_char("getManifest"),
557 utf_new_char("()Ljava/util/jar/Manifest;"),
558 class_java_lang_Object,
562 throw_main_exception_exit();
564 o = asm_calljavafunction(m, o, NULL, NULL, NULL);
567 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
572 /* get Main Attributes */
574 m = class_resolveclassmethod(o->vftbl->class,
575 utf_new_char("getMainAttributes"),
576 utf_new_char("()Ljava/util/jar/Attributes;"),
577 class_java_lang_Object,
581 throw_main_exception_exit();
583 o = asm_calljavafunction(m, o, NULL, NULL, NULL);
586 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
591 /* get property Main-Class */
593 m = class_resolveclassmethod(o->vftbl->class,
594 utf_new_char("getValue"),
595 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
596 class_java_lang_Object,
600 throw_main_exception_exit();
602 s = javastring_new_char("Main-Class");
604 o = asm_calljavafunction(m, o, s, NULL, NULL);
607 throw_main_exception_exit();
609 return javastring_tochar(o);
613 void exit_handler(void);
616 /* main ************************************************************************
620 *******************************************************************************/
622 int main(int argc, char **argv)
627 /* local variables ********************************************************/
629 char logfilename[200] = "";
635 char *specificmethodname = NULL;
636 char *specificsignature = NULL;
638 #if defined(ENABLE_JVMTI)
645 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
646 stackbottom = &dummy;
649 if (atexit(exit_handler))
650 throw_cacao_exception_exit(string_java_lang_InternalError,
651 "Unable to register exit_handler");
653 /* initialize global variables */
655 cacao_exiting = false;
658 /************ Collect info from the environment ************************/
660 #if defined(DISABLE_GC)
661 nogc_init(HEAP_MAXSIZE, HEAP_STARTSIZE);
664 /* set the bootclasspath */
666 cp = getenv("BOOTCLASSPATH");
668 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
669 strcpy(bootclasspath, cp);
672 cplen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_VM_ZIP_PATH) +
674 strlen(CLASSPATH_INSTALL_DIR) +
675 strlen(CLASSPATH_GLIBJ_ZIP_PATH) +
678 bootclasspath = MNEW(char, cplen);
679 strcpy(bootclasspath, CACAO_INSTALL_PREFIX);
680 strcat(bootclasspath, CACAO_VM_ZIP_PATH);
681 strcat(bootclasspath, ":");
682 strcat(bootclasspath, CLASSPATH_INSTALL_DIR);
683 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP_PATH);
687 /* set the classpath */
689 cp = getenv("CLASSPATH");
691 classpath = MNEW(char, strlen(cp) + strlen("0"));
692 strcat(classpath, cp);
695 classpath = MNEW(char, strlen(".") + strlen("0"));
696 strcpy(classpath, ".");
700 /***************** Interpret the command line *****************/
705 heapmaxsize = HEAP_MAXSIZE;
706 heapstartsize = HEAP_STARTSIZE;
707 opt_stacksize = STACK_SIZE;
709 /* initialize properties before commandline handling */
711 if (!properties_init())
712 throw_cacao_exception_exit(string_java_lang_InternalError,
713 "Unable to init properties");
715 while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
720 case OPT_BOOTCLASSPATH:
721 /* Forget default bootclasspath and set the argument as new boot */
723 MFREE(bootclasspath, char, strlen(bootclasspath));
725 bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
726 strcpy(bootclasspath, opt_arg);
729 case OPT_BOOTCLASSPATH_A:
730 /* append to end of bootclasspath */
731 cplen = strlen(bootclasspath);
733 bootclasspath = MREALLOC(bootclasspath,
736 cplen + strlen(":") +
737 strlen(opt_arg) + strlen("0"));
739 strcat(bootclasspath, ":");
740 strcat(bootclasspath, opt_arg);
743 case OPT_BOOTCLASSPATH_P:
744 /* prepend in front of bootclasspath */
748 bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
749 cplen + strlen("0"));
751 strcpy(bootclasspath, opt_arg);
752 strcat(bootclasspath, ":");
753 strcat(bootclasspath, cp);
755 MFREE(cp, char, cplen);
759 /* forget old classpath and set the argument as new classpath */
760 MFREE(classpath, char, strlen(classpath));
762 classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
763 strcpy(classpath, opt_arg);
770 #if defined(ENABLE_JVMTI)
778 set_jvmti_phase(JVMTI_PHASE_ONLOAD);
780 set_jvmti_phase(JVMTI_PHASE_PRIMORDIAL);
786 for (j = 0; j < strlen(opt_arg); j++) {
787 if (opt_arg[j] == '=') {
789 properties_add(opt_arg, opt_arg + j + 1);
794 /* if no '=' is given, just create an empty property */
796 properties_add(opt_arg, "");
808 c = opt_arg[strlen(opt_arg) - 1];
810 if (c == 'k' || c == 'K') {
811 j = 1024 * atoi(opt_arg);
813 } else if (c == 'm' || c == 'M') {
814 j = 1024 * 1024 * atoi(opt_arg);
821 else if (i == OPT_MS)
837 compileverbose = true;
840 case OPT_VERBOSESPECIFIC:
841 if (strcmp("class", opt_arg) == 0)
842 opt_verboseclass = true;
844 else if (strcmp("gc", opt_arg) == 0)
845 opt_verbosegc = true;
847 else if (strcmp("jni", opt_arg) == 0)
848 opt_verbosejni = true;
851 case OPT_VERBOSEEXCEPTION:
852 opt_verboseexception = true;
855 #ifdef TYPECHECK_VERBOSE
857 typecheckverbose = true;
861 case OPT_VERBOSECALL:
870 case OPT_FULLVERSION:
874 case OPT_SHOWVERSION:
887 opt_liberalutf = true;
895 getcompilingtime = true;
896 getloadingtime = true;
899 #if defined(ENABLE_STATISTICS)
906 strcpy(logfilename, opt_arg);
910 for (j = 0; j < strlen(opt_arg); j++) {
911 switch (opt_arg[j]) {
926 makeinitializations = false;
935 specificmethodname = opt_arg;
936 makeinitializations = false;
940 specificsignature = opt_arg;
946 makeinitializations = false;
949 case OPT_SHOW: /* Display options */
950 for (j = 0; j < strlen(opt_arg); j++) {
951 switch (opt_arg[j]) {
953 opt_showdisassemble = true;
954 compileverbose = true;
957 showconstantpool = true;
960 opt_showddatasegment = true;
963 opt_showexceptionstubs = true;
966 opt_showintermediate = true;
967 compileverbose = true;
973 opt_shownativestub = true;
989 for (j = 0; j < strlen(opt_arg); j++) {
990 switch (opt_arg[j]) {
992 /* define in options.h; Used in main.c, jit.c & inline.c */
1001 inlinevirtuals = true;
1005 inlineexceptions = true;
1008 inlineparamopt = true;
1011 inlineoutsiders = true;
1019 #ifdef STATIC_ANALYSIS
1021 opt_rt = true; /* default for inlining */
1025 opt_xta = true; /* in test currently */
1029 printf("\nVTA is not yet available\n");
1031 /***opt_vta = true; not yet **/
1035 #if defined(ENABLE_LSRA)
1050 #if defined(ENABLE_JIT)
1053 printf("-Xjit option not enabled.\n");
1059 #if defined(ENABLE_INTRP)
1062 printf("-Xint option not enabled.\n");
1067 #if defined(ENABLE_INTRP)
1068 case OPT_STATIC_SUPERS:
1069 opt_static_supers = atoi(opt_arg);
1072 case OPT_NO_DYNAMIC:
1073 opt_no_dynamic = true;
1076 case OPT_NO_REPLICATION:
1077 opt_no_replication = true;
1080 case OPT_NO_QUICKSUPER:
1081 opt_no_quicksuper = true;
1090 printf("Unknown option: %s\n", argv[opt_ind]);
1095 if (opt_ind >= argc)
1099 /* transform dots into slashes in the class name */
1101 mainstring = argv[opt_ind++];
1104 /* do not mangle jar filename */
1106 for (i = strlen(mainstring) - 1; i >= 0; i--) {
1107 if (mainstring[i] == '.') mainstring[i] = '/';
1111 /* put jarfile in classpath */
1115 classpath = MNEW(char, strlen(mainstring) + strlen(":") +
1116 strlen(classpath) + strlen("0"));
1118 strcpy(classpath, mainstring);
1119 strcat(classpath, ":");
1120 strcat(classpath, cp);
1122 MFREE(cp, char, strlen(cp));
1125 /**************************** Program start *****************************/
1127 log_init(logfilename);
1130 log_text("CACAO started -------------------------------------------------------");
1132 /* initialize JavaVM */
1134 vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
1136 /* Get the default initialization arguments and set the class path */
1138 JNI_GetDefaultJavaVMInitArgs(&vm_args);
1140 vm_args.minHeapSize = heapstartsize;
1141 vm_args.maxHeapSize = heapmaxsize;
1143 vm_args.classpath = classpath;
1145 /* load and initialize a Java VM, return a JNI interface pointer in env */
1147 JNI_CreateJavaVM(&jvm, &env, &vm_args);
1149 #if defined(ENABLE_JVMTI)
1150 set_jvmti_phase(JVMTI_PHASE_START);
1153 /* initialize the garbage collector */
1155 gc_init(heapmaxsize, heapstartsize);
1157 #if defined(ENABLE_INTRP)
1158 /* allocate main thread stack */
1161 intrp_main_stack = (u1 *) alloca(opt_stacksize);
1162 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1166 cacao_initializing = true;
1168 #if defined(USE_THREADS)
1169 #if defined(NATIVE_THREADS)
1175 /* initialize the string hashtable stuff: lock (must be done
1176 _after_ threads_preinit) */
1179 throw_main_exception_exit();
1181 /* initialize the utf8 hashtable stuff: lock, often used utf8
1182 strings (must be done _after_ threads_preinit) */
1185 throw_main_exception_exit();
1187 /* initialize the classcache hashtable stuff: lock, hashtable
1188 (must be done _after_ threads_preinit) */
1190 if (!classcache_init())
1191 throw_main_exception_exit();
1193 /* initialize the loader with bootclasspath (must be done _after_
1197 throw_main_exception_exit();
1199 suck_add_from_property("java.endorsed.dirs");
1200 suck_add(bootclasspath);
1202 /* initialize the memory subsystem (must be done _after_
1206 throw_main_exception_exit();
1208 /* initialize the finalizer stuff: lock, linked list (must be done
1209 _after_ threads_preinit) */
1211 if (!finalizer_init())
1212 throw_main_exception_exit();
1214 /* install architecture dependent signal handler used for exceptions */
1218 /* initialize the codegen subsystems */
1222 /* initializes jit compiler */
1226 /* machine dependent initialization */
1230 /* initialize the loader subsystems (must be done _after_
1233 if (!loader_init((u1 *) &dummy))
1234 throw_main_exception_exit();
1237 throw_main_exception_exit();
1240 throw_main_exception_exit();
1242 if (!exceptions_init())
1243 throw_main_exception_exit();
1245 if (!builtin_init())
1246 throw_main_exception_exit();
1248 #if defined(USE_THREADS)
1249 if (!threads_init((u1 *) &dummy))
1250 throw_main_exception_exit();
1253 /* That's important, otherwise we get into trouble, if the Runtime
1254 static initializer is called before (circular dependency. This
1255 is with classpath 0.09. Another important thing is, that this
1256 has to happen after initThreads!!! */
1258 if (!initialize_class(class_java_lang_System))
1259 throw_main_exception_exit();
1261 /* JNI init creates a Java object (this means running Java code) */
1264 throw_main_exception_exit();
1266 #if defined(USE_THREADS)
1267 /* finally, start the finalizer thread */
1269 if (!finalizer_start_thread())
1270 throw_main_exception_exit();
1273 cacao_initializing = false;
1276 /* start worker routines **************************************************/
1279 classinfo *mainclass; /* java/lang/Class */
1281 java_objectarray *a;
1284 /* set return value to OK */
1289 /* open jar file with java.util.jar.JarFile */
1290 mainstring = getmainclassnamefromjar(mainstring);
1293 /* load the main class */
1295 if (!(mainclass = load_class_from_sysloader(utf_new_char(mainstring))))
1296 throw_main_exception_exit();
1298 /* error loading class, clear exceptionptr for new exception */
1300 if (*exceptionptr || !mainclass) {
1301 /* *exceptionptr = NULL; */
1303 /* *exceptionptr = */
1304 /* new_exception_message(string_java_lang_NoClassDefFoundError, */
1306 throw_main_exception_exit();
1309 if (!link_class(mainclass))
1310 throw_main_exception_exit();
1312 /* find the `main' method of the main class */
1314 m = class_resolveclassmethod(mainclass,
1315 utf_new_char("main"),
1316 utf_new_char("([Ljava/lang/String;)V"),
1317 class_java_lang_Object,
1320 if (*exceptionptr) {
1321 throw_main_exception_exit();
1324 /* there is no main method or it isn't static */
1326 if (!m || !(m->flags & ACC_STATIC)) {
1327 *exceptionptr = NULL;
1330 new_exception_message(string_java_lang_NoSuchMethodError,
1332 throw_main_exception_exit();
1335 /* build argument array */
1337 a = builtin_anewarray(argc - opt_ind, class_java_lang_String);
1338 for (i = opt_ind; i < argc; i++) {
1339 a->data[i - opt_ind] =
1340 (java_objectheader *) javastring_new(utf_new_char(argv[i]));
1343 #ifdef TYPEINFO_DEBUG_TEST
1344 /* test the typeinfo system */
1347 /*class_showmethods(currentThread->group->header.vftbl->class); */
1349 #if defined(ENABLE_JVMTI) && defined(NATIVE_THREADS)
1352 if (debuggee == (-1)) {
1353 log_text("fork error");
1356 if (debuggee == 0) {
1357 /* child: allow helper process to trace us */
1358 if (TRACEME != 0) exit(0);
1360 /* give parent/helper debugger process control */
1361 kill(0, SIGTRAP); /* do we need this at this stage ? */
1363 /* continue with normal startup */
1367 /* parent/helper debugger process */
1370 remotedbgjvmtienv = new_jvmtienv();
1371 /* set eventcallbacks */
1372 if (JVMTI_ERROR_NONE ==
1374 SetEventCallbacks(remotedbgjvmtienv,
1375 &jvmti_jdwp_EventCallbacks,
1376 sizeof(jvmti_jdwp_EventCallbacks))){
1377 log_text("unable to setup event callbacks");
1381 /* setup listening process (JDWP) */
1382 setup_debugger_process(transport);
1384 /* start to be debugged program */
1387 /* exit debugger process - todo: cleanup */
1399 asm_calljavafunction(m, a, NULL, NULL, NULL);
1401 /* exception occurred? */
1402 if (*exceptionptr) {
1403 throw_main_exception();
1407 #if defined(USE_THREADS)
1408 #if defined(NATIVE_THREADS)
1411 killThread(currentThread);
1415 /* now exit the JavaVM */
1417 /* (*jvm)->DestroyJavaVM(jvm); */
1422 /************* If requested, compile all methods ********************/
1429 classcache_name_entry *nmen;
1430 classcache_class_entry *clsen;
1432 /* create all classes found in the bootclasspath */
1433 /* XXX currently only works with zip/jar's */
1435 loader_load_all_classes();
1437 /* link all classes */
1439 for (slot = 0; slot < hashtable_classcache.size; slot++) {
1440 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
1442 for (; nmen; nmen = nmen->hashlink) {
1443 /* iterate over all class entries */
1445 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
1446 c = clsen->classobj;
1451 if (!(c->state & CLASS_LINKED)) {
1452 if (!link_class(c)) {
1453 fprintf(stderr, "Error linking: ");
1454 utf_fprint_classname(stderr, c->name);
1455 fprintf(stderr, "\n");
1457 /* print out exception and cause */
1459 exceptions_print_exception(*exceptionptr);
1461 /* goto next class */
1467 /* compile all class methods */
1469 for (i = 0; i < c->methodscount; i++) {
1470 m = &(c->methods[i]);
1473 if (!jit_compile(m)) {
1474 fprintf(stderr, "Error compiling: ");
1475 utf_fprint_classname(stderr, c->name);
1476 fprintf(stderr, ".");
1477 utf_fprint(stderr, m->name);
1478 utf_fprint(stderr, m->descriptor);
1479 fprintf(stderr, "\n");
1481 /* print out exception and cause */
1483 exceptions_print_exception(*exceptionptr);
1493 /******** If requested, compile a specific method ***************/
1495 if (specificmethodname) {
1498 /* create, load and link the main class */
1500 if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
1501 throw_main_exception_exit();
1503 if (!link_class(mainclass))
1504 throw_main_exception_exit();
1506 if (specificsignature) {
1507 m = class_resolveclassmethod(mainclass,
1508 utf_new_char(specificmethodname),
1509 utf_new_char(specificsignature),
1513 m = class_resolveclassmethod(mainclass,
1514 utf_new_char(specificmethodname),
1521 char message[MAXLOGTEXT];
1522 sprintf(message, "%s%s", specificmethodname,
1523 specificsignature ? specificsignature : "");
1526 new_exception_message(string_java_lang_NoSuchMethodException,
1529 throw_main_exception_exit();
1537 /* keep compiler happy */
1543 /* cacao_exit ******************************************************************
1545 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1547 *******************************************************************************/
1549 void cacao_exit(s4 status)
1553 assert(class_java_lang_System);
1554 assert(class_java_lang_System->state & CLASS_LOADED);
1556 #if defined(ENABLE_JVMTI)
1557 set_jvmti_phase(JVMTI_PHASE_DEAD);
1561 if (!link_class(class_java_lang_System))
1562 throw_main_exception_exit();
1564 /* signal that we are exiting */
1566 cacao_exiting = true;
1568 /* call java.lang.System.exit(I)V */
1570 m = class_resolveclassmethod(class_java_lang_System,
1571 utf_new_char("exit"),
1573 class_java_lang_Object,
1577 throw_main_exception_exit();
1579 /* call the exit function with passed exit status */
1581 /* both inlinevirtual and outsiders not allowed on exit */
1582 /* not sure if permanant or temp restriction */
1583 if (inlinevirtuals) inlineoutsiders = false;
1585 asm_calljavafunction(m, (void *) (ptrint) status, NULL, NULL, NULL);
1587 /* this should never happen */
1590 throw_exception_exit();
1592 throw_cacao_exception_exit(string_java_lang_InternalError,
1593 "System.exit(I)V returned without exception");
1597 /*************************** Shutdown function *********************************
1599 Terminates the system immediately without freeing memory explicitly (to be
1600 used only for abnormal termination)
1602 *******************************************************************************/
1604 void cacao_shutdown(s4 status)
1607 #if defined(ENABLE_JVMTI)
1611 if (opt_verbose || getcompilingtime || opt_stat) {
1612 log_text("CACAO terminated by shutdown");
1613 dolog("Exit status: %d\n", (s4) status);
1620 /* exit_handler ****************************************************************
1622 The exit_handler function is called upon program termination.
1624 ATTENTION: Don't free system resources here! Some threads may still
1625 be running as this is called from VMRuntime.exit(). The OS does the
1628 *******************************************************************************/
1630 void exit_handler(void)
1632 /********************* Print debug tables ************************/
1634 #if !defined(NDEBUG)
1636 class_showmethods(mainclass);
1638 if (showconstantpool)
1639 class_showconstantpool(mainclass);
1645 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
1646 clear_thread_flags(); /* restores standard file descriptor
1650 if (opt_verbose || getcompilingtime || opt_stat) {
1651 log_text("CACAO terminated");
1653 #if defined(ENABLE_STATISTICS)
1656 #ifdef TYPECHECK_STATISTICS
1657 typecheck_print_statistics(get_logfile());
1663 if (getcompilingtime)
1667 /* vm_print_profile(stderr);*/
1672 * These are local overrides for various environment variables in Emacs.
1673 * Please do not remove this and leave it at the end of the file, where
1674 * Emacs will automagically detect them.
1675 * ---------------------------------------------------------------------
1678 * indent-tabs-mode: t