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