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