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