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