* src/vm/vm.hpp (VM::start_runtime_agents): Added new helper method.
[cacao.git] / src / vm / vm.cpp
1 /* src/vm/vm.cpp - VM startup and shutdown functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include <exception>
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <stdlib.h>
35
36 #include "vm/types.h"
37
38 #include "arch.h"
39 #include "md-abi.h"
40
41 #include "vm/jit/abi-asm.h"
42
43 #include "mm/codememory.h"
44 #include "mm/dumpmemory.hpp"
45 #include "mm/gc.hpp"
46 #include "mm/memory.hpp"
47
48 #include "native/jni.hpp"
49 #include "native/llni.h"
50 #include "native/localref.hpp"
51 #include "native/native.hpp"
52
53 #include "native/vm/nativevm.hpp"
54
55 #include "threads/lock.hpp"
56 #include "threads/thread.hpp"
57
58 #include "toolbox/logging.hpp"
59
60 #include "vm/array.hpp"
61
62 #if defined(ENABLE_ASSERTION)
63 #include "vm/assertion.hpp"
64 #endif
65
66 #include "vm/jit/builtin.hpp"
67 #include "vm/classcache.hpp"
68 #include "vm/exceptions.hpp"
69 #include "vm/finalizer.hpp"
70 #include "vm/global.h"
71 #include "vm/globals.hpp"
72 #include "vm/initialize.hpp"
73 #include "vm/options.h"
74 #include "vm/os.hpp"
75 #include "vm/primitive.hpp"
76 #include "vm/properties.hpp"
77 #include "vm/signallocal.hpp"
78 #include "vm/statistics.h"
79 #include "vm/string.hpp"
80 #include "vm/suck.hpp"
81 #include "vm/vm.hpp"
82
83 #include "vm/jit/argument.hpp"
84 #include "vm/jit/asmpart.h"
85 #include "vm/jit/code.hpp"
86
87 #if defined(ENABLE_DISASSEMBLER)
88 # include "vm/jit/disass.h"
89 #endif
90
91 #include "vm/jit/jit.hpp"
92 #include "vm/jit/methodtree.h"
93
94 #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");
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         // Print the run-time VM configuration after all stuff is set and
1449         // the VM is initialized.
1450         if (opt_PrintConfig)
1451                 print_run_time_config();
1452
1453         // Start runtime agents after the VM is created.
1454         if (!start_runtime_agents())
1455                 os::abort("vm_create: start_runtime_agents failed");
1456 }
1457
1458
1459 /**
1460  * Print build-time (default) VM configuration.
1461  */
1462 void VM::print_build_time_config(void)
1463 {
1464         puts("CACAO "VERSION" configure/build options:");
1465         puts("");
1466         puts("  ./configure: "VERSION_CONFIGURE_ARGS"");
1467 #if defined(__VERSION__)
1468         puts("  CC         : "VERSION_CC" ("__VERSION__")");
1469         puts("  CXX        : "VERSION_CXX" ("__VERSION__")");
1470 #else
1471         puts("  CC         : "VERSION_CC"");
1472         puts("  CXX        : "VERSION_CXX"");
1473 #endif
1474         puts("  CFLAGS     : "VERSION_CFLAGS"");
1475         puts("  CXXFLAGS   : "VERSION_CXXFLAGS"");
1476
1477         puts("");
1478
1479         puts("Build-time (default) variables:\n");
1480         printf("  maximum heap size              : %d\n", HEAP_MAXSIZE);
1481         printf("  initial heap size              : %d\n", HEAP_STARTSIZE);
1482         printf("  stack size                     : %d\n", STACK_SIZE);
1483
1484 #if defined(ENABLE_JRE_LAYOUT)
1485         // When we're building with JRE-layout, the default paths are the
1486         // same as the runtime paths.
1487 #else
1488 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
1489         puts("  gnu.classpath.boot.library.path: "JAVA_RUNTIME_LIBRARY_LIBDIR);
1490         puts("  java.boot.class.path           : "CACAO_VM_ZIP":"JAVA_RUNTIME_LIBRARY_CLASSES"");
1491 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
1492         puts("  sun.boot.library.path          : "JAVA_RUNTIME_LIBRARY_LIBDIR);
1493         puts("  java.boot.class.path           : "JAVA_RUNTIME_LIBRARY_CLASSES);
1494 # endif
1495 #endif
1496
1497         puts("");
1498 }
1499
1500
1501 /**
1502  * Print run-time VM configuration.
1503  */
1504 void VM::print_run_time_config()
1505 {
1506         puts("Run-time variables:\n");
1507         printf("  maximum heap size              : %d\n", opt_heapmaxsize);
1508         printf("  initial heap size              : %d\n", opt_heapstartsize);
1509         printf("  stack size                     : %d\n", opt_stacksize);
1510
1511 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
1512         printf("  gnu.classpath.boot.library.path: %s\n", _properties.get("gnu.classpath.boot.library.path"));
1513 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
1514         printf("  sun.boot.library.path          : %s\n", _properties.get("sun.boot.library.path"));
1515 #endif
1516
1517         printf("  java.boot.class.path           : %s\n", _properties.get("java.boot.class.path"));
1518         printf("  java.class.path                : %s\n", _properties.get("java.class.path"));
1519
1520         puts("");
1521 }
1522
1523
1524 /**
1525  * Start runtime agents which are provided by the JRE but need to be
1526  * started explicitly by the VM.
1527  */
1528 bool VM::start_runtime_agents()
1529 {
1530 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
1531
1532         // Nothing to do.
1533
1534 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
1535
1536         // Check whether the management agent should be loaded.
1537         if ((_properties.get("com.sun.management.jmxremote") != NULL) ||
1538                 (_properties.get("com.sun.management.snmp") != NULL))
1539         {
1540
1541                 // Load the management agent class.
1542                 classinfo* class_sun_management_Agent;
1543                 if (!(class_sun_management_Agent = load_class_from_sysloader(utf_new_char("sun/management/Agent"))))
1544                         return false;
1545
1546                 // Link the management agent class.
1547                 if (!link_class(class_sun_management_Agent))
1548                         return false;
1549
1550                 // Actually start the management agent.
1551                 methodinfo* m = class_resolveclassmethod(class_sun_management_Agent,
1552                                                                                                  utf_new_char("startAgent"),
1553                                                                                                  utf_void__void,
1554                                                                                                  class_java_lang_Object,
1555                                                                                                  false);
1556
1557                 if (m == NULL)
1558                         return false;
1559
1560                 (void) vm_call_method(m, NULL);
1561
1562                 if (exceptions_get_exception() != NULL)
1563                         return false;
1564         }
1565
1566 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
1567
1568         // Nothing to do.
1569
1570 #else
1571 # error unknown classpath configuration
1572 #endif
1573
1574         return true;
1575 }
1576
1577
1578 /* vm_run **********************************************************************
1579
1580    Runs the main-method of the passed class.
1581
1582 *******************************************************************************/
1583
1584 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1585 {
1586         methodinfo* m;
1587         int         status;
1588
1589 #if !defined(NDEBUG)
1590         if (opt_CompileAll) {
1591                 vm_compile_all();
1592                 return;
1593         }
1594 #endif
1595
1596         /* Get the main class or jar file argument. */
1597
1598         char* mainname = NULL;
1599
1600         if (opt_index < vm_args->nOptions) {
1601                 /* Get main-class argument. */
1602
1603                 mainname = vm_args->options[opt_index].optionString;
1604
1605                 /* If the main class argument is a jar file, put it into the
1606                    classpath. */
1607
1608                 if (opt_jar == true) {
1609                         char* p = MNEW(char, strlen(mainname) + strlen("0"));
1610
1611                         strcpy(p, mainname);
1612
1613 #if defined(ENABLE_JAVASE)
1614                         VM::get_current()->get_properties().put("java.class.path", p);
1615 #endif
1616                 }
1617                 else {
1618                         /* Replace dots with slashes in the class name. */
1619
1620                         for (unsigned int i = 0; i < strlen(mainname); i++)
1621                                 if (mainname[i] == '.')
1622                                         mainname[i] = '/';
1623                 }
1624
1625                 /* Move index to first argument. */
1626
1627                 opt_index++;
1628         }
1629
1630         /* Do we have a main-class argument? */
1631
1632         if (mainname == NULL)
1633                 usage();
1634
1635 #if !defined(NDEBUG)
1636         if (opt_CompileMethod != NULL) {
1637                 vm_compile_method(mainname);
1638                 return;
1639         }
1640 #endif
1641
1642         /* Build argument array. */
1643
1644         int32_t oalength = vm_args->nOptions - opt_index;
1645
1646         ObjectArray oa(oalength, class_java_lang_String);
1647
1648         if (oa.is_null())
1649                 vm_exit(1);
1650
1651         for (int i = 0; i < oalength; i++) {
1652                 char* option = vm_args->options[opt_index + i].optionString;
1653
1654                 utf*           u = utf_new_char(option);
1655                 java_handle_t* s = javastring_new(u);
1656
1657                 oa.set_element(i, s);
1658         }
1659
1660         /* set return value to OK */
1661
1662         status = 0;
1663
1664         if (opt_jar == true) {
1665                 /* open jar file with java.util.jar.JarFile */
1666
1667                 mainname = vm_get_mainclass_from_jar(mainname);
1668
1669                 if (mainname == NULL)
1670                         vm_exit(1);
1671         }
1672
1673         /* load the main class */
1674
1675         utf* mainutf = utf_new_char(mainname);
1676
1677 #if defined(ENABLE_JAVAME_CLDC1_1)
1678         classinfo* mainclass = load_class_bootstrap(mainutf);
1679 #else
1680         classinfo* mainclass = load_class_from_sysloader(mainutf);
1681 #endif
1682
1683         /* error loading class */
1684
1685         java_handle_t* e = exceptions_get_and_clear_exception();
1686
1687         if ((e != NULL) || (mainclass == NULL)) {
1688                 exceptions_throw_noclassdeffounderror_cause(e);
1689                 exceptions_print_stacktrace(); 
1690                 vm_exit(1);
1691         }
1692
1693         if (!link_class(mainclass)) {
1694                 exceptions_print_stacktrace();
1695                 vm_exit(1);
1696         }
1697                         
1698         /* find the `main' method of the main class */
1699
1700         m = class_resolveclassmethod(mainclass,
1701                                                                  utf_new_char("main"), 
1702                                                                  utf_new_char("([Ljava/lang/String;)V"),
1703                                                                  class_java_lang_Object,
1704                                                                  false);
1705
1706         if (exceptions_get_exception()) {
1707                 exceptions_print_stacktrace();
1708                 vm_exit(1);
1709         }
1710
1711         /* there is no main method or it isn't static */
1712
1713         if ((m == NULL) || !(m->flags & ACC_STATIC)) {
1714                 exceptions_clear_exception();
1715                 exceptions_throw_nosuchmethoderror(mainclass,
1716                                                                                    utf_new_char("main"), 
1717                                                                                    utf_new_char("([Ljava/lang/String;)V"));
1718
1719                 exceptions_print_stacktrace();
1720                 vm_exit(1);
1721         }
1722
1723 #ifdef TYPEINFO_DEBUG_TEST
1724         /* test the typeinfo system */
1725         typeinfo_test();
1726 #endif
1727
1728         /* set ThreadMXBean variables */
1729
1730 //      _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
1731 //      _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
1732
1733 //      if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
1734 //              _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
1735 //              _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
1736 //                      _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
1737 #warning Move to C++
1738
1739         /* start the main thread */
1740
1741         (void) vm_call_method(m, NULL, oa.get_handle());
1742
1743         /* exception occurred? */
1744
1745         if (exceptions_get_exception()) {
1746                 exceptions_print_stacktrace();
1747                 status = 1;
1748         }
1749
1750 #if defined(ENABLE_THREADS)
1751     /* Detach the main thread so that it appears to have ended when
1752            the application's main method exits. */
1753
1754         if (!thread_detach_current_thread())
1755                 os::abort("vm_run: Could not detach main thread.");
1756 #endif
1757
1758         /* Destroy the JavaVM. */
1759
1760         (void) vm_destroy(vm);
1761
1762         /* And exit. */
1763
1764         vm_exit(status);
1765 }
1766
1767
1768 /* vm_destroy ******************************************************************
1769
1770    Unloads a Java VM and reclaims its resources.
1771
1772 *******************************************************************************/
1773
1774 int vm_destroy(JavaVM *vm)
1775 {
1776 #if defined(ENABLE_THREADS)
1777         /* Create a a trivial new Java waiter thread called
1778            "DestroyJavaVM". */
1779
1780         JavaVMAttachArgs args;
1781
1782         args.name  = (char*) "DestroyJavaVM";
1783         args.group = NULL;
1784
1785         if (!thread_attach_current_thread(&args, false))
1786                 return 1;
1787
1788         /* Wait until we are the last non-daemon thread. */
1789
1790         threads_join_all_threads();
1791 #endif
1792
1793         /* VM is gone. */
1794
1795 //      _created = false;
1796 #warning Move to C++
1797
1798         /* Everything is ok. */
1799
1800         return 0;
1801 }
1802
1803
1804 /* vm_exit *********************************************************************
1805
1806    Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1807
1808 *******************************************************************************/
1809
1810 void vm_exit(s4 status)
1811 {
1812         methodinfo *m;
1813
1814         /* signal that we are exiting */
1815
1816 //      _exiting = true;
1817 #warning Move to C++
1818
1819         assert(class_java_lang_System);
1820         assert(class_java_lang_System->state & CLASS_LOADED);
1821
1822 #if defined(ENABLE_JVMTI)
1823         if (jvmti || (dbgcom!=NULL)) {
1824                 jvmti_set_phase(JVMTI_PHASE_DEAD);
1825                 if (jvmti) jvmti_agentunload();
1826         }
1827 #endif
1828
1829         if (!link_class(class_java_lang_System)) {
1830                 exceptions_print_stacktrace();
1831                 exit(1);
1832         }
1833
1834         /* call java.lang.System.exit(I)V */
1835
1836         m = class_resolveclassmethod(class_java_lang_System,
1837                                                                  utf_new_char("exit"),
1838                                                                  utf_int__void,
1839                                                                  class_java_lang_Object,
1840                                                                  true);
1841         
1842         if (m == NULL) {
1843                 exceptions_print_stacktrace();
1844                 exit(1);
1845         }
1846
1847         /* call the exit function with passed exit status */
1848
1849         (void) vm_call_method(m, NULL, status);
1850
1851         /* If we had an exception, just ignore the exception and exit with
1852            the proper code. */
1853
1854         vm_shutdown(status);
1855 }
1856
1857
1858 /* vm_shutdown *****************************************************************
1859
1860    Terminates the system immediately without freeing memory explicitly
1861    (to be used only for abnormal termination).
1862         
1863 *******************************************************************************/
1864
1865 void vm_shutdown(s4 status)
1866 {
1867         if (opt_verbose 
1868 #if defined(ENABLE_STATISTICS)
1869                 || opt_getcompilingtime || opt_stat
1870 #endif
1871            ) 
1872         {
1873                 log_text("CACAO terminated by shutdown");
1874                 dolog("Exit status: %d\n", (s4) status);
1875
1876         }
1877
1878 #if defined(ENABLE_JVMTI)
1879         /* terminate cacaodbgserver */
1880         if (dbgcom!=NULL) {
1881                 mutex_lock(&dbgcomlock);
1882                 dbgcom->running=1;
1883                 mutex_unlock(&dbgcomlock);
1884                 jvmti_cacaodbgserver_quit();
1885         }       
1886 #endif
1887
1888         exit(status);
1889 }
1890
1891
1892 /* vm_exit_handler *************************************************************
1893
1894    The exit_handler function is called upon program termination.
1895
1896    ATTENTION: Don't free system resources here! Some threads may still
1897    be running as this is called from VMRuntime.exit(). The OS does the
1898    cleanup for us.
1899
1900 *******************************************************************************/
1901
1902 void vm_exit_handler(void)
1903 {
1904 #if !defined(NDEBUG)
1905         if (showmethods)
1906                 class_showmethods(mainclass);
1907
1908         if (showconstantpool)
1909                 class_showconstantpool(mainclass);
1910
1911         if (showutf)
1912                 utf_show();
1913
1914 # if defined(ENABLE_PROFILING)
1915         if (opt_prof)
1916                 profile_printstats();
1917 # endif
1918 #endif /* !defined(NDEBUG) */
1919
1920 #if defined(ENABLE_RT_TIMING)
1921         rt_timing_print_time_stats(stderr);
1922 #endif
1923
1924 #if defined(ENABLE_CYCLES_STATS)
1925         builtin_print_cycles_stats(stderr);
1926         stacktrace_print_cycles_stats(stderr);
1927 #endif
1928
1929         if (opt_verbose 
1930 #if defined(ENABLE_STATISTICS)
1931                 || opt_getcompilingtime || opt_stat
1932 #endif
1933            ) 
1934         {
1935                 log_text("CACAO terminated");
1936
1937 #if defined(ENABLE_STATISTICS)
1938                 if (opt_stat) {
1939                         print_stats();
1940 #ifdef TYPECHECK_STATISTICS
1941                         typecheck_print_statistics(get_logfile());
1942 #endif
1943                 }
1944
1945                 if (opt_getcompilingtime)
1946                         print_times();
1947 #endif /* defined(ENABLE_STATISTICS) */
1948         }
1949         /* vm_print_profile(stderr);*/
1950 }
1951
1952
1953 /* vm_abort_disassemble ********************************************************
1954
1955    Prints an error message, disassemble the given code range (if
1956    enabled) and aborts the VM.
1957
1958    IN:
1959        pc.......PC to disassemble
1960            count....number of instructions to disassemble
1961
1962 *******************************************************************************/
1963
1964 void vm_abort_disassemble(void *pc, int count, const char *text, ...)
1965 {
1966         va_list ap;
1967 #if defined(ENABLE_DISASSEMBLER)
1968         int     i;
1969 #endif
1970
1971         /* Print debug message. */
1972
1973         log_start();
1974
1975         va_start(ap, text);
1976         log_vprint(text, ap);
1977         va_end(ap);
1978
1979         log_finish();
1980
1981         /* Print the PC. */
1982
1983 #if SIZEOF_VOID_P == 8
1984         log_println("PC=0x%016lx", pc);
1985 #else
1986         log_println("PC=0x%08x", pc);
1987 #endif
1988
1989 #if defined(ENABLE_DISASSEMBLER)
1990         log_println("machine instructions at PC:");
1991
1992         /* Disassemble the given number of instructions. */
1993
1994         for (i = 0; i < count; i++)
1995                 // FIXME disassinstr should use void*.
1996                 pc = disassinstr((u1*) pc);
1997 #endif
1998
1999         os::abort("Aborting...");
2000 }
2001
2002
2003 /* vm_get_mainclass_from_jar ***************************************************
2004
2005    Gets the name of the main class from a JAR's manifest file.
2006
2007 *******************************************************************************/
2008
2009 static char *vm_get_mainclass_from_jar(char *mainname)
2010 {
2011         classinfo     *c;
2012         java_handle_t *o;
2013         methodinfo    *m;
2014         java_handle_t *s;
2015
2016 #if defined(ENABLE_JAVAME_CLDC1_1)
2017         c = load_class_bootstrap(utf_new_char("java/util/jar/JarFile"));
2018 #else
2019         c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
2020 #endif
2021
2022         if (c == NULL) {
2023                 exceptions_print_stacktrace();
2024                 return NULL;
2025         }
2026
2027         /* create JarFile object */
2028
2029         o = builtin_new(c);
2030
2031         if (o == NULL) {
2032                 exceptions_print_stacktrace();
2033                 return NULL;
2034         }
2035
2036         m = class_resolveclassmethod(c,
2037                                                                  utf_init, 
2038                                                                  utf_java_lang_String__void,
2039                                                                  class_java_lang_Object,
2040                                                                  true);
2041
2042         if (m == NULL) {
2043                 exceptions_print_stacktrace();
2044                 return NULL;
2045         }
2046
2047         s = javastring_new_from_ascii(mainname);
2048
2049         (void) vm_call_method(m, o, s);
2050
2051         if (exceptions_get_exception()) {
2052                 exceptions_print_stacktrace();
2053                 return NULL;
2054         }
2055
2056         /* get manifest object */
2057
2058         m = class_resolveclassmethod(c,
2059                                                                  utf_new_char("getManifest"), 
2060                                                                  utf_new_char("()Ljava/util/jar/Manifest;"),
2061                                                                  class_java_lang_Object,
2062                                                                  true);
2063
2064         if (m == NULL) {
2065                 exceptions_print_stacktrace();
2066                 return NULL;
2067         }
2068
2069         o = vm_call_method(m, o);
2070
2071         if (o == NULL) {
2072                 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainname);
2073                 return NULL;
2074         }
2075
2076
2077         /* get Main Attributes */
2078
2079         LLNI_class_get(o, c);
2080
2081         m = class_resolveclassmethod(c,
2082                                                                  utf_new_char("getMainAttributes"), 
2083                                                                  utf_new_char("()Ljava/util/jar/Attributes;"),
2084                                                                  class_java_lang_Object,
2085                                                                  true);
2086
2087         if (m == NULL) {
2088                 exceptions_print_stacktrace();
2089                 return NULL;
2090         }
2091
2092         o = vm_call_method(m, o);
2093
2094         if (o == NULL) {
2095                 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainname);
2096                 return NULL;
2097         }
2098
2099
2100         /* get property Main-Class */
2101
2102         LLNI_class_get(o, c);
2103
2104         m = class_resolveclassmethod(c,
2105                                                                  utf_new_char("getValue"), 
2106                                                                  utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
2107                                                                  class_java_lang_Object,
2108                                                                  true);
2109
2110         if (m == NULL) {
2111                 exceptions_print_stacktrace();
2112                 return NULL;
2113         }
2114
2115         s = javastring_new_from_ascii("Main-Class");
2116
2117         o = vm_call_method(m, o, s);
2118
2119         if (o == NULL) {
2120                 fprintf(stderr, "Failed to load Main-Class manifest attribute from\n");
2121                 fprintf(stderr, "%s\n", mainname);
2122                 return NULL;
2123         }
2124
2125         return javastring_tochar(o);
2126 }
2127
2128
2129 /* vm_compile_all **************************************************************
2130
2131    Compile all methods found in the bootclasspath.
2132
2133 *******************************************************************************/
2134
2135 #if !defined(NDEBUG)
2136 static void vm_compile_all(void)
2137 {
2138         classinfo              *c;
2139         methodinfo             *m;
2140         u4                      slot;
2141         classcache_name_entry  *nmen;
2142         classcache_class_entry *clsen;
2143         s4                      i;
2144
2145         /* create all classes found in the bootclasspath */
2146         /* XXX currently only works with zip/jar's */
2147
2148         loader_load_all_classes();
2149
2150         /* link all classes */
2151
2152         for (slot = 0; slot < hashtable_classcache.size; slot++) {
2153                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
2154
2155                 for (; nmen; nmen = nmen->hashlink) {
2156                         /* iterate over all class entries */
2157
2158                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
2159                                 c = clsen->classobj;
2160
2161                                 if (c == NULL)
2162                                         continue;
2163
2164                                 if (!(c->state & CLASS_LINKED)) {
2165                                         if (!link_class(c)) {
2166                                                 fprintf(stderr, "Error linking: ");
2167                                                 utf_fprint_printable_ascii_classname(stderr, c->name);
2168                                                 fprintf(stderr, "\n");
2169
2170                                                 /* print out exception and cause */
2171
2172                                                 exceptions_print_current_exception();
2173
2174                                                 /* goto next class */
2175
2176                                                 continue;
2177                                         }
2178                                 }
2179
2180                                 /* compile all class methods */
2181
2182                                 for (i = 0; i < c->methodscount; i++) {
2183                                         m = &(c->methods[i]);
2184
2185                                         if (m->jcode != NULL) {
2186                                                 if (!jit_compile(m)) {
2187                                                         fprintf(stderr, "Error compiling: ");
2188                                                         utf_fprint_printable_ascii_classname(stderr, c->name);
2189                                                         fprintf(stderr, ".");
2190                                                         utf_fprint_printable_ascii(stderr, m->name);
2191                                                         utf_fprint_printable_ascii(stderr, m->descriptor);
2192                                                         fprintf(stderr, "\n");
2193
2194                                                         /* print out exception and cause */
2195
2196                                                         exceptions_print_current_exception();
2197                                                 }
2198                                         }
2199                                 }
2200                         }
2201                 }
2202         }
2203 }
2204 #endif /* !defined(NDEBUG) */
2205
2206
2207 /* vm_compile_method ***********************************************************
2208
2209    Compile a specific method.
2210
2211 *******************************************************************************/
2212
2213 #if !defined(NDEBUG)
2214 static void vm_compile_method(char* mainname)
2215 {
2216         methodinfo *m;
2217
2218         /* create, load and link the main class */
2219
2220         mainclass = load_class_bootstrap(utf_new_char(mainname));
2221
2222         if (mainclass == NULL)
2223                 exceptions_print_stacktrace();
2224
2225         if (!link_class(mainclass))
2226                 exceptions_print_stacktrace();
2227
2228         if (opt_CompileSignature != NULL) {
2229                 m = class_resolveclassmethod(mainclass,
2230                                                                          utf_new_char(opt_CompileMethod),
2231                                                                          utf_new_char(opt_CompileSignature),
2232                                                                          mainclass,
2233                                                                          false);
2234         }
2235         else {
2236                 m = class_resolveclassmethod(mainclass,
2237                                                                          utf_new_char(opt_CompileMethod),
2238                                                                          NULL,
2239                                                                          mainclass,
2240                                                                          false);
2241         }
2242
2243         if (m == NULL)
2244                 os::abort("vm_compile_method: java.lang.NoSuchMethodException: %s.%s",
2245                                  opt_CompileMethod, opt_CompileSignature ? opt_CompileSignature : "");
2246                 
2247         jit_compile(m);
2248 }
2249 #endif /* !defined(NDEBUG) */
2250
2251
2252 /* vm_call_array ***************************************************************
2253
2254    Calls a Java method with a variable number of arguments, passed via
2255    an argument array.
2256
2257    ATTENTION: This function has to be used outside the nativeworld.
2258
2259 *******************************************************************************/
2260
2261 #define VM_CALL_ARRAY(name, type)                                 \
2262 static type vm_call##name##_array(methodinfo *m, uint64_t *array) \
2263 {                                                                 \
2264         methoddesc *md;                                               \
2265         void       *pv;                                               \
2266         type        value;                                            \
2267                                                                   \
2268         assert(m->code != NULL);                                      \
2269                                                                   \
2270         md = m->parseddesc;                                           \
2271         pv = m->code->entrypoint;                                     \
2272                                                                   \
2273         STATISTICS(count_calls_native_to_java++);                     \
2274                                                                   \
2275         value = asm_vm_call_method##name(pv, array, md->memuse);      \
2276                                                                   \
2277         return value;                                                 \
2278 }
2279
2280 static java_handle_t *vm_call_array(methodinfo *m, uint64_t *array)
2281 {
2282         methoddesc    *md;
2283         void          *pv;
2284         java_object_t *o;
2285
2286         assert(m->code != NULL);
2287
2288         md = m->parseddesc;
2289         pv = m->code->entrypoint;
2290
2291         STATISTICS(count_calls_native_to_java++);
2292
2293         o = asm_vm_call_method(pv, array, md->memuse);
2294
2295         if (md->returntype.type == TYPE_VOID)
2296                 o = NULL;
2297
2298         return LLNI_WRAP(o);
2299 }
2300
2301 VM_CALL_ARRAY(_int,    int32_t)
2302 VM_CALL_ARRAY(_long,   int64_t)
2303 VM_CALL_ARRAY(_float,  float)
2304 VM_CALL_ARRAY(_double, double)
2305
2306
2307 /* vm_call_method **************************************************************
2308
2309    Calls a Java method with a variable number of arguments.
2310
2311 *******************************************************************************/
2312
2313 #define VM_CALL_METHOD(name, type)                                  \
2314 type vm_call_method##name(methodinfo *m, java_handle_t *o, ...)     \
2315 {                                                                   \
2316         va_list ap;                                                     \
2317         type    value;                                                  \
2318                                                                     \
2319         va_start(ap, o);                                                \
2320         value = vm_call_method##name##_valist(m, o, ap);                \
2321         va_end(ap);                                                     \
2322                                                                     \
2323         return value;                                                   \
2324 }
2325
2326 VM_CALL_METHOD(,        java_handle_t *)
2327 VM_CALL_METHOD(_int,    int32_t)
2328 VM_CALL_METHOD(_long,   int64_t)
2329 VM_CALL_METHOD(_float,  float)
2330 VM_CALL_METHOD(_double, double)
2331
2332
2333 /* vm_call_method_valist *******************************************************
2334
2335    Calls a Java method with a variable number of arguments, passed via
2336    a va_list.
2337
2338 *******************************************************************************/
2339
2340 #define VM_CALL_METHOD_VALIST(name, type)                               \
2341 type vm_call_method##name##_valist(methodinfo *m, java_handle_t *o,     \
2342                                                                    va_list ap)                          \
2343 {                                                                       \
2344         uint64_t *array;                                                    \
2345         type      value;                                                    \
2346                                                                         \
2347         if (m->code == NULL)                                                \
2348                 if (!jit_compile(m))                                            \
2349                         return 0;                                                   \
2350                                                                         \
2351         THREAD_NATIVEWORLD_EXIT;                                            \
2352                                                                                                                                                 \
2353         DumpMemoryArea dma;                                                                                                     \
2354                                                                         \
2355         array = argument_vmarray_from_valist(m, o, ap);                     \
2356         value = vm_call##name##_array(m, array);                            \
2357                                                                         \
2358         THREAD_NATIVEWORLD_ENTER;                                           \
2359                                                                         \
2360         return value;                                                       \
2361 }
2362
2363 VM_CALL_METHOD_VALIST(,        java_handle_t *)
2364 VM_CALL_METHOD_VALIST(_int,    int32_t)
2365 VM_CALL_METHOD_VALIST(_long,   int64_t)
2366 VM_CALL_METHOD_VALIST(_float,  float)
2367 VM_CALL_METHOD_VALIST(_double, double)
2368
2369
2370 /* vm_call_method_jvalue *******************************************************
2371
2372    Calls a Java method with a variable number of arguments, passed via
2373    a jvalue array.
2374
2375 *******************************************************************************/
2376
2377 #define VM_CALL_METHOD_JVALUE(name, type)                               \
2378 type vm_call_method##name##_jvalue(methodinfo *m, java_handle_t *o,     \
2379                                                            const jvalue *args)                  \
2380 {                                                                       \
2381         uint64_t *array;                                                    \
2382         type      value;                                                    \
2383                                                                         \
2384         if (m->code == NULL)                                                \
2385                 if (!jit_compile(m))                                            \
2386                         return 0;                                                   \
2387                                                                         \
2388         THREAD_NATIVEWORLD_EXIT;                                            \
2389                                                                                                                                                 \
2390         DumpMemoryArea dma;                                                                                                     \
2391                                                                         \
2392         array = argument_vmarray_from_jvalue(m, o, args);                   \
2393         value = vm_call##name##_array(m, array);                            \
2394                                                                         \
2395         THREAD_NATIVEWORLD_ENTER;                                           \
2396                                                                         \
2397         return value;                                                       \
2398 }
2399
2400 VM_CALL_METHOD_JVALUE(,        java_handle_t *)
2401 VM_CALL_METHOD_JVALUE(_int,    int32_t)
2402 VM_CALL_METHOD_JVALUE(_long,   int64_t)
2403 VM_CALL_METHOD_JVALUE(_float,  float)
2404 VM_CALL_METHOD_JVALUE(_double, double)
2405
2406
2407 /* vm_call_method_objectarray **************************************************
2408
2409    Calls a Java method with a variable number if arguments, passed via
2410    an objectarray of boxed values. Returns a boxed value.
2411
2412 *******************************************************************************/
2413
2414 java_handle_t *vm_call_method_objectarray(methodinfo *m, java_handle_t *o,
2415                                                                                   java_handle_objectarray_t *params)
2416 {
2417         uint64_t      *array;
2418         java_handle_t *xptr;
2419         java_handle_t *ro;
2420         imm_union      value;
2421
2422         /* Prevent compiler warnings. */
2423
2424         ro = NULL;
2425
2426         /* compile methods which are not yet compiled */
2427
2428         if (m->code == NULL)
2429                 if (!jit_compile(m))
2430                         return NULL;
2431
2432         /* leave the nativeworld */
2433
2434         THREAD_NATIVEWORLD_EXIT;
2435
2436         // Create new dump memory area.
2437         DumpMemoryArea dma;
2438
2439         /* Fill the argument array from a object-array. */
2440
2441         array = argument_vmarray_from_objectarray(m, o, params);
2442
2443         if (array == NULL) {
2444                 /* enter the nativeworld again */
2445
2446                 THREAD_NATIVEWORLD_ENTER;
2447
2448                 exceptions_throw_illegalargumentexception();
2449
2450                 return NULL;
2451         }
2452
2453         switch (m->parseddesc->returntype.primitivetype) {
2454         case PRIMITIVETYPE_VOID:
2455                 value.a = vm_call_array(m, array);
2456                 break;
2457
2458         case PRIMITIVETYPE_BOOLEAN:
2459         case PRIMITIVETYPE_BYTE:
2460         case PRIMITIVETYPE_CHAR:
2461         case PRIMITIVETYPE_SHORT:
2462         case PRIMITIVETYPE_INT:
2463                 value.i = vm_call_int_array(m, array);
2464                 break;
2465
2466         case PRIMITIVETYPE_LONG:
2467                 value.l = vm_call_long_array(m, array);
2468                 break;
2469
2470         case PRIMITIVETYPE_FLOAT:
2471                 value.f = vm_call_float_array(m, array);
2472                 break;
2473
2474         case PRIMITIVETYPE_DOUBLE:
2475                 value.d = vm_call_double_array(m, array);
2476                 break;
2477
2478         case TYPE_ADR:
2479                 ro = vm_call_array(m, array);
2480                 break;
2481
2482         default:
2483                 os::abort("vm_call_method_objectarray: invalid return type %d", m->parseddesc->returntype.primitivetype);
2484         }
2485
2486         /* enter the nativeworld again */
2487
2488         THREAD_NATIVEWORLD_ENTER;
2489
2490         /* box the return value if necesarry */
2491
2492         if (m->parseddesc->returntype.primitivetype != TYPE_ADR)
2493                 ro = Primitive::box(m->parseddesc->returntype.primitivetype, value);
2494
2495         /* check for an exception */
2496
2497         xptr = exceptions_get_exception();
2498
2499         if (xptr != NULL) {
2500                 /* clear exception pointer, we are calling JIT code again */
2501
2502                 exceptions_clear_exception();
2503
2504                 exceptions_throw_invocationtargetexception(xptr);
2505         }
2506
2507         return ro;
2508 }
2509
2510
2511 /* Legacy C interface *********************************************************/
2512
2513 extern "C" {
2514
2515 JavaVM* VM_get_javavm()      { return VM::get_current()->get_javavm(); }
2516 JNIEnv* VM_get_jnienv()      { return VM::get_current()->get_jnienv(); }
2517 bool    VM_is_initializing() { return VM::get_current()->is_initializing(); }
2518 bool    VM_is_created()      { return VM::get_current()->is_created(); }
2519 int64_t VM_get_starttime()   { return VM::get_current()->get_starttime(); }
2520
2521 void vm_abort(const char* text, ...)
2522 {
2523         va_list ap;
2524
2525         log_println("vm_abort: WARNING, port me to C++ and use os::abort() instead.");
2526
2527         // Print the log message.
2528         log_start();
2529
2530         va_start(ap, text);
2531         log_vprint(text, ap);
2532         va_end(ap);
2533
2534         log_finish();
2535
2536         // Print a backtrace.
2537         os::print_backtrace();
2538
2539         // Now abort the VM.
2540         os::abort();
2541 }
2542
2543 }
2544
2545
2546 /*
2547  * These are local overrides for various environment variables in Emacs.
2548  * Please do not remove this and leave it at the end of the file, where
2549  * Emacs will automagically detect them.
2550  * ---------------------------------------------------------------------
2551  * Local variables:
2552  * mode: c++
2553  * indent-tabs-mode: t
2554  * c-basic-offset: 4
2555  * tab-width: 4
2556  * End:
2557  * vim:noexpandtab:sw=4:ts=4:
2558  */