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