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