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