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