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