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