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