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