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