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