* src/vm/signal.c (signal_init): Disable signal-thread handling
[cacao.git] / src / vm / vm.c
1 /* src/vm/vm.c - VM startup and shutdown functions
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    $Id: vm.c 4357 2006-01-22 23:33:38Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <stdlib.h>
35
36 #if defined(WITH_JRE_LAYOUT)
37 # include <libgen.h>
38 # include <unistd.h>
39 #endif
40
41 #include "vm/types.h"
42
43 #include "mm/gc-common.h"
44 #include "mm/memory.h"
45
46 #include "native/jni.h"
47 #include "native/native.h"
48 #include "native/include/java_lang_String.h"
49
50 #if defined(ENABLE_THREADS)
51 # include "threads/native/threads.h"
52 #endif
53
54 #include "toolbox/logging.h"
55
56 #include "vm/builtin.h"
57 #include "vm/exceptions.h"
58 #include "vm/finalizer.h"
59 #include "vm/global.h"
60 #include "vm/initialize.h"
61 #include "vm/properties.h"
62 #include "vm/signallocal.h"
63 #include "vm/stringlocal.h"
64 #include "vm/vm.h"
65
66 #include "vm/jit/jit.h"
67 #include "vm/jit/md.h"
68 #include "vm/jit/asmpart.h"
69
70 #if defined(ENABLE_PROFILING)
71 # include "vm/jit/optimizing/profile.h"
72 #endif
73
74 #include "vm/jit/optimizing/recompile.h"
75
76 #include "vmcore/classcache.h"
77 #include "vmcore/options.h"
78 #include "vmcore/suck.h"
79
80 #if defined(ENABLE_JVMTI)
81 # include "native/jvmti/cacaodbg.h"
82 #endif
83
84
85 /* Invocation API variables ***************************************************/
86
87 _Jv_JavaVM *_Jv_jvm;                    /* denotes a Java VM                  */
88 _Jv_JNIEnv *_Jv_env;                    /* pointer to native method interface */
89
90
91 /* global variables ***********************************************************/
92
93 s4 vms = 0;                             /* number of VMs created              */
94
95 bool vm_initializing = false;
96 bool vm_exiting = false;
97
98 char      *cacao_prefix = NULL;
99 char      *cacao_libjvm = NULL;
100 char      *classpath_libdir = NULL;
101
102 char      *_Jv_bootclasspath;           /* contains the boot classpath        */
103 char      *_Jv_classpath;               /* contains the classpath             */
104 char      *_Jv_java_library_path;
105
106 char      *mainstring = NULL;
107 classinfo *mainclass = NULL;
108
109 char *specificmethodname = NULL;
110 char *specificsignature = NULL;
111
112 bool startit = true;
113
114 #if defined(ENABLE_INTRP)
115 u1 *intrp_main_stack = NULL;
116 #endif
117
118
119 /* define heap sizes **********************************************************/
120
121 #define HEAP_MAXSIZE      128 * 1024 * 1024 /* default 128MB                  */
122 #define HEAP_STARTSIZE      2 * 1024 * 1024 /* default 2MB                    */
123 #define STACK_SIZE                64 * 1024 /* default 64kB                   */
124
125
126 /* define command line options ************************************************/
127
128 enum {
129         OPT_FOO,
130
131         /* Java options */
132
133         OPT_JAR,
134
135         OPT_D32,
136         OPT_D64,
137
138         OPT_CLASSPATH,
139         OPT_D,
140
141         OPT_VERBOSE,
142
143         OPT_VERSION,
144         OPT_SHOWVERSION,
145         OPT_FULLVERSION,
146
147         OPT_HELP,
148         OPT_X,
149         OPT_XX,
150
151         OPT_ESA,
152         OPT_DSA,
153
154         /* Java non-standard options */
155
156         OPT_JIT,
157         OPT_INTRP,
158
159         OPT_BOOTCLASSPATH,
160         OPT_BOOTCLASSPATH_A,
161         OPT_BOOTCLASSPATH_P,
162
163         OPT_BOOTCLASSPATH_C,
164
165 #if defined(ENABLE_PROFILING)
166         OPT_PROF,
167         OPT_PROF_OPTION,
168 #endif
169
170         OPT_MS,
171         OPT_MX,
172
173         /* CACAO options */
174
175         OPT_VERBOSE1,
176         OPT_NOIEEE,
177         OPT_SOFTNULL,
178
179 #if defined(ENABLE_STATISTICS)
180         OPT_TIME,
181         OPT_STAT,
182 #endif
183
184         OPT_LOG,
185         OPT_CHECK,
186         OPT_LOAD,
187         OPT_SHOW,
188         OPT_DEBUGCOLOR,
189
190 #if !defined(NDEBUG)
191         OPT_ALL,
192         OPT_METHOD,
193         OPT_SIGNATURE,
194 #endif
195
196 #if defined(ENABLE_VERIFIER)
197         OPT_NOVERIFY,
198 #if defined(TYPECHECK_VERBOSE)
199         OPT_VERBOSETC,
200 #endif
201 #endif /* defined(ENABLE_VERIFIER) */
202         OPT_EAGER,
203
204         /* optimization options */
205
206 #if defined(ENABLE_LOOP)
207         OPT_OLOOP,
208 #endif
209         
210 #if defined(ENABLE_IFCONV)
211         OPT_IFCONV,
212 #endif
213
214 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
215         OPT_LSRA,
216 #endif
217
218 #if defined(ENABLE_INLINING)
219         OPT_INLINING,
220 #if !defined(NDEBUG)
221         OPT_INLINE_LOG,
222 #endif
223 #if defined(ENABLE_INLINING_DEBUG)
224         OPT_INLINE_DEBUG_ALL,
225         OPT_INLINE_DEBUG_END,
226         OPT_INLINE_DEBUG_MIN,
227         OPT_INLINE_DEBUG_MAX,
228         OPT_INLINE_REPLACE_VERBOSE,
229         OPT_INLINE_REPLACE_VERBOSE2,
230 #endif /* defined(ENABLE_INLINING_DEBUG) */
231 #endif /* defined(ENABLE_INLINING) */
232
233 #if defined(ENABLE_INTRP)
234         /* interpreter options */
235
236         OPT_NO_DYNAMIC,
237         OPT_NO_REPLICATION,
238         OPT_NO_QUICKSUPER,
239         OPT_STATIC_SUPERS,
240         OPT_TRACE,
241 #endif
242
243         OPT_SS,
244
245 #ifdef ENABLE_JVMTI
246         OPT_DEBUG,
247         OPT_XRUNJDWP,
248         OPT_NOAGENT,
249         OPT_AGENTLIB,
250         OPT_AGENTPATH,
251 #endif
252
253         DUMMY
254 };
255
256
257 opt_struct opts[] = {
258         { "foo",               false, OPT_FOO },
259
260         /* Java options */
261
262         { "jar",               false, OPT_JAR },
263
264         { "d32",               false, OPT_D32 },
265         { "d64",               false, OPT_D64 },
266         { "client",            false, OPT_IGNORE },
267         { "server",            false, OPT_IGNORE },
268         { "jvm",               false, OPT_IGNORE },
269         { "hotspot",           false, OPT_IGNORE },
270
271         { "classpath",         true,  OPT_CLASSPATH },
272         { "cp",                true,  OPT_CLASSPATH },
273         { "D",                 true,  OPT_D },
274         { "version",           false, OPT_VERSION },
275         { "showversion",       false, OPT_SHOWVERSION },
276         { "fullversion",       false, OPT_FULLVERSION },
277         { "help",              false, OPT_HELP },
278         { "?",                 false, OPT_HELP },
279         { "X",                 false, OPT_X },
280         { "XX",                false, OPT_XX },
281
282         { "esa",                     false, OPT_ESA },
283         { "enablesystemassertions",  false, OPT_ESA },
284         { "dsa",                     false, OPT_DSA },
285         { "disablesystemassertions", false, OPT_DSA },
286
287         { "noasyncgc",         false, OPT_IGNORE },
288 #if defined(ENABLE_VERIFIER)
289         { "noverify",          false, OPT_NOVERIFY },
290 #endif
291         { "v",                 false, OPT_VERBOSE1 },
292         { "verbose:",          true,  OPT_VERBOSE },
293
294 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
295         { "verbosetc",         false, OPT_VERBOSETC },
296 #endif
297 #if defined(__ALPHA__)
298         { "noieee",            false, OPT_NOIEEE },
299 #endif
300         { "softnull",          false, OPT_SOFTNULL },
301 #if defined(ENABLE_STATISTICS)
302         { "time",              false, OPT_TIME },
303         { "stat",              false, OPT_STAT },
304 #endif
305         { "log",               true,  OPT_LOG },
306         { "c",                 true,  OPT_CHECK },
307         { "l",                 false, OPT_LOAD },
308         { "eager",             false, OPT_EAGER },
309
310 #if !defined(NDEBUG)
311         { "all",               false, OPT_ALL },
312         { "sig",               true,  OPT_SIGNATURE },
313 #endif
314
315 #if defined(ENABLE_LOOP)
316         { "oloop",             false, OPT_OLOOP },
317 #endif
318 #if defined(ENABLE_IFCONV)
319         { "ifconv",            false, OPT_IFCONV },
320 #endif
321 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
322         { "lsra",              false, OPT_LSRA },
323 #endif
324
325 #if defined(ENABLE_INTRP)
326         /* interpreter options */
327
328         { "trace",             false, OPT_TRACE },
329         { "static-supers",     true,  OPT_STATIC_SUPERS },
330         { "no-dynamic",        false, OPT_NO_DYNAMIC },
331         { "no-replication",    false, OPT_NO_REPLICATION },
332         { "no-quicksuper",     false, OPT_NO_QUICKSUPER },
333 #endif
334
335         /* JVMTI Agent Command Line Options */
336 #ifdef ENABLE_JVMTI
337         { "agentlib:",         true,  OPT_AGENTLIB },
338         { "agentpath:",        true,  OPT_AGENTPATH },
339 #endif
340
341         /* Java non-standard options */
342
343         { "Xjit",              false, OPT_JIT },
344         { "Xint",              false, OPT_INTRP },
345         { "Xbootclasspath:",   true,  OPT_BOOTCLASSPATH },
346         { "Xbootclasspath/a:", true,  OPT_BOOTCLASSPATH_A },
347         { "Xbootclasspath/p:", true,  OPT_BOOTCLASSPATH_P },
348         { "Xbootclasspath/c:", true,  OPT_BOOTCLASSPATH_C },
349
350 #ifdef ENABLE_JVMTI
351         { "Xdebug",            false, OPT_DEBUG },
352         { "Xnoagent",          false, OPT_NOAGENT },
353         { "Xrunjdwp",          true,  OPT_XRUNJDWP },
354 #endif 
355
356         { "Xms",               true,  OPT_MS },
357         { "ms",                true,  OPT_MS },
358         { "Xmx",               true,  OPT_MX },
359         { "mx",                true,  OPT_MX },
360         { "Xss",               true,  OPT_SS },
361         { "ss",                true,  OPT_SS },
362
363 #if defined(ENABLE_PROFILING)
364         { "Xprof:",            true,  OPT_PROF_OPTION },
365         { "Xprof",             false, OPT_PROF },
366 #endif
367
368         /* inlining options */
369
370 #if defined(ENABLE_INLINING)
371 #if defined(ENABLE_INLINING_DEBUG)
372         { "ia",                false, OPT_INLINE_DEBUG_ALL },
373         { "ii",                true,  OPT_INLINE_DEBUG_MIN },
374         { "im",                true,  OPT_INLINE_DEBUG_MAX },
375         { "ie",                true,  OPT_INLINE_DEBUG_END },
376         { "ir",                false, OPT_INLINE_REPLACE_VERBOSE },
377         { "iR",                false, OPT_INLINE_REPLACE_VERBOSE2 },
378 #endif /* defined(ENABLE_INLINING_DEBUG) */
379 #if !defined(NDEBUG)
380         { "il",                false, OPT_INLINE_LOG },
381 #endif
382         { "i",                 false, OPT_INLINING },
383 #endif /* defined(ENABLE_INLINING) */
384
385         /* keep these at the end of the list */
386
387 #if !defined(NDEBUG)
388         { "m",                 true,  OPT_METHOD },
389 #endif
390
391         { "s",                 true,  OPT_SHOW },
392         { "debug-color",      false,  OPT_DEBUGCOLOR },
393
394         { NULL,                false, 0 }
395 };
396
397
398 /* usage ***********************************************************************
399
400    Prints the correct usage syntax to stdout.
401
402 *******************************************************************************/
403
404 void usage(void)
405 {
406         puts("Usage: cacao [-options] classname [arguments]");
407         puts("               (to run a class file)");
408         puts("   or  cacao [-options] -jar jarfile [arguments]");
409         puts("               (to run a standalone jar file)\n");
410
411         puts("where options include:");
412         puts("    -d32                     use 32-bit data model if available");
413         puts("    -d64                     use 64-bit data model if available");
414         puts("    -client                  compatibility (currently ignored)");
415         puts("    -server                  compatibility (currently ignored)");
416         puts("    -jvm                     compatibility (currently ignored)");
417         puts("    -hotspot                 compatibility (currently ignored)\n");
418
419         puts("    -cp <path>               specify a path to look for classes");
420         puts("    -classpath <path>        specify a path to look for classes");
421         puts("    -D<name>=<value>         add an entry to the property list");
422         puts("    -verbose[:class|gc|jni]  enable specific verbose output");
423         puts("    -version                 print product version and exit");
424         puts("    -fullversion             print jpackage-compatible product version and exit");
425         puts("    -showversion             print product version and continue");
426         puts("    -help, -?                print this help message");
427         puts("    -X                       print help on non-standard Java options");
428         puts("    -XX                      print help on CACAO options");
429         puts("    -esa | -enablesystemassertions");
430         puts("                             enable system assertions");
431         puts("    -dsa | -disablesystemassertions");
432         puts("                             disable system assertions");
433
434 #ifdef ENABLE_JVMTI
435         puts("    -agentlib:<agent-lib-name>=<options>  library to load containg JVMTI agent");
436         puts ("                                         for jdwp help use: -agentlib:jdwp=help");
437         puts("    -agentpath:<path-to-agent>=<options>  path to library containg JVMTI agent");
438 #endif
439
440         /* exit with error code */
441
442         exit(1);
443 }   
444
445
446 static void Xusage(void)
447 {
448 #if defined(ENABLE_JIT)
449         puts("    -Xjit                    JIT mode execution (default)");
450 #endif
451 #if defined(ENABLE_INTRP)
452         puts("    -Xint                    interpreter mode execution");
453 #endif
454         puts("    -Xbootclasspath:<zip/jar files and directories separated by :>");
455     puts("                             value is set as bootstrap class path");
456         puts("    -Xbootclasspath/a:<zip/jar files and directories separated by :>");
457         puts("                             value is appended to the bootstrap class path");
458         puts("    -Xbootclasspath/p:<zip/jar files and directories separated by :>");
459         puts("                             value is prepended to the bootstrap class path");
460         puts("    -Xbootclasspath/c:<zip/jar files and directories separated by :>");
461         puts("                             value is used as Java core library, but the");
462         puts("                             hardcoded VM interface classes are prepended");
463         printf("    -Xms<size>               set the initial size of the heap (default: %dMB)\n", HEAP_STARTSIZE / 1024 / 1024);
464         printf("    -Xmx<size>               set the maximum size of the heap (default: %dMB)\n", HEAP_MAXSIZE / 1024 / 1024);
465         printf("    -Xss<size>               set the thread stack size (default: %dkB)\n", STACK_SIZE / 1024);
466
467 #if defined(ENABLE_PROFILING)
468         puts("    -Xprof[:bb]              collect and print profiling data");
469 #endif
470
471 #if defined(ENABLE_JVMTI)
472     /* -Xdebug option depend on gnu classpath JDWP options. options: 
473          transport=dt_socket,address=<hostname:port>,server=(y|n),suspend(y|n) */
474         puts("    -Xdebug                  enable remote debugging\n");
475         puts("    -Xrunjdwp transport=[dt_socket|...],address=<hostname:port>,server=[y|n],suspend=[y|n]\n");
476         puts("                             enable remote debugging\n");
477 #endif 
478
479         /* exit with error code */
480
481         exit(1);
482 }   
483
484
485 static void XXusage(void)
486 {
487         puts("    -v                       write state-information");
488         puts("    -verbose[:call|exception|jit]");
489         puts("                             enable specific verbose output");
490         puts("    -debug-color             colored output for ANSI terms");
491 #ifdef TYPECHECK_VERBOSE
492         puts("    -verbosetc               write debug messages while typechecking");
493 #endif
494 #if defined(__ALPHA__)
495         puts("    -noieee                  don't use ieee compliant arithmetic");
496 #endif
497 #if defined(ENABLE_VERIFIER)
498         puts("    -noverify                don't verify classfiles");
499 #endif
500         puts("    -softnull                use software nullpointer check");
501 #if defined(ENABLE_STATISTICS)
502         puts("    -time                    measure the runtime");
503         puts("    -stat                    detailed compiler statistics");
504 #endif
505         puts("    -log logfile             specify a name for the logfile");
506         puts("    -c(heck)b(ounds)         don't check array bounds");
507         puts("            s(ync)           don't check for synchronization");
508 #if defined(ENABLE_LOOP)
509         puts("    -oloop                   optimize array accesses in loops");
510 #endif
511         puts("    -l                       don't start the class after loading");
512         puts("    -eager                   perform eager class loading and linking");
513 #if !defined(NDEBUG)
514         puts("    -all                     compile all methods, no execution");
515         puts("    -m                       compile only a specific method");
516         puts("    -sig                     specify signature for a specific method");
517 #endif
518
519         puts("    -s...                    show...");
520         puts("      (c)onstants            the constant pool");
521         puts("      (m)ethods              class fields and methods");
522         puts("      (u)tf                  the utf - hash");
523         puts("      (i)ntermediate         intermediate representation");
524 #if defined(ENABLE_DISASSEMBLER)
525         puts("      (a)ssembler            disassembled listing");
526         puts("      n(o)ps                 show NOPs in disassembler output");
527         puts("      (e)xceptionstubs       disassembled exception stubs (only with -sa)");
528         puts("      (n)ative               disassembled native stubs");
529 #endif
530         puts("           (d)atasegment     data segment listing");
531
532 #if defined(ENABLE_INLINING)
533         puts("    -i                       activate inlining");
534 #if !defined(NDEBUG)
535         puts("    -il                      log inlining");
536 #endif
537 #if defined(ENABLE_INLINING_DEBUG)
538         puts("    -ia                      use inlining for all methods");
539         puts("    -ii <size>               set minimum size for inlined result");
540         puts("    -im <size>               set maximum size for inlined result");
541         puts("    -ie <number>             stop inlining after the given number of roots");
542         puts("    -ir                      log on-stack replacement");
543         puts("    -iR                      log on-stack replacement, more verbose");
544 #endif /* defined(ENABLE_INLINING_DEBUG) */
545 #endif /* defined(ENABLE_INLINING) */
546
547 #if defined(ENABLE_IFCONV)
548         puts("    -ifconv                  use if-conversion");
549 #endif
550 #if defined(ENABLE_LSRA)
551         puts("    -lsra                    use linear scan register allocation");
552 #endif
553 #if defined(ENABLE_SSA)
554         puts("    -lsra                    use linear scan register allocation (with SSA)");
555 #endif
556
557         /* exit with error code */
558
559         exit(1);
560 }
561
562
563 /* version *********************************************************************
564
565    Only prints cacao version information.
566
567 *******************************************************************************/
568
569 static void version(bool opt_exit)
570 {
571         puts("java version \""JAVA_VERSION"\"");
572         puts("CACAO version "VERSION"");
573
574         puts("Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,");
575         puts("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,");
576         puts("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,");
577         puts("J. Wenninger, Institut f. Computersprachen - TU Wien\n");
578
579         puts("This program is free software; you can redistribute it and/or");
580         puts("modify it under the terms of the GNU General Public License as");
581         puts("published by the Free Software Foundation; either version 2, or (at");
582         puts("your option) any later version.\n");
583
584         puts("This program is distributed in the hope that it will be useful, but");
585         puts("WITHOUT ANY WARRANTY; without even the implied warranty of");
586         puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU");
587         puts("General Public License for more details.\n");
588
589         puts("Configure/Build options:\n");
590         puts("  ./configure: "VERSION_CONFIGURE_ARGS"");
591 #if defined(__VERSION__)
592         puts("  CC         : "VERSION_CC" ("__VERSION__")");
593 #else
594         puts("  CC         : "VERSION_CC"");
595 #endif
596         puts("  CFLAGS     : "VERSION_CFLAGS"\n");
597
598         puts("Default variables:\n");
599         printf("  maximum heap size              : %d\n", HEAP_MAXSIZE);
600         printf("  initial heap size              : %d\n", HEAP_STARTSIZE);
601         printf("  stack size                     : %d\n", STACK_SIZE);
602 #if defined(WITH_CLASSPATH_GNU)
603         puts("  java.boot.class.path           : "CACAO_VM_ZIP":"CLASSPATH_CLASSES"");
604 #else
605         puts("  java.boot.class.path           : "CLASSPATH_CLASSES"");
606 #endif
607         puts("  gnu.classpath.boot.library.path: "CLASSPATH_LIBDIR"/classpath\n");
608
609         puts("Runtime variables:\n");
610         printf("  maximum heap size              : %d\n", opt_heapmaxsize);
611         printf("  initial heap size              : %d\n", opt_heapstartsize);
612         printf("  stack size                     : %d\n", opt_stacksize);
613         printf("  java.boot.class.path           : %s\n", _Jv_bootclasspath);
614         printf("  gnu.classpath.boot.library.path: %s\n", classpath_libdir);
615         printf("  java.class.path                : %s\n", _Jv_classpath);
616
617         /* exit normally, if requested */
618
619         if (opt_exit)
620                 exit(0);
621 }
622
623
624 /* fullversion *****************************************************************
625
626    Prints a Sun compatible version information (required e.g. by
627    jpackage, www.jpackage.org).
628
629 *******************************************************************************/
630
631 static void fullversion(void)
632 {
633         puts("java full version \"cacao-"JAVA_VERSION"\"");
634
635         /* exit normally */
636
637         exit(0);
638 }
639
640
641 /* forward declarations *******************************************************/
642
643 static char *vm_get_mainclass_from_jar(char *mainstring);
644 #if !defined(NDEBUG)
645 static void  vm_compile_all(void);
646 static void  vm_compile_method(void);
647 #endif
648
649
650 /* vm_createjvm ****************************************************************
651
652    Implementation for JNI_CreateJavaVM.
653
654 *******************************************************************************/
655
656 bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
657 {
658         JavaVMInitArgs *_vm_args;
659         _Jv_JNIEnv     *env;
660         _Jv_JavaVM     *vm;
661
662         /* get the arguments for the new JVM */
663
664         _vm_args = (JavaVMInitArgs *) vm_args;
665
666         /* get the VM and Env tables (must be set before vm_create) */
667
668         env = NEW(_Jv_JNIEnv);
669
670 #if defined(ENABLE_JAVASE)
671         env->env = &_Jv_JNINativeInterface;
672 #endif
673
674         /* XXX Set the global variable.  Maybe we should do that differently. */
675
676         _Jv_env = env;
677
678         /* create and fill a JavaVM structure */
679
680         vm = NEW(_Jv_JavaVM);
681
682 #if defined(ENABLE_JAVASE)
683         vm->functions = &_Jv_JNIInvokeInterface;
684 #endif
685
686         /* XXX Set the global variable.  Maybe we should do that differently. */
687         /* XXX JVMTI Agents needs a JavaVM  */
688
689         _Jv_jvm = vm;
690
691         /* actually create the JVM */
692
693         if (!vm_create(_vm_args))
694                 goto error;
695
696 #if defined(ENABLE_JAVASE)
697         /* setup the local ref table (must be created after vm_create) */
698
699         if (!jni_init_localref_table())
700                 goto error;
701 #endif
702
703         /* now return the values */
704
705         *p_vm  = (JavaVM *) vm;
706         *p_env = (void *) env;
707
708         return true;
709
710  error:
711         /* release allocated memory */
712
713         FREE(env, _Jv_JNIEnv);
714         FREE(vm, _Jv_JavaVM);
715
716         return false;
717 }
718
719
720 /* vm_create *******************************************************************
721
722    Creates a JVM.  Called by vm_createjvm.
723
724 *******************************************************************************/
725
726 bool vm_create(JavaVMInitArgs *vm_args)
727 {
728         char *cp;
729         s4    len;
730         s4    opt;
731         s4    i, j;
732         bool  opt_version;
733         bool  opt_exit;
734
735 #if defined(ENABLE_JVMTI)
736         lt_dlhandle  handle;
737         char *libname, *agentarg;
738         bool jdwp,agentbypath;
739         jdwp = agentbypath = false;
740 #endif
741
742         /* check the JNI version requested */
743
744         switch (vm_args->version) {
745         case JNI_VERSION_1_1:
746                 break;
747         case JNI_VERSION_1_2:
748         case JNI_VERSION_1_4:
749                 break;
750         default:
751                 return false;
752         }
753
754         /* we only support 1 JVM instance */
755
756         if (vms > 0)
757                 return false;
758
759         if (atexit(vm_exit_handler))
760                 vm_abort("atexit failed: %s\n", strerror(errno));
761
762         if (opt_verbose)
763                 log_text("CACAO started -------------------------------------------------------");
764
765         /* set the VM starttime */
766
767         _Jv_jvm->starttime = builtin_currenttimemillis();
768
769         /* get stuff from the environment *****************************************/
770
771 #if defined(WITH_JRE_LAYOUT)
772         /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
773
774         cacao_prefix = MNEW(char, 4096);
775
776         if (readlink("/proc/self/exe", cacao_prefix, 4095) == -1)
777                 vm_abort("readlink failed: %s\n", strerror(errno));
778
779         /* get the path of the current executable */
780
781         cacao_prefix = dirname(cacao_prefix);
782
783         if ((strlen(cacao_prefix) + strlen("/..") + strlen("0")) > 4096)
784                 vm_abort("libjvm name to long for buffer\n");
785
786         /* concatenate the library name */
787
788         strcat(cacao_prefix, "/..");
789
790         /* now set path to libjvm.so */
791
792         len = strlen(cacao_prefix) + strlen("/lib/libjvm") + strlen("0");
793
794         cacao_libjvm = MNEW(char, len);
795         strcpy(cacao_libjvm, cacao_prefix);
796         strcat(cacao_libjvm, "/lib/libjvm");
797
798         /* and finally set the path to GNU Classpath libraries */
799
800         len = strlen(cacao_prefix) + strlen("/lib/classpath") + strlen("0");
801
802         classpath_libdir = MNEW(char, len);
803         strcpy(classpath_libdir, cacao_prefix);
804         strcat(classpath_libdir, "/lib/classpath");
805 #else
806         cacao_prefix     = CACAO_PREFIX;
807         cacao_libjvm     = CACAO_LIBDIR"/libjvm";
808         classpath_libdir = CLASSPATH_LIBDIR"/classpath";
809 #endif
810
811         /* set the bootclasspath */
812
813         cp = getenv("BOOTCLASSPATH");
814
815         if (cp != NULL) {
816                 _Jv_bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
817                 strcpy(_Jv_bootclasspath, cp);
818         }
819         else {
820 #if defined(WITH_JRE_LAYOUT)
821                 len =
822 # if defined(WITH_CLASSPATH_GNU)
823                         strlen(cacao_prefix) +
824                         strlen("/share/cacao/vm.zip") +
825                         strlen(":") +
826 # endif
827                         strlen(cacao_prefix) +
828                         strlen("/share/classpath/glibj.zip") +
829                         strlen("0");
830
831                 _Jv_bootclasspath = MNEW(char, len);
832 # if defined(WITH_CLASSPATH_GNU)
833                 strcat(_Jv_bootclasspath, cacao_prefix);
834                 strcat(_Jv_bootclasspath, "/share/cacao/vm.zip");
835                 strcat(_Jv_bootclasspath, ":");
836 # endif
837                 strcat(_Jv_bootclasspath, cacao_prefix);
838                 strcat(_Jv_bootclasspath, "/share/classpath/glibj.zip");
839 #else
840                 len =
841 # if defined(WITH_CLASSPATH_GNU)
842                         strlen(CACAO_VM_ZIP) +
843                         strlen(":") +
844 # endif
845                         strlen(CLASSPATH_CLASSES) +
846                         strlen("0");
847
848                 _Jv_bootclasspath = MNEW(char, len);
849 # if defined(WITH_CLASSPATH_GNU)
850                 strcat(_Jv_bootclasspath, CACAO_VM_ZIP);
851                 strcat(_Jv_bootclasspath, ":");
852 # endif
853                 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
854 #endif
855         }
856
857         /* set the classpath */
858
859         cp = getenv("CLASSPATH");
860
861         if (cp != NULL) {
862                 _Jv_classpath = MNEW(char, strlen(cp) + strlen("0"));
863                 strcat(_Jv_classpath, cp);
864         }
865         else {
866                 _Jv_classpath = MNEW(char, strlen(".") + strlen("0"));
867                 strcpy(_Jv_classpath, ".");
868         }
869
870         /* get and set java.library.path */
871
872         _Jv_java_library_path = getenv("LD_LIBRARY_PATH");
873
874         if (_Jv_java_library_path == NULL)
875                 _Jv_java_library_path = "";
876
877         /* interpret the options **************************************************/
878
879         opt_version       = false;
880         opt_exit          = false;
881
882         checknull         = false;
883         opt_noieee        = false;
884
885         opt_heapmaxsize   = HEAP_MAXSIZE;
886         opt_heapstartsize = HEAP_STARTSIZE;
887         opt_stacksize     = STACK_SIZE;
888
889
890 #if defined(ENABLE_JVMTI)
891         /* initialize JVMTI related  **********************************************/
892         jvmti = false;
893 #endif
894
895         /* initialize and fill properties before command-line handling */
896
897         if (!properties_init())
898                 vm_abort("properties_init failed");
899
900         /* iterate over all passed options */
901
902         while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
903                 switch (opt) {
904                 case OPT_FOO:
905                         opt_foo = true;
906                         break;
907
908                 case OPT_IGNORE:
909                         break;
910                         
911                 case OPT_JAR:
912                         opt_jar = true;
913                         break;
914
915                 case OPT_D32:
916 #if SIZEOF_VOID_P == 8
917                         puts("Running a 32-bit JVM is not supported on this platform.");
918                         exit(1);
919 #endif
920                         break;
921
922                 case OPT_D64:
923 #if SIZEOF_VOID_P == 4
924                         puts("Running a 64-bit JVM is not supported on this platform.");
925                         exit(1);
926 #endif
927                         break;
928
929                 case OPT_CLASSPATH:
930                         /* forget old classpath and set the argument as new classpath */
931                         MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
932
933                         _Jv_classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
934                         strcpy(_Jv_classpath, opt_arg);
935                         break;
936
937                 case OPT_D:
938                         for (i = 0; i < strlen(opt_arg); i++) {
939                                 if (opt_arg[i] == '=') {
940                                         opt_arg[i] = '\0';
941                                         properties_add(opt_arg, opt_arg + i + 1);
942                                         goto opt_d_done;
943                                 }
944                         }
945
946                         /* if no '=' is given, just create an empty property */
947
948                         properties_add(opt_arg, "");
949
950                 opt_d_done:
951                         break;
952
953                 case OPT_BOOTCLASSPATH:
954                         /* Forget default bootclasspath and set the argument as
955                            new boot classpath. */
956
957                         MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
958
959                         _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
960                         strcpy(_Jv_bootclasspath, opt_arg);
961                         break;
962
963                 case OPT_BOOTCLASSPATH_A:
964                         /* append to end of bootclasspath */
965
966                         len = strlen(_Jv_bootclasspath);
967
968                         _Jv_bootclasspath = MREALLOC(_Jv_bootclasspath,
969                                                                                  char,
970                                                                                  len + strlen("0"),
971                                                                                  len + strlen(":") +
972                                                                                  strlen(opt_arg) + strlen("0"));
973
974                         strcat(_Jv_bootclasspath, ":");
975                         strcat(_Jv_bootclasspath, opt_arg);
976                         break;
977
978                 case OPT_BOOTCLASSPATH_P:
979                         /* prepend in front of bootclasspath */
980
981                         cp = _Jv_bootclasspath;
982                         len = strlen(cp);
983
984                         _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
985                                                                          len + strlen("0"));
986
987                         strcpy(_Jv_bootclasspath, opt_arg);
988                         strcat(_Jv_bootclasspath, ":");
989                         strcat(_Jv_bootclasspath, cp);
990
991                         MFREE(cp, char, len);
992                         break;
993
994                 case OPT_BOOTCLASSPATH_C:
995                         /* use as Java core library, but prepend VM interface classes */
996
997                         MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
998
999                         len = strlen(CACAO_VM_ZIP) +
1000                                 strlen(":") +
1001                                 strlen(opt_arg) +
1002                                 strlen("0");
1003
1004                         _Jv_bootclasspath = MNEW(char, len);
1005
1006                         strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
1007                         strcat(_Jv_bootclasspath, ":");
1008                         strcat(_Jv_bootclasspath, opt_arg);
1009                         break;
1010
1011 #if defined(ENABLE_JVMTI)
1012                 case OPT_DEBUG:
1013                         /* this option exists only for compatibility reasons */
1014                         break;
1015
1016                 case OPT_NOAGENT:
1017                         /* I don't know yet what Xnoagent should do. This is only for 
1018                            compatiblity with eclipse - motse */
1019                         break;
1020
1021                 case OPT_XRUNJDWP:
1022                         agentbypath = true;
1023                         jvmti       = true;
1024                         jdwp        = true;
1025
1026                         len =
1027                                 strlen(CACAO_LIBDIR) +
1028                                 strlen("/libjdwp.so=") +
1029                                 strlen(opt_arg) +
1030                                 strlen("0");
1031
1032                         agentarg = MNEW(char, len);
1033
1034                         strcpy(agentarg, CACAO_LIBDIR);
1035                         strcat(agentarg, "/libjdwp.so=");
1036                         strcat(agentarg, &opt_arg[1]);
1037                         break;
1038
1039                 case OPT_AGENTPATH:
1040                         agentbypath = true;
1041
1042                 case OPT_AGENTLIB:
1043                         jvmti = true;
1044                         agentarg = opt_arg;
1045                         break;
1046 #endif
1047                         
1048                 case OPT_MX:
1049                 case OPT_MS:
1050                 case OPT_SS:
1051                         {
1052                                 char c;
1053                                 c = opt_arg[strlen(opt_arg) - 1];
1054
1055                                 if ((c == 'k') || (c == 'K')) {
1056                                         j = atoi(opt_arg) * 1024;
1057
1058                                 } else if ((c == 'm') || (c == 'M')) {
1059                                         j = atoi(opt_arg) * 1024 * 1024;
1060
1061                                 } else
1062                                         j = atoi(opt_arg);
1063
1064                                 if (opt == OPT_MX)
1065                                         opt_heapmaxsize = j;
1066                                 else if (opt == OPT_MS)
1067                                         opt_heapstartsize = j;
1068                                 else
1069                                         opt_stacksize = j;
1070                         }
1071                         break;
1072
1073                 case OPT_VERBOSE1:
1074                         opt_verbose = true;
1075                         break;
1076
1077                 case OPT_VERBOSE:
1078                         if (strcmp("class", opt_arg) == 0)
1079                                 opt_verboseclass = true;
1080
1081                         else if (strcmp("gc", opt_arg) == 0)
1082                                 opt_verbosegc = true;
1083
1084                         else if (strcmp("jni", opt_arg) == 0)
1085                                 opt_verbosejni = true;
1086
1087                         else if (strcmp("call", opt_arg) == 0)
1088                                 opt_verbosecall = true;
1089
1090                         else if (strcmp("jit", opt_arg) == 0) {
1091                                 opt_verbose = true;
1092                                 loadverbose = true;
1093                                 linkverbose = true;
1094                                 initverbose = true;
1095                                 compileverbose = true;
1096                         }
1097                         else if (strcmp("exception", opt_arg) == 0)
1098                                 opt_verboseexception = true;
1099                         break;
1100                 case OPT_DEBUGCOLOR:
1101                         opt_debugcolor = true;
1102                         break;
1103
1104 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
1105                 case OPT_VERBOSETC:
1106                         opt_typecheckverbose = true;
1107                         break;
1108 #endif
1109                                 
1110                 case OPT_VERSION:
1111                         opt_version = true;
1112                         opt_exit    = true;
1113                         break;
1114
1115                 case OPT_FULLVERSION:
1116                         fullversion();
1117                         break;
1118
1119                 case OPT_SHOWVERSION:
1120                         opt_version = true;
1121                         break;
1122
1123                 case OPT_NOIEEE:
1124                         opt_noieee = true;
1125                         break;
1126
1127 #if defined(ENABLE_VERIFIER)
1128                 case OPT_NOVERIFY:
1129                         opt_verify = false;
1130                         break;
1131 #endif
1132
1133                 case OPT_SOFTNULL:
1134                         checknull = true;
1135                         break;
1136
1137 #if defined(ENABLE_STATISTICS)
1138                 case OPT_TIME:
1139                         opt_getcompilingtime = true;
1140                         opt_getloadingtime = true;
1141                         break;
1142                                         
1143                 case OPT_STAT:
1144                         opt_stat = true;
1145                         break;
1146 #endif
1147                                         
1148                 case OPT_LOG:
1149                         log_init(opt_arg);
1150                         break;
1151                         
1152                 case OPT_CHECK:
1153                         for (i = 0; i < strlen(opt_arg); i++) {
1154                                 switch (opt_arg[i]) {
1155                                 case 'b':
1156                                         checkbounds = false;
1157                                         break;
1158                                 case 's':
1159                                         checksync = false;
1160                                         break;
1161                                 default:
1162                                         usage();
1163                                 }
1164                         }
1165                         break;
1166                         
1167                 case OPT_LOAD:
1168                         opt_run = false;
1169                         makeinitializations = false;
1170                         break;
1171
1172                 case OPT_EAGER:
1173                         opt_eager = true;
1174                         break;
1175
1176 #if !defined(NDEBUG)
1177                 case OPT_ALL:
1178                         compileall = true;
1179                         opt_run = false;
1180                         makeinitializations = false;
1181                         break;
1182
1183                 case OPT_METHOD:
1184                         opt_run = false;
1185                         opt_method = opt_arg;
1186                         makeinitializations = false;
1187                         break;
1188
1189                 case OPT_SIGNATURE:
1190                         opt_signature = opt_arg;
1191                         break;
1192 #endif
1193
1194                 case OPT_SHOW:       /* Display options */
1195                         for (i = 0; i < strlen(opt_arg); i++) {         
1196                                 switch (opt_arg[i]) {
1197                                 case 'c':
1198                                         showconstantpool = true;
1199                                         break;
1200
1201                                 case 'u':
1202                                         showutf = true;
1203                                         break;
1204
1205                                 case 'm':
1206                                         showmethods = true;
1207                                         break;
1208
1209                                 case 'i':
1210                                         opt_showintermediate = true;
1211                                         compileverbose = true;
1212                                         break;
1213
1214 #if defined(ENABLE_DISASSEMBLER)
1215                                 case 'a':
1216                                         opt_showdisassemble = true;
1217                                         compileverbose = true;
1218                                         break;
1219
1220                                 case 'o':
1221                                         opt_shownops = true;
1222                                         break;
1223
1224                                 case 'e':
1225                                         opt_showexceptionstubs = true;
1226                                         break;
1227
1228                                 case 'n':
1229                                         opt_shownativestub = true;
1230                                         break;
1231 #endif
1232
1233                                 case 'd':
1234                                         opt_showddatasegment = true;
1235                                         break;
1236
1237                                 default:
1238                                         usage();
1239                                 }
1240                         }
1241                         break;
1242                         
1243 #if defined(ENABLE_LOOP)
1244                 case OPT_OLOOP:
1245                         opt_loops = true;
1246                         break;
1247 #endif
1248
1249 #if defined(ENABLE_INLINING)
1250 #if defined(ENABLE_INLINING_DEBUG)
1251                 case OPT_INLINE_DEBUG_ALL:
1252                         opt_inline_debug_all = true;
1253                         break;
1254                 case OPT_INLINE_DEBUG_END:
1255                         opt_inline_debug_end_counter = atoi(opt_arg);
1256                         break;
1257                 case OPT_INLINE_DEBUG_MIN:
1258                         opt_inline_debug_min_size = atoi(opt_arg);
1259                         break;
1260                 case OPT_INLINE_DEBUG_MAX:
1261                         opt_inline_debug_max_size = atoi(opt_arg);
1262                         break;
1263                 case OPT_INLINE_REPLACE_VERBOSE:
1264                         opt_replace_verbose = 1;
1265                         break;
1266                 case OPT_INLINE_REPLACE_VERBOSE2:
1267                         opt_replace_verbose = 2;
1268                         break;
1269 #endif /* defined(ENABLE_INLINING_DEBUG) */
1270 #if !defined(NDEBUG)
1271                 case OPT_INLINE_LOG:
1272                         opt_inline_debug_log = true;
1273                         break;
1274 #endif /* !defined(NDEBUG) */
1275
1276                 case OPT_INLINING:
1277                         opt_inlining = true;
1278                         break;
1279 #endif /* defined(ENABLE_INLINING) */
1280
1281 #if defined(ENABLE_IFCONV)
1282                 case OPT_IFCONV:
1283                         opt_ifconv = true;
1284                         break;
1285 #endif
1286
1287 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1288                 case OPT_LSRA:
1289                         opt_lsra = true;
1290                         break;
1291 #endif
1292
1293                 case OPT_HELP:
1294                         usage();
1295                         break;
1296
1297                 case OPT_X:
1298                         Xusage();
1299                         break;
1300
1301                 case OPT_XX:
1302                         XXusage();
1303                         break;
1304
1305                 case OPT_ESA:
1306                         _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = true;
1307                         break;
1308
1309                 case OPT_DSA:
1310                         _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = false;
1311                         break;
1312
1313 #if defined(ENABLE_PROFILING)
1314                 case OPT_PROF_OPTION:
1315                         /* use <= to get the last \0 too */
1316
1317                         for (i = 0, j = 0; i <= strlen(opt_arg); i++) {
1318                                 if (opt_arg[i] == ',')
1319                                         opt_arg[i] = '\0';
1320
1321                                 if (opt_arg[i] == '\0') {
1322                                         if (strcmp("bb", opt_arg + j) == 0)
1323                                                 opt_prof_bb = true;
1324
1325                                         else {
1326                                                 printf("Unknown option: -Xprof:%s\n", opt_arg + j);
1327                                                 usage();
1328                                         }
1329
1330                                         /* set k to next char */
1331
1332                                         j = i + 1;
1333                                 }
1334                         }
1335                         /* fall through */
1336
1337                 case OPT_PROF:
1338                         opt_prof = true;
1339                         break;
1340 #endif
1341
1342                 case OPT_JIT:
1343 #if defined(ENABLE_JIT)
1344                         opt_jit = true;
1345 #else
1346                         printf("-Xjit option not enabled.\n");
1347                         exit(1);
1348 #endif
1349                         break;
1350
1351                 case OPT_INTRP:
1352 #if defined(ENABLE_INTRP)
1353                         opt_intrp = true;
1354 #else
1355                         printf("-Xint option not enabled.\n");
1356                         exit(1);
1357 #endif
1358                         break;
1359
1360 #if defined(ENABLE_INTRP)
1361                 case OPT_STATIC_SUPERS:
1362                         opt_static_supers = atoi(opt_arg);
1363                         break;
1364
1365                 case OPT_NO_DYNAMIC:
1366                         opt_no_dynamic = true;
1367                         break;
1368
1369                 case OPT_NO_REPLICATION:
1370                         opt_no_replication = true;
1371                         break;
1372
1373                 case OPT_NO_QUICKSUPER:
1374                         opt_no_quicksuper = true;
1375                         break;
1376
1377                 case OPT_TRACE:
1378                         vm_debug = true;
1379                         break;
1380 #endif
1381
1382                 default:
1383                         printf("Unknown option: %s\n",
1384                                    vm_args->options[opt_index].optionString);
1385                         usage();
1386                 }
1387         }
1388
1389         /* get the main class *****************************************************/
1390
1391         if (opt_index < vm_args->nOptions) {
1392                 mainstring = vm_args->options[opt_index++].optionString;
1393
1394                 /* Put the jar file into the classpath (if any). */
1395
1396                 if (opt_jar == true) {
1397                         /* free old classpath */
1398
1399                         MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1400
1401                         /* put jarfile into classpath */
1402
1403                         _Jv_classpath = MNEW(char, strlen(mainstring) + strlen("0"));
1404
1405                         strcpy(_Jv_classpath, mainstring);
1406                 }
1407                 else {
1408                         /* replace .'s with /'s in classname */
1409
1410                         for (i = strlen(mainstring) - 1; i >= 0; i--)
1411                                 if (mainstring[i] == '.')
1412                                         mainstring[i] = '/';
1413                 }
1414         }
1415
1416 #if defined(ENABLE_JVMTI)
1417         if (jvmti) {
1418                 jvmti_set_phase(JVMTI_PHASE_ONLOAD);
1419                 jvmti_agentload(agentarg, agentbypath, &handle, &libname);
1420
1421                 if (jdwp)
1422                         MFREE(agentarg, char, strlen(agentarg));
1423
1424                 jvmti_set_phase(JVMTI_PHASE_PRIMORDIAL);
1425         }
1426 #endif
1427
1428         /* Now re-set some of the properties that may have changed. This
1429            must be done after _all_ environment variables have been
1430            processes (e.g. -jar handling). */
1431
1432         if (!properties_postinit())
1433                 vm_abort("properties_postinit failed");
1434
1435         /* Now we have all options handled and we can print the version
1436            information. */
1437
1438         if (opt_version)
1439                 version(opt_exit);
1440
1441         /* initialize this JVM ****************************************************/
1442
1443         vm_initializing = true;
1444
1445 #if defined(ENABLE_THREADS)
1446         /* pre-initialize some core thread stuff, like the stopworldlock,
1447            thus this has to happen _before_ gc_init()!!! */
1448
1449         threads_preinit();
1450 #endif
1451
1452         /* initialize the garbage collector */
1453
1454         gc_init(opt_heapmaxsize, opt_heapstartsize);
1455
1456 #if defined(ENABLE_INTRP)
1457         /* Allocate main thread stack on the Java heap. */
1458
1459         if (opt_intrp) {
1460                 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1461                 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1462         }
1463 #endif
1464
1465         /* initialize the string hashtable stuff: lock (must be done
1466            _after_ threads_preinit) */
1467
1468         if (!string_init())
1469                 throw_main_exception_exit();
1470
1471         /* initialize the utf8 hashtable stuff: lock, often used utf8
1472            strings (must be done _after_ threads_preinit) */
1473
1474         if (!utf8_init())
1475                 throw_main_exception_exit();
1476
1477         /* initialize the classcache hashtable stuff: lock, hashtable
1478            (must be done _after_ threads_preinit) */
1479
1480         if (!classcache_init())
1481                 throw_main_exception_exit();
1482
1483         /* initialize the loader with bootclasspath (must be done _after_
1484            thread_preinit) */
1485
1486         if (!suck_init())
1487                 throw_main_exception_exit();
1488
1489         suck_add_from_property("java.endorsed.dirs");
1490         suck_add(_Jv_bootclasspath);
1491
1492         /* initialize the memory subsystem (must be done _after_
1493            threads_preinit) */
1494
1495         if (!memory_init())
1496                 throw_main_exception_exit();
1497
1498         /* initialize the finalizer stuff (must be done _after_
1499            threads_preinit) */
1500
1501         if (!finalizer_init())
1502                 throw_main_exception_exit();
1503
1504         /* install architecture dependent signal handlers */
1505
1506         signal_init();
1507
1508         /* initialize the codegen subsystems */
1509
1510         codegen_init();
1511
1512         /* initializes jit compiler */
1513
1514         jit_init();
1515
1516         /* machine dependent initialization */
1517
1518 #if defined(ENABLE_JIT)
1519 # if defined(ENABLE_INTRP)
1520         if (opt_intrp)
1521                 intrp_md_init();
1522         else
1523 # endif
1524                 md_init();
1525 #else
1526         intrp_md_init();
1527 #endif
1528
1529         /* initialize the loader subsystems (must be done _after_
1530        classcache_init) */
1531
1532         if (!loader_init())
1533                 vm_abort("loader_init failed");
1534
1535         if (!linker_init())
1536                 vm_abort("linker_init failed");
1537
1538         if (!native_init())
1539                 throw_main_exception_exit();
1540
1541         if (!exceptions_init())
1542                 throw_main_exception_exit();
1543
1544         if (!builtin_init())
1545                 throw_main_exception_exit();
1546
1547 #if defined(ENABLE_JAVASE)
1548         /* Initialize the JNI subsystem (must be done _before_
1549            threads_init, as threads_init can call JNI methods
1550            (e.g. NewGlobalRef). */
1551
1552         if (!jni_init())
1553                 throw_main_exception_exit();
1554 #endif
1555
1556 #if defined(ENABLE_THREADS)
1557         if (!threads_init())
1558                 throw_main_exception_exit();
1559 #endif
1560
1561 #if defined(ENABLE_PROFILING)
1562         /* initialize profiling */
1563
1564         if (!profile_init())
1565                 throw_main_exception_exit();
1566 #endif
1567
1568 #if defined(ENABLE_THREADS)
1569         /* initialize recompilation */
1570
1571         if (!recompile_init())
1572                 throw_main_exception_exit();
1573
1574         /* start the signal handler thread */
1575
1576 /*      if (!signal_start_thread()) */
1577 /*              throw_main_exception_exit(); */
1578
1579         /* finally, start the finalizer thread */
1580
1581         if (!finalizer_start_thread())
1582                 throw_main_exception_exit();
1583
1584         /* start the recompilation thread (must be done before the
1585            profiling thread) */
1586
1587         if (!recompile_start_thread())
1588                 throw_main_exception_exit();
1589
1590 # if defined(ENABLE_PROFILING)
1591         /* start the profile sampling thread */
1592
1593 /*      if (opt_prof) */
1594 /*              if (!profile_start_thread()) */
1595 /*                      throw_main_exception_exit(); */
1596 # endif
1597 #endif
1598
1599 #if defined(ENABLE_JVMTI)
1600         if (jvmti) {
1601                 /* add agent library to native library hashtable */
1602                 native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
1603         }
1604 #endif
1605
1606         /* increment the number of VMs */
1607
1608         vms++;
1609
1610         /* initialization is done */
1611
1612         vm_initializing = false;
1613
1614         /* everything's ok */
1615
1616         return true;
1617 }
1618
1619
1620 /* vm_run **********************************************************************
1621
1622    Runs the main-method of the passed class.
1623
1624 *******************************************************************************/
1625
1626 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1627 {
1628         utf               *mainutf;
1629         classinfo         *mainclass;
1630         methodinfo        *m;
1631         java_objectarray  *oa; 
1632         s4                 oalength;
1633         utf               *u;
1634         java_objectheader *s;
1635         s4                 status;
1636         s4                 i;
1637
1638 #if !defined(NDEBUG)
1639         if (compileall) {
1640                 vm_compile_all();
1641                 return;
1642         }
1643
1644         if (opt_method != NULL) {
1645                 vm_compile_method();
1646                 return;
1647         }
1648 #endif /* !defined(NDEBUG) */
1649
1650         /* should we run the main-method? */
1651
1652         if (mainstring == NULL)
1653                 usage();
1654
1655         /* set return value to OK */
1656
1657         status = 0;
1658
1659         if (opt_jar == true)
1660                 /* open jar file with java.util.jar.JarFile */
1661                 mainstring = vm_get_mainclass_from_jar(mainstring);
1662
1663         /* load the main class */
1664
1665         mainutf = utf_new_char(mainstring);
1666
1667 #if defined(ENABLE_JAVAME_CLDC1_1)
1668         mainclass = load_class_bootstrap(mainutf);
1669 #else
1670         mainclass = load_class_from_sysloader(mainutf);
1671 #endif
1672
1673         /* error loading class */
1674
1675         if ((exceptions_get_exception() != NULL) || (mainclass == NULL))
1676                 throw_main_exception_exit();
1677
1678         if (!link_class(mainclass))
1679                 throw_main_exception_exit();
1680                         
1681         /* find the `main' method of the main class */
1682
1683         m = class_resolveclassmethod(mainclass,
1684                                                                  utf_new_char("main"), 
1685                                                                  utf_new_char("([Ljava/lang/String;)V"),
1686                                                                  class_java_lang_Object,
1687                                                                  false);
1688
1689         if (*exceptionptr) {
1690                 throw_main_exception_exit();
1691         }
1692
1693         /* there is no main method or it isn't static */
1694
1695         if ((m == NULL) || !(m->flags & ACC_STATIC)) {
1696                 exceptions_clear_exception();
1697                 exceptions_throw_nosuchmethoderror(mainclass,
1698                                                                                    utf_new_char("main"), 
1699                                                                                    utf_new_char("([Ljava/lang/String;)V"));
1700
1701                 throw_main_exception_exit();
1702         }
1703
1704         /* build argument array */
1705
1706         oalength = vm_args->nOptions - opt_index;
1707
1708         oa = builtin_anewarray(oalength, class_java_lang_String);
1709
1710         for (i = 0; i < oalength; i++) {
1711                 u = utf_new_char(vm_args->options[opt_index + i].optionString);
1712                 s = javastring_new(u);
1713
1714                 oa->data[i] = s;
1715         }
1716
1717 #ifdef TYPEINFO_DEBUG_TEST
1718         /* test the typeinfo system */
1719         typeinfo_test();
1720 #endif
1721         /*class_showmethods(currentThread->group->header.vftbl->class); */
1722
1723 #if defined(ENABLE_JVMTI)
1724         jvmti_set_phase(JVMTI_PHASE_LIVE);
1725 #endif
1726
1727         /* set ThreadMXBean variables */
1728
1729         _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
1730         _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
1731
1732         if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
1733                 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
1734                 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
1735                         _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
1736
1737         /* start the main thread */
1738
1739         (void) vm_call_method(m, NULL, oa);
1740
1741         /* exception occurred? */
1742
1743         if (*exceptionptr) {
1744                 throw_main_exception();
1745                 status = 1;
1746         }
1747
1748         /* unload the JavaVM */
1749
1750         vm_destroy(vm);
1751
1752         /* and exit */
1753
1754         vm_exit(status);
1755 }
1756
1757
1758 /* vm_destroy ******************************************************************
1759
1760    Unloads a Java VM and reclaims its resources.
1761
1762 *******************************************************************************/
1763
1764 s4 vm_destroy(JavaVM *vm)
1765 {
1766 #if defined(ENABLE_THREADS)
1767         threads_join_all_threads();
1768 #endif
1769
1770         /* everything's ok */
1771
1772         return 0;
1773 }
1774
1775
1776 /* vm_exit *********************************************************************
1777
1778    Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1779
1780 *******************************************************************************/
1781
1782 void vm_exit(s4 status)
1783 {
1784         methodinfo *m;
1785
1786         /* signal that we are exiting */
1787
1788         vm_exiting = true;
1789
1790         assert(class_java_lang_System);
1791         assert(class_java_lang_System->state & CLASS_LOADED);
1792
1793 #if defined(ENABLE_JVMTI)
1794         if (jvmti || (dbgcom!=NULL)) {
1795                 jvmti_set_phase(JVMTI_PHASE_DEAD);
1796                 if (jvmti) jvmti_agentunload();
1797         }
1798 #endif
1799
1800         if (!link_class(class_java_lang_System))
1801                 throw_main_exception_exit();
1802
1803         /* call java.lang.System.exit(I)V */
1804
1805         m = class_resolveclassmethod(class_java_lang_System,
1806                                                                  utf_new_char("exit"),
1807                                                                  utf_int__void,
1808                                                                  class_java_lang_Object,
1809                                                                  true);
1810         
1811         if (m == NULL)
1812                 throw_main_exception_exit();
1813
1814         /* call the exit function with passed exit status */
1815
1816         (void) vm_call_method(m, NULL, status);
1817
1818         /* If we had an exception, just ignore the exception and exit with
1819            the proper code. */
1820
1821         vm_shutdown(status);
1822 }
1823
1824
1825 /* vm_shutdown *****************************************************************
1826
1827    Terminates the system immediately without freeing memory explicitly
1828    (to be used only for abnormal termination).
1829         
1830 *******************************************************************************/
1831
1832 void vm_shutdown(s4 status)
1833 {
1834         if (opt_verbose 
1835 #if defined(ENABLE_STATISTICS)
1836                 || opt_getcompilingtime || opt_stat
1837 #endif
1838            ) 
1839         {
1840                 log_text("CACAO terminated by shutdown");
1841                 dolog("Exit status: %d\n", (s4) status);
1842
1843         }
1844
1845 #if defined(ENABLE_JVMTI)
1846         /* terminate cacaodbgserver */
1847         if (dbgcom!=NULL) {
1848                 pthread_mutex_lock(&dbgcomlock);
1849                 dbgcom->running=1;
1850                 pthread_mutex_unlock(&dbgcomlock);
1851                 jvmti_cacaodbgserver_quit();
1852         }       
1853 #endif
1854
1855         exit(status);
1856 }
1857
1858
1859 /* vm_exit_handler *************************************************************
1860
1861    The exit_handler function is called upon program termination.
1862
1863    ATTENTION: Don't free system resources here! Some threads may still
1864    be running as this is called from VMRuntime.exit(). The OS does the
1865    cleanup for us.
1866
1867 *******************************************************************************/
1868
1869 void vm_exit_handler(void)
1870 {
1871 #if !defined(NDEBUG)
1872         if (showmethods)
1873                 class_showmethods(mainclass);
1874
1875         if (showconstantpool)
1876                 class_showconstantpool(mainclass);
1877
1878         if (showutf)
1879                 utf_show();
1880
1881 # if defined(ENABLE_PROFILING)
1882         if (opt_prof)
1883                 profile_printstats();
1884 # endif
1885 #endif /* !defined(NDEBUG) */
1886
1887 #if defined(ENABLE_RT_TIMING)
1888         rt_timing_print_time_stats(stderr);
1889 #endif
1890
1891 #if defined(ENABLE_CYCLES_STATS)
1892         builtin_print_cycles_stats(stderr);
1893         stacktrace_print_cycles_stats(stderr);
1894 #endif
1895
1896         if (opt_verbose 
1897 #if defined(ENABLE_STATISTICS)
1898                 || opt_getcompilingtime || opt_stat
1899 #endif
1900            ) 
1901         {
1902                 log_text("CACAO terminated");
1903
1904 #if defined(ENABLE_STATISTICS)
1905                 if (opt_stat) {
1906                         print_stats();
1907 #ifdef TYPECHECK_STATISTICS
1908                         typecheck_print_statistics(get_logfile());
1909 #endif
1910                 }
1911
1912                 if (opt_getcompilingtime)
1913                         print_times();
1914 #endif /* defined(ENABLE_STATISTICS) */
1915         }
1916         /* vm_print_profile(stderr);*/
1917 }
1918
1919
1920 /* vm_abort ********************************************************************
1921
1922    Prints an error message and aborts the VM.
1923
1924 *******************************************************************************/
1925
1926 void vm_abort(const char *text, ...)
1927 {
1928         va_list ap;
1929
1930         /* print the log message */
1931
1932         log_start();
1933
1934         va_start(ap, text);
1935         log_vprint(text, ap);
1936         va_end(ap);
1937
1938         log_finish();
1939
1940         /* now abort the VM */
1941
1942         abort();
1943 }
1944
1945
1946 /* vm_get_mainclass_from_jar ***************************************************
1947
1948    Gets the name of the main class from a JAR's manifest file.
1949
1950 *******************************************************************************/
1951
1952 static char *vm_get_mainclass_from_jar(char *mainstring)
1953 {
1954         classinfo         *c;
1955         java_objectheader *o;
1956         methodinfo        *m;
1957         java_lang_String  *s;
1958
1959         c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
1960
1961         if (c == NULL)
1962                 throw_main_exception_exit();
1963         
1964         /* create JarFile object */
1965
1966         o = builtin_new(c);
1967
1968         if (o == NULL)
1969                 throw_main_exception_exit();
1970
1971
1972         m = class_resolveclassmethod(c,
1973                                                                  utf_init, 
1974                                                                  utf_java_lang_String__void,
1975                                                                  class_java_lang_Object,
1976                                                                  true);
1977
1978         if (m == NULL)
1979                 throw_main_exception_exit();
1980
1981         s = javastring_new_from_ascii(mainstring);
1982
1983         (void) vm_call_method(m, o, s);
1984
1985         if (*exceptionptr)
1986                 throw_main_exception_exit();
1987
1988         /* get manifest object */
1989
1990         m = class_resolveclassmethod(c,
1991                                                                  utf_new_char("getManifest"), 
1992                                                                  utf_new_char("()Ljava/util/jar/Manifest;"),
1993                                                                  class_java_lang_Object,
1994                                                                  true);
1995
1996         if (m == NULL)
1997                 throw_main_exception_exit();
1998
1999         o = vm_call_method(m, o);
2000
2001         if (o == NULL) {
2002                 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
2003                 vm_exit(1);
2004         }
2005
2006
2007         /* get Main Attributes */
2008
2009         m = class_resolveclassmethod(o->vftbl->class,
2010                                                                  utf_new_char("getMainAttributes"), 
2011                                                                  utf_new_char("()Ljava/util/jar/Attributes;"),
2012                                                                  class_java_lang_Object,
2013                                                                  true);
2014
2015         if (m == NULL)
2016                 throw_main_exception_exit();
2017
2018         o = vm_call_method(m, o);
2019
2020         if (o == NULL) {
2021                 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
2022                 vm_exit(1);
2023         }
2024
2025
2026         /* get property Main-Class */
2027
2028         m = class_resolveclassmethod(o->vftbl->class,
2029                                                                  utf_new_char("getValue"), 
2030                                                                  utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
2031                                                                  class_java_lang_Object,
2032                                                                  true);
2033
2034         if (m == NULL)
2035                 throw_main_exception_exit();
2036
2037         s = javastring_new_from_ascii("Main-Class");
2038
2039         o = vm_call_method(m, o, s);
2040
2041         if (o == NULL)
2042                 throw_main_exception_exit();
2043
2044         return javastring_tochar(o);
2045 }
2046
2047
2048 /* vm_compile_all **************************************************************
2049
2050    Compile all methods found in the bootclasspath.
2051
2052 *******************************************************************************/
2053
2054 #if !defined(NDEBUG)
2055 static void vm_compile_all(void)
2056 {
2057         classinfo              *c;
2058         methodinfo             *m;
2059         u4                      slot;
2060         classcache_name_entry  *nmen;
2061         classcache_class_entry *clsen;
2062         s4                      i;
2063
2064         /* create all classes found in the bootclasspath */
2065         /* XXX currently only works with zip/jar's */
2066
2067         loader_load_all_classes();
2068
2069         /* link all classes */
2070
2071         for (slot = 0; slot < hashtable_classcache.size; slot++) {
2072                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
2073
2074                 for (; nmen; nmen = nmen->hashlink) {
2075                         /* iterate over all class entries */
2076
2077                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
2078                                 c = clsen->classobj;
2079
2080                                 if (c == NULL)
2081                                         continue;
2082
2083                                 if (!(c->state & CLASS_LINKED)) {
2084                                         if (!link_class(c)) {
2085                                                 fprintf(stderr, "Error linking: ");
2086                                                 utf_fprint_printable_ascii_classname(stderr, c->name);
2087                                                 fprintf(stderr, "\n");
2088
2089                                                 /* print out exception and cause */
2090
2091                                                 exceptions_print_current_exception();
2092
2093                                                 /* goto next class */
2094
2095                                                 continue;
2096                                         }
2097                                 }
2098
2099                                 /* compile all class methods */
2100
2101                                 for (i = 0; i < c->methodscount; i++) {
2102                                         m = &(c->methods[i]);
2103
2104                                         if (m->jcode != NULL) {
2105                                                 if (!jit_compile(m)) {
2106                                                         fprintf(stderr, "Error compiling: ");
2107                                                         utf_fprint_printable_ascii_classname(stderr, c->name);
2108                                                         fprintf(stderr, ".");
2109                                                         utf_fprint_printable_ascii(stderr, m->name);
2110                                                         utf_fprint_printable_ascii(stderr, m->descriptor);
2111                                                         fprintf(stderr, "\n");
2112
2113                                                         /* print out exception and cause */
2114
2115                                                         exceptions_print_current_exception();
2116                                                 }
2117                                         }
2118                                 }
2119                         }
2120                 }
2121         }
2122 }
2123 #endif /* !defined(NDEBUG) */
2124
2125
2126 /* vm_compile_method ***********************************************************
2127
2128    Compile a specific method.
2129
2130 *******************************************************************************/
2131
2132 #if !defined(NDEBUG)
2133 static void vm_compile_method(void)
2134 {
2135         methodinfo *m;
2136
2137         /* create, load and link the main class */
2138
2139         if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
2140                 throw_main_exception_exit();
2141
2142         if (!link_class(mainclass))
2143                 throw_main_exception_exit();
2144
2145         if (opt_signature != NULL) {
2146                 m = class_resolveclassmethod(mainclass,
2147                                                                          utf_new_char(opt_method),
2148                                                                          utf_new_char(opt_signature),
2149                                                                          mainclass,
2150                                                                          false);
2151         }
2152         else {
2153                 m = class_resolveclassmethod(mainclass,
2154                                                                          utf_new_char(opt_method),
2155                                                                          NULL,
2156                                                                          mainclass,
2157                                                                          false);
2158         }
2159
2160         if (m == NULL)
2161                 vm_abort("vm_compile_method: java.lang.NoSuchMethodException: %s.%s",
2162                                  opt_method, opt_signature ? opt_signature : "");
2163                 
2164         jit_compile(m);
2165 }
2166 #endif /* !defined(NDEBUG) */
2167
2168
2169 /* vm_vmargs_from_valist *******************************************************
2170
2171    XXX
2172
2173 *******************************************************************************/
2174
2175 static void vm_vmargs_from_valist(methodinfo *m, java_objectheader *o,
2176                                                                   vm_arg *vmargs, va_list ap)
2177 {
2178         typedesc *paramtypes;
2179         s4        i;
2180
2181         paramtypes = m->parseddesc->paramtypes;
2182
2183         /* if method is non-static fill first block and skip `this' pointer */
2184
2185         i = 0;
2186
2187         if (o != NULL) {
2188                 /* the `this' pointer */
2189                 vmargs[0].type   = TYPE_ADR;
2190                 vmargs[0].data.l = (u8) (ptrint) o;
2191
2192                 paramtypes++;
2193                 i++;
2194         } 
2195
2196         for (; i < m->parseddesc->paramcount; i++, paramtypes++) {
2197                 switch (paramtypes->decltype) {
2198                 /* primitive types */
2199                 case PRIMITIVETYPE_BOOLEAN: 
2200                 case PRIMITIVETYPE_BYTE:
2201                 case PRIMITIVETYPE_CHAR:
2202                 case PRIMITIVETYPE_SHORT: 
2203                 case PRIMITIVETYPE_INT:
2204                         vmargs[i].type   = TYPE_INT;
2205                         vmargs[i].data.l = (s8) va_arg(ap, s4);
2206                         break;
2207
2208                 case PRIMITIVETYPE_LONG:
2209                         vmargs[i].type   = TYPE_LNG;
2210                         vmargs[i].data.l = (s8) va_arg(ap, s8);
2211                         break;
2212
2213                 case PRIMITIVETYPE_FLOAT:
2214                         vmargs[i].type   = TYPE_FLT;
2215 #if defined(__ALPHA__)
2216                         /* this keeps the assembler function much simpler */
2217
2218                         vmargs[i].data.d = (jdouble) va_arg(ap, jdouble);
2219 #else
2220                         vmargs[i].data.f = (jfloat) va_arg(ap, jdouble);
2221 #endif
2222                         break;
2223
2224                 case PRIMITIVETYPE_DOUBLE:
2225                         vmargs[i].type   = TYPE_DBL;
2226                         vmargs[i].data.d = (jdouble) va_arg(ap, jdouble);
2227                         break;
2228
2229                 case TYPE_ADR: 
2230                         vmargs[i].type   = TYPE_ADR;
2231                         vmargs[i].data.l = (u8) (ptrint) va_arg(ap, void*);
2232                         break;
2233                 }
2234         }
2235 }
2236
2237
2238 /* vm_vmargs_from_jvalue *******************************************************
2239
2240    XXX
2241
2242 *******************************************************************************/
2243
2244 static void vm_vmargs_from_jvalue(methodinfo *m, java_objectheader *o,
2245                                                                   vm_arg *vmargs, jvalue *args)
2246 {
2247         typedesc *paramtypes;
2248         s4        i;
2249         s4        j;
2250
2251         paramtypes = m->parseddesc->paramtypes;
2252
2253         /* if method is non-static fill first block and skip `this' pointer */
2254
2255         i = 0;
2256
2257         if (o != NULL) {
2258                 /* the `this' pointer */
2259                 vmargs[0].type   = TYPE_ADR;
2260                 vmargs[0].data.l = (u8) (ptrint) o;
2261
2262                 paramtypes++;
2263                 i++;
2264         } 
2265
2266         for (j = 0; i < m->parseddesc->paramcount; i++, j++, paramtypes++) {
2267                 switch (paramtypes->decltype) {
2268                 /* primitive types */
2269                 case PRIMITIVETYPE_BOOLEAN: 
2270                 case PRIMITIVETYPE_BYTE:
2271                 case PRIMITIVETYPE_CHAR:
2272                 case PRIMITIVETYPE_SHORT: 
2273                 case PRIMITIVETYPE_INT:
2274                         vmargs[i].type   = TYPE_INT;
2275                         vmargs[i].data.l = (s8) args[j].i;
2276                         break;
2277
2278                 case PRIMITIVETYPE_LONG:
2279                         vmargs[i].type   = TYPE_LNG;
2280                         vmargs[i].data.l = (s8) args[j].j;
2281                         break;
2282
2283                 case PRIMITIVETYPE_FLOAT:
2284                         vmargs[i].type = TYPE_FLT;
2285 #if defined(__ALPHA__)
2286                         /* this keeps the assembler function much simpler */
2287
2288                         vmargs[i].data.d = (jdouble) args[j].f;
2289 #else
2290                         vmargs[i].data.f = args[j].f;
2291 #endif
2292                         break;
2293
2294                 case PRIMITIVETYPE_DOUBLE:
2295                         vmargs[i].type   = TYPE_DBL;
2296                         vmargs[i].data.d = args[j].d;
2297                         break;
2298
2299                 case TYPE_ADR: 
2300                         vmargs[i].type   = TYPE_ADR;
2301                         vmargs[i].data.l = (u8) (ptrint) args[j].l;
2302                         break;
2303                 }
2304         }
2305 }
2306
2307
2308 /* vm_call_method **************************************************************
2309
2310    Calls a Java method with a variable number of arguments and returns
2311    an address.
2312
2313 *******************************************************************************/
2314
2315 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
2316 {
2317         va_list            ap;
2318         java_objectheader *ro;
2319
2320         va_start(ap, o);
2321         ro = vm_call_method_valist(m, o, ap);
2322         va_end(ap);
2323
2324         return ro;
2325 }
2326
2327
2328 /* vm_call_method_valist *******************************************************
2329
2330    Calls a Java method with a variable number of arguments, passed via
2331    a va_list, and returns an address.
2332
2333 *******************************************************************************/
2334
2335 java_objectheader *vm_call_method_valist(methodinfo *m, java_objectheader *o,
2336                                                                                  va_list ap)
2337 {
2338         s4                 vmargscount;
2339         vm_arg            *vmargs;
2340         java_objectheader *ro;
2341         s4                 dumpsize;
2342
2343         /* mark start of dump memory area */
2344
2345         dumpsize = dump_size();
2346
2347         /* get number of Java method arguments */
2348
2349         vmargscount = m->parseddesc->paramcount;
2350
2351         /* allocate vm_arg array */
2352
2353         vmargs = DMNEW(vm_arg, vmargscount);
2354
2355         /* fill the vm_arg array from a va_list */
2356
2357         vm_vmargs_from_valist(m, o, vmargs, ap);
2358
2359         /* call the Java method */
2360
2361         ro = vm_call_method_vmarg(m, vmargscount, vmargs);
2362
2363         /* release dump area */
2364
2365         dump_release(dumpsize);
2366
2367         return ro;
2368 }
2369
2370
2371 /* vm_call_method_jvalue *******************************************************
2372
2373    Calls a Java method with a variable number of arguments, passed via
2374    a jvalue array, and returns an address.
2375
2376 *******************************************************************************/
2377
2378 java_objectheader *vm_call_method_jvalue(methodinfo *m, java_objectheader *o,
2379                                                                                  jvalue *args)
2380 {
2381         s4                 vmargscount;
2382         vm_arg            *vmargs;
2383         java_objectheader *ro;
2384         s4                 dumpsize;
2385
2386         /* mark start of dump memory area */
2387
2388         dumpsize = dump_size();
2389
2390         /* get number of Java method arguments */
2391
2392         vmargscount = m->parseddesc->paramcount;
2393
2394         /* allocate vm_arg array */
2395
2396         vmargs = DMNEW(vm_arg, vmargscount);
2397
2398         /* fill the vm_arg array from a va_list */
2399
2400         vm_vmargs_from_jvalue(m, o, vmargs, args);
2401
2402         /* call the Java method */
2403
2404         ro = vm_call_method_vmarg(m, vmargscount, vmargs);
2405
2406         /* release dump area */
2407
2408         dump_release(dumpsize);
2409
2410         return ro;
2411 }
2412
2413
2414 /* vm_call_method_vmarg ********************************************************
2415
2416    Calls a Java method with a variable number of arguments, passed via
2417    a vm_arg array, and returns an address.
2418
2419 *******************************************************************************/
2420
2421 java_objectheader *vm_call_method_vmarg(methodinfo *m, s4 vmargscount,
2422                                                                                 vm_arg *vmargs)
2423 {
2424         java_objectheader *o;
2425
2426 #if defined(ENABLE_JIT)
2427 # if defined(ENABLE_INTRP)
2428         if (opt_intrp)
2429                 o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
2430         else
2431 # endif
2432                 o = asm_vm_call_method(m, vmargscount, vmargs);
2433 #else
2434         o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
2435 #endif
2436
2437         return o;
2438 }
2439
2440
2441 /* vm_call_method_int **********************************************************
2442
2443    Calls a Java method with a variable number of arguments and returns
2444    an integer (s4).
2445
2446 *******************************************************************************/
2447
2448 s4 vm_call_method_int(methodinfo *m, java_objectheader *o, ...)
2449 {
2450         va_list ap;
2451         s4      i;
2452
2453         va_start(ap, o);
2454         i = vm_call_method_int_valist(m, o, ap);
2455         va_end(ap);
2456
2457         return i;
2458 }
2459
2460
2461 /* vm_call_method_int_valist ***************************************************
2462
2463    Calls a Java method with a variable number of arguments, passed via
2464    a va_list, and returns an integer (s4).
2465
2466 *******************************************************************************/
2467
2468 s4 vm_call_method_int_valist(methodinfo *m, java_objectheader *o, va_list ap)
2469 {
2470         s4      vmargscount;
2471         vm_arg *vmargs;
2472         s4      i;
2473         s4      dumpsize;
2474
2475         /* mark start of dump memory area */
2476
2477         dumpsize = dump_size();
2478
2479         /* get number of Java method arguments */
2480
2481         vmargscount = m->parseddesc->paramcount;
2482
2483         /* allocate vm_arg array */
2484
2485         vmargs = DMNEW(vm_arg, vmargscount);
2486
2487         /* fill the vm_arg array from a va_list */
2488
2489         vm_vmargs_from_valist(m, o, vmargs, ap);
2490
2491         /* call the Java method */
2492
2493         i = vm_call_method_int_vmarg(m, vmargscount, vmargs);
2494
2495         /* release dump area */
2496
2497         dump_release(dumpsize);
2498
2499         return i;
2500 }
2501
2502
2503 /* vm_call_method_int_jvalue ***************************************************
2504
2505    Calls a Java method with a variable number of arguments, passed via
2506    a jvalue array, and returns an integer (s4).
2507
2508 *******************************************************************************/
2509
2510 s4 vm_call_method_int_jvalue(methodinfo *m, java_objectheader *o, jvalue *args)
2511 {
2512         s4      vmargscount;
2513         vm_arg *vmargs;
2514         s4      i;
2515         s4      dumpsize;
2516
2517         /* mark start of dump memory area */
2518
2519         dumpsize = dump_size();
2520
2521         /* get number of Java method arguments */
2522
2523         vmargscount = m->parseddesc->paramcount;
2524
2525         /* allocate vm_arg array */
2526
2527         vmargs = DMNEW(vm_arg, vmargscount);
2528
2529         /* fill the vm_arg array from a va_list */
2530
2531         vm_vmargs_from_jvalue(m, o, vmargs, args);
2532
2533         /* call the Java method */
2534
2535         i = vm_call_method_int_vmarg(m, vmargscount, vmargs);
2536
2537         /* release dump area */
2538
2539         dump_release(dumpsize);
2540
2541         return i;
2542 }
2543
2544
2545 /* vm_call_method_int_vmarg ****************************************************
2546
2547    Calls a Java method with a variable number of arguments, passed via
2548    a vm_arg array, and returns an integer (s4).
2549
2550 *******************************************************************************/
2551
2552 s4 vm_call_method_int_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
2553 {
2554         s4 i;
2555
2556 #if defined(ENABLE_JIT)
2557 # if defined(ENABLE_INTRP)
2558         if (opt_intrp)
2559                 i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
2560         else
2561 # endif
2562                 i = asm_vm_call_method_int(m, vmargscount, vmargs);
2563 #else
2564         i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
2565 #endif
2566
2567         return i;
2568 }
2569
2570
2571 /* vm_call_method_long *********************************************************
2572
2573    Calls a Java method with a variable number of arguments and returns
2574    a long (s8).
2575
2576 *******************************************************************************/
2577
2578 s8 vm_call_method_long(methodinfo *m, java_objectheader *o, ...)
2579 {
2580         va_list ap;
2581         s8      l;
2582
2583         va_start(ap, o);
2584         l = vm_call_method_long_valist(m, o, ap);
2585         va_end(ap);
2586
2587         return l;
2588 }
2589
2590
2591 /* vm_call_method_long_valist **************************************************
2592
2593    Calls a Java method with a variable number of arguments, passed via
2594    a va_list, and returns a long (s8).
2595
2596 *******************************************************************************/
2597
2598 s8 vm_call_method_long_valist(methodinfo *m, java_objectheader *o, va_list ap)
2599 {
2600         s4      vmargscount;
2601         vm_arg *vmargs;
2602         s8      l;
2603         s4      dumpsize;
2604
2605         /* mark start of dump memory area */
2606
2607         dumpsize = dump_size();
2608
2609         /* get number of Java method arguments */
2610
2611         vmargscount = m->parseddesc->paramcount;
2612
2613         /* allocate vm_arg array */
2614
2615         vmargs = DMNEW(vm_arg, vmargscount);
2616
2617         /* fill the vm_arg array from a va_list */
2618
2619         vm_vmargs_from_valist(m, o, vmargs, ap);
2620
2621         /* call the Java method */
2622
2623         l = vm_call_method_long_vmarg(m, vmargscount, vmargs);
2624
2625         /* release dump area */
2626
2627         dump_release(dumpsize);
2628
2629         return l;
2630 }
2631
2632
2633 /* vm_call_method_long_jvalue **************************************************
2634
2635    Calls a Java method with a variable number of arguments, passed via
2636    a jvalue array, and returns a long (s8).
2637
2638 *******************************************************************************/
2639
2640 s8 vm_call_method_long_jvalue(methodinfo *m, java_objectheader *o, jvalue *args)
2641 {
2642         s4      vmargscount;
2643         vm_arg *vmargs;
2644         s8      l;
2645         s4      dumpsize;
2646
2647         /* mark start of dump memory area */
2648
2649         dumpsize = dump_size();
2650
2651         /* get number of Java method arguments */
2652
2653         vmargscount = m->parseddesc->paramcount;
2654
2655         /* allocate vm_arg array */
2656
2657         vmargs = DMNEW(vm_arg, vmargscount);
2658
2659         /* fill the vm_arg array from a va_list */
2660
2661         vm_vmargs_from_jvalue(m, o, vmargs, args);
2662
2663         /* call the Java method */
2664
2665         l = vm_call_method_long_vmarg(m, vmargscount, vmargs);
2666
2667         /* release dump area */
2668
2669         dump_release(dumpsize);
2670
2671         return l;
2672 }
2673
2674
2675 /* vm_call_method_long_vmarg ***************************************************
2676
2677    Calls a Java method with a variable number of arguments, passed via
2678    a vm_arg array, and returns a long (s8).
2679
2680 *******************************************************************************/
2681
2682 s8 vm_call_method_long_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
2683 {
2684         s8 l;
2685
2686 #if defined(ENABLE_JIT)
2687 # if defined(ENABLE_INTRP)
2688         if (opt_intrp)
2689                 l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
2690         else
2691 # endif
2692                 l = asm_vm_call_method_long(m, vmargscount, vmargs);
2693 #else
2694         l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
2695 #endif
2696
2697         return l;
2698 }
2699
2700
2701 /* vm_call_method_float ********************************************************
2702
2703    Calls a Java method with a variable number of arguments and returns
2704    an float.
2705
2706 *******************************************************************************/
2707
2708 float vm_call_method_float(methodinfo *m, java_objectheader *o, ...)
2709 {
2710         va_list ap;
2711         float   f;
2712
2713         va_start(ap, o);
2714         f = vm_call_method_float_valist(m, o, ap);
2715         va_end(ap);
2716
2717         return f;
2718 }
2719
2720
2721 /* vm_call_method_float_valist *************************************************
2722
2723    Calls a Java method with a variable number of arguments, passed via
2724    a va_list, and returns a float.
2725
2726 *******************************************************************************/
2727
2728 float vm_call_method_float_valist(methodinfo *m, java_objectheader *o,
2729                                                                   va_list ap)
2730 {
2731         s4      vmargscount;
2732         vm_arg *vmargs;
2733         float   f;
2734         s4      dumpsize;
2735
2736         /* mark start of dump memory area */
2737
2738         dumpsize = dump_size();
2739
2740         /* get number of Java method arguments */
2741
2742         vmargscount = m->parseddesc->paramcount;
2743
2744         /* allocate vm_arg array */
2745
2746         vmargs = DMNEW(vm_arg, vmargscount);
2747
2748         /* fill the vm_arg array from a va_list */
2749
2750         vm_vmargs_from_valist(m, o, vmargs, ap);
2751
2752         /* call the Java method */
2753
2754         f = vm_call_method_float_vmarg(m, vmargscount, vmargs);
2755
2756         /* release dump area */
2757
2758         dump_release(dumpsize);
2759
2760         return f;
2761 }
2762
2763
2764 /* vm_call_method_float_jvalue *************************************************
2765
2766    Calls a Java method with a variable number of arguments, passed via
2767    a jvalue array, and returns a float.
2768
2769 *******************************************************************************/
2770
2771 float vm_call_method_float_jvalue(methodinfo *m, java_objectheader *o,
2772                                                                   jvalue *args)
2773 {
2774         s4      vmargscount;
2775         vm_arg *vmargs;
2776         float   f;
2777         s4      dumpsize;
2778
2779         /* mark start of dump memory area */
2780
2781         dumpsize = dump_size();
2782
2783         /* get number of Java method arguments */
2784
2785         vmargscount = m->parseddesc->paramcount;
2786
2787         /* allocate vm_arg array */
2788
2789         vmargs = DMNEW(vm_arg, vmargscount);
2790
2791         /* fill the vm_arg array from a va_list */
2792
2793         vm_vmargs_from_jvalue(m, o, vmargs, args);
2794
2795         /* call the Java method */
2796
2797         f = vm_call_method_float_vmarg(m, vmargscount, vmargs);
2798
2799         /* release dump area */
2800
2801         dump_release(dumpsize);
2802
2803         return f;
2804 }
2805
2806
2807 /* vm_call_method_float_vmarg **************************************************
2808
2809    Calls a Java method with a variable number of arguments and returns
2810    an float.
2811
2812 *******************************************************************************/
2813
2814 float vm_call_method_float_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
2815 {
2816         float f;
2817
2818 #if defined(ENABLE_JIT)
2819 # if defined(ENABLE_INTRP)
2820         if (opt_intrp)
2821                 f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
2822         else
2823 # endif
2824                 f = asm_vm_call_method_float(m, vmargscount, vmargs);
2825 #else
2826         f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
2827 #endif
2828
2829         return f;
2830 }
2831
2832
2833 /* vm_call_method_double *******************************************************
2834
2835    Calls a Java method with a variable number of arguments and returns
2836    a double.
2837
2838 *******************************************************************************/
2839
2840 double vm_call_method_double(methodinfo *m, java_objectheader *o, ...)
2841 {
2842         va_list ap;
2843         double  d;
2844
2845         va_start(ap, o);
2846         d = vm_call_method_double_valist(m, o, ap);
2847         va_end(ap);
2848
2849         return d;
2850 }
2851
2852
2853 /* vm_call_method_double_valist ************************************************
2854
2855    Calls a Java method with a variable number of arguments, passed via
2856    a va_list, and returns a double.
2857
2858 *******************************************************************************/
2859
2860 double vm_call_method_double_valist(methodinfo *m, java_objectheader *o,
2861                                                                         va_list ap)
2862 {
2863         s4      vmargscount;
2864         vm_arg *vmargs;
2865         double  d;
2866         s4      dumpsize;
2867
2868         /* mark start of dump memory area */
2869
2870         dumpsize = dump_size();
2871
2872         /* get number of Java method arguments */
2873
2874         vmargscount = m->parseddesc->paramcount;
2875
2876         /* allocate vm_arg array */
2877
2878         vmargs = DMNEW(vm_arg, vmargscount);
2879
2880         /* fill the vm_arg array from a va_list */
2881
2882         vm_vmargs_from_valist(m, o, vmargs, ap);
2883
2884         /* call the Java method */
2885
2886         d = vm_call_method_double_vmarg(m, vmargscount, vmargs);
2887
2888         /* release dump area */
2889
2890         dump_release(dumpsize);
2891
2892         return d;
2893 }
2894
2895
2896 /* vm_call_method_double_jvalue ************************************************
2897
2898    Calls a Java method with a variable number of arguments, passed via
2899    a jvalue array, and returns a double.
2900
2901 *******************************************************************************/
2902
2903 double vm_call_method_double_jvalue(methodinfo *m, java_objectheader *o,
2904                                                                         jvalue *args)
2905 {
2906         s4      vmargscount;
2907         vm_arg *vmargs;
2908         double  d;
2909         s4      dumpsize;
2910
2911         /* mark start of dump memory area */
2912
2913         dumpsize = dump_size();
2914
2915         /* get number of Java method arguments */
2916
2917         vmargscount = m->parseddesc->paramcount;
2918
2919         /* allocate vm_arg array */
2920
2921         vmargs = DMNEW(vm_arg, vmargscount);
2922
2923         /* fill the vm_arg array from a va_list */
2924
2925         vm_vmargs_from_jvalue(m, o, vmargs, args);
2926
2927         /* call the Java method */
2928
2929         d = vm_call_method_double_vmarg(m, vmargscount, vmargs);
2930
2931         /* release dump area */
2932
2933         dump_release(dumpsize);
2934
2935         return d;
2936 }
2937
2938
2939 /* vm_call_method_double_vmarg *************************************************
2940
2941    Calls a Java method with a variable number of arguments and returns
2942    a double.
2943
2944 *******************************************************************************/
2945
2946 double vm_call_method_double_vmarg(methodinfo *m, s4 vmargscount,
2947                                                                    vm_arg *vmargs)
2948 {
2949         double d;
2950
2951 #if defined(ENABLE_JIT)
2952 # if defined(ENABLE_INTRP)
2953         if (opt_intrp)
2954                 d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
2955         else
2956 # endif
2957                 d = asm_vm_call_method_double(m, vmargscount, vmargs);
2958 #else
2959         d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
2960 #endif
2961
2962         return d;
2963 }
2964
2965
2966 /*
2967  * These are local overrides for various environment variables in Emacs.
2968  * Please do not remove this and leave it at the end of the file, where
2969  * Emacs will automagically detect them.
2970  * ---------------------------------------------------------------------
2971  * Local variables:
2972  * mode: c
2973  * indent-tabs-mode: t
2974  * c-basic-offset: 4
2975  * tab-width: 4
2976  * End:
2977  */