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