* src/vm/array.hpp (ArrayTemplate::get_raw_data_ptr): Added as pure virtual.
[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.hpp"
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.hpp"
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.hpp"
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         OPT_XCHECK_JNI,
193
194         /* CACAO options */
195
196         OPT_VERBOSE1,
197
198 #if defined(ENABLE_STATISTICS)
199         OPT_TIME,
200         OPT_STAT,
201 #endif
202
203         OPT_LOG,
204         OPT_CHECK,
205         OPT_LOAD,
206         OPT_SHOW,
207         OPT_DEBUGCOLOR,
208
209 #if !defined(NDEBUG)
210         OPT_ALL,
211         OPT_METHOD,
212         OPT_SIGNATURE,
213 #endif
214
215 #if defined(ENABLE_VERIFIER)
216         OPT_NOVERIFY,
217         OPT_XVERIFY_ALL,
218         OPT_XVERIFY_NONE,
219 #if defined(TYPECHECK_VERBOSE)
220         OPT_VERBOSETC,
221 #endif
222 #endif /* defined(ENABLE_VERIFIER) */
223
224         /* optimization options */
225
226 #if defined(ENABLE_LOOP)
227         OPT_OLOOP,
228 #endif
229         
230 #if defined(ENABLE_IFCONV)
231         OPT_IFCONV,
232 #endif
233
234 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
235         OPT_LSRA,
236 #endif
237
238 #if defined(ENABLE_INTRP)
239         /* interpreter options */
240
241         OPT_NO_DYNAMIC,
242         OPT_NO_REPLICATION,
243         OPT_NO_QUICKSUPER,
244         OPT_STATIC_SUPERS,
245         OPT_TRACE,
246 #endif
247
248         OPT_SS,
249
250 #ifdef ENABLE_JVMTI
251         OPT_DEBUG,
252         OPT_XRUNJDWP,
253         OPT_NOAGENT,
254         OPT_AGENTLIB,
255         OPT_AGENTPATH,
256 #endif
257
258 #if defined(ENABLE_DEBUG_FILTER)
259         OPT_FILTER_VERBOSECALL_INCLUDE,
260         OPT_FILTER_VERBOSECALL_EXCLUDE,
261         OPT_FILTER_SHOW_METHOD,
262 #endif
263
264         DUMMY
265 };
266
267
268 opt_struct opts[] = {
269         { "foo",               false, OPT_FOO },
270
271         /* Java options */
272
273         { "jar",               false, OPT_JAR },
274
275         { "d32",               false, OPT_D32 },
276         { "d64",               false, OPT_D64 },
277         { "client",            false, OPT_IGNORE },
278         { "server",            false, OPT_IGNORE },
279         { "jvm",               false, OPT_IGNORE },
280         { "hotspot",           false, OPT_IGNORE },
281
282         { "classpath",         true,  OPT_CLASSPATH },
283         { "cp",                true,  OPT_CLASSPATH },
284         { "D",                 true,  OPT_D },
285         { "version",           false, OPT_VERSION },
286         { "showversion",       false, OPT_SHOWVERSION },
287         { "fullversion",       false, OPT_FULLVERSION },
288         { "help",              false, OPT_HELP },
289         { "?",                 false, OPT_HELP },
290         { "X",                 false, OPT_X },
291         { "XX:",               true,  OPT_XX },
292
293         { "ea:",               true,  OPT_EA },
294         { "da:",               true,  OPT_DA },
295         { "ea",                false, OPT_EA_NOARG },
296         { "da",                false, OPT_DA_NOARG },
297
298         { "enableassertions:",  true,  OPT_EA },
299         { "disableassertions:", true,  OPT_DA },
300         { "enableassertions",   false, OPT_EA_NOARG },
301         { "disableassertions",  false, OPT_DA_NOARG },
302
303         { "esa",                     false, OPT_ESA },
304         { "enablesystemassertions",  false, OPT_ESA },
305         { "dsa",                     false, OPT_DSA },
306         { "disablesystemassertions", false, OPT_DSA },
307
308         { "noasyncgc",         false, OPT_IGNORE },
309 #if defined(ENABLE_VERIFIER)
310         { "noverify",          false, OPT_NOVERIFY },
311         { "Xverify:all",       false, OPT_XVERIFY_ALL },
312         { "Xverify:none",      false, OPT_XVERIFY_NONE },
313 #endif
314         { "v",                 false, OPT_VERBOSE1 },
315         { "verbose:",          true,  OPT_VERBOSE },
316
317 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
318         { "verbosetc",         false, OPT_VERBOSETC },
319 #endif
320 #if defined(ENABLE_STATISTICS)
321         { "time",              false, OPT_TIME },
322         { "stat",              false, OPT_STAT },
323 #endif
324         { "log",               true,  OPT_LOG },
325         { "c",                 true,  OPT_CHECK },
326         { "l",                 false, OPT_LOAD },
327
328 #if !defined(NDEBUG)
329         { "all",               false, OPT_ALL },
330         { "sig",               true,  OPT_SIGNATURE },
331 #endif
332
333 #if defined(ENABLE_LOOP)
334         { "oloop",             false, OPT_OLOOP },
335 #endif
336 #if defined(ENABLE_IFCONV)
337         { "ifconv",            false, OPT_IFCONV },
338 #endif
339 #if defined(ENABLE_LSRA)
340         { "lsra",              false, OPT_LSRA },
341 #endif
342 #if  defined(ENABLE_SSA)
343         { "lsra",              true, OPT_LSRA },
344 #endif
345
346 #if defined(ENABLE_INTRP)
347         /* interpreter options */
348
349         { "trace",             false, OPT_TRACE },
350         { "static-supers",     true,  OPT_STATIC_SUPERS },
351         { "no-dynamic",        false, OPT_NO_DYNAMIC },
352         { "no-replication",    false, OPT_NO_REPLICATION },
353         { "no-quicksuper",     false, OPT_NO_QUICKSUPER },
354 #endif
355
356         /* JVMTI Agent Command Line Options */
357 #ifdef ENABLE_JVMTI
358         { "agentlib:",         true,  OPT_AGENTLIB },
359         { "agentpath:",        true,  OPT_AGENTPATH },
360 #endif
361
362         /* Java non-standard options */
363
364         { "Xjit",              false, OPT_JIT },
365         { "Xint",              false, OPT_INTRP },
366         { "Xbootclasspath:",   true,  OPT_BOOTCLASSPATH },
367         { "Xbootclasspath/a:", true,  OPT_BOOTCLASSPATH_A },
368         { "Xbootclasspath/p:", true,  OPT_BOOTCLASSPATH_P },
369         { "Xbootclasspath/c:", true,  OPT_BOOTCLASSPATH_C },
370
371 #ifdef ENABLE_JVMTI
372         { "Xdebug",            false, OPT_DEBUG },
373         { "Xnoagent",          false, OPT_NOAGENT },
374         { "Xrunjdwp",          true,  OPT_XRUNJDWP },
375 #endif 
376
377         { "Xms",               true,  OPT_MS },
378         { "ms",                true,  OPT_MS },
379         { "Xmx",               true,  OPT_MX },
380         { "mx",                true,  OPT_MX },
381         { "Xss",               true,  OPT_SS },
382         { "ss",                true,  OPT_SS },
383
384         { "Xcheck:jni",        false, OPT_XCHECK_JNI },
385
386 #if defined(ENABLE_PROFILING)
387         { "Xprof:",            true,  OPT_PROF_OPTION },
388         { "Xprof",             false, OPT_PROF },
389 #endif
390
391         /* keep these at the end of the list */
392
393 #if !defined(NDEBUG)
394         { "m",                 true,  OPT_METHOD },
395 #endif
396
397         { "s",                 true,  OPT_SHOW },
398         { "debug-color",      false,  OPT_DEBUGCOLOR },
399
400 #if defined(ENABLE_DEBUG_FILTER)
401         { "XXfi",              true,  OPT_FILTER_VERBOSECALL_INCLUDE },
402         { "XXfx",              true,  OPT_FILTER_VERBOSECALL_EXCLUDE },
403         { "XXfm",              true,  OPT_FILTER_SHOW_METHOD },
404 #endif
405
406         { NULL,                false, 0 }
407 };
408
409
410 /* usage ***********************************************************************
411
412    Prints the correct usage syntax to stdout.
413
414 *******************************************************************************/
415
416 void usage(void)
417 {
418         puts("Usage: cacao [-options] classname [arguments]");
419         puts("               (to run a class file)");
420         puts("   or  cacao [-options] -jar jarfile [arguments]");
421         puts("               (to run a standalone jar file)\n");
422
423         puts("where options include:");
424         puts("    -d32                     use 32-bit data model if available");
425         puts("    -d64                     use 64-bit data model if available");
426         puts("    -client                  compatibility (currently ignored)");
427         puts("    -server                  compatibility (currently ignored)");
428         puts("    -jvm                     compatibility (currently ignored)");
429         puts("    -hotspot                 compatibility (currently ignored)\n");
430
431         puts("    -cp <path>               specify a path to look for classes");
432         puts("    -classpath <path>        specify a path to look for classes");
433         puts("    -D<name>=<value>         add an entry to the property list");
434         puts("    -verbose[:class|gc|jni]  enable specific verbose output");
435         puts("    -version                 print product version and exit");
436         puts("    -fullversion             print jpackage-compatible product version and exit");
437         puts("    -showversion             print product version and continue");
438         puts("    -help, -?                print this help message");
439         puts("    -X                       print help on non-standard Java options");
440         puts("    -XX                      print help on debugging options");
441     puts("    -ea[:<packagename>...|:<classname>]");
442     puts("    -enableassertions[:<packagename>...|:<classname>]");
443         puts("                             enable assertions with specified granularity");
444         puts("    -da[:<packagename>...|:<classname>]");
445         puts("    -disableassertions[:<packagename>...|:<classname>]");
446         puts("                             disable assertions with specified granularity");
447         puts("    -esa | -enablesystemassertions");
448         puts("                             enable system assertions");
449         puts("    -dsa | -disablesystemassertions");
450         puts("                             disable system assertions");
451
452 #ifdef ENABLE_JVMTI
453         puts("    -agentlib:<agent-lib-name>=<options>  library to load containg JVMTI agent");
454         puts ("                                         for jdwp help use: -agentlib:jdwp=help");
455         puts("    -agentpath:<path-to-agent>=<options>  path to library containg JVMTI agent");
456 #endif
457
458         /* exit with error code */
459
460         exit(1);
461 }   
462
463
464 static void Xusage(void)
465 {
466 #if defined(ENABLE_JIT)
467         puts("    -Xjit                    JIT mode execution (default)");
468 #endif
469 #if defined(ENABLE_INTRP)
470         puts("    -Xint                    interpreter mode execution");
471 #endif
472         puts("    -Xbootclasspath:<zip/jar files and directories separated by :>");
473     puts("                             value is set as bootstrap class path");
474         puts("    -Xbootclasspath/a:<zip/jar files and directories separated by :>");
475         puts("                             value is appended to the bootstrap class path");
476         puts("    -Xbootclasspath/p:<zip/jar files and directories separated by :>");
477         puts("                             value is prepended to the bootstrap class path");
478         puts("    -Xbootclasspath/c:<zip/jar files and directories separated by :>");
479         puts("                             value is used as Java core library, but the");
480         puts("                             hardcoded VM interface classes are prepended");
481         printf("    -Xms<size>               set the initial size of the heap (default: %dMB)\n", HEAP_STARTSIZE / 1024 / 1024);
482         printf("    -Xmx<size>               set the maximum size of the heap (default: %dMB)\n", HEAP_MAXSIZE / 1024 / 1024);
483         printf("    -Xss<size>               set the thread stack size (default: %dkB)\n", STACK_SIZE / 1024);
484
485 #if defined(ENABLE_PROFILING)
486         puts("    -Xprof[:bb]              collect and print profiling data");
487 #endif
488
489 #if defined(ENABLE_JVMTI)
490     /* -Xdebug option depend on gnu classpath JDWP options. options: 
491          transport=dt_socket,address=<hostname:port>,server=(y|n),suspend(y|n) */
492         puts("    -Xdebug                  enable remote debugging\n");
493         puts("    -Xrunjdwp transport=[dt_socket|...],address=<hostname:port>,server=[y|n],suspend=[y|n]\n");
494         puts("                             enable remote debugging\n");
495 #endif 
496
497         /* exit with error code */
498
499         exit(1);
500 }   
501
502
503 #if 0
504 static void XXusage(void)
505 {
506         puts("    -v                       write state-information");
507 #if !defined(NDEBUG)
508         puts("    -verbose:jit             enable specific verbose output");
509         puts("    -debug-color             colored output for ANSI terms");
510 #endif
511 #ifdef TYPECHECK_VERBOSE
512         puts("    -verbosetc               write debug messages while typechecking");
513 #endif
514 #if defined(ENABLE_VERIFIER)
515         puts("    -noverify                don't verify classfiles");
516 #endif
517 #if defined(ENABLE_STATISTICS)
518         puts("    -time                    measure the runtime");
519         puts("    -stat                    detailed compiler statistics");
520 #endif
521         puts("    -log logfile             specify a name for the logfile");
522         puts("    -c(heck)b(ounds)         don't check array bounds");
523         puts("            s(ync)           don't check for synchronization");
524 #if defined(ENABLE_LOOP)
525         puts("    -oloop                   optimize array accesses in loops");
526 #endif
527         puts("    -l                       don't start the class after loading");
528 #if !defined(NDEBUG)
529         puts("    -all                     compile all methods, no execution");
530         puts("    -m                       compile only a specific method");
531         puts("    -sig                     specify signature for a specific method");
532 #endif
533
534         puts("    -s...                    show...");
535         puts("      (c)onstants            the constant pool");
536         puts("      (m)ethods              class fields and methods");
537         puts("      (u)tf                  the utf - hash");
538         puts("      (i)ntermediate         intermediate representation");
539 #if defined(ENABLE_DISASSEMBLER)
540         puts("      (a)ssembler            disassembled listing");
541         puts("      n(o)ps                 show NOPs in disassembler output");
542 #endif
543         puts("      (d)atasegment          data segment listing");
544
545 #if defined(ENABLE_IFCONV)
546         puts("    -ifconv                  use if-conversion");
547 #endif
548 #if defined(ENABLE_LSRA)
549         puts("    -lsra                    use linear scan register allocation");
550 #endif
551 #if defined(ENABLE_SSA)
552         puts("    -lsra:...                use linear scan register allocation (with SSA)");
553         puts("       (d)ead code elimination");
554         puts("       (c)opy propagation");
555 #endif
556 #if defined(ENABLE_DEBUG_FILTER)
557         puts("    -XXfi <regex>            begin of dynamic scope for verbosecall filter");
558         puts("    -XXfx <regex>            end of dynamic scope for verbosecall filter");
559         puts("    -XXfm <regex>            filter for show options");
560 #endif
561         /* exit with error code */
562
563         exit(1);
564 }
565 #endif
566
567
568 /* version *********************************************************************
569
570    Only prints cacao version information.
571
572 *******************************************************************************/
573
574 static void version(bool opt_exit)
575 {
576         puts("java version \""JAVA_VERSION"\"");
577         puts("CACAO version "VERSION"\n");
578
579         puts("Copyright (C) 1996-2005, 2006, 2007, 2008");
580         puts("CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO");
581         puts("This is free software; see the source for copying conditions.  There is NO");
582         puts("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
583
584         /* exit normally, if requested */
585
586         if (opt_exit)
587                 exit(0);
588 }
589
590
591 /* fullversion *****************************************************************
592
593    Prints a Sun compatible version information (required e.g. by
594    jpackage, www.jpackage.org).
595
596 *******************************************************************************/
597
598 static void fullversion(void)
599 {
600         puts("java full version \"cacao-"JAVA_VERSION"\"");
601
602         /* exit normally */
603
604         exit(0);
605 }
606
607
608 /* forward declarations *******************************************************/
609
610 static char *vm_get_mainclass_from_jar(char *mainstring);
611 #if !defined(NDEBUG)
612 static void  vm_compile_all(void);
613 static void  vm_compile_method(char* mainname);
614 #endif
615
616
617 /**
618  * Implementation for JNI_CreateJavaVM.  This function creates a VM
619  * object.
620  *
621  * @param p_vm
622  * @param p_env
623  * @param vm_args
624  *
625  * @return true on success, false otherwise.
626  */
627 bool VM::create(JavaVM** p_vm, void** p_env, void* vm_args)
628 {
629         JavaVMInitArgs* _vm_args;
630
631         // Get the arguments for the new JVM.
632         _vm_args = (JavaVMInitArgs *) vm_args;
633
634         // Instantiate a new VM.
635         try {
636                 _vm = new VM(_vm_args);
637         }
638         catch (std::exception e) {
639                 // FIXME How can we delete the resources allocated?
640 //              /* release allocated memory */
641 //              FREE(env, _Jv_JNIEnv);
642 //              FREE(vm, _Jv_JavaVM);
643
644                 _vm = NULL;
645
646                 return false;
647         }
648
649         // Return the values.
650
651         *p_vm  = _vm->get_javavm();
652         *p_env = _vm->get_jnienv();
653
654         return true;
655 }
656
657
658 /**
659  * C wrapper for VM::create.
660  */
661 extern "C" {
662         bool VM_create(JavaVM** p_vm, void** p_env, void* vm_args)
663         {
664                 return VM::create(p_vm, p_env, vm_args);
665         }
666 }
667
668
669 /**
670  * VM constructor.
671  */
672 VM::VM(JavaVMInitArgs* vm_args)
673 {
674         // Very first thing to do: we are initializing.
675         _initializing = true;
676
677         // Make ourself globally visible.
678         // XXX Is this a good idea?
679         _vm = this;
680
681         /* create and fill a JavaVM structure */
682
683         _javavm = new JavaVM();
684
685 #if defined(ENABLE_JNI)
686         _javavm->functions = &_Jv_JNIInvokeInterface;
687 #endif
688
689         /* get the VM and Env tables (must be set before vm_create) */
690         /* XXX JVMTI Agents needs a JavaVM  */
691
692         _jnienv = new JNIEnv();
693
694 #if defined(ENABLE_JNI)
695         _jnienv->functions = &_Jv_JNINativeInterface;
696 #endif
697
698         /* actually create the JVM */
699
700         int   len;
701         char *p;
702         char *boot_class_path;
703         char *class_path;
704         int   opt;
705         bool  opt_version;
706         bool  opt_exit;
707
708 #if defined(ENABLE_JVMTI)
709         lt_dlhandle  handle;
710         char *libname, *agentarg;
711         bool jdwp,agentbypath;
712         jdwp = agentbypath = false;
713 #endif
714
715 #if defined(ENABLE_JNI)
716         /* Check the JNI version requested. */
717
718         if (!jni_version_check(vm_args->version))
719                 throw std::exception();
720 #endif
721
722         /* We only support 1 JVM instance. */
723
724         if (vms > 0)
725                 throw std::exception();
726
727         /* Install the exit handler. */
728
729         if (atexit(vm_exit_handler))
730                 os::abort("atexit failed: %s\n", strerror(errno));
731
732         /* Set some options. */
733
734         opt_version       = false;
735         opt_exit          = false;
736
737         opt_heapmaxsize   = HEAP_MAXSIZE;
738         opt_heapstartsize = HEAP_STARTSIZE;
739         opt_stacksize     = STACK_SIZE;
740
741         // First of all, parse the -XX options.
742
743 #if defined(ENABLE_VMLOG)
744         vmlog_cacao_init_options();
745 #endif
746
747         options_xx(vm_args);
748
749         // After -XX options are parsed, print the build-time
750         // configuration, if requested.
751         if (opt_PrintConfig)
752                 print_build_time_config();
753
754 #if defined(ENABLE_VMLOG)
755         vmlog_cacao_init();
756 #endif
757
758         /* We need to check if the actual size of a java.lang.Class object
759            is smaller or equal than the assumption made in
760            src/vm/class.hpp. */
761
762 #warning FIXME We need to check the size of java.lang.Class!!!
763 //      if (sizeof(java_lang_Class) > sizeof(dummy_java_lang_Class))
764 //              vm_abort("vm_create: java_lang_Class structure is bigger than classinfo.object (%d > %d)", sizeof(java_lang_Class), sizeof(dummy_java_lang_Class));
765
766         /* set the VM starttime */
767
768         _starttime = builtin_currenttimemillis();
769
770 #if defined(ENABLE_JVMTI)
771         /* initialize JVMTI related  **********************************************/
772         jvmti = false;
773 #endif
774
775         /* iterate over all passed options */
776
777         while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
778                 switch (opt) {
779                 case OPT_FOO:
780                         opt_foo = true;
781                         break;
782
783                 case OPT_IGNORE:
784                         break;
785                         
786                 case OPT_JAR:
787                         opt_jar = true;
788                         break;
789
790                 case OPT_D32:
791 #if SIZEOF_VOID_P == 8
792                         puts("Running a 32-bit JVM is not supported on this platform.");
793                         exit(1);
794 #endif
795                         break;
796
797                 case OPT_D64:
798 #if SIZEOF_VOID_P == 4
799                         puts("Running a 64-bit JVM is not supported on this platform.");
800                         exit(1);
801 #endif
802                         break;
803
804                 case OPT_CLASSPATH:
805                         /* Forget old classpath and set the argument as new
806                            classpath. */
807
808                         // FIXME Make class_path const char*.
809                         class_path = (char*) _properties.get("java.class.path");
810
811                         p = MNEW(char, strlen(opt_arg) + strlen("0"));
812
813                         strcpy(p, opt_arg);
814
815 #if defined(ENABLE_JAVASE)
816                         _properties.put("java.class.path", p);
817 #endif
818
819                         MFREE(class_path, char, strlen(class_path));
820                         break;
821
822                 case OPT_D:
823                         for (unsigned int i = 0; i < strlen(opt_arg); i++) {
824                                 if (opt_arg[i] == '=') {
825                                         opt_arg[i] = '\0';
826                                         _properties.put(opt_arg, opt_arg + i + 1);
827                                         goto opt_d_done;
828                                 }
829                         }
830
831                         /* if no '=' is given, just create an empty property */
832
833                         _properties.put(opt_arg, "");
834
835                 opt_d_done:
836                         break;
837
838                 case OPT_BOOTCLASSPATH:
839                         /* Forget default bootclasspath and set the argument as
840                            new boot classpath. */
841
842                         // FIXME Make boot_class_path const char*.
843                         boot_class_path = (char*) _properties.get("sun.boot.class.path");
844
845                         p = MNEW(char, strlen(opt_arg) + strlen("0"));
846
847                         strcpy(p, opt_arg);
848
849                         _properties.put("sun.boot.class.path", p);
850                         _properties.put("java.boot.class.path", p);
851
852                         MFREE(boot_class_path, char, strlen(boot_class_path));
853                         break;
854
855                 case OPT_BOOTCLASSPATH_A:
856                         /* Append to bootclasspath. */
857
858                         // FIXME Make boot_class_path const char*.
859                         boot_class_path = (char*) _properties.get("sun.boot.class.path");
860
861                         len = strlen(boot_class_path);
862
863                         // XXX (char*) quick hack
864                         p = (char*) MREALLOC(boot_class_path,
865                                                  char,
866                                                  len + strlen("0"),
867                                                  len + strlen(":") +
868                                                  strlen(opt_arg) + strlen("0"));
869
870                         strcat(p, ":");
871                         strcat(p, opt_arg);
872
873                         _properties.put("sun.boot.class.path", p);
874                         _properties.put("java.boot.class.path", p);
875                         break;
876
877                 case OPT_BOOTCLASSPATH_P:
878                         /* Prepend to bootclasspath. */
879
880                         // FIXME Make boot_class_path const char*.
881                         boot_class_path = (char*) _properties.get("sun.boot.class.path");
882
883                         len = strlen(boot_class_path);
884
885                         p = MNEW(char, strlen(opt_arg) + strlen(":") + len + strlen("0"));
886
887                         strcpy(p, opt_arg);
888                         strcat(p, ":");
889                         strcat(p, boot_class_path);
890
891                         _properties.put("sun.boot.class.path", p);
892                         _properties.put("java.boot.class.path", p);
893
894                         MFREE(boot_class_path, char, len);
895                         break;
896
897                 case OPT_BOOTCLASSPATH_C:
898                         /* Use as Java core library, but prepend VM interface
899                            classes. */
900
901                         // FIXME Make boot_class_path const char*.
902                         boot_class_path = (char*) _properties.get("sun.boot.class.path");
903
904                         len =
905                                 strlen(CACAO_VM_ZIP) +
906                                 strlen(":") +
907                                 strlen(opt_arg) +
908                                 strlen("0");
909
910                         p = MNEW(char, len);
911
912                         strcpy(p, CACAO_VM_ZIP);
913                         strcat(p, ":");
914                         strcat(p, opt_arg);
915
916                         _properties.put("sun.boot.class.path", p);
917                         _properties.put("java.boot.class.path", p);
918
919                         MFREE(boot_class_path, char, strlen(boot_class_path));
920                         break;
921
922 #if defined(ENABLE_JVMTI)
923                 case OPT_DEBUG:
924                         /* this option exists only for compatibility reasons */
925                         break;
926
927                 case OPT_NOAGENT:
928                         /* I don't know yet what Xnoagent should do. This is only for 
929                            compatiblity with eclipse - motse */
930                         break;
931
932                 case OPT_XRUNJDWP:
933                         agentbypath = true;
934                         jvmti       = true;
935                         jdwp        = true;
936
937                         len =
938                                 strlen(CACAO_LIBDIR) +
939                                 strlen("/libjdwp.so=") +
940                                 strlen(opt_arg) +
941                                 strlen("0");
942
943                         agentarg = MNEW(char, len);
944
945                         strcpy(agentarg, CACAO_LIBDIR);
946                         strcat(agentarg, "/libjdwp.so=");
947                         strcat(agentarg, &opt_arg[1]);
948                         break;
949
950                 case OPT_AGENTPATH:
951                         agentbypath = true;
952
953                 case OPT_AGENTLIB:
954                         jvmti = true;
955                         agentarg = opt_arg;
956                         break;
957 #endif
958                         
959                 case OPT_MX:
960                 case OPT_MS:
961                 case OPT_SS:
962                         {
963                                 char c;
964                                 int j;
965
966                                 c = opt_arg[strlen(opt_arg) - 1];
967
968                                 if ((c == 'k') || (c == 'K')) {
969                                         j = atoi(opt_arg) * 1024;
970
971                                 } else if ((c == 'm') || (c == 'M')) {
972                                         j = atoi(opt_arg) * 1024 * 1024;
973
974                                 } else
975                                         j = atoi(opt_arg);
976
977                                 if (opt == OPT_MX)
978                                         opt_heapmaxsize = j;
979                                 else if (opt == OPT_MS)
980                                         opt_heapstartsize = j;
981                                 else
982                                         opt_stacksize = j;
983                         }
984                         break;
985
986                 case OPT_XCHECK_JNI:
987                         // HotSpot compatibility option.
988                         break;
989
990                 case OPT_VERBOSE1:
991                         opt_verbose = true;
992                         break;
993
994                 case OPT_VERBOSE:
995                         if (strcmp("class", opt_arg) == 0) {
996                                 opt_verboseclass = true;
997                         }
998                         else if (strcmp("gc", opt_arg) == 0) {
999                                 opt_verbosegc = true;
1000                         }
1001                         else if (strcmp("jni", opt_arg) == 0) {
1002                                 opt_verbosejni = true;
1003                         }
1004 #if !defined(NDEBUG)
1005                         else if (strcmp("jit", opt_arg) == 0) {
1006                                 opt_verbose = true;
1007                                 loadverbose = true;
1008                                 initverbose = true;
1009                                 compileverbose = true;
1010                         }
1011 #endif
1012                         else {
1013                                 printf("Unknown -verbose option: %s\n", opt_arg);
1014                                 usage();
1015                         }
1016                         break;
1017
1018                 case OPT_DEBUGCOLOR:
1019                         opt_debugcolor = true;
1020                         break;
1021
1022 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
1023                 case OPT_VERBOSETC:
1024                         opt_typecheckverbose = true;
1025                         break;
1026 #endif
1027                                 
1028                 case OPT_VERSION:
1029                         opt_version = true;
1030                         opt_exit    = true;
1031                         break;
1032
1033                 case OPT_FULLVERSION:
1034                         fullversion();
1035                         break;
1036
1037                 case OPT_SHOWVERSION:
1038                         opt_version = true;
1039                         break;
1040
1041 #if defined(ENABLE_VERIFIER)
1042                 case OPT_XVERIFY_ALL:
1043                         opt_verify = true;
1044                         break;
1045
1046                 case OPT_NOVERIFY:
1047                 case OPT_XVERIFY_NONE:
1048                         opt_verify = false;
1049                         break;
1050 #endif
1051
1052 #if defined(ENABLE_STATISTICS)
1053                 case OPT_TIME:
1054                         opt_getcompilingtime = true;
1055                         opt_getloadingtime = true;
1056                         break;
1057                                         
1058                 case OPT_STAT:
1059                         opt_stat = true;
1060                         break;
1061 #endif
1062                                         
1063                 case OPT_LOG:
1064                         log_init(opt_arg);
1065                         break;
1066                         
1067                 case OPT_CHECK:
1068                         for (unsigned int i = 0; i < strlen(opt_arg); i++) {
1069                                 switch (opt_arg[i]) {
1070                                 case 'b':
1071                                         checkbounds = false;
1072                                         break;
1073                                 case 's':
1074                                         checksync = false;
1075                                         break;
1076                                 default:
1077                                         usage();
1078                                 }
1079                         }
1080                         break;
1081                         
1082                 case OPT_LOAD:
1083                         opt_run = false;
1084                         makeinitializations = false;
1085                         break;
1086
1087 #if !defined(NDEBUG)
1088                 case OPT_ALL:
1089                         compileall = true;
1090                         opt_run = false;
1091                         makeinitializations = false;
1092                         break;
1093
1094                 case OPT_METHOD:
1095                         opt_run = false;
1096                         opt_method = opt_arg;
1097                         makeinitializations = false;
1098                         break;
1099
1100                 case OPT_SIGNATURE:
1101                         opt_signature = opt_arg;
1102                         break;
1103 #endif
1104
1105                 case OPT_SHOW:       /* Display options */
1106                         for (unsigned int i = 0; i < strlen(opt_arg); i++) {            
1107                                 switch (opt_arg[i]) {
1108                                 case 'c':
1109                                         showconstantpool = true;
1110                                         break;
1111
1112                                 case 'u':
1113                                         showutf = true;
1114                                         break;
1115
1116                                 case 'm':
1117                                         showmethods = true;
1118                                         break;
1119
1120                                 case 'i':
1121                                         opt_showintermediate = true;
1122                                         compileverbose = true;
1123                                         break;
1124
1125 #if defined(ENABLE_DISASSEMBLER)
1126                                 case 'a':
1127                                         opt_showdisassemble = true;
1128                                         compileverbose = true;
1129                                         break;
1130 #endif
1131
1132                                 case 'd':
1133                                         opt_showddatasegment = true;
1134                                         break;
1135
1136                                 default:
1137                                         usage();
1138                                 }
1139                         }
1140                         break;
1141                         
1142 #if defined(ENABLE_LOOP)
1143                 case OPT_OLOOP:
1144                         opt_loops = true;
1145                         break;
1146 #endif
1147
1148 #if defined(ENABLE_IFCONV)
1149                 case OPT_IFCONV:
1150                         opt_ifconv = true;
1151                         break;
1152 #endif
1153
1154 #if defined(ENABLE_LSRA)
1155                 case OPT_LSRA:
1156                         opt_lsra = true;
1157                         break;
1158 #endif
1159 #if  defined(ENABLE_SSA)
1160                 case OPT_LSRA:
1161                         opt_lsra = true;
1162                         for (unsigned int i = 0; i < strlen(opt_arg); i++) {            
1163                                 switch (opt_arg[i]) {
1164                                 case 'c':
1165                                         opt_ssa_cp = true;
1166                                         break;
1167
1168                                 case 'd':
1169                                         opt_ssa_dce = true;
1170                                         break;
1171
1172                                 case ':':
1173                                         break;
1174
1175                                 default:
1176                                         usage();
1177                                 }
1178                         }
1179                         break;
1180 #endif
1181
1182                 case OPT_HELP:
1183                         usage();
1184                         break;
1185
1186                 case OPT_X:
1187                         Xusage();
1188                         break;
1189
1190                 case OPT_XX:
1191                         /* Already parsed. */
1192                         break;
1193
1194                 case OPT_EA:
1195 #if defined(ENABLE_ASSERTION)
1196                         assertion_ea_da(opt_arg, true);
1197 #endif
1198                         break;
1199
1200                 case OPT_DA:
1201 #if defined(ENABLE_ASSERTION)
1202                         assertion_ea_da(opt_arg, false);
1203 #endif
1204                         break;
1205
1206                 case OPT_EA_NOARG:
1207 #if defined(ENABLE_ASSERTION)
1208                         assertion_user_enabled = true;
1209 #endif
1210                         break;
1211
1212                 case OPT_DA_NOARG:
1213 #if defined(ENABLE_ASSERTION)
1214                         assertion_user_enabled = false;
1215 #endif
1216                         break;
1217
1218                 case OPT_ESA:
1219 #if defined(ENABLE_ASSERTION)
1220                         assertion_system_enabled = true;
1221 #endif
1222                         break;
1223
1224                 case OPT_DSA:
1225 #if defined(ENABLE_ASSERTION)
1226                         assertion_system_enabled = false;
1227 #endif
1228                         break;
1229
1230 #if defined(ENABLE_PROFILING)
1231                 case OPT_PROF_OPTION:
1232                         /* use <= to get the last \0 too */
1233
1234                         for (unsigned int i = 0, j = 0; i <= strlen(opt_arg); i++) {
1235                                 if (opt_arg[i] == ',')
1236                                         opt_arg[i] = '\0';
1237
1238                                 if (opt_arg[i] == '\0') {
1239                                         if (strcmp("bb", opt_arg + j) == 0)
1240                                                 opt_prof_bb = true;
1241
1242                                         else {
1243                                                 printf("Unknown option: -Xprof:%s\n", opt_arg + j);
1244                                                 usage();
1245                                         }
1246
1247                                         /* set k to next char */
1248
1249                                         j = i + 1;
1250                                 }
1251                         }
1252                         /* fall through */
1253
1254                 case OPT_PROF:
1255                         opt_prof = true;
1256                         break;
1257 #endif
1258
1259                 case OPT_JIT:
1260 #if defined(ENABLE_JIT)
1261                         opt_jit = true;
1262 #else
1263                         printf("-Xjit option not enabled.\n");
1264                         exit(1);
1265 #endif
1266                         break;
1267
1268                 case OPT_INTRP:
1269 #if defined(ENABLE_INTRP)
1270                         opt_intrp = true;
1271 #else
1272                         printf("-Xint option not enabled.\n");
1273                         exit(1);
1274 #endif
1275                         break;
1276
1277 #if defined(ENABLE_INTRP)
1278                 case OPT_STATIC_SUPERS:
1279                         opt_static_supers = atoi(opt_arg);
1280                         break;
1281
1282                 case OPT_NO_DYNAMIC:
1283                         opt_no_dynamic = true;
1284                         break;
1285
1286                 case OPT_NO_REPLICATION:
1287                         opt_no_replication = true;
1288                         break;
1289
1290                 case OPT_NO_QUICKSUPER:
1291                         opt_no_quicksuper = true;
1292                         break;
1293
1294                 case OPT_TRACE:
1295                         vm_debug = true;
1296                         break;
1297 #endif
1298
1299 #if defined(ENABLE_DEBUG_FILTER)
1300                 case OPT_FILTER_VERBOSECALL_INCLUDE:
1301                         opt_filter_verbosecall_include = opt_arg;
1302                         break;
1303
1304                 case OPT_FILTER_VERBOSECALL_EXCLUDE:
1305                         opt_filter_verbosecall_exclude = opt_arg;
1306                         break;
1307
1308                 case OPT_FILTER_SHOW_METHOD:
1309                         opt_filter_show_method = opt_arg;
1310                         break;
1311
1312 #endif
1313                 default:
1314                         printf("Unknown option: %s\n",
1315                                    vm_args->options[opt_index].optionString);
1316                         usage();
1317                 }
1318         }
1319
1320         // Print the preliminary run-time VM configuration after options
1321         // are parsed.
1322         if (opt_PrintConfig)
1323                 print_run_time_config();
1324
1325 #if defined(ENABLE_JVMTI)
1326         if (jvmti) {
1327                 jvmti_set_phase(JVMTI_PHASE_ONLOAD);
1328                 jvmti_agentload(agentarg, agentbypath, &handle, &libname);
1329
1330                 if (jdwp)
1331                         MFREE(agentarg, char, strlen(agentarg));
1332
1333                 jvmti_set_phase(JVMTI_PHASE_PRIMORDIAL);
1334         }
1335 #endif
1336
1337         /* initialize the garbage collector */
1338
1339         gc_init(opt_heapmaxsize, opt_heapstartsize);
1340
1341 #if defined(ENABLE_THREADS)
1342         /* AFTER: gc_init */
1343
1344         threads_preinit();
1345         lock_init();
1346 #endif
1347
1348         /* install architecture dependent signal handlers */
1349
1350         if (!signal_init())
1351                 os::abort("vm_create: signal_init failed");
1352
1353 #if defined(ENABLE_INTRP)
1354         /* Allocate main thread stack on the Java heap. */
1355
1356         if (opt_intrp) {
1357                 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1358                 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1359         }
1360 #endif
1361
1362         /* AFTER: threads_preinit */
1363
1364         if (!string_init())
1365                 os::abort("vm_create: string_init failed");
1366
1367         /* AFTER: threads_preinit */
1368
1369         utf8_init();
1370
1371         /* AFTER: thread_preinit */
1372
1373         if (!suck_init())
1374                 os::abort("vm_create: suck_init failed");
1375
1376         suck_add_from_property("java.endorsed.dirs");
1377
1378         /* Now we have all options handled and we can print the version
1379            information.
1380
1381            AFTER: suck_add_from_property("java.endorsed.dirs"); */
1382
1383         if (opt_version)
1384                 version(opt_exit);
1385
1386         /* AFTER: utf8_init */
1387
1388         // FIXME Make boot_class_path const char*.
1389         boot_class_path = (char*) _properties.get("sun.boot.class.path");
1390         suck_add(boot_class_path);
1391
1392         /* initialize the classcache hashtable stuff: lock, hashtable
1393            (must be done _after_ threads_preinit) */
1394
1395         if (!classcache_init())
1396                 os::abort("vm_create: classcache_init failed");
1397
1398         /* Initialize the code memory management. */
1399         /* AFTER: threads_preinit */
1400
1401         codememory_init();
1402
1403         /* initialize the finalizer stuff (must be done _after_
1404            threads_preinit) */
1405
1406         if (!finalizer_init())
1407                 os::abort("vm_create: finalizer_init failed");
1408
1409         /* Initialize the JIT compiler. */
1410
1411         jit_init();
1412         code_init();
1413         methodtree_init();
1414
1415 #if defined(ENABLE_PYTHON)
1416         pythonpass_init();
1417 #endif
1418
1419         /* AFTER: utf8_init, classcache_init */
1420
1421         loader_preinit();
1422         linker_preinit();
1423
1424         // AFTER: loader_preinit, linker_preinit
1425         Primitive::initialize_table();
1426
1427         loader_init();
1428         linker_init();
1429
1430         // AFTER: loader_init, linker_init
1431         Primitive::post_initialize_table();
1432         method_init();
1433
1434 #if defined(ENABLE_JIT)
1435         trap_init();
1436 #endif
1437
1438         if (!builtin_init())
1439                 os::abort("vm_create: builtin_init failed");
1440
1441         /* Register the native methods implemented in the VM. */
1442         /* BEFORE: threads_init */
1443
1444         nativevm_preinit();
1445
1446 #if defined(ENABLE_JNI)
1447         /* Initialize the JNI subsystem (must be done _before_
1448            threads_init, as threads_init can call JNI methods
1449            (e.g. NewGlobalRef). */
1450
1451         if (!jni_init())
1452                 os::abort("vm_create: jni_init failed");
1453 #endif
1454
1455 #if defined(ENABLE_JNI) || defined(ENABLE_HANDLES)
1456         /* Initialize the local reference table for the main thread. */
1457         /* BEFORE: threads_init */
1458
1459         if (!localref_table_init())
1460                 os::abort("vm_create: localref_table_init failed");
1461 #endif
1462
1463         /* Iinitialize some important system classes. */
1464         /* BEFORE: threads_init */
1465
1466         initialize_init();
1467
1468 #if defined(ENABLE_THREADS)
1469         threads_init();
1470 #endif
1471
1472         /* Initialize the native VM subsystem. */
1473         /* AFTER: threads_init (at least for SUN's classes) */
1474
1475         nativevm_init();
1476
1477 #if defined(ENABLE_PROFILING)
1478         /* initialize profiling */
1479
1480         if (!profile_init())
1481                 os::abort("vm_create: profile_init failed");
1482 #endif
1483
1484 #if defined(ENABLE_THREADS)
1485         /* start the signal handler thread */
1486
1487 #if defined(__LINUX__)
1488         /* XXX Remove for exact-GC. */
1489         if (threads_pthreads_implementation_nptl)
1490 #endif
1491                 if (!signal_start_thread())
1492                         os::abort("vm_create: signal_start_thread failed");
1493
1494         /* finally, start the finalizer thread */
1495
1496         if (!finalizer_start_thread())
1497                 os::abort("vm_create: finalizer_start_thread failed");
1498
1499 # if !defined(NDEBUG)
1500         /* start the memory profiling thread */
1501
1502         if (opt_ProfileMemoryUsage || opt_ProfileGCMemoryUsage)
1503                 if (!memory_start_thread())
1504                         os::abort("vm_create: memory_start_thread failed");
1505 # endif
1506
1507         // Start the recompilation thread (must be done before the
1508         // profiling thread).
1509         // FIXME Only works for one recompiler.
1510         _recompiler.start();
1511
1512 # if defined(ENABLE_PROFILING)
1513         /* start the profile sampling thread */
1514
1515 /*      if (opt_prof) */
1516 /*              if (!profile_start_thread()) */
1517 /*                      os::abort("vm_create: profile_start_thread failed"); */
1518 # endif
1519 #endif
1520
1521 #if defined(ENABLE_JVMTI)
1522 # if defined(ENABLE_GC_CACAO)
1523         /* XXX this will not work with the new indirection cells for classloaders!!! */
1524         assert(0);
1525 # endif
1526         if (jvmti) {
1527                 /* add agent library to native library hashtable */
1528                 native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
1529         }
1530 #endif
1531
1532         /* Increment the number of VMs. */
1533
1534         vms++;
1535
1536         // Initialization is done, VM is created.
1537         _created      = true;
1538         _initializing = false;
1539         
1540         // Print the run-time VM configuration after all stuff is set and
1541         // the VM is initialized.
1542         if (opt_PrintConfig)
1543                 print_run_time_config();
1544 }
1545
1546
1547 /**
1548  * Print build-time (default) VM configuration.
1549  */
1550 void VM::print_build_time_config(void)
1551 {
1552         puts("CACAO "VERSION" configure/build options:");
1553         puts("");
1554         puts("  ./configure: "VERSION_CONFIGURE_ARGS"");
1555 #if defined(__VERSION__)
1556         puts("  CC         : "VERSION_CC" ("__VERSION__")");
1557         puts("  CXX        : "VERSION_CXX" ("__VERSION__")");
1558 #else
1559         puts("  CC         : "VERSION_CC"");
1560         puts("  CXX        : "VERSION_CXX"");
1561 #endif
1562         puts("  CFLAGS     : "VERSION_CFLAGS"");
1563         puts("  CXXFLAGS   : "VERSION_CXXFLAGS"");
1564
1565         puts("");
1566
1567         puts("Build-time (default) variables:\n");
1568         printf("  maximum heap size              : %d\n", HEAP_MAXSIZE);
1569         printf("  initial heap size              : %d\n", HEAP_STARTSIZE);
1570         printf("  stack size                     : %d\n", STACK_SIZE);
1571
1572 #if defined(ENABLE_JRE_LAYOUT)
1573         // When we're building with JRE-layout, the default paths are the
1574         // same as the runtime paths.
1575 #else
1576 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
1577         puts("  gnu.classpath.boot.library.path: "JAVA_RUNTIME_LIBRARY_LIBDIR);
1578         puts("  java.boot.class.path           : "CACAO_VM_ZIP":"JAVA_RUNTIME_LIBRARY_CLASSES"");
1579 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
1580         puts("  sun.boot.library.path          : "JAVA_RUNTIME_LIBRARY_LIBDIR);
1581         puts("  java.boot.class.path           : "JAVA_RUNTIME_LIBRARY_CLASSES);
1582 # endif
1583 #endif
1584
1585         puts("");
1586 }
1587
1588
1589 /**
1590  * Print run-time VM configuration.
1591  */
1592 void VM::print_run_time_config()
1593 {
1594         puts("Run-time variables:\n");
1595         printf("  maximum heap size              : %d\n", opt_heapmaxsize);
1596         printf("  initial heap size              : %d\n", opt_heapstartsize);
1597         printf("  stack size                     : %d\n", opt_stacksize);
1598
1599 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
1600         printf("  gnu.classpath.boot.library.path: %s\n", _properties.get("gnu.classpath.boot.library.path"));
1601 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
1602         printf("  sun.boot.library.path          : %s\n", _properties.get("sun.boot.library.path"));
1603 #endif
1604
1605         printf("  java.boot.class.path           : %s\n", _properties.get("java.boot.class.path"));
1606         printf("  java.class.path                : %s\n", _properties.get("java.class.path"));
1607
1608         puts("");
1609 }
1610
1611
1612 /* vm_run **********************************************************************
1613
1614    Runs the main-method of the passed class.
1615
1616 *******************************************************************************/
1617
1618 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1619 {
1620         methodinfo* m;
1621         int         status;
1622
1623 #if !defined(NDEBUG)
1624         if (compileall) {
1625                 vm_compile_all();
1626                 return;
1627         }
1628 #endif
1629
1630         /* Get the main class or jar file argument. */
1631
1632         char* mainname = NULL;
1633
1634         if (opt_index < vm_args->nOptions) {
1635                 /* Get main-class argument. */
1636
1637                 mainname = vm_args->options[opt_index].optionString;
1638
1639                 /* If the main class argument is a jar file, put it into the
1640                    classpath. */
1641
1642                 if (opt_jar == true) {
1643                         char* p = MNEW(char, strlen(mainname) + strlen("0"));
1644
1645                         strcpy(p, mainname);
1646
1647 #if defined(ENABLE_JAVASE)
1648                         VM::get_current()->get_properties().put("java.class.path", p);
1649 #endif
1650                 }
1651                 else {
1652                         /* Replace dots with slashes in the class name. */
1653
1654                         for (unsigned int i = 0; i < strlen(mainname); i++)
1655                                 if (mainname[i] == '.')
1656                                         mainname[i] = '/';
1657                 }
1658
1659                 /* Move index to first argument. */
1660
1661                 opt_index++;
1662         }
1663
1664         /* Do we have a main-class argument? */
1665
1666         if (mainname == NULL)
1667                 usage();
1668
1669 #if !defined(NDEBUG)
1670         if (opt_method != NULL) {
1671                 vm_compile_method(mainname);
1672                 return;
1673         }
1674 #endif
1675
1676         /* Build argument array. */
1677
1678         int32_t oalength = vm_args->nOptions - opt_index;
1679
1680         ObjectArray oa(oalength, class_java_lang_String);
1681
1682         if (oa.is_null())
1683                 vm_exit(1);
1684
1685         for (int i = 0; i < oalength; i++) {
1686                 char* option = vm_args->options[opt_index + i].optionString;
1687
1688                 utf*           u = utf_new_char(option);
1689                 java_handle_t* s = javastring_new(u);
1690
1691                 oa.set_element(i, s);
1692         }
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         utf* mainutf = utf_new_char(mainname);
1710
1711 #if defined(ENABLE_JAVAME_CLDC1_1)
1712         classinfo* mainclass = load_class_bootstrap(mainutf);
1713 #else
1714         classinfo* mainclass = load_class_from_sysloader(mainutf);
1715 #endif
1716
1717         /* error loading class */
1718
1719         java_handle_t* 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.get_handle());
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  */