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