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