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