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