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