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