a1c1b3d5083983c295c451a31a8d8cb4ca58ab0a
[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    $Id: vm.c 8239 2007-07-29 19:21:18Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36
37 #if defined(WITH_JRE_LAYOUT)
38 # include <libgen.h>
39 # include <unistd.h>
40 #endif
41
42 #include "vm/types.h"
43
44 #include "arch.h"
45 #include "md-abi.h"
46
47 #include "vm/jit/abi-asm.h"
48
49 #include "mm/gc-common.h"
50 #include "mm/memory.h"
51
52 #include "native/jni.h"
53 #include "native/native.h"
54
55 #include "native/include/java_lang_Object.h"             /* required by j.l.C */
56 #include "native/include/java_lang_String.h"             /* required by j.l.C */
57
58 #if defined(WITH_CLASSPATH_SUN)
59 # include "native/include/java_nio_ByteBuffer.h"        /* required by j.l.CL */
60 # include "native/include/java_lang_ClassLoader.h"       /* required by j.l.C */
61 #endif
62
63 #include "native/include/java_lang_Class.h"
64
65 #include "native/include/java_lang_Byte.h"
66 #include "native/include/java_lang_Character.h"
67 #include "native/include/java_lang_Short.h"
68 #include "native/include/java_lang_Integer.h"
69 #include "native/include/java_lang_Boolean.h"
70 #include "native/include/java_lang_Long.h"
71 #include "native/include/java_lang_Float.h"
72 #include "native/include/java_lang_Double.h"
73
74 #include "native/vm/nativevm.h"
75
76 #include "threads/threads-common.h"
77
78 #include "toolbox/logging.h"
79
80 #include "vm/builtin.h"
81 #include "vm/exceptions.h"
82 #include "vm/finalizer.h"
83 #include "vm/global.h"
84 #include "vm/initialize.h"
85 #include "vm/properties.h"
86 #include "vm/signallocal.h"
87 #include "vm/stringlocal.h"
88 #include "vm/vm.h"
89
90 #include "vm/jit/jit.h"
91 #include "vm/jit/md.h"
92 #include "vm/jit/asmpart.h"
93
94 #if defined(ENABLE_PROFILING)
95 # include "vm/jit/optimizing/profile.h"
96 #endif
97
98 #include "vm/jit/optimizing/recompile.h"
99
100 #include "vmcore/classcache.h"
101 #include "vmcore/options.h"
102 #include "vmcore/primitive.h"
103 #include "vmcore/statistics.h"
104 #include "vmcore/suck.h"
105
106 #if defined(ENABLE_JVMTI)
107 # include "native/jvmti/cacaodbg.h"
108 #endif
109
110 #if defined(ENABLE_VMLOG)
111 #include <vmlog_cacao.h>
112 #endif
113
114
115 /* Invocation API variables ***************************************************/
116
117 _Jv_JavaVM *_Jv_jvm;                    /* denotes a Java VM                  */
118 _Jv_JNIEnv *_Jv_env;                    /* pointer to native method interface */
119
120
121 /* global variables ***********************************************************/
122
123 s4 vms = 0;                             /* number of VMs created              */
124
125 bool vm_initializing = false;
126 bool vm_exiting = false;
127
128 char      *cacao_prefix = NULL;
129 char      *cacao_libjvm = NULL;
130 char      *classpath_libdir = NULL;
131
132 char      *_Jv_bootclasspath;           /* contains the boot classpath        */
133 char      *_Jv_classpath;               /* contains the classpath             */
134 char      *_Jv_java_library_path;
135
136 char      *mainstring = NULL;
137 classinfo *mainclass = NULL;
138
139 char *specificmethodname = NULL;
140 char *specificsignature = NULL;
141
142 bool startit = true;
143
144 #if defined(ENABLE_INTRP)
145 u1 *intrp_main_stack = NULL;
146 #endif
147
148
149 /* define heap sizes **********************************************************/
150
151 #define HEAP_MAXSIZE      128 * 1024 * 1024 /* default 128MB                  */
152 #define HEAP_STARTSIZE      2 * 1024 * 1024 /* default 2MB                    */
153 #define STACK_SIZE                64 * 1024 /* default 64kB                   */
154
155
156 /* define command line options ************************************************/
157
158 enum {
159         OPT_FOO,
160
161         /* Java options */
162
163         OPT_JAR,
164
165         OPT_D32,
166         OPT_D64,
167
168         OPT_CLASSPATH,
169         OPT_D,
170
171         OPT_VERBOSE,
172
173         OPT_VERSION,
174         OPT_SHOWVERSION,
175         OPT_FULLVERSION,
176
177         OPT_HELP,
178         OPT_X,
179         OPT_XX,
180
181         OPT_EA,
182         OPT_DA,
183
184         OPT_ESA,
185         OPT_DSA,
186
187         /* Java non-standard options */
188
189         OPT_JIT,
190         OPT_INTRP,
191
192         OPT_BOOTCLASSPATH,
193         OPT_BOOTCLASSPATH_A,
194         OPT_BOOTCLASSPATH_P,
195
196         OPT_BOOTCLASSPATH_C,
197
198 #if defined(ENABLE_PROFILING)
199         OPT_PROF,
200         OPT_PROF_OPTION,
201 #endif
202
203         OPT_MS,
204         OPT_MX,
205
206         /* CACAO options */
207
208         OPT_VERBOSE1,
209         OPT_NOIEEE,
210
211 #if defined(ENABLE_STATISTICS)
212         OPT_TIME,
213         OPT_STAT,
214 #endif
215
216         OPT_LOG,
217         OPT_CHECK,
218         OPT_LOAD,
219         OPT_SHOW,
220         OPT_DEBUGCOLOR,
221
222 #if !defined(NDEBUG)
223         OPT_ALL,
224         OPT_METHOD,
225         OPT_SIGNATURE,
226 #endif
227
228 #if defined(ENABLE_VERIFIER)
229         OPT_NOVERIFY,
230 #if defined(TYPECHECK_VERBOSE)
231         OPT_VERBOSETC,
232 #endif
233 #endif /* defined(ENABLE_VERIFIER) */
234
235         /* optimization options */
236
237 #if defined(ENABLE_LOOP)
238         OPT_OLOOP,
239 #endif
240         
241 #if defined(ENABLE_IFCONV)
242         OPT_IFCONV,
243 #endif
244
245 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
246         OPT_LSRA,
247 #endif
248
249 #if defined(ENABLE_INLINING)
250         OPT_INLINING,
251 #if !defined(NDEBUG)
252         OPT_INLINE_LOG,
253 #endif
254 #if defined(ENABLE_INLINING_DEBUG)
255         OPT_INLINE_DEBUG_ALL,
256         OPT_INLINE_DEBUG_END,
257         OPT_INLINE_DEBUG_MIN,
258         OPT_INLINE_DEBUG_MAX,
259 #endif /* defined(ENABLE_INLINING_DEBUG) */
260 #endif /* defined(ENABLE_INLINING) */
261
262 #if defined(ENABLE_INTRP)
263         /* interpreter options */
264
265         OPT_NO_DYNAMIC,
266         OPT_NO_REPLICATION,
267         OPT_NO_QUICKSUPER,
268         OPT_STATIC_SUPERS,
269         OPT_TRACE,
270 #endif
271
272         OPT_SS,
273
274 #ifdef ENABLE_JVMTI
275         OPT_DEBUG,
276         OPT_XRUNJDWP,
277         OPT_NOAGENT,
278         OPT_AGENTLIB,
279         OPT_AGENTPATH,
280 #endif
281
282 #if defined(ENABLE_DEBUG_FILTER)
283         OPT_FILTER_VERBOSECALL_INCLUDE,
284         OPT_FILTER_VERBOSECALL_EXCLUDE,
285         OPT_FILTER_SHOW_METHOD,
286 #endif
287
288         DUMMY
289 };
290
291
292 opt_struct opts[] = {
293         { "foo",               false, OPT_FOO },
294
295         /* Java options */
296
297         { "jar",               false, OPT_JAR },
298
299         { "d32",               false, OPT_D32 },
300         { "d64",               false, OPT_D64 },
301         { "client",            false, OPT_IGNORE },
302         { "server",            false, OPT_IGNORE },
303         { "jvm",               false, OPT_IGNORE },
304         { "hotspot",           false, OPT_IGNORE },
305
306         { "classpath",         true,  OPT_CLASSPATH },
307         { "cp",                true,  OPT_CLASSPATH },
308         { "D",                 true,  OPT_D },
309         { "version",           false, OPT_VERSION },
310         { "showversion",       false, OPT_SHOWVERSION },
311         { "fullversion",       false, OPT_FULLVERSION },
312         { "help",              false, OPT_HELP },
313         { "?",                 false, OPT_HELP },
314         { "X",                 false, OPT_X },
315         { "XX:",               true,  OPT_XX },
316         { "XX",                false, OPT_XX },
317
318         { "ea:",               true,  OPT_EA },
319         { "da:",               true,  OPT_DA },
320         { "ea",                false, OPT_EA },
321         { "da",                false, OPT_DA },
322
323         { "esa",                     false, OPT_ESA },
324         { "enablesystemassertions",  false, OPT_ESA },
325         { "dsa",                     false, OPT_DSA },
326         { "disablesystemassertions", false, OPT_DSA },
327
328         { "noasyncgc",         false, OPT_IGNORE },
329 #if defined(ENABLE_VERIFIER)
330         { "noverify",          false, OPT_NOVERIFY },
331 #endif
332         { "v",                 false, OPT_VERBOSE1 },
333         { "verbose:",          true,  OPT_VERBOSE },
334
335 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
336         { "verbosetc",         false, OPT_VERBOSETC },
337 #endif
338 #if defined(__ALPHA__)
339         { "noieee",            false, OPT_NOIEEE },
340 #endif
341 #if defined(ENABLE_STATISTICS)
342         { "time",              false, OPT_TIME },
343         { "stat",              false, OPT_STAT },
344 #endif
345         { "log",               true,  OPT_LOG },
346         { "c",                 true,  OPT_CHECK },
347         { "l",                 false, OPT_LOAD },
348
349 #if !defined(NDEBUG)
350         { "all",               false, OPT_ALL },
351         { "sig",               true,  OPT_SIGNATURE },
352 #endif
353
354 #if defined(ENABLE_LOOP)
355         { "oloop",             false, OPT_OLOOP },
356 #endif
357 #if defined(ENABLE_IFCONV)
358         { "ifconv",            false, OPT_IFCONV },
359 #endif
360 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
361         { "lsra",              false, OPT_LSRA },
362 #endif
363
364 #if defined(ENABLE_INTRP)
365         /* interpreter options */
366
367         { "trace",             false, OPT_TRACE },
368         { "static-supers",     true,  OPT_STATIC_SUPERS },
369         { "no-dynamic",        false, OPT_NO_DYNAMIC },
370         { "no-replication",    false, OPT_NO_REPLICATION },
371         { "no-quicksuper",     false, OPT_NO_QUICKSUPER },
372 #endif
373
374         /* JVMTI Agent Command Line Options */
375 #ifdef ENABLE_JVMTI
376         { "agentlib:",         true,  OPT_AGENTLIB },
377         { "agentpath:",        true,  OPT_AGENTPATH },
378 #endif
379
380         /* Java non-standard options */
381
382         { "Xjit",              false, OPT_JIT },
383         { "Xint",              false, OPT_INTRP },
384         { "Xbootclasspath:",   true,  OPT_BOOTCLASSPATH },
385         { "Xbootclasspath/a:", true,  OPT_BOOTCLASSPATH_A },
386         { "Xbootclasspath/p:", true,  OPT_BOOTCLASSPATH_P },
387         { "Xbootclasspath/c:", true,  OPT_BOOTCLASSPATH_C },
388
389 #ifdef ENABLE_JVMTI
390         { "Xdebug",            false, OPT_DEBUG },
391         { "Xnoagent",          false, OPT_NOAGENT },
392         { "Xrunjdwp",          true,  OPT_XRUNJDWP },
393 #endif 
394
395         { "Xms",               true,  OPT_MS },
396         { "ms",                true,  OPT_MS },
397         { "Xmx",               true,  OPT_MX },
398         { "mx",                true,  OPT_MX },
399         { "Xss",               true,  OPT_SS },
400         { "ss",                true,  OPT_SS },
401
402 #if defined(ENABLE_PROFILING)
403         { "Xprof:",            true,  OPT_PROF_OPTION },
404         { "Xprof",             false, OPT_PROF },
405 #endif
406
407         /* inlining options */
408
409 #if defined(ENABLE_INLINING)
410 #if defined(ENABLE_INLINING_DEBUG)
411         { "ia",                false, OPT_INLINE_DEBUG_ALL },
412         { "ii",                true,  OPT_INLINE_DEBUG_MIN },
413         { "im",                true,  OPT_INLINE_DEBUG_MAX },
414         { "ie",                true,  OPT_INLINE_DEBUG_END },
415 #endif /* defined(ENABLE_INLINING_DEBUG) */
416 #if !defined(NDEBUG)
417         { "il",                false, OPT_INLINE_LOG },
418 #endif
419         { "i",                 false, OPT_INLINING },
420 #endif /* defined(ENABLE_INLINING) */
421
422         /* keep these at the end of the list */
423
424 #if !defined(NDEBUG)
425         { "m",                 true,  OPT_METHOD },
426 #endif
427
428         { "s",                 true,  OPT_SHOW },
429         { "debug-color",      false,  OPT_DEBUGCOLOR },
430
431 #if defined(ENABLE_DEBUG_FILTER)
432         { "XXfi",              true,  OPT_FILTER_VERBOSECALL_INCLUDE },
433         { "XXfx",              true,  OPT_FILTER_VERBOSECALL_EXCLUDE },
434         { "XXfm",              true,  OPT_FILTER_SHOW_METHOD },
435 #endif
436
437         { NULL,                false, 0 }
438 };
439
440
441 /* usage ***********************************************************************
442
443    Prints the correct usage syntax to stdout.
444
445 *******************************************************************************/
446
447 void usage(void)
448 {
449         puts("Usage: cacao [-options] classname [arguments]");
450         puts("               (to run a class file)");
451         puts("   or  cacao [-options] -jar jarfile [arguments]");
452         puts("               (to run a standalone jar file)\n");
453
454         puts("where options include:");
455         puts("    -d32                     use 32-bit data model if available");
456         puts("    -d64                     use 64-bit data model if available");
457         puts("    -client                  compatibility (currently ignored)");
458         puts("    -server                  compatibility (currently ignored)");
459         puts("    -jvm                     compatibility (currently ignored)");
460         puts("    -hotspot                 compatibility (currently ignored)\n");
461
462         puts("    -cp <path>               specify a path to look for classes");
463         puts("    -classpath <path>        specify a path to look for classes");
464         puts("    -D<name>=<value>         add an entry to the property list");
465         puts("    -verbose[:class|gc|jni]  enable specific verbose output");
466         puts("    -version                 print product version and exit");
467         puts("    -fullversion             print jpackage-compatible product version and exit");
468         puts("    -showversion             print product version and continue");
469         puts("    -help, -?                print this help message");
470         puts("    -X                       print help on non-standard Java options");
471         puts("    -XX                      print help on debugging options");
472     puts("    -ea[:<packagename>...|:<classname>]");
473     puts("    -enableassertions[:<packagename>...|:<classname>]");
474         puts("                             enable assertions with specified granularity");
475         puts("    -da[:<packagename>...|:<classname>]");
476         puts("    -disableassertions[:<packagename>...|:<classname>]");
477         puts("                             disable assertions with specified granularity");
478         puts("    -esa | -enablesystemassertions");
479         puts("                             enable system assertions");
480         puts("    -dsa | -disablesystemassertions");
481         puts("                             disable system assertions");
482
483 #ifdef ENABLE_JVMTI
484         puts("    -agentlib:<agent-lib-name>=<options>  library to load containg JVMTI agent");
485         puts ("                                         for jdwp help use: -agentlib:jdwp=help");
486         puts("    -agentpath:<path-to-agent>=<options>  path to library containg JVMTI agent");
487 #endif
488
489         /* exit with error code */
490
491         exit(1);
492 }   
493
494
495 static void Xusage(void)
496 {
497 #if defined(ENABLE_JIT)
498         puts("    -Xjit                    JIT mode execution (default)");
499 #endif
500 #if defined(ENABLE_INTRP)
501         puts("    -Xint                    interpreter mode execution");
502 #endif
503         puts("    -Xbootclasspath:<zip/jar files and directories separated by :>");
504     puts("                             value is set as bootstrap class path");
505         puts("    -Xbootclasspath/a:<zip/jar files and directories separated by :>");
506         puts("                             value is appended to the bootstrap class path");
507         puts("    -Xbootclasspath/p:<zip/jar files and directories separated by :>");
508         puts("                             value is prepended to the bootstrap class path");
509         puts("    -Xbootclasspath/c:<zip/jar files and directories separated by :>");
510         puts("                             value is used as Java core library, but the");
511         puts("                             hardcoded VM interface classes are prepended");
512         printf("    -Xms<size>               set the initial size of the heap (default: %dMB)\n", HEAP_STARTSIZE / 1024 / 1024);
513         printf("    -Xmx<size>               set the maximum size of the heap (default: %dMB)\n", HEAP_MAXSIZE / 1024 / 1024);
514         printf("    -Xss<size>               set the thread stack size (default: %dkB)\n", STACK_SIZE / 1024);
515
516 #if defined(ENABLE_PROFILING)
517         puts("    -Xprof[:bb]              collect and print profiling data");
518 #endif
519
520 #if defined(ENABLE_JVMTI)
521     /* -Xdebug option depend on gnu classpath JDWP options. options: 
522          transport=dt_socket,address=<hostname:port>,server=(y|n),suspend(y|n) */
523         puts("    -Xdebug                  enable remote debugging\n");
524         puts("    -Xrunjdwp transport=[dt_socket|...],address=<hostname:port>,server=[y|n],suspend=[y|n]\n");
525         puts("                             enable remote debugging\n");
526 #endif 
527
528         /* exit with error code */
529
530         exit(1);
531 }   
532
533
534 static void XXusage(void)
535 {
536         puts("    -v                       write state-information");
537 #if !defined(NDEBUG)
538         puts("    -verbose[:jit|threads]");
539         puts("                             enable specific verbose output");
540         puts("    -debug-color             colored output for ANSI terms");
541 #endif
542 #ifdef TYPECHECK_VERBOSE
543         puts("    -verbosetc               write debug messages while typechecking");
544 #endif
545 #if defined(__ALPHA__)
546         puts("    -noieee                  don't use ieee compliant arithmetic");
547 #endif
548 #if defined(ENABLE_VERIFIER)
549         puts("    -noverify                don't verify classfiles");
550 #endif
551 #if defined(ENABLE_STATISTICS)
552         puts("    -time                    measure the runtime");
553         puts("    -stat                    detailed compiler statistics");
554 #endif
555         puts("    -log logfile             specify a name for the logfile");
556         puts("    -c(heck)b(ounds)         don't check array bounds");
557         puts("            s(ync)           don't check for synchronization");
558 #if defined(ENABLE_LOOP)
559         puts("    -oloop                   optimize array accesses in loops");
560 #endif
561         puts("    -l                       don't start the class after loading");
562 #if !defined(NDEBUG)
563         puts("    -all                     compile all methods, no execution");
564         puts("    -m                       compile only a specific method");
565         puts("    -sig                     specify signature for a specific method");
566 #endif
567
568         puts("    -s...                    show...");
569         puts("      (c)onstants            the constant pool");
570         puts("      (m)ethods              class fields and methods");
571         puts("      (u)tf                  the utf - hash");
572         puts("      (i)ntermediate         intermediate representation");
573 #if defined(ENABLE_DISASSEMBLER)
574         puts("      (a)ssembler            disassembled listing");
575         puts("      n(o)ps                 show NOPs in disassembler output");
576         puts("      (e)xceptionstubs       disassembled exception stubs (only with -sa)");
577         puts("      (n)ative               disassembled native stubs");
578 #endif
579         puts("      (d)atasegment          data segment listing");
580
581 #if defined(ENABLE_INLINING)
582         puts("    -i                       activate inlining");
583 #if !defined(NDEBUG)
584         puts("    -il                      log inlining");
585 #endif
586 #if defined(ENABLE_INLINING_DEBUG)
587         puts("    -ia                      use inlining for all methods");
588         puts("    -ii <size>               set minimum size for inlined result");
589         puts("    -im <size>               set maximum size for inlined result");
590         puts("    -ie <number>             stop inlining after the given number of roots");
591 #endif /* defined(ENABLE_INLINING_DEBUG) */
592 #endif /* defined(ENABLE_INLINING) */
593
594 #if defined(ENABLE_IFCONV)
595         puts("    -ifconv                  use if-conversion");
596 #endif
597 #if defined(ENABLE_LSRA)
598         puts("    -lsra                    use linear scan register allocation");
599 #endif
600 #if defined(ENABLE_SSA)
601         puts("    -lsra                    use linear scan register allocation (with SSA)");
602 #endif
603 #if defined(ENABLE_DEBUG_FILTER)
604         puts("    -XXfi <regex>            begin of dynamic scope for verbosecall filter");
605         puts("    -XXfx <regex>            end of dynamic scope for verbosecall filter");
606         puts("    -XXfm <regex>            filter for show options");
607 #endif
608         /* exit with error code */
609
610         exit(1);
611 }
612
613
614 /* version *********************************************************************
615
616    Only prints cacao version information.
617
618 *******************************************************************************/
619
620 static void version(bool opt_exit)
621 {
622         puts("java version \""JAVA_VERSION"\"");
623         puts("CACAO version "VERSION"");
624
625         puts("Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,");
626         puts("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,");
627         puts("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,");
628         puts("J. Wenninger, Institut f. Computersprachen - TU Wien\n");
629
630         puts("This program is free software; you can redistribute it and/or");
631         puts("modify it under the terms of the GNU General Public License as");
632         puts("published by the Free Software Foundation; either version 2, or (at");
633         puts("your option) any later version.\n");
634
635         puts("This program is distributed in the hope that it will be useful, but");
636         puts("WITHOUT ANY WARRANTY; without even the implied warranty of");
637         puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU");
638         puts("General Public License for more details.");
639
640         /* exit normally, if requested */
641
642         if (opt_exit)
643                 exit(0);
644 }
645
646
647 /* fullversion *****************************************************************
648
649    Prints a Sun compatible version information (required e.g. by
650    jpackage, www.jpackage.org).
651
652 *******************************************************************************/
653
654 static void fullversion(void)
655 {
656         puts("java full version \"cacao-"JAVA_VERSION"\"");
657
658         /* exit normally */
659
660         exit(0);
661 }
662
663
664 void vm_printconfig(void)
665 {
666         puts("Configure/Build options:\n");
667         puts("  ./configure: "VERSION_CONFIGURE_ARGS"");
668 #if defined(__VERSION__)
669         puts("  CC         : "VERSION_CC" ("__VERSION__")");
670 #else
671         puts("  CC         : "VERSION_CC"");
672 #endif
673         puts("  CFLAGS     : "VERSION_CFLAGS"\n");
674
675         puts("Default variables:\n");
676         printf("  maximum heap size              : %d\n", HEAP_MAXSIZE);
677         printf("  initial heap size              : %d\n", HEAP_STARTSIZE);
678         printf("  stack size                     : %d\n", STACK_SIZE);
679 #if defined(WITH_CLASSPATH_GNU)
680         puts("  java.boot.class.path           : "CACAO_VM_ZIP":"CLASSPATH_CLASSES"");
681 #else
682         puts("  java.boot.class.path           : "CLASSPATH_CLASSES"");
683 #endif
684         puts("  gnu.classpath.boot.library.path: "CLASSPATH_LIBDIR"/classpath\n");
685
686         puts("Runtime variables:\n");
687         printf("  maximum heap size              : %d\n", opt_heapmaxsize);
688         printf("  initial heap size              : %d\n", opt_heapstartsize);
689         printf("  stack size                     : %d\n", opt_stacksize);
690         printf("  libjvm.so                      : %s\n", cacao_libjvm);
691         printf("  java.boot.class.path           : %s\n", _Jv_bootclasspath);
692         printf("  gnu.classpath.boot.library.path: %s\n", classpath_libdir);
693         printf("  java.class.path                : %s\n", _Jv_classpath);
694 }
695
696
697 /* forward declarations *******************************************************/
698
699 static char *vm_get_mainclass_from_jar(char *mainstring);
700 #if !defined(NDEBUG)
701 static void  vm_compile_all(void);
702 static void  vm_compile_method(void);
703 #endif
704
705
706 /* vm_createjvm ****************************************************************
707
708    Implementation for JNI_CreateJavaVM.
709
710 *******************************************************************************/
711
712 bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
713 {
714         JavaVMInitArgs *_vm_args;
715         _Jv_JNIEnv     *env;
716         _Jv_JavaVM     *vm;
717
718         /* get the arguments for the new JVM */
719
720         _vm_args = (JavaVMInitArgs *) vm_args;
721
722         /* get the VM and Env tables (must be set before vm_create) */
723
724         env = NEW(_Jv_JNIEnv);
725
726 #if defined(ENABLE_JNI)
727         env->env = &_Jv_JNINativeInterface;
728 #endif
729
730         /* XXX Set the global variable.  Maybe we should do that differently. */
731
732         _Jv_env = env;
733
734         /* create and fill a JavaVM structure */
735
736         vm = NEW(_Jv_JavaVM);
737
738 #if defined(ENABLE_JNI)
739         vm->functions = &_Jv_JNIInvokeInterface;
740 #endif
741
742         /* XXX Set the global variable.  Maybe we should do that differently. */
743         /* XXX JVMTI Agents needs a JavaVM  */
744
745         _Jv_jvm = vm;
746
747         /* actually create the JVM */
748
749         if (!vm_create(_vm_args))
750                 goto error;
751
752 #if defined(ENABLE_JNI)
753         /* setup the local ref table (must be created after vm_create) */
754
755         if (!jni_init_localref_table())
756                 goto error;
757 #endif
758
759         /* now return the values */
760
761         *p_vm  = (JavaVM *) vm;
762         *p_env = (void *) env;
763
764         return true;
765
766  error:
767         /* release allocated memory */
768
769         FREE(env, _Jv_JNIEnv);
770         FREE(vm, _Jv_JavaVM);
771
772         return false;
773 }
774
775
776 /* vm_create *******************************************************************
777
778    Creates a JVM.  Called by vm_createjvm.
779
780 *******************************************************************************/
781
782 bool vm_create(JavaVMInitArgs *vm_args)
783 {
784         char *cp;
785         s4    len;
786         s4    opt;
787         s4    i, j;
788         bool  opt_version;
789         bool  opt_exit;
790
791 #if defined(ENABLE_JVMTI)
792         lt_dlhandle  handle;
793         char *libname, *agentarg;
794         bool jdwp,agentbypath;
795         jdwp = agentbypath = false;
796 #endif
797
798 #if defined(ENABLE_VMLOG)
799         vmlog_cacao_init(vm_args);
800 #endif
801
802         /* check the JNI version requested */
803
804         switch (vm_args->version) {
805         case JNI_VERSION_1_1:
806                 break;
807         case JNI_VERSION_1_2:
808         case JNI_VERSION_1_4:
809                 break;
810         default:
811                 return false;
812         }
813
814         /* we only support 1 JVM instance */
815
816         if (vms > 0)
817                 return false;
818
819         if (atexit(vm_exit_handler))
820                 vm_abort("atexit failed: %s\n", strerror(errno));
821
822         if (opt_verbose)
823                 log_text("CACAO started -------------------------------------------------------");
824
825         /* We need to check if the actual size of a java.lang.Class object
826            is smaller or equal than the assumption made in
827            src/vmcore/class.h. */
828
829         if (sizeof(java_lang_Class) > sizeof(dummy_java_lang_Class))
830                 vm_abort("vm_create: java_lang_Class structure is bigger than classinfo.object (%d > %d)", sizeof(java_lang_Class), sizeof(dummy_java_lang_Class));
831
832         /* set the VM starttime */
833
834         _Jv_jvm->starttime = builtin_currenttimemillis();
835
836         /* get stuff from the environment *****************************************/
837
838 #if defined(WITH_JRE_LAYOUT)
839         /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
840
841         cacao_prefix = MNEW(char, 4096);
842
843         if (readlink("/proc/self/exe", cacao_prefix, 4095) == -1)
844                 vm_abort("readlink failed: %s\n", strerror(errno));
845
846         /* get the path of the current executable */
847
848         cacao_prefix = dirname(cacao_prefix);
849
850         if ((strlen(cacao_prefix) + strlen("/..") + strlen("0")) > 4096)
851                 vm_abort("libjvm name to long for buffer\n");
852
853         /* concatenate the library name */
854
855         strcat(cacao_prefix, "/..");
856
857         /* now set path to libjvm.so */
858
859         len = strlen(cacao_prefix) + strlen("/lib/libjvm") + strlen("0");
860
861         cacao_libjvm = MNEW(char, len);
862         strcpy(cacao_libjvm, cacao_prefix);
863         strcat(cacao_libjvm, "/lib/libjvm");
864
865         /* and finally set the path to GNU Classpath libraries */
866
867         len = strlen(cacao_prefix) + strlen("/lib/classpath") + strlen("0");
868
869         classpath_libdir = MNEW(char, len);
870         strcpy(classpath_libdir, cacao_prefix);
871         strcat(classpath_libdir, "/lib/classpath");
872 #else
873         cacao_prefix     = CACAO_PREFIX;
874         cacao_libjvm     = CACAO_LIBDIR"/libjvm";
875
876 # if defined(WITH_CLASSPATH_GNU)
877         classpath_libdir = CLASSPATH_LIBDIR"/classpath";
878 # else
879         classpath_libdir = CLASSPATH_LIBDIR;
880 # endif
881 #endif
882
883         /* set the bootclasspath */
884
885         cp = getenv("BOOTCLASSPATH");
886
887         if (cp != NULL) {
888                 _Jv_bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
889                 strcpy(_Jv_bootclasspath, cp);
890         }
891         else {
892 #if defined(WITH_JRE_LAYOUT)
893                 len =
894 # if defined(WITH_CLASSPATH_GNU)
895                         strlen(cacao_prefix) +
896                         strlen("/share/cacao/vm.zip") +
897                         strlen(":") +
898 # endif
899                         strlen(cacao_prefix) +
900                         strlen("/share/classpath/glibj.zip") +
901                         strlen("0");
902
903                 _Jv_bootclasspath = MNEW(char, len);
904 # if defined(WITH_CLASSPATH_GNU)
905                 strcat(_Jv_bootclasspath, cacao_prefix);
906                 strcat(_Jv_bootclasspath, "/share/cacao/vm.zip");
907                 strcat(_Jv_bootclasspath, ":");
908 # endif
909                 strcat(_Jv_bootclasspath, cacao_prefix);
910                 strcat(_Jv_bootclasspath, "/share/classpath/glibj.zip");
911 #else
912 # if defined(WITH_CLASSPATH_GNU)
913                 len =
914                         strlen(CACAO_VM_ZIP) +
915                         strlen(":") +
916                         strlen(CLASSPATH_CLASSES) +
917                         strlen("0");
918 # elif defined(WITH_CLASSPATH_SUN)
919                 /* This is the bootclasspath taken from HotSpot (see
920                    hotspot/src/share/vm/runtime/os.cpp
921                    (os::set_boot_path)). */
922
923                 len =
924                         strlen(CLASSPATH_PREFIX"/lib/resources.jar:"
925                                    CLASSPATH_PREFIX"/lib/rt.jar:"
926                                    CLASSPATH_PREFIX"/lib/sunrsasign.jar:"
927                                    CLASSPATH_PREFIX"/lib/jsse.jar:"
928                                    CLASSPATH_PREFIX"/lib/jce.jar:"
929                                    CLASSPATH_PREFIX"/lib/charsets.jar:"
930                                    CLASSPATH_PREFIX"/classes") +
931                         strlen("0");
932 # elif defined(WITH_CLASSPATH_CLDC1_1)
933                 len =
934                         strlen(CLASSPATH_CLASSES) +
935                         strlen("0");
936 # else
937 #  error unknown classpath configuration
938 # endif
939
940                 _Jv_bootclasspath = MNEW(char, len);
941
942 # if defined(WITH_CLASSPATH_GNU)
943                 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
944                 strcat(_Jv_bootclasspath, ":");
945                 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
946 # elif defined(WITH_CLASSPATH_SUN)
947                 strcpy(_Jv_bootclasspath,
948                            CLASSPATH_PREFIX"/lib/resources.jar:"
949                            CLASSPATH_PREFIX"/lib/rt.jar:"
950                            CLASSPATH_PREFIX"/lib/sunrsasign.jar:"
951                            CLASSPATH_PREFIX"/lib/jsse.jar:"
952                            CLASSPATH_PREFIX"/lib/jce.jar:"
953                            CLASSPATH_PREFIX"/lib/charsets.jar:"
954                            CLASSPATH_PREFIX"/classes");
955 # elif defined(WITH_CLASSPATH_CLDC1_1)
956                 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
957 # else
958 #  error unknown classpath configuration
959 # endif
960 #endif
961         }
962
963         /* set the classpath */
964
965         cp = getenv("CLASSPATH");
966
967         if (cp != NULL) {
968                 _Jv_classpath = MNEW(char, strlen(cp) + strlen("0"));
969                 strcat(_Jv_classpath, cp);
970         }
971         else {
972                 _Jv_classpath = MNEW(char, strlen(".") + strlen("0"));
973                 strcpy(_Jv_classpath, ".");
974         }
975
976         /* Get and set java.library.path. */
977
978         _Jv_java_library_path = getenv("LD_LIBRARY_PATH");
979
980         if (_Jv_java_library_path == NULL)
981                 _Jv_java_library_path = "";
982
983         /* interpret the options **************************************************/
984
985         opt_version       = false;
986         opt_exit          = false;
987
988         opt_noieee        = false;
989
990         opt_heapmaxsize   = HEAP_MAXSIZE;
991         opt_heapstartsize = HEAP_STARTSIZE;
992         opt_stacksize     = STACK_SIZE;
993
994
995 #if defined(ENABLE_JVMTI)
996         /* initialize JVMTI related  **********************************************/
997         jvmti = false;
998 #endif
999
1000         /* Initialize and fill properties before command-line handling. */
1001
1002         if (!properties_init())
1003                 vm_abort("vm_create: properties_init failed");
1004
1005         /* Set the classpath properties. */
1006
1007 #if defined(ENABLE_JAVASE)
1008         properties_add("java.boot.class.path", _Jv_bootclasspath);
1009         properties_add("sun.boot.class.path", _Jv_bootclasspath);
1010         properties_add("java.class.path", _Jv_classpath);
1011 #endif
1012
1013         /* iterate over all passed options */
1014
1015         while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
1016                 switch (opt) {
1017                 case OPT_FOO:
1018                         opt_foo = true;
1019                         break;
1020
1021                 case OPT_IGNORE:
1022                         break;
1023                         
1024                 case OPT_JAR:
1025                         opt_jar = true;
1026                         break;
1027
1028                 case OPT_D32:
1029 #if SIZEOF_VOID_P == 8
1030                         puts("Running a 32-bit JVM is not supported on this platform.");
1031                         exit(1);
1032 #endif
1033                         break;
1034
1035                 case OPT_D64:
1036 #if SIZEOF_VOID_P == 4
1037                         puts("Running a 64-bit JVM is not supported on this platform.");
1038                         exit(1);
1039 #endif
1040                         break;
1041
1042                 case OPT_CLASSPATH:
1043                         /* forget old classpath and set the argument as new classpath */
1044                         MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1045
1046                         _Jv_classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
1047                         strcpy(_Jv_classpath, opt_arg);
1048
1049 #if defined(ENABLE_JAVASE)
1050                         properties_add("java.class.path", _Jv_classpath);
1051 #endif
1052                         break;
1053
1054                 case OPT_D:
1055                         for (i = 0; i < strlen(opt_arg); i++) {
1056                                 if (opt_arg[i] == '=') {
1057                                         opt_arg[i] = '\0';
1058                                         properties_add(opt_arg, opt_arg + i + 1);
1059                                         goto opt_d_done;
1060                                 }
1061                         }
1062
1063                         /* if no '=' is given, just create an empty property */
1064
1065                         properties_add(opt_arg, "");
1066
1067                 opt_d_done:
1068                         break;
1069
1070                 case OPT_BOOTCLASSPATH:
1071                         /* Forget default bootclasspath and set the argument as
1072                            new boot classpath. */
1073
1074                         MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1075
1076                         _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
1077                         strcpy(_Jv_bootclasspath, opt_arg);
1078
1079 #if defined(ENABLE_JAVASE)
1080                         properties_add("java.boot.class.path", _Jv_bootclasspath);
1081                         properties_add("sun.boot.class.path", _Jv_bootclasspath);
1082 #endif
1083                         break;
1084
1085                 case OPT_BOOTCLASSPATH_A:
1086                         /* append to end of bootclasspath */
1087
1088                         len = strlen(_Jv_bootclasspath);
1089
1090                         _Jv_bootclasspath = MREALLOC(_Jv_bootclasspath,
1091                                                                                  char,
1092                                                                                  len + strlen("0"),
1093                                                                                  len + strlen(":") +
1094                                                                                  strlen(opt_arg) + strlen("0"));
1095
1096                         strcat(_Jv_bootclasspath, ":");
1097                         strcat(_Jv_bootclasspath, opt_arg);
1098
1099 #if defined(ENABLE_JAVASE)
1100                         properties_add("java.boot.class.path", _Jv_bootclasspath);
1101                         properties_add("sun.boot.class.path", _Jv_bootclasspath);
1102 #endif
1103                         break;
1104
1105                 case OPT_BOOTCLASSPATH_P:
1106                         /* prepend in front of bootclasspath */
1107
1108                         cp = _Jv_bootclasspath;
1109                         len = strlen(cp);
1110
1111                         _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
1112                                                                          len + strlen("0"));
1113
1114                         strcpy(_Jv_bootclasspath, opt_arg);
1115                         strcat(_Jv_bootclasspath, ":");
1116                         strcat(_Jv_bootclasspath, cp);
1117
1118                         MFREE(cp, char, len);
1119
1120 #if defined(ENABLE_JAVASE)
1121                         properties_add("java.boot.class.path", _Jv_bootclasspath);
1122                         properties_add("sun.boot.class.path", _Jv_bootclasspath);
1123 #endif
1124                         break;
1125
1126                 case OPT_BOOTCLASSPATH_C:
1127                         /* use as Java core library, but prepend VM interface classes */
1128
1129                         MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1130
1131                         len = strlen(CACAO_VM_ZIP) +
1132                                 strlen(":") +
1133                                 strlen(opt_arg) +
1134                                 strlen("0");
1135
1136                         _Jv_bootclasspath = MNEW(char, len);
1137
1138                         strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
1139                         strcat(_Jv_bootclasspath, ":");
1140                         strcat(_Jv_bootclasspath, opt_arg);
1141
1142 #if defined(ENABLE_JAVASE)
1143                         properties_add("java.boot.class.path", _Jv_bootclasspath);
1144                         properties_add("sun.boot.class.path", _Jv_bootclasspath);
1145 #endif
1146                         break;
1147
1148 #if defined(ENABLE_JVMTI)
1149                 case OPT_DEBUG:
1150                         /* this option exists only for compatibility reasons */
1151                         break;
1152
1153                 case OPT_NOAGENT:
1154                         /* I don't know yet what Xnoagent should do. This is only for 
1155                            compatiblity with eclipse - motse */
1156                         break;
1157
1158                 case OPT_XRUNJDWP:
1159                         agentbypath = true;
1160                         jvmti       = true;
1161                         jdwp        = true;
1162
1163                         len =
1164                                 strlen(CACAO_LIBDIR) +
1165                                 strlen("/libjdwp.so=") +
1166                                 strlen(opt_arg) +
1167                                 strlen("0");
1168
1169                         agentarg = MNEW(char, len);
1170
1171                         strcpy(agentarg, CACAO_LIBDIR);
1172                         strcat(agentarg, "/libjdwp.so=");
1173                         strcat(agentarg, &opt_arg[1]);
1174                         break;
1175
1176                 case OPT_AGENTPATH:
1177                         agentbypath = true;
1178
1179                 case OPT_AGENTLIB:
1180                         jvmti = true;
1181                         agentarg = opt_arg;
1182                         break;
1183 #endif
1184                         
1185                 case OPT_MX:
1186                 case OPT_MS:
1187                 case OPT_SS:
1188                         {
1189                                 char c;
1190                                 c = opt_arg[strlen(opt_arg) - 1];
1191
1192                                 if ((c == 'k') || (c == 'K')) {
1193                                         j = atoi(opt_arg) * 1024;
1194
1195                                 } else if ((c == 'm') || (c == 'M')) {
1196                                         j = atoi(opt_arg) * 1024 * 1024;
1197
1198                                 } else
1199                                         j = atoi(opt_arg);
1200
1201                                 if (opt == OPT_MX)
1202                                         opt_heapmaxsize = j;
1203                                 else if (opt == OPT_MS)
1204                                         opt_heapstartsize = j;
1205                                 else
1206                                         opt_stacksize = j;
1207                         }
1208                         break;
1209
1210                 case OPT_VERBOSE1:
1211                         opt_verbose = true;
1212                         break;
1213
1214                 case OPT_VERBOSE:
1215                         if (strcmp("class", opt_arg) == 0) {
1216                                 opt_verboseclass = true;
1217                         }
1218                         else if (strcmp("gc", opt_arg) == 0) {
1219                                 opt_verbosegc = true;
1220                         }
1221                         else if (strcmp("jni", opt_arg) == 0) {
1222                                 opt_verbosejni = true;
1223                         }
1224 #if !defined(NDEBUG)
1225                         else if (strcmp("jit", opt_arg) == 0) {
1226                                 opt_verbose = true;
1227                                 loadverbose = true;
1228                                 linkverbose = true;
1229                                 initverbose = true;
1230                                 compileverbose = true;
1231                         }
1232                         else if (strcmp("threads", opt_arg) == 0) {
1233                                 opt_verbosethreads = true;
1234                         }
1235 #endif
1236                         else {
1237                                 printf("Unknown -verbose option: %s\n", opt_arg);
1238                                 usage();
1239                         }
1240                         break;
1241
1242                 case OPT_DEBUGCOLOR:
1243                         opt_debugcolor = true;
1244                         break;
1245
1246 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
1247                 case OPT_VERBOSETC:
1248                         opt_typecheckverbose = true;
1249                         break;
1250 #endif
1251                                 
1252                 case OPT_VERSION:
1253                         opt_version = true;
1254                         opt_exit    = true;
1255                         break;
1256
1257                 case OPT_FULLVERSION:
1258                         fullversion();
1259                         break;
1260
1261                 case OPT_SHOWVERSION:
1262                         opt_version = true;
1263                         break;
1264
1265                 case OPT_NOIEEE:
1266                         opt_noieee = true;
1267                         break;
1268
1269 #if defined(ENABLE_VERIFIER)
1270                 case OPT_NOVERIFY:
1271                         opt_verify = false;
1272                         break;
1273 #endif
1274
1275 #if defined(ENABLE_STATISTICS)
1276                 case OPT_TIME:
1277                         opt_getcompilingtime = true;
1278                         opt_getloadingtime = true;
1279                         break;
1280                                         
1281                 case OPT_STAT:
1282                         opt_stat = true;
1283                         break;
1284 #endif
1285                                         
1286                 case OPT_LOG:
1287                         log_init(opt_arg);
1288                         break;
1289                         
1290                 case OPT_CHECK:
1291                         for (i = 0; i < strlen(opt_arg); i++) {
1292                                 switch (opt_arg[i]) {
1293                                 case 'b':
1294                                         checkbounds = false;
1295                                         break;
1296                                 case 's':
1297                                         checksync = false;
1298                                         break;
1299                                 default:
1300                                         usage();
1301                                 }
1302                         }
1303                         break;
1304                         
1305                 case OPT_LOAD:
1306                         opt_run = false;
1307                         makeinitializations = false;
1308                         break;
1309
1310 #if !defined(NDEBUG)
1311                 case OPT_ALL:
1312                         compileall = true;
1313                         opt_run = false;
1314                         makeinitializations = false;
1315                         break;
1316
1317                 case OPT_METHOD:
1318                         opt_run = false;
1319                         opt_method = opt_arg;
1320                         makeinitializations = false;
1321                         break;
1322
1323                 case OPT_SIGNATURE:
1324                         opt_signature = opt_arg;
1325                         break;
1326 #endif
1327
1328                 case OPT_SHOW:       /* Display options */
1329                         for (i = 0; i < strlen(opt_arg); i++) {         
1330                                 switch (opt_arg[i]) {
1331                                 case 'c':
1332                                         showconstantpool = true;
1333                                         break;
1334
1335                                 case 'u':
1336                                         showutf = true;
1337                                         break;
1338
1339                                 case 'm':
1340                                         showmethods = true;
1341                                         break;
1342
1343                                 case 'i':
1344                                         opt_showintermediate = true;
1345                                         compileverbose = true;
1346                                         break;
1347
1348 #if defined(ENABLE_DISASSEMBLER)
1349                                 case 'a':
1350                                         opt_showdisassemble = true;
1351                                         compileverbose = true;
1352                                         break;
1353
1354                                 case 'o':
1355                                         opt_shownops = true;
1356                                         break;
1357
1358                                 case 'e':
1359                                         opt_showexceptionstubs = true;
1360                                         break;
1361
1362                                 case 'n':
1363                                         opt_shownativestub = true;
1364                                         break;
1365 #endif
1366
1367                                 case 'd':
1368                                         opt_showddatasegment = true;
1369                                         break;
1370
1371                                 default:
1372                                         usage();
1373                                 }
1374                         }
1375                         break;
1376                         
1377 #if defined(ENABLE_LOOP)
1378                 case OPT_OLOOP:
1379                         opt_loops = true;
1380                         break;
1381 #endif
1382
1383 #if defined(ENABLE_INLINING)
1384 #if defined(ENABLE_INLINING_DEBUG)
1385                 case OPT_INLINE_DEBUG_ALL:
1386                         opt_inline_debug_all = true;
1387                         break;
1388                 case OPT_INLINE_DEBUG_END:
1389                         opt_inline_debug_end_counter = atoi(opt_arg);
1390                         break;
1391                 case OPT_INLINE_DEBUG_MIN:
1392                         opt_inline_debug_min_size = atoi(opt_arg);
1393                         break;
1394                 case OPT_INLINE_DEBUG_MAX:
1395                         opt_inline_debug_max_size = atoi(opt_arg);
1396                         break;
1397 #endif /* defined(ENABLE_INLINING_DEBUG) */
1398 #if !defined(NDEBUG)
1399                 case OPT_INLINE_LOG:
1400                         opt_inline_debug_log = true;
1401                         break;
1402 #endif /* !defined(NDEBUG) */
1403
1404                 case OPT_INLINING:
1405                         opt_inlining = true;
1406                         break;
1407 #endif /* defined(ENABLE_INLINING) */
1408
1409 #if defined(ENABLE_IFCONV)
1410                 case OPT_IFCONV:
1411                         opt_ifconv = true;
1412                         break;
1413 #endif
1414
1415 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1416                 case OPT_LSRA:
1417                         opt_lsra = true;
1418                         break;
1419 #endif
1420
1421                 case OPT_HELP:
1422                         usage();
1423                         break;
1424
1425                 case OPT_X:
1426                         Xusage();
1427                         break;
1428
1429                 case OPT_XX:
1430                         options_xx(opt_arg);
1431                         break;
1432
1433                 case OPT_EA:
1434                         /* currently ignored */
1435                         break;
1436
1437                 case OPT_DA:
1438                         /* currently ignored */
1439                         break;
1440
1441                 case OPT_ESA:
1442                         _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = true;
1443                         break;
1444
1445                 case OPT_DSA:
1446                         _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = false;
1447                         break;
1448
1449 #if defined(ENABLE_PROFILING)
1450                 case OPT_PROF_OPTION:
1451                         /* use <= to get the last \0 too */
1452
1453                         for (i = 0, j = 0; i <= strlen(opt_arg); i++) {
1454                                 if (opt_arg[i] == ',')
1455                                         opt_arg[i] = '\0';
1456
1457                                 if (opt_arg[i] == '\0') {
1458                                         if (strcmp("bb", opt_arg + j) == 0)
1459                                                 opt_prof_bb = true;
1460
1461                                         else {
1462                                                 printf("Unknown option: -Xprof:%s\n", opt_arg + j);
1463                                                 usage();
1464                                         }
1465
1466                                         /* set k to next char */
1467
1468                                         j = i + 1;
1469                                 }
1470                         }
1471                         /* fall through */
1472
1473                 case OPT_PROF:
1474                         opt_prof = true;
1475                         break;
1476 #endif
1477
1478                 case OPT_JIT:
1479 #if defined(ENABLE_JIT)
1480                         opt_jit = true;
1481 #else
1482                         printf("-Xjit option not enabled.\n");
1483                         exit(1);
1484 #endif
1485                         break;
1486
1487                 case OPT_INTRP:
1488 #if defined(ENABLE_INTRP)
1489                         opt_intrp = true;
1490 #else
1491                         printf("-Xint option not enabled.\n");
1492                         exit(1);
1493 #endif
1494                         break;
1495
1496 #if defined(ENABLE_INTRP)
1497                 case OPT_STATIC_SUPERS:
1498                         opt_static_supers = atoi(opt_arg);
1499                         break;
1500
1501                 case OPT_NO_DYNAMIC:
1502                         opt_no_dynamic = true;
1503                         break;
1504
1505                 case OPT_NO_REPLICATION:
1506                         opt_no_replication = true;
1507                         break;
1508
1509                 case OPT_NO_QUICKSUPER:
1510                         opt_no_quicksuper = true;
1511                         break;
1512
1513                 case OPT_TRACE:
1514                         vm_debug = true;
1515                         break;
1516 #endif
1517
1518 #if defined(ENABLE_DEBUG_FILTER)
1519                 case OPT_FILTER_VERBOSECALL_INCLUDE:
1520                         opt_filter_verbosecall_include = opt_arg;
1521                         break;
1522
1523                 case OPT_FILTER_VERBOSECALL_EXCLUDE:
1524                         opt_filter_verbosecall_exclude = opt_arg;
1525                         break;
1526
1527                 case OPT_FILTER_SHOW_METHOD:
1528                         opt_filter_show_method = opt_arg;
1529                         break;
1530
1531 #endif
1532                 default:
1533                         printf("Unknown option: %s\n",
1534                                    vm_args->options[opt_index].optionString);
1535                         usage();
1536                 }
1537         }
1538
1539         /* get the main class *****************************************************/
1540
1541         if (opt_index < vm_args->nOptions) {
1542                 mainstring = vm_args->options[opt_index++].optionString;
1543
1544                 /* Put the jar file into the classpath (if any). */
1545
1546                 if (opt_jar == true) {
1547                         /* free old classpath */
1548
1549                         MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1550
1551                         /* put jarfile into classpath */
1552
1553                         _Jv_classpath = MNEW(char, strlen(mainstring) + strlen("0"));
1554
1555                         strcpy(_Jv_classpath, mainstring);
1556
1557 #if defined(ENABLE_JAVASE)
1558                         properties_add("java.class.path", _Jv_classpath);
1559 #endif
1560                 }
1561                 else {
1562                         /* replace .'s with /'s in classname */
1563
1564                         for (i = strlen(mainstring) - 1; i >= 0; i--)
1565                                 if (mainstring[i] == '.')
1566                                         mainstring[i] = '/';
1567                 }
1568         }
1569
1570 #if defined(ENABLE_JVMTI)
1571         if (jvmti) {
1572                 jvmti_set_phase(JVMTI_PHASE_ONLOAD);
1573                 jvmti_agentload(agentarg, agentbypath, &handle, &libname);
1574
1575                 if (jdwp)
1576                         MFREE(agentarg, char, strlen(agentarg));
1577
1578                 jvmti_set_phase(JVMTI_PHASE_PRIMORDIAL);
1579         }
1580 #endif
1581
1582         /* initialize this JVM ****************************************************/
1583
1584         vm_initializing = true;
1585
1586         /* initialize the garbage collector */
1587
1588         gc_init(opt_heapmaxsize, opt_heapstartsize);
1589
1590 #if defined(ENABLE_THREADS)
1591         /* AFTER: gc_init (directly after, as this initializes the
1592            stopworldlock lock */
1593
1594         threads_preinit();
1595 #endif
1596
1597         /* install architecture dependent signal handlers */
1598
1599         if (!signal_init())
1600                 vm_abort("vm_create: signal_init failed");
1601
1602 #if defined(ENABLE_INTRP)
1603         /* Allocate main thread stack on the Java heap. */
1604
1605         if (opt_intrp) {
1606                 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1607                 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1608         }
1609 #endif
1610
1611         /* AFTER: threads_preinit */
1612
1613         if (!string_init())
1614                 vm_abort("vm_create: string_init failed");
1615
1616         /* AFTER: threads_preinit */
1617
1618         if (!utf8_init())
1619                 vm_abort("vm_create: utf8_init failed");
1620
1621         /* AFTER: thread_preinit */
1622
1623         if (!suck_init())
1624                 vm_abort("vm_create: suck_init failed");
1625
1626         suck_add_from_property("java.endorsed.dirs");
1627
1628         /* Now we have all options handled and we can print the version
1629            information.
1630
1631            AFTER: suck_add_from_property("java.endorsed.dirs"); */
1632
1633         if (opt_version)
1634                 version(opt_exit);
1635
1636         /* AFTER: utf8_init */
1637
1638         suck_add(_Jv_bootclasspath);
1639
1640         /* initialize the classcache hashtable stuff: lock, hashtable
1641            (must be done _after_ threads_preinit) */
1642
1643         if (!classcache_init())
1644                 vm_abort("vm_create: classcache_init failed");
1645
1646         /* initialize the memory subsystem (must be done _after_
1647            threads_preinit) */
1648
1649         if (!memory_init())
1650                 vm_abort("vm_create: memory_init failed");
1651
1652         /* initialize the finalizer stuff (must be done _after_
1653            threads_preinit) */
1654
1655         if (!finalizer_init())
1656                 vm_abort("vm_create: finalizer_init failed");
1657
1658         /* initialize the codegen subsystems */
1659
1660         codegen_init();
1661
1662         /* initializes jit compiler */
1663
1664         jit_init();
1665
1666         /* machine dependent initialization */
1667
1668 #if defined(ENABLE_JIT)
1669 # if defined(ENABLE_INTRP)
1670         if (opt_intrp)
1671                 intrp_md_init();
1672         else
1673 # endif
1674                 md_init();
1675 #else
1676         intrp_md_init();
1677 #endif
1678
1679         /* initialize the loader subsystems (must be done _after_
1680        classcache_init) */
1681
1682         if (!loader_init())
1683                 vm_abort("vm_create: loader_init failed");
1684
1685         /* Link some important VM classes. */
1686         /* AFTER: utf8_init */
1687
1688         if (!linker_init())
1689                 vm_abort("vm_create: linker_init failed");
1690
1691         if (!primitive_init())
1692                 vm_abort("vm_create: primitive_init failed");
1693
1694         if (!exceptions_init())
1695                 vm_abort("vm_create: exceptions_init failed");
1696
1697         if (!builtin_init())
1698                 vm_abort("vm_create: builtin_init failed");
1699
1700         /* Initialize the native subsystem. */
1701         /* BEFORE: threads_init */
1702
1703         if (!native_init())
1704                 vm_abort("vm_create: native_init failed");
1705
1706         /* Register the native methods implemented in the VM. */
1707         /* BEFORE: threads_init */
1708
1709         if (!nativevm_preinit())
1710                 vm_abort("vm_create: nativevm_preinit failed");
1711
1712 #if defined(ENABLE_JNI)
1713         /* Initialize the JNI subsystem (must be done _before_
1714            threads_init, as threads_init can call JNI methods
1715            (e.g. NewGlobalRef). */
1716
1717         if (!jni_init())
1718                 vm_abort("vm_create: jni_init failed");
1719 #endif
1720
1721 #if defined(ENABLE_THREADS)
1722         if (!threads_init())
1723                 vm_abort("vm_create: threads_init failed");
1724 #endif
1725
1726         /* Initialize the native VM subsystem. */
1727         /* AFTER: threads_init (at least for SUN's classes) */
1728
1729         if (!nativevm_init())
1730                 vm_abort("vm_create: nativevm_init failed");
1731
1732 #if defined(ENABLE_PROFILING)
1733         /* initialize profiling */
1734
1735         if (!profile_init())
1736                 vm_abort("vm_create: profile_init failed");
1737 #endif
1738
1739 #if defined(ENABLE_THREADS)
1740         /* initialize recompilation */
1741
1742         if (!recompile_init())
1743                 vm_abort("vm_create: recompile_init failed");
1744
1745         /* start the signal handler thread */
1746
1747 #if defined(__LINUX__)
1748         /* XXX Remove for exact-GC. */
1749         if (threads_pthreads_implementation_nptl)
1750 #endif
1751                 if (!signal_start_thread())
1752                         vm_abort("vm_create: signal_start_thread failed");
1753
1754         /* finally, start the finalizer thread */
1755
1756         if (!finalizer_start_thread())
1757                 vm_abort("vm_create: finalizer_start_thread failed");
1758
1759 # if !defined(NDEBUG)
1760         /* start the memory profiling thread */
1761
1762         if (opt_ProfileMemoryUsage || opt_ProfileGCMemoryUsage)
1763                 if (!memory_start_thread())
1764                         vm_abort("vm_create: memory_start_thread failed");
1765 # endif
1766
1767         /* start the recompilation thread (must be done before the
1768            profiling thread) */
1769
1770         if (!recompile_start_thread())
1771                 vm_abort("vm_create: recompile_start_thread failed");
1772
1773 # if defined(ENABLE_PROFILING)
1774         /* start the profile sampling thread */
1775
1776 /*      if (opt_prof) */
1777 /*              if (!profile_start_thread()) */
1778 /*                      vm_abort("vm_create: profile_start_thread failed"); */
1779 # endif
1780 #endif
1781
1782 #if defined(ENABLE_JVMTI)
1783         if (jvmti) {
1784                 /* add agent library to native library hashtable */
1785                 native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
1786         }
1787 #endif
1788
1789         /* increment the number of VMs */
1790
1791         vms++;
1792
1793         /* initialization is done */
1794
1795         vm_initializing = false;
1796
1797         /* everything's ok */
1798
1799         return true;
1800 }
1801
1802
1803 /* vm_run **********************************************************************
1804
1805    Runs the main-method of the passed class.
1806
1807 *******************************************************************************/
1808
1809 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1810 {
1811         utf               *mainutf;
1812         classinfo         *mainclass;
1813         java_objectheader *e;
1814         methodinfo        *m;
1815         java_objectarray  *oa; 
1816         s4                 oalength;
1817         utf               *u;
1818         java_objectheader *s;
1819         s4                 status;
1820         s4                 i;
1821
1822 #if !defined(NDEBUG)
1823         if (compileall) {
1824                 vm_compile_all();
1825                 return;
1826         }
1827
1828         if (opt_method != NULL) {
1829                 vm_compile_method();
1830                 return;
1831         }
1832 #endif /* !defined(NDEBUG) */
1833
1834         /* should we run the main-method? */
1835
1836         if (mainstring == NULL)
1837                 usage();
1838
1839         /* set return value to OK */
1840
1841         status = 0;
1842
1843         if (opt_jar == true) {
1844                 /* open jar file with java.util.jar.JarFile */
1845
1846                 mainstring = vm_get_mainclass_from_jar(mainstring);
1847
1848                 if (mainstring == NULL)
1849                         vm_exit(1);
1850         }
1851
1852         /* load the main class */
1853
1854         mainutf = utf_new_char(mainstring);
1855
1856 #if defined(ENABLE_JAVAME_CLDC1_1)
1857         mainclass = load_class_bootstrap(mainutf);
1858 #else
1859         mainclass = load_class_from_sysloader(mainutf);
1860 #endif
1861
1862         /* error loading class */
1863
1864         e = exceptions_get_and_clear_exception();
1865
1866         if ((e != NULL) || (mainclass == NULL)) {
1867                 exceptions_throw_noclassdeffounderror_cause(e);
1868                 exceptions_print_stacktrace(); 
1869                 vm_exit(1);
1870         }
1871
1872         if (!link_class(mainclass)) {
1873                 exceptions_print_stacktrace();
1874                 vm_exit(1);
1875         }
1876                         
1877         /* find the `main' method of the main class */
1878
1879         m = class_resolveclassmethod(mainclass,
1880                                                                  utf_new_char("main"), 
1881                                                                  utf_new_char("([Ljava/lang/String;)V"),
1882                                                                  class_java_lang_Object,
1883                                                                  false);
1884
1885         if (exceptions_get_exception()) {
1886                 exceptions_print_stacktrace();
1887                 vm_exit(1);
1888         }
1889
1890         /* there is no main method or it isn't static */
1891
1892         if ((m == NULL) || !(m->flags & ACC_STATIC)) {
1893                 exceptions_clear_exception();
1894                 exceptions_throw_nosuchmethoderror(mainclass,
1895                                                                                    utf_new_char("main"), 
1896                                                                                    utf_new_char("([Ljava/lang/String;)V"));
1897
1898                 exceptions_print_stacktrace();
1899                 vm_exit(1);
1900         }
1901
1902         /* build argument array */
1903
1904         oalength = vm_args->nOptions - opt_index;
1905
1906         oa = builtin_anewarray(oalength, class_java_lang_String);
1907
1908         for (i = 0; i < oalength; i++) {
1909                 u = utf_new_char(vm_args->options[opt_index + i].optionString);
1910                 s = javastring_new(u);
1911
1912                 oa->data[i] = s;
1913         }
1914
1915 #ifdef TYPEINFO_DEBUG_TEST
1916         /* test the typeinfo system */
1917         typeinfo_test();
1918 #endif
1919         /*class_showmethods(currentThread->group->header.vftbl->class); */
1920
1921 #if defined(ENABLE_JVMTI)
1922         jvmti_set_phase(JVMTI_PHASE_LIVE);
1923 #endif
1924
1925         /* set ThreadMXBean variables */
1926
1927         _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
1928         _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
1929
1930         if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
1931                 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
1932                 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
1933                         _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
1934
1935         /* start the main thread */
1936
1937         (void) vm_call_method(m, NULL, oa);
1938
1939         /* exception occurred? */
1940
1941         if (exceptions_get_exception()) {
1942                 exceptions_print_stacktrace();
1943                 status = 1;
1944         }
1945
1946         /* unload the JavaVM */
1947
1948         (void) vm_destroy(vm);
1949
1950         /* and exit */
1951
1952         vm_exit(status);
1953 }
1954
1955
1956 /* vm_destroy ******************************************************************
1957
1958    Unloads a Java VM and reclaims its resources.
1959
1960 *******************************************************************************/
1961
1962 s4 vm_destroy(JavaVM *vm)
1963 {
1964 #if defined(ENABLE_THREADS)
1965         threads_join_all_threads();
1966 #endif
1967
1968         /* everything's ok */
1969
1970         return 0;
1971 }
1972
1973
1974 /* vm_exit *********************************************************************
1975
1976    Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1977
1978 *******************************************************************************/
1979
1980 void vm_exit(s4 status)
1981 {
1982         methodinfo *m;
1983
1984         /* signal that we are exiting */
1985
1986         vm_exiting = true;
1987
1988         assert(class_java_lang_System);
1989         assert(class_java_lang_System->state & CLASS_LOADED);
1990
1991 #if defined(ENABLE_JVMTI)
1992         if (jvmti || (dbgcom!=NULL)) {
1993                 jvmti_set_phase(JVMTI_PHASE_DEAD);
1994                 if (jvmti) jvmti_agentunload();
1995         }
1996 #endif
1997
1998         if (!link_class(class_java_lang_System)) {
1999                 exceptions_print_stacktrace();
2000                 exit(1);
2001         }
2002
2003         /* call java.lang.System.exit(I)V */
2004
2005         m = class_resolveclassmethod(class_java_lang_System,
2006                                                                  utf_new_char("exit"),
2007                                                                  utf_int__void,
2008                                                                  class_java_lang_Object,
2009                                                                  true);
2010         
2011         if (m == NULL) {
2012                 exceptions_print_stacktrace();
2013                 exit(1);
2014         }
2015
2016         /* call the exit function with passed exit status */
2017
2018         (void) vm_call_method(m, NULL, status);
2019
2020         /* If we had an exception, just ignore the exception and exit with
2021            the proper code. */
2022
2023         vm_shutdown(status);
2024 }
2025
2026
2027 /* vm_shutdown *****************************************************************
2028
2029    Terminates the system immediately without freeing memory explicitly
2030    (to be used only for abnormal termination).
2031         
2032 *******************************************************************************/
2033
2034 void vm_shutdown(s4 status)
2035 {
2036         if (opt_verbose 
2037 #if defined(ENABLE_STATISTICS)
2038                 || opt_getcompilingtime || opt_stat
2039 #endif
2040            ) 
2041         {
2042                 log_text("CACAO terminated by shutdown");
2043                 dolog("Exit status: %d\n", (s4) status);
2044
2045         }
2046
2047 #if defined(ENABLE_JVMTI)
2048         /* terminate cacaodbgserver */
2049         if (dbgcom!=NULL) {
2050                 pthread_mutex_lock(&dbgcomlock);
2051                 dbgcom->running=1;
2052                 pthread_mutex_unlock(&dbgcomlock);
2053                 jvmti_cacaodbgserver_quit();
2054         }       
2055 #endif
2056
2057         exit(status);
2058 }
2059
2060
2061 /* vm_exit_handler *************************************************************
2062
2063    The exit_handler function is called upon program termination.
2064
2065    ATTENTION: Don't free system resources here! Some threads may still
2066    be running as this is called from VMRuntime.exit(). The OS does the
2067    cleanup for us.
2068
2069 *******************************************************************************/
2070
2071 void vm_exit_handler(void)
2072 {
2073 #if !defined(NDEBUG)
2074         if (showmethods)
2075                 class_showmethods(mainclass);
2076
2077         if (showconstantpool)
2078                 class_showconstantpool(mainclass);
2079
2080         if (showutf)
2081                 utf_show();
2082
2083 # if defined(ENABLE_PROFILING)
2084         if (opt_prof)
2085                 profile_printstats();
2086 # endif
2087 #endif /* !defined(NDEBUG) */
2088
2089 #if defined(ENABLE_RT_TIMING)
2090         rt_timing_print_time_stats(stderr);
2091 #endif
2092
2093 #if defined(ENABLE_CYCLES_STATS)
2094         builtin_print_cycles_stats(stderr);
2095         stacktrace_print_cycles_stats(stderr);
2096 #endif
2097
2098         if (opt_verbose 
2099 #if defined(ENABLE_STATISTICS)
2100                 || opt_getcompilingtime || opt_stat
2101 #endif
2102            ) 
2103         {
2104                 log_text("CACAO terminated");
2105
2106 #if defined(ENABLE_STATISTICS)
2107                 if (opt_stat) {
2108                         print_stats();
2109 #ifdef TYPECHECK_STATISTICS
2110                         typecheck_print_statistics(get_logfile());
2111 #endif
2112                 }
2113
2114                 if (opt_getcompilingtime)
2115                         print_times();
2116 #endif /* defined(ENABLE_STATISTICS) */
2117         }
2118         /* vm_print_profile(stderr);*/
2119 }
2120
2121
2122 /* vm_abort ********************************************************************
2123
2124    Prints an error message and aborts the VM.
2125
2126 *******************************************************************************/
2127
2128 void vm_abort(const char *text, ...)
2129 {
2130         va_list ap;
2131
2132         /* print the log message */
2133
2134         log_start();
2135
2136         va_start(ap, text);
2137         log_vprint(text, ap);
2138         va_end(ap);
2139
2140         log_finish();
2141
2142         /* now abort the VM */
2143
2144         abort();
2145 }
2146
2147
2148 /* vm_get_mainclass_from_jar ***************************************************
2149
2150    Gets the name of the main class from a JAR's manifest file.
2151
2152 *******************************************************************************/
2153
2154 static char *vm_get_mainclass_from_jar(char *mainstring)
2155 {
2156         classinfo         *c;
2157         java_objectheader *o;
2158         methodinfo        *m;
2159         java_objectheader *s;
2160
2161         c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
2162
2163         if (c == NULL) {
2164                 exceptions_print_stacktrace();
2165                 return NULL;
2166         }
2167
2168         /* create JarFile object */
2169
2170         o = builtin_new(c);
2171
2172         if (o == NULL) {
2173                 exceptions_print_stacktrace();
2174                 return NULL;
2175         }
2176
2177         m = class_resolveclassmethod(c,
2178                                                                  utf_init, 
2179                                                                  utf_java_lang_String__void,
2180                                                                  class_java_lang_Object,
2181                                                                  true);
2182
2183         if (m == NULL) {
2184                 exceptions_print_stacktrace();
2185                 return NULL;
2186         }
2187
2188         s = javastring_new_from_ascii(mainstring);
2189
2190         (void) vm_call_method(m, o, s);
2191
2192         if (exceptions_get_exception()) {
2193                 exceptions_print_stacktrace();
2194                 return NULL;
2195         }
2196
2197         /* get manifest object */
2198
2199         m = class_resolveclassmethod(c,
2200                                                                  utf_new_char("getManifest"), 
2201                                                                  utf_new_char("()Ljava/util/jar/Manifest;"),
2202                                                                  class_java_lang_Object,
2203                                                                  true);
2204
2205         if (m == NULL) {
2206                 exceptions_print_stacktrace();
2207                 return NULL;
2208         }
2209
2210         o = vm_call_method(m, o);
2211
2212         if (o == NULL) {
2213                 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
2214                 return NULL;
2215         }
2216
2217
2218         /* get Main Attributes */
2219
2220         m = class_resolveclassmethod(o->vftbl->class,
2221                                                                  utf_new_char("getMainAttributes"), 
2222                                                                  utf_new_char("()Ljava/util/jar/Attributes;"),
2223                                                                  class_java_lang_Object,
2224                                                                  true);
2225
2226         if (m == NULL) {
2227                 exceptions_print_stacktrace();
2228                 return NULL;
2229         }
2230
2231         o = vm_call_method(m, o);
2232
2233         if (o == NULL) {
2234                 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
2235                 return NULL;
2236         }
2237
2238
2239         /* get property Main-Class */
2240
2241         m = class_resolveclassmethod(o->vftbl->class,
2242                                                                  utf_new_char("getValue"), 
2243                                                                  utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
2244                                                                  class_java_lang_Object,
2245                                                                  true);
2246
2247         if (m == NULL) {
2248                 exceptions_print_stacktrace();
2249                 return NULL;
2250         }
2251
2252         s = javastring_new_from_ascii("Main-Class");
2253
2254         o = vm_call_method(m, o, s);
2255
2256         if (o == NULL) {
2257                 exceptions_print_stacktrace();
2258                 return NULL;
2259         }
2260
2261         return javastring_tochar(o);
2262 }
2263
2264
2265 /* vm_compile_all **************************************************************
2266
2267    Compile all methods found in the bootclasspath.
2268
2269 *******************************************************************************/
2270
2271 #if !defined(NDEBUG)
2272 static void vm_compile_all(void)
2273 {
2274         classinfo              *c;
2275         methodinfo             *m;
2276         u4                      slot;
2277         classcache_name_entry  *nmen;
2278         classcache_class_entry *clsen;
2279         s4                      i;
2280
2281         /* create all classes found in the bootclasspath */
2282         /* XXX currently only works with zip/jar's */
2283
2284         loader_load_all_classes();
2285
2286         /* link all classes */
2287
2288         for (slot = 0; slot < hashtable_classcache.size; slot++) {
2289                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
2290
2291                 for (; nmen; nmen = nmen->hashlink) {
2292                         /* iterate over all class entries */
2293
2294                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
2295                                 c = clsen->classobj;
2296
2297                                 if (c == NULL)
2298                                         continue;
2299
2300                                 if (!(c->state & CLASS_LINKED)) {
2301                                         if (!link_class(c)) {
2302                                                 fprintf(stderr, "Error linking: ");
2303                                                 utf_fprint_printable_ascii_classname(stderr, c->name);
2304                                                 fprintf(stderr, "\n");
2305
2306                                                 /* print out exception and cause */
2307
2308                                                 exceptions_print_current_exception();
2309
2310                                                 /* goto next class */
2311
2312                                                 continue;
2313                                         }
2314                                 }
2315
2316                                 /* compile all class methods */
2317
2318                                 for (i = 0; i < c->methodscount; i++) {
2319                                         m = &(c->methods[i]);
2320
2321                                         if (m->jcode != NULL) {
2322                                                 if (!jit_compile(m)) {
2323                                                         fprintf(stderr, "Error compiling: ");
2324                                                         utf_fprint_printable_ascii_classname(stderr, c->name);
2325                                                         fprintf(stderr, ".");
2326                                                         utf_fprint_printable_ascii(stderr, m->name);
2327                                                         utf_fprint_printable_ascii(stderr, m->descriptor);
2328                                                         fprintf(stderr, "\n");
2329
2330                                                         /* print out exception and cause */
2331
2332                                                         exceptions_print_current_exception();
2333                                                 }
2334                                         }
2335                                 }
2336                         }
2337                 }
2338         }
2339 }
2340 #endif /* !defined(NDEBUG) */
2341
2342
2343 /* vm_compile_method ***********************************************************
2344
2345    Compile a specific method.
2346
2347 *******************************************************************************/
2348
2349 #if !defined(NDEBUG)
2350 static void vm_compile_method(void)
2351 {
2352         methodinfo *m;
2353
2354         /* create, load and link the main class */
2355
2356         mainclass = load_class_bootstrap(utf_new_char(mainstring));
2357
2358         if (mainclass == NULL)
2359                 exceptions_print_stacktrace();
2360
2361         if (!link_class(mainclass))
2362                 exceptions_print_stacktrace();
2363
2364         if (opt_signature != NULL) {
2365                 m = class_resolveclassmethod(mainclass,
2366                                                                          utf_new_char(opt_method),
2367                                                                          utf_new_char(opt_signature),
2368                                                                          mainclass,
2369                                                                          false);
2370         }
2371         else {
2372                 m = class_resolveclassmethod(mainclass,
2373                                                                          utf_new_char(opt_method),
2374                                                                          NULL,
2375                                                                          mainclass,
2376                                                                          false);
2377         }
2378
2379         if (m == NULL)
2380                 vm_abort("vm_compile_method: java.lang.NoSuchMethodException: %s.%s",
2381                                  opt_method, opt_signature ? opt_signature : "");
2382                 
2383         jit_compile(m);
2384 }
2385 #endif /* !defined(NDEBUG) */
2386
2387
2388 /* vm_array_store_int **********************************************************
2389
2390    Helper function to store an integer into the argument array, taking
2391    care of architecture specific issues.
2392
2393 *******************************************************************************/
2394
2395 static void vm_array_store_int(uint64_t *array, paramdesc *pd, int32_t value)
2396 {
2397         int32_t index;
2398
2399         if (!pd->inmemory) {
2400                 index        = pd->index;
2401                 array[index] = (int64_t) value;
2402         }
2403         else {
2404                 index        = ARG_CNT + pd->index;
2405 #if SIZEOF_VOID_P == 8
2406                 array[index] = (int64_t) value;
2407 #else
2408 # if WORDS_BIGENDIAN == 1
2409                 array[index] = ((int64_t) value) << 32;
2410 # else
2411                 array[index] = (int64_t) value;
2412 # endif
2413 #endif
2414         }
2415 }
2416
2417
2418 /* vm_array_store_lng **********************************************************
2419
2420    Helper function to store a long into the argument array, taking
2421    care of architecture specific issues.
2422
2423 *******************************************************************************/
2424
2425 static void vm_array_store_lng(uint64_t *array, paramdesc *pd, int64_t value)
2426 {
2427         int32_t index;
2428
2429 #if SIZEOF_VOID_P == 8
2430         if (!pd->inmemory)
2431                 index = pd->index;
2432         else
2433                 index = ARG_CNT + pd->index;
2434
2435         array[index] = value;
2436 #else
2437         if (!pd->inmemory) {
2438                 /* move low and high 32-bits into it's own argument slot */
2439
2440                 index        = GET_LOW_REG(pd->index);
2441                 array[index] = value & 0x00000000ffffffff;
2442
2443                 index        = GET_HIGH_REG(pd->index);
2444                 array[index] = value >> 32;
2445         }
2446         else {
2447                 index        = ARG_CNT + pd->index;
2448                 array[index] = value;
2449         }
2450 #endif
2451 }
2452
2453
2454 /* vm_array_store_flt **********************************************************
2455
2456    Helper function to store a float into the argument array, taking
2457    care of architecture specific issues.
2458
2459 *******************************************************************************/
2460
2461 static void vm_array_store_flt(uint64_t *array, paramdesc *pd, uint64_t value)
2462 {
2463         int32_t index;
2464
2465         if (!pd->inmemory) {
2466 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2467                 index        = pd->index;
2468 #else
2469                 index        = INT_ARG_CNT + pd->index;
2470 #endif
2471 #if WORDS_BIGENDIAN == 1 && !defined(__POWERPC64__)
2472                 array[index] = value >> 32;
2473 #else
2474                 array[index] = value;
2475 #endif
2476         }
2477         else {
2478                 index        = ARG_CNT + pd->index;
2479 #if defined(__SPARC_64__)
2480                 array[index] = value >> 32;
2481 #else
2482                 array[index] = value;
2483 #endif
2484         }
2485 }
2486
2487
2488 /* vm_array_store_dbl **********************************************************
2489
2490    Helper function to store a double into the argument array, taking
2491    care of architecture specific issues.
2492
2493 *******************************************************************************/
2494
2495 static void vm_array_store_dbl(uint64_t *array, paramdesc *pd, uint64_t value)
2496 {
2497         int32_t index;
2498
2499         if (!pd->inmemory) {
2500 #if SIZEOF_VOID_P != 8 && defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2501                 index        = GET_LOW_REG(pd->index);
2502                 array[index] = value & 0x00000000ffffffff;
2503
2504                 index        = GET_HIGH_REG(pd->index);
2505                 array[index] = value >> 32;
2506 #else
2507                 index        = INT_ARG_CNT + pd->index;
2508                 array[index] = value;
2509 #endif
2510         }
2511         else {
2512                 index        = ARG_CNT + pd->index;
2513                 array[index] = value;
2514         }
2515 }
2516
2517
2518 /* vm_array_store_adr **********************************************************
2519
2520    Helper function to store an address into the argument array, taking
2521    care of architecture specific issues.
2522
2523 *******************************************************************************/
2524
2525 static void vm_array_store_adr(uint64_t *array, paramdesc *pd, void *value)
2526 {
2527         int32_t index;
2528
2529         if (!pd->inmemory) {
2530 #if defined(HAS_ADDRESS_REGISTER_FILE)
2531                 /* When the architecture has address registers, place them
2532                    after integer and float registers. */
2533
2534                 index        = INT_ARG_CNT + FLT_ARG_CNT + pd->index;
2535 #else
2536                 index        = pd->index;
2537 #endif
2538                 array[index] = (uint64_t) (intptr_t) value;
2539         }
2540         else {
2541                 index        = ARG_CNT + pd->index;
2542 #if SIZEOF_VOID_P == 8
2543                 array[index] = (uint64_t) (intptr_t) value;
2544 #else
2545 # if WORDS_BIGENDIAN == 1 && !defined(__POWERPC64__)
2546                 array[index] = ((uint64_t) (intptr_t) value) << 32;
2547 # else
2548                 array[index] = (uint64_t) (intptr_t) value;
2549 # endif
2550 #endif
2551         }
2552 }
2553
2554
2555 /* vm_vmargs_from_valist *******************************************************
2556
2557    XXX
2558
2559 *******************************************************************************/
2560
2561 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
2562 static void vm_vmargs_from_valist(methodinfo *m, java_objectheader *o,
2563                                                                   vm_arg *vmargs, va_list ap)
2564 {
2565         typedesc *paramtypes;
2566         s4        i;
2567
2568         paramtypes = m->parseddesc->paramtypes;
2569
2570         /* if method is non-static fill first block and skip `this' pointer */
2571
2572         i = 0;
2573
2574         if (o != NULL) {
2575                 /* the `this' pointer */
2576                 vmargs[0].type   = TYPE_ADR;
2577                 vmargs[0].data.l = (u8) (ptrint) o;
2578
2579                 paramtypes++;
2580                 i++;
2581         } 
2582
2583         for (; i < m->parseddesc->paramcount; i++, paramtypes++) {
2584                 switch (paramtypes->type) {
2585                 case TYPE_INT:
2586                         vmargs[i].type   = TYPE_INT;
2587                         vmargs[i].data.l = (s8) va_arg(ap, s4);
2588                         break;
2589
2590                 case TYPE_LNG:
2591                         vmargs[i].type   = TYPE_LNG;
2592                         vmargs[i].data.l = (s8) va_arg(ap, s8);
2593                         break;
2594
2595                 case TYPE_FLT:
2596                         vmargs[i].type   = TYPE_FLT;
2597 #if defined(__ALPHA__)
2598                         /* this keeps the assembler function much simpler */
2599
2600                         vmargs[i].data.d = (jdouble) va_arg(ap, jdouble);
2601 #else
2602                         vmargs[i].data.f = (jfloat) va_arg(ap, jdouble);
2603 #endif
2604                         break;
2605
2606                 case TYPE_DBL:
2607                         vmargs[i].type   = TYPE_DBL;
2608                         vmargs[i].data.d = (jdouble) va_arg(ap, jdouble);
2609                         break;
2610
2611                 case TYPE_ADR: 
2612                         vmargs[i].type   = TYPE_ADR;
2613                         vmargs[i].data.l = (u8) (ptrint) va_arg(ap, void*);
2614                         break;
2615                 }
2616         }
2617 }
2618 #else
2619 uint64_t *vm_array_from_valist(methodinfo *m, java_objectheader *o, va_list ap)
2620 {
2621         methoddesc *md;
2622         paramdesc  *pd;
2623         typedesc   *td;
2624         uint64_t   *array;
2625         int32_t     i;
2626         imm_union   value;
2627
2628         /* get the descriptors */
2629
2630         md = m->parseddesc;
2631         pd = md->params;
2632         td = md->paramtypes;
2633
2634         /* allocate argument array */
2635
2636         array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2637
2638         /* if method is non-static fill first block and skip `this' pointer */
2639
2640         i = 0;
2641
2642         if (o != NULL) {
2643                 /* the `this' pointer */
2644                 vm_array_store_adr(array, pd, o);
2645
2646                 pd++;
2647                 td++;
2648                 i++;
2649         } 
2650
2651         for (; i < md->paramcount; i++, pd++, td++) {
2652                 switch (td->type) {
2653                 case TYPE_INT:
2654                         value.i = va_arg(ap, int32_t);
2655                         vm_array_store_int(array, pd, value.i);
2656                         break;
2657
2658                 case TYPE_LNG:
2659                         value.l = va_arg(ap, int64_t);
2660                         vm_array_store_lng(array, pd, value.l);
2661                         break;
2662
2663                 case TYPE_FLT:
2664 #if defined(__ALPHA__) || defined(__POWERPC64__)
2665                         /* this keeps the assembler function much simpler */
2666
2667                         value.d = (double) va_arg(ap, double);
2668 #else
2669                         value.f = (float) va_arg(ap, double);
2670 #endif
2671                         vm_array_store_flt(array, pd, value.l);
2672                         break;
2673
2674                 case TYPE_DBL:
2675                         value.d = va_arg(ap, double);
2676                         vm_array_store_dbl(array, pd, value.l);
2677                         break;
2678
2679                 case TYPE_ADR: 
2680                         value.a = va_arg(ap, void*);
2681                         vm_array_store_adr(array, pd, value.a);
2682                         break;
2683                 }
2684         }
2685
2686         return array;
2687 }
2688 #endif
2689
2690
2691 /* vm_vmargs_from_jvalue *******************************************************
2692
2693    XXX
2694
2695 *******************************************************************************/
2696
2697 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
2698 static void vm_vmargs_from_jvalue(methodinfo *m, java_objectheader *o,
2699                                                                   vm_arg *vmargs, const jvalue *args)
2700 {
2701         typedesc *paramtypes;
2702         s4        i;
2703         s4        j;
2704
2705         paramtypes = m->parseddesc->paramtypes;
2706
2707         /* if method is non-static fill first block and skip `this' pointer */
2708
2709         i = 0;
2710
2711         if (o != NULL) {
2712                 /* the `this' pointer */
2713                 vmargs[0].type   = TYPE_ADR;
2714                 vmargs[0].data.l = (u8) (ptrint) o;
2715
2716                 paramtypes++;
2717                 i++;
2718         } 
2719
2720         for (j = 0; i < m->parseddesc->paramcount; i++, j++, paramtypes++) {
2721                 switch (paramtypes->decltype) {
2722                 case TYPE_INT:
2723                         vmargs[i].type   = TYPE_INT;
2724                         vmargs[i].data.l = (s8) args[j].i;
2725                         break;
2726
2727                 case TYPE_LNG:
2728                         vmargs[i].type   = TYPE_LNG;
2729                         vmargs[i].data.l = (s8) args[j].j;
2730                         break;
2731
2732                 case TYPE_FLT:
2733                         vmargs[i].type = TYPE_FLT;
2734 #if defined(__ALPHA__)
2735                         /* this keeps the assembler function much simpler */
2736
2737                         vmargs[i].data.d = (jdouble) args[j].f;
2738 #else
2739                         vmargs[i].data.f = args[j].f;
2740 #endif
2741                         break;
2742
2743                 case TYPE_DBL:
2744                         vmargs[i].type   = TYPE_DBL;
2745                         vmargs[i].data.d = args[j].d;
2746                         break;
2747
2748                 case TYPE_ADR: 
2749                         vmargs[i].type   = TYPE_ADR;
2750                         vmargs[i].data.l = (u8) (ptrint) args[j].l;
2751                         break;
2752                 }
2753         }
2754 }
2755 #else
2756 static uint64_t *vm_array_from_jvalue(methodinfo *m, java_objectheader *o,
2757                                                                           const jvalue *args)
2758 {
2759         methoddesc *md;
2760         paramdesc  *pd;
2761         typedesc   *td;
2762         uint64_t   *array;
2763         int32_t     i;
2764         int32_t     j;
2765
2766         /* get the descriptors */
2767
2768         md = m->parseddesc;
2769         pd = md->params;
2770         td = md->paramtypes;
2771
2772         /* allocate argument array */
2773
2774 #if defined(HAS_ADDRESS_REGISTER_FILE)
2775         array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + ADR_ARG_CNT + md->memuse);
2776 #else
2777         array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2778 #endif
2779
2780         /* if method is non-static fill first block and skip `this' pointer */
2781
2782         i = 0;
2783
2784         if (o != NULL) {
2785                 /* the `this' pointer */
2786                 vm_array_store_adr(array, pd, o);
2787
2788                 pd++;
2789                 td++;
2790                 i++;
2791         } 
2792
2793         for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2794                 switch (td->decltype) {
2795                 case TYPE_INT:
2796                         vm_array_store_int(array, pd, args[j].i);
2797                         break;
2798
2799                 case TYPE_LNG:
2800                         vm_array_store_lng(array, pd, args[j].j);
2801                         break;
2802
2803                 case TYPE_FLT:
2804                         vm_array_store_flt(array, pd, args[j].j);
2805                         break;
2806
2807                 case TYPE_DBL:
2808                         vm_array_store_dbl(array, pd, args[j].j);
2809                         break;
2810
2811                 case TYPE_ADR: 
2812                         vm_array_store_adr(array, pd, args[j].l);
2813                         break;
2814                 }
2815         }
2816
2817         return array;
2818 }
2819 #endif
2820
2821 /* vm_vmargs_from_objectarray **************************************************
2822
2823    XXX
2824
2825 *******************************************************************************/
2826
2827 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
2828 bool vm_vmargs_from_objectarray(methodinfo *m, java_objectheader *o,
2829                                                                 vm_arg *vmargs, java_objectarray *params)
2830 {
2831         java_objectheader *param;
2832         typedesc          *paramtypes;
2833         classinfo         *c;
2834         int32_t            i;
2835         int32_t            j;
2836         int64_t            value;
2837
2838         paramtypes = m->parseddesc->paramtypes;
2839
2840         /* if method is non-static fill first block and skip `this' pointer */
2841
2842         i = 0;
2843
2844         if (o != NULL) {
2845                 /* this pointer */
2846                 vmargs[0].type   = TYPE_ADR;
2847                 vmargs[0].data.l = (uint64_t) (intptr_t) o;
2848
2849                 paramtypes++;
2850                 i++;
2851         }
2852
2853         for (j = 0; i < m->parseddesc->paramcount; i++, j++, paramtypes++) {
2854                 switch (paramtypes->type) {
2855                 /* primitive types */
2856                 case TYPE_INT:
2857                 case TYPE_LNG:
2858                 case TYPE_FLT:
2859                 case TYPE_DBL:
2860                         param = params->data[j];
2861
2862                         if (param == NULL)
2863                                 goto illegal_arg;
2864
2865                         /* internally used data type */
2866                         vmargs[i].type = paramtypes->type;
2867
2868                         /* convert the value according to its declared type */
2869
2870                         c = param->vftbl->class;
2871
2872                         switch (paramtypes->decltype) {
2873                         case PRIMITIVETYPE_BOOLEAN:
2874                                 if (c == class_java_lang_Boolean)
2875                                         value = (int64_t) ((java_lang_Boolean *) param)->value;
2876                                 else
2877                                         goto illegal_arg;
2878
2879                                 vmargs[i].data.l = value;
2880                                 break;
2881
2882                         case PRIMITIVETYPE_BYTE:
2883                                 if (c == class_java_lang_Byte)
2884                                         value = (int64_t) ((java_lang_Byte *) param)->value;
2885                                 else
2886                                         goto illegal_arg;
2887
2888                                 vmargs[i].data.l = value;
2889                                 break;
2890
2891                         case PRIMITIVETYPE_CHAR:
2892                                 if (c == class_java_lang_Character)
2893                                         value = (int64_t) ((java_lang_Character *) param)->value;
2894                                 else
2895                                         goto illegal_arg;
2896
2897                                 vmargs[i].data.l = value;
2898                                 break;
2899
2900                         case PRIMITIVETYPE_SHORT:
2901                                 if (c == class_java_lang_Short)
2902                                         value = (int64_t) ((java_lang_Short *) param)->value;
2903                                 else if (c == class_java_lang_Byte)
2904                                         value = (int64_t) ((java_lang_Byte *) param)->value;
2905                                 else
2906                                         goto illegal_arg;
2907
2908                                 vmargs[i].data.l = value;
2909                                 break;
2910
2911                         case PRIMITIVETYPE_INT:
2912                                 if (c == class_java_lang_Integer)
2913                                         value = (int64_t) ((java_lang_Integer *) param)->value;
2914                                 else if (c == class_java_lang_Short)
2915                                         value = (int64_t) ((java_lang_Short *) param)->value;
2916                                 else if (c == class_java_lang_Byte)
2917                                         value = (int64_t) ((java_lang_Byte *) param)->value;
2918                                 else
2919                                         goto illegal_arg;
2920
2921                                 vmargs[i].data.l = value;
2922                                 break;
2923
2924                         case PRIMITIVETYPE_LONG:
2925                                 if (c == class_java_lang_Long)
2926                                         value = (int64_t) ((java_lang_Long *) param)->value;
2927                                 else if (c == class_java_lang_Integer)
2928                                         value = (int64_t) ((java_lang_Integer *) param)->value;
2929                                 else if (c == class_java_lang_Short)
2930                                         value = (int64_t) ((java_lang_Short *) param)->value;
2931                                 else if (c == class_java_lang_Byte)
2932                                         value = (int64_t) ((java_lang_Byte *) param)->value;
2933                                 else
2934                                         goto illegal_arg;
2935
2936                                 vmargs[i].data.l = value;
2937                                 break;
2938
2939                         case PRIMITIVETYPE_FLOAT:
2940                                 if (c == class_java_lang_Float)
2941                                         vmargs[i].data.f = (jfloat) ((java_lang_Float *) param)->value;
2942                                 else
2943                                         goto illegal_arg;
2944                                 break;
2945
2946                         case PRIMITIVETYPE_DOUBLE:
2947                                 if (c == class_java_lang_Double)
2948                                         vmargs[i].data.d = (jdouble) ((java_lang_Double *) param)->value;
2949                                 else if (c == class_java_lang_Float)
2950                                         vmargs[i].data.f = (jfloat) ((java_lang_Float *) param)->value;
2951                                 else
2952                                         goto illegal_arg;
2953                                 break;
2954
2955                         default:
2956                                 goto illegal_arg;
2957                         }
2958                         break;
2959                 
2960                 case TYPE_ADR:
2961                         if (!resolve_class_from_typedesc(paramtypes, true, true, &c))
2962                                 return false;
2963
2964                         if (params->data[j] != 0) {
2965                                 if (paramtypes->arraydim > 0) {
2966                                         if (!builtin_arrayinstanceof(params->data[j], c))
2967                                                 goto illegal_arg;
2968
2969                                 } else {
2970                                         if (!builtin_instanceof(params->data[j], c))
2971                                                 goto illegal_arg;
2972                                 }
2973                         }
2974
2975                         vmargs[i].type   = TYPE_ADR;
2976                         vmargs[i].data.l = (u8) (ptrint) params->data[j];
2977                         break;
2978
2979                 default:
2980                         goto illegal_arg;
2981                 }
2982         }
2983
2984 /*      if (rettype) */
2985 /*              *rettype = descr->returntype.decltype; */
2986
2987         return true;
2988
2989 illegal_arg:
2990         exceptions_throw_illegalargumentexception();
2991         return false;
2992 }
2993 #else
2994 uint64_t *vm_array_from_objectarray(methodinfo *m, java_objectheader *o,
2995                                                                         java_objectarray *params)
2996 {
2997         methoddesc        *md;
2998         paramdesc         *pd;
2999         typedesc          *td;
3000         uint64_t          *array;
3001         java_objectheader *param;
3002         classinfo         *c;
3003         int32_t            i;
3004         int32_t            j;
3005         imm_union          value;
3006
3007         /* get the descriptors */
3008
3009         md = m->parseddesc;
3010         pd = md->params;
3011         td = md->paramtypes;
3012
3013         /* allocate argument array */
3014
3015         array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
3016
3017         /* if method is non-static fill first block and skip `this' pointer */
3018
3019         i = 0;
3020
3021         if (o != NULL) {
3022                 /* this pointer */
3023                 vm_array_store_adr(array, pd, o);
3024
3025                 pd++;
3026                 td++;
3027                 i++;
3028         }
3029
3030         for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
3031                 param = params->data[j];
3032
3033                 switch (td->type) {
3034                 case TYPE_INT:
3035                         if (param == NULL)
3036                                 goto illegal_arg;
3037
3038                         /* convert the value according to its declared type */
3039
3040                         c = param->vftbl->class;
3041
3042                         switch (td->decltype) {
3043                         case PRIMITIVETYPE_BOOLEAN:
3044                                 if (c == class_java_lang_Boolean)
3045                                         value.i = ((java_lang_Boolean *) param)->value;
3046                                 else
3047                                         goto illegal_arg;
3048                                 break;
3049
3050                         case PRIMITIVETYPE_BYTE:
3051                                 if (c == class_java_lang_Byte)
3052                                         value.i = ((java_lang_Byte *) param)->value;
3053                                 else
3054                                         goto illegal_arg;
3055                                 break;
3056
3057                         case PRIMITIVETYPE_CHAR:
3058                                 if (c == class_java_lang_Character)
3059                                         value.i = ((java_lang_Character *) param)->value;
3060                                 else
3061                                         goto illegal_arg;
3062                                 break;
3063
3064                         case PRIMITIVETYPE_SHORT:
3065                                 if (c == class_java_lang_Short)
3066                                         value.i = ((java_lang_Short *) param)->value;
3067                                 else if (c == class_java_lang_Byte)
3068                                         value.i = ((java_lang_Byte *) param)->value;
3069                                 else
3070                                         goto illegal_arg;
3071                                 break;
3072
3073                         case PRIMITIVETYPE_INT:
3074                                 if (c == class_java_lang_Integer)
3075                                         value.i = ((java_lang_Integer *) param)->value;
3076                                 else if (c == class_java_lang_Short)
3077                                         value.i = ((java_lang_Short *) param)->value;
3078                                 else if (c == class_java_lang_Byte)
3079                                         value.i = ((java_lang_Byte *) param)->value;
3080                                 else
3081                                         goto illegal_arg;
3082                                 break;
3083
3084                         default:
3085                                 goto illegal_arg;
3086                         }
3087
3088                         vm_array_store_int(array, pd, value.i);
3089                         break;
3090
3091                 case TYPE_LNG:
3092                         if (param == NULL)
3093                                 goto illegal_arg;
3094
3095                         /* convert the value according to its declared type */
3096
3097                         c = param->vftbl->class;
3098
3099                         switch (td->decltype) {
3100                         case PRIMITIVETYPE_LONG:
3101                                 if (c == class_java_lang_Long)
3102                                         value.l = ((java_lang_Long *) param)->value;
3103                                 else if (c == class_java_lang_Integer)
3104                                         value.l = (int64_t) ((java_lang_Integer *) param)->value;
3105                                 else if (c == class_java_lang_Short)
3106                                         value.l = (int64_t) ((java_lang_Short *) param)->value;
3107                                 else if (c == class_java_lang_Byte)
3108                                         value.l = (int64_t) ((java_lang_Byte *) param)->value;
3109                                 else
3110                                         goto illegal_arg;
3111                                 break;
3112
3113                         default:
3114                                 goto illegal_arg;
3115                         }
3116
3117                         vm_array_store_lng(array, pd, value.l);
3118                         break;
3119
3120                 case TYPE_FLT:
3121                         if (param == NULL)
3122                                 goto illegal_arg;
3123
3124                         /* convert the value according to its declared type */
3125
3126                         c = param->vftbl->class;
3127
3128                         switch (td->decltype) {
3129                         case PRIMITIVETYPE_FLOAT:
3130                                 if (c == class_java_lang_Float)
3131                                         value.f = ((java_lang_Float *) param)->value;
3132                                 else
3133                                         goto illegal_arg;
3134                                 break;
3135
3136                         default:
3137                                 goto illegal_arg;
3138                         }
3139
3140                         vm_array_store_flt(array, pd, value.l);
3141                         break;
3142
3143                 case TYPE_DBL:
3144                         if (param == NULL)
3145                                 goto illegal_arg;
3146
3147                         /* convert the value according to its declared type */
3148
3149                         c = param->vftbl->class;
3150
3151                         switch (td->decltype) {
3152                         case PRIMITIVETYPE_DOUBLE:
3153                                 if (c == class_java_lang_Double)
3154                                         value.d = ((java_lang_Double *) param)->value;
3155                                 else if (c == class_java_lang_Float)
3156                                         value.f = ((java_lang_Float *) param)->value;
3157                                 else
3158                                         goto illegal_arg;
3159                                 break;
3160
3161                         default:
3162                                 goto illegal_arg;
3163                         }
3164
3165                         vm_array_store_dbl(array, pd, value.l);
3166                         break;
3167                 
3168                 case TYPE_ADR:
3169                         if (!resolve_class_from_typedesc(td, true, true, &c))
3170                                 return false;
3171
3172                         if (param != NULL) {
3173                                 if (td->arraydim > 0) {
3174                                         if (!builtin_arrayinstanceof(param, c))
3175                                                 goto illegal_arg;
3176                                 }
3177                                 else {
3178                                         if (!builtin_instanceof(param, c))
3179                                                 goto illegal_arg;
3180                                 }
3181                         }
3182
3183                         vm_array_store_adr(array, pd, param);
3184                         break;
3185
3186                 default:
3187                         goto illegal_arg;
3188                 }
3189         }
3190
3191         return array;
3192
3193 illegal_arg:
3194         exceptions_throw_illegalargumentexception();
3195         return NULL;
3196 }
3197 #endif
3198
3199
3200 /* vm_call_method **************************************************************
3201
3202    Calls a Java method with a variable number of arguments and returns
3203    an address.
3204
3205 *******************************************************************************/
3206
3207 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
3208 {
3209         va_list            ap;
3210         java_objectheader *ro;
3211
3212         va_start(ap, o);
3213         ro = vm_call_method_valist(m, o, ap);
3214         va_end(ap);
3215
3216         return ro;
3217 }
3218
3219
3220 /* vm_call_method_valist *******************************************************
3221
3222    Calls a Java method with a variable number of arguments, passed via
3223    a va_list, and returns an address.
3224
3225 *******************************************************************************/
3226
3227 java_objectheader *vm_call_method_valist(methodinfo *m, java_objectheader *o,
3228                                                                                  va_list ap)
3229 {
3230 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3231         s4                 vmargscount;
3232         vm_arg            *vmargs;
3233         java_objectheader *ro;
3234         s4                 dumpsize;
3235
3236         /* mark start of dump memory area */
3237
3238         dumpsize = dump_size();
3239
3240         /* get number of Java method arguments */
3241
3242         vmargscount = m->parseddesc->paramcount;
3243
3244         /* allocate vm_arg array */
3245
3246         vmargs = DMNEW(vm_arg, vmargscount);
3247
3248         /* fill the vm_arg array from a va_list */
3249
3250         vm_vmargs_from_valist(m, o, vmargs, ap);
3251
3252         /* call the Java method */
3253
3254         ro = vm_call_method_vmarg(m, vmargscount, vmargs);
3255
3256         /* release dump area */
3257
3258         dump_release(dumpsize);
3259
3260         return ro;
3261 #else
3262         java_objectheader *ro;
3263         int32_t            dumpsize;
3264         uint64_t          *array;
3265
3266         /* mark start of dump memory area */
3267
3268         dumpsize = dump_size();
3269
3270         /* fill the argument array from a va_list */
3271
3272         array = vm_array_from_valist(m, o, ap);
3273
3274         /* call the Java method */
3275
3276         ro = vm_call_array(m, array);
3277
3278         /* release dump area */
3279
3280         dump_release(dumpsize);
3281
3282         return ro;
3283 #endif
3284 }
3285
3286
3287 /* vm_call_method_jvalue *******************************************************
3288
3289    Calls a Java method with a variable number of arguments, passed via
3290    a jvalue array, and returns an address.
3291
3292 *******************************************************************************/
3293
3294 java_objectheader *vm_call_method_jvalue(methodinfo *m, java_objectheader *o,
3295                                                                                  const jvalue *args)
3296 {
3297 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3298         s4                 vmargscount;
3299         vm_arg            *vmargs;
3300         java_objectheader *ro;
3301         s4                 dumpsize;
3302
3303         /* mark start of dump memory area */
3304
3305         dumpsize = dump_size();
3306
3307         /* get number of Java method arguments */
3308
3309         vmargscount = m->parseddesc->paramcount;
3310
3311         /* allocate vm_arg array */
3312
3313         vmargs = DMNEW(vm_arg, vmargscount);
3314
3315         /* fill the vm_arg array from a va_list */
3316
3317         vm_vmargs_from_jvalue(m, o, vmargs, args);
3318
3319         /* call the Java method */
3320
3321         ro = vm_call_method_vmarg(m, vmargscount, vmargs);
3322
3323         /* release dump area */
3324
3325         dump_release(dumpsize);
3326
3327         return ro;
3328 #else
3329         java_objectheader *ro;
3330         int32_t            dumpsize;
3331         uint64_t          *array;
3332
3333         /* mark start of dump memory area */
3334
3335         dumpsize = dump_size();
3336
3337         /* fill the argument array from a va_list */
3338
3339         array = vm_array_from_jvalue(m, o, args);
3340
3341         /* call the Java method */
3342
3343         ro = vm_call_array(m, array);
3344
3345         /* release dump area */
3346
3347         dump_release(dumpsize);
3348
3349         return ro;
3350 #endif
3351 }
3352
3353
3354 /* vm_call_array ***************************************************************
3355
3356    Calls a Java method with a variable number of arguments, passed via
3357    an argument array, and returns an address.
3358
3359 *******************************************************************************/
3360
3361 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3362 java_objectheader *vm_call_method_vmarg(methodinfo *m, s4 vmargscount,
3363                                                                                 vm_arg *vmargs)
3364 {
3365         java_objectheader *o;
3366
3367         STATISTICS(count_calls_native_to_java++);
3368
3369 #if defined(ENABLE_JIT)
3370 # if defined(ENABLE_INTRP)
3371         if (opt_intrp)
3372                 o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
3373         else
3374 # endif
3375                 o = asm_vm_call_method(m, vmargscount, vmargs);
3376 #else
3377         o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
3378 #endif
3379
3380         return o;
3381 }
3382 #else
3383 java_objectheader *vm_call_array(methodinfo *m, uint64_t *array)
3384 {
3385         methoddesc        *md;
3386         java_objectheader *o;
3387
3388         md = m->parseddesc;
3389
3390         /* compile the method if not already done */
3391
3392         if (m->code == NULL)
3393                 if (!jit_compile(m))
3394                         return NULL;
3395
3396         STATISTICS(count_calls_native_to_java++);
3397
3398 #if defined(ENABLE_JIT)
3399 # if defined(ENABLE_INTRP)
3400         if (opt_intrp)
3401                 o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
3402         else
3403 # endif
3404                 o = asm_vm_call_method(m->code->entrypoint, array, md->memuse);
3405 #else
3406         o = intrp_asm_vm_call_method(m, vmargscount, vmargs);
3407 #endif
3408
3409         return o;
3410 }
3411 #endif
3412
3413
3414 /* vm_call_int_array ***********************************************************
3415
3416    Calls a Java method with a variable number of arguments, passed via
3417    an argument array, and returns an integer (int32_t).
3418
3419 *******************************************************************************/
3420
3421 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3422 s4 vm_call_method_int_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
3423 {
3424         s4 i;
3425
3426         STATISTICS(count_calls_native_to_java++);
3427
3428 #if defined(ENABLE_JIT)
3429 # if defined(ENABLE_INTRP)
3430         if (opt_intrp)
3431                 i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
3432         else
3433 # endif
3434                 i = asm_vm_call_method_int(m, vmargscount, vmargs);
3435 #else
3436         i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
3437 #endif
3438
3439         return i;
3440 }
3441 #else
3442 int32_t vm_call_int_array(methodinfo *m, uint64_t *array)
3443 {
3444         methoddesc *md;
3445         int32_t     i;
3446
3447         md = m->parseddesc;
3448
3449         /* compile the method if not already done */
3450
3451         if (m->code == NULL)
3452                 if (!jit_compile(m))
3453                         return 0;
3454
3455         STATISTICS(count_calls_native_to_java++);
3456
3457 #if defined(ENABLE_JIT)
3458 # if defined(ENABLE_INTRP)
3459         if (opt_intrp)
3460                 i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
3461         else
3462 # endif
3463                 i = asm_vm_call_method_int(m->code->entrypoint, array, md->memuse);
3464 #else
3465         i = intrp_asm_vm_call_method_int(m, vmargscount, vmargs);
3466 #endif
3467
3468         return i;
3469 }
3470 #endif
3471
3472
3473 /* vm_call_method_int **********************************************************
3474
3475    Calls a Java method with a variable number of arguments and returns
3476    an integer (s4).
3477
3478 *******************************************************************************/
3479
3480 s4 vm_call_method_int(methodinfo *m, java_objectheader *o, ...)
3481 {
3482         va_list ap;
3483         s4      i;
3484
3485         va_start(ap, o);
3486         i = vm_call_method_int_valist(m, o, ap);
3487         va_end(ap);
3488
3489         return i;
3490 }
3491
3492
3493 /* vm_call_method_int_valist ***************************************************
3494
3495    Calls a Java method with a variable number of arguments, passed via
3496    a va_list, and returns an integer (int32_t).
3497
3498 *******************************************************************************/
3499
3500 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3501 s4 vm_call_method_int_valist(methodinfo *m, java_objectheader *o, va_list ap)
3502 {
3503         s4      vmargscount;
3504         vm_arg *vmargs;
3505         s4      i;
3506         s4      dumpsize;
3507
3508         /* mark start of dump memory area */
3509
3510         dumpsize = dump_size();
3511
3512         /* get number of Java method arguments */
3513
3514         vmargscount = m->parseddesc->paramcount;
3515
3516         /* allocate vm_arg array */
3517
3518         vmargs = DMNEW(vm_arg, vmargscount);
3519
3520         /* fill the vm_arg array from a va_list */
3521
3522         vm_vmargs_from_valist(m, o, vmargs, ap);
3523
3524         /* call the Java method */
3525
3526         i = vm_call_method_int_vmarg(m, vmargscount, vmargs);
3527
3528         /* release dump area */
3529
3530         dump_release(dumpsize);
3531
3532         return i;
3533 }
3534 #else
3535 int32_t vm_call_method_int_valist(methodinfo *m, java_objectheader *o, va_list ap)
3536 {
3537         int32_t   dumpsize;
3538         uint64_t *array;
3539         int32_t   i;
3540
3541         /* mark start of dump memory area */
3542
3543         dumpsize = dump_size();
3544
3545         /* fill the argument array from a va_list */
3546
3547         array = vm_array_from_valist(m, o, ap);
3548
3549         /* call the Java method */
3550
3551         i = vm_call_int_array(m, array);
3552
3553         /* release dump area */
3554
3555         dump_release(dumpsize);
3556
3557         return i;
3558 }
3559 #endif
3560
3561
3562 /* vm_call_method_int_jvalue ***************************************************
3563
3564    Calls a Java method with a variable number of arguments, passed via
3565    a jvalue array, and returns an integer (s4).
3566
3567 *******************************************************************************/
3568
3569 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3570 s4 vm_call_method_int_jvalue(methodinfo *m, java_objectheader *o,
3571                                                          const jvalue *args)
3572 {
3573         s4      vmargscount;
3574         vm_arg *vmargs;
3575         s4      i;
3576         s4      dumpsize;
3577
3578         /* mark start of dump memory area */
3579
3580         dumpsize = dump_size();
3581
3582         /* get number of Java method arguments */
3583
3584         vmargscount = m->parseddesc->paramcount;
3585
3586         /* allocate vm_arg array */
3587
3588         vmargs = DMNEW(vm_arg, vmargscount);
3589
3590         /* fill the vm_arg array from a va_list */
3591
3592         vm_vmargs_from_jvalue(m, o, vmargs, args);
3593
3594         /* call the Java method */
3595
3596         i = vm_call_method_int_vmarg(m, vmargscount, vmargs);
3597
3598         /* release dump area */
3599
3600         dump_release(dumpsize);
3601
3602         return i;
3603 }
3604 #else
3605 int32_t vm_call_method_int_jvalue(methodinfo *m, java_objectheader *o,
3606                                                                   const jvalue *args)
3607 {
3608         int32_t   dumpsize;
3609         uint64_t *array;
3610         int32_t   i;
3611
3612         /* mark start of dump memory area */
3613
3614         dumpsize = dump_size();
3615
3616         /* fill the argument array from a va_list */
3617
3618         array = vm_array_from_jvalue(m, o, args);
3619
3620         /* call the Java method */
3621
3622         i = vm_call_int_array(m, array);
3623
3624         /* release dump area */
3625
3626         dump_release(dumpsize);
3627
3628         return i;
3629 }
3630 #endif
3631
3632
3633 /* vm_call_long_array **********************************************************
3634
3635    Calls a Java method with a variable number of arguments, passed via
3636    an argument array, and returns a long (int64_t).
3637
3638 *******************************************************************************/
3639
3640 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3641 s8 vm_call_method_long_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
3642 {
3643         s8 l;
3644
3645         STATISTICS(count_calls_native_to_java++);
3646
3647 #if defined(ENABLE_JIT)
3648 # if defined(ENABLE_INTRP)
3649         if (opt_intrp)
3650                 l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
3651         else
3652 # endif
3653                 l = asm_vm_call_method_long(m, vmargscount, vmargs);
3654 #else
3655         l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
3656 #endif
3657
3658         return l;
3659 }
3660 #else
3661 int64_t vm_call_long_array(methodinfo *m, uint64_t *array)
3662 {
3663         methoddesc *md;
3664         int64_t     l;
3665
3666         md = m->parseddesc;
3667
3668         /* compile the method if not already done */
3669
3670         if (m->code == NULL)
3671                 if (!jit_compile(m))
3672                         return 0;
3673
3674         STATISTICS(count_calls_native_to_java++);
3675
3676 #if defined(ENABLE_JIT)
3677 # if defined(ENABLE_INTRP)
3678         if (opt_intrp)
3679                 l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
3680         else
3681 # endif
3682                 l = asm_vm_call_method_long(m->code->entrypoint, array, md->memuse);
3683 #else
3684         l = intrp_asm_vm_call_method_long(m, vmargscount, vmargs);
3685 #endif
3686
3687         return l;
3688 }
3689 #endif
3690
3691
3692 /* vm_call_method_long *********************************************************
3693
3694    Calls a Java method with a variable number of arguments and returns
3695    a long (s8).
3696
3697 *******************************************************************************/
3698
3699 s8 vm_call_method_long(methodinfo *m, java_objectheader *o, ...)
3700 {
3701         va_list ap;
3702         s8      l;
3703
3704         va_start(ap, o);
3705         l = vm_call_method_long_valist(m, o, ap);
3706         va_end(ap);
3707
3708         return l;
3709 }
3710
3711
3712 /* vm_call_method_long_valist **************************************************
3713
3714    Calls a Java method with a variable number of arguments, passed via
3715    a va_list, and returns a long (s8).
3716
3717 *******************************************************************************/
3718
3719 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3720 s8 vm_call_method_long_valist(methodinfo *m, java_objectheader *o, va_list ap)
3721 {
3722         s4      vmargscount;
3723         vm_arg *vmargs;
3724         s8      l;
3725         s4      dumpsize;
3726
3727         /* mark start of dump memory area */
3728
3729         dumpsize = dump_size();
3730
3731         /* get number of Java method arguments */
3732
3733         vmargscount = m->parseddesc->paramcount;
3734
3735         /* allocate vm_arg array */
3736
3737         vmargs = DMNEW(vm_arg, vmargscount);
3738
3739         /* fill the vm_arg array from a va_list */
3740
3741         vm_vmargs_from_valist(m, o, vmargs, ap);
3742
3743         /* call the Java method */
3744
3745         l = vm_call_method_long_vmarg(m, vmargscount, vmargs);
3746
3747         /* release dump area */
3748
3749         dump_release(dumpsize);
3750
3751         return l;
3752 }
3753 #else
3754 int64_t vm_call_method_long_valist(methodinfo *m, java_objectheader *o, va_list ap)
3755 {
3756         int32_t   dumpsize;
3757         uint64_t *array;
3758         int64_t   l;
3759
3760         /* mark start of dump memory area */
3761
3762         dumpsize = dump_size();
3763
3764         /* fill the argument array from a va_list */
3765
3766         array = vm_array_from_valist(m, o, ap);
3767
3768         /* call the Java method */
3769
3770         l = vm_call_long_array(m, array);
3771
3772         /* release dump area */
3773
3774         dump_release(dumpsize);
3775
3776         return l;
3777 }
3778 #endif
3779
3780
3781 /* vm_call_method_long_jvalue **************************************************
3782
3783    Calls a Java method with a variable number of arguments, passed via
3784    a jvalue array, and returns a long (s8).
3785
3786 *******************************************************************************/
3787
3788 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3789 s8 vm_call_method_long_jvalue(methodinfo *m, java_objectheader *o,
3790                                                           const jvalue *args)
3791 {
3792         s4      vmargscount;
3793         vm_arg *vmargs;
3794         s8      l;
3795         s4      dumpsize;
3796
3797         /* mark start of dump memory area */
3798
3799         dumpsize = dump_size();
3800
3801         /* get number of Java method arguments */
3802
3803         vmargscount = m->parseddesc->paramcount;
3804
3805         /* allocate vm_arg array */
3806
3807         vmargs = DMNEW(vm_arg, vmargscount);
3808
3809         /* fill the vm_arg array from a va_list */
3810
3811         vm_vmargs_from_jvalue(m, o, vmargs, args);
3812
3813         /* call the Java method */
3814
3815         l = vm_call_method_long_vmarg(m, vmargscount, vmargs);
3816
3817         /* release dump area */
3818
3819         dump_release(dumpsize);
3820
3821         return l;
3822 }
3823 #else
3824 int64_t vm_call_method_long_jvalue(methodinfo *m, java_objectheader *o,
3825                                                                    const jvalue *args)
3826 {
3827         int32_t   dumpsize;
3828         uint64_t *array;
3829         int64_t   l;
3830
3831         /* mark start of dump memory area */
3832
3833         dumpsize = dump_size();
3834
3835         /* fill the argument array from a va_list */
3836
3837         array = vm_array_from_jvalue(m, o, args);
3838
3839         /* call the Java method */
3840
3841         l = vm_call_long_array(m, array);
3842
3843         /* release dump area */
3844
3845         dump_release(dumpsize);
3846
3847         return l;
3848 }
3849 #endif
3850
3851
3852 /* vm_call_float_array *********************************************************
3853
3854    Calls a Java method with a variable number of arguments and returns
3855    an float.
3856
3857 *******************************************************************************/
3858
3859 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3860 float vm_call_method_float_vmarg(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
3861 {
3862         float f;
3863
3864         vm_abort("IMPLEMENT ME!");
3865
3866         STATISTICS(count_calls_native_to_java++);
3867
3868 #if defined(ENABLE_JIT)
3869 # if defined(ENABLE_INTRP)
3870         if (opt_intrp)
3871                 f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
3872         else
3873 # endif
3874                 f = asm_vm_call_method_float(m, vmargscount, vmargs);
3875 #else
3876         f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
3877 #endif
3878
3879         return f;
3880 }
3881 #else
3882 float vm_call_float_array(methodinfo *m, uint64_t *array)
3883 {
3884         methoddesc *md;
3885         float       f;
3886
3887         md = m->parseddesc;
3888
3889         /* compile the method if not already done */
3890
3891         if (m->code == NULL)
3892                 if (!jit_compile(m))
3893                         return 0;
3894
3895         STATISTICS(count_calls_native_to_java++);
3896
3897 #if defined(ENABLE_JIT)
3898 # if defined(ENABLE_INTRP)
3899         if (opt_intrp)
3900                 f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
3901         else
3902 # endif
3903                 f = asm_vm_call_method_float(m->code->entrypoint, array, md->memuse);
3904 #else
3905         f = intrp_asm_vm_call_method_float(m, vmargscount, vmargs);
3906 #endif
3907
3908         return f;
3909 }
3910 #endif
3911
3912 /* vm_call_method_float ********************************************************
3913
3914    Calls a Java method with a variable number of arguments and returns
3915    an float.
3916
3917 *******************************************************************************/
3918
3919 float vm_call_method_float(methodinfo *m, java_objectheader *o, ...)
3920 {
3921         va_list ap;
3922         float   f;
3923
3924         va_start(ap, o);
3925         f = vm_call_method_float_valist(m, o, ap);
3926         va_end(ap);
3927
3928         return f;
3929 }
3930
3931
3932 /* vm_call_method_float_valist *************************************************
3933
3934    Calls a Java method with a variable number of arguments, passed via
3935    a va_list, and returns a float.
3936
3937 *******************************************************************************/
3938
3939 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
3940 float vm_call_method_float_valist(methodinfo *m, java_objectheader *o,
3941                                                                   va_list ap)
3942 {
3943         s4      vmargscount;
3944         vm_arg *vmargs;
3945         float   f;
3946         s4      dumpsize;
3947
3948         /* mark start of dump memory area */
3949
3950         dumpsize = dump_size();
3951
3952         /* get number of Java method arguments */
3953
3954         vmargscount = m->parseddesc->paramcount;
3955
3956         /* allocate vm_arg array */
3957
3958         vmargs = DMNEW(vm_arg, vmargscount);
3959
3960         /* fill the vm_arg array from a va_list */
3961
3962         vm_vmargs_from_valist(m, o, vmargs, ap);
3963
3964         /* call the Java method */
3965
3966         f = vm_call_method_float_vmarg(m, vmargscount, vmargs);
3967
3968         /* release dump area */
3969
3970         dump_release(dumpsize);
3971
3972         return f;
3973 }
3974 #else
3975 float vm_call_method_float_valist(methodinfo *m, java_objectheader *o, va_list ap)
3976 {
3977         int32_t   dumpsize;
3978         uint64_t *array;
3979         float     f;
3980
3981         /* mark start of dump memory area */
3982
3983         dumpsize = dump_size();
3984
3985         /* fill the argument array from a va_list */
3986
3987         array = vm_array_from_valist(m, o, ap);
3988
3989         /* call the Java method */
3990
3991         f = vm_call_float_array(m, array);
3992
3993         /* release dump area */
3994
3995         dump_release(dumpsize);
3996
3997         return f;
3998 }
3999 #endif
4000
4001 /* vm_call_method_float_jvalue *************************************************
4002
4003    Calls a Java method with a variable number of arguments, passed via
4004    a jvalue array, and returns a float.
4005
4006 *******************************************************************************/
4007
4008 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
4009 float vm_call_method_float_jvalue(methodinfo *m, java_objectheader *o,
4010                                                                   const jvalue *args)
4011 {
4012         s4      vmargscount;
4013         vm_arg *vmargs;
4014         float   f;
4015         s4      dumpsize;
4016
4017         /* mark start of dump memory area */
4018
4019         dumpsize = dump_size();
4020
4021         /* get number of Java method arguments */
4022
4023         vmargscount = m->parseddesc->paramcount;
4024
4025         /* allocate vm_arg array */
4026
4027         vmargs = DMNEW(vm_arg, vmargscount);
4028
4029         /* fill the vm_arg array from a va_list */
4030
4031         vm_vmargs_from_jvalue(m, o, vmargs, args);
4032
4033         /* call the Java method */
4034
4035         f = vm_call_method_float_vmarg(m, vmargscount, vmargs);
4036
4037         /* release dump area */
4038
4039         dump_release(dumpsize);
4040
4041         return f;
4042 }
4043 #else
4044 float vm_call_method_float_jvalue(methodinfo *m, java_objectheader *o, const jvalue *args)
4045 {
4046         int32_t   dumpsize;
4047         uint64_t *array;
4048         float     f;
4049
4050         /* mark start of dump memory area */
4051
4052         dumpsize = dump_size();
4053
4054         /* fill the argument array from a va_list */
4055
4056         array = vm_array_from_jvalue(m, o, args);
4057
4058         /* call the Java method */
4059
4060         f = vm_call_float_array(m, array);
4061
4062         /* release dump area */
4063
4064         dump_release(dumpsize);
4065
4066         return f;
4067 }
4068 #endif
4069
4070
4071 /* vm_call_double_array ********************************************************
4072
4073    Calls a Java method with a variable number of arguments and returns
4074    a double.
4075
4076 *******************************************************************************/
4077
4078 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
4079 double vm_call_method_double_vmarg(methodinfo *m, s4 vmargscount,
4080                                                                    vm_arg *vmargs)
4081 {
4082         double d;
4083
4084         vm_abort("IMPLEMENT ME!");
4085
4086         STATISTICS(count_calls_native_to_java++);
4087
4088 #if defined(ENABLE_JIT)
4089 # if defined(ENABLE_INTRP)
4090         if (opt_intrp)
4091                 d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
4092         else
4093 # endif
4094                 d = asm_vm_call_method_double(m, vmargscount, vmargs);
4095 #else
4096         d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
4097 #endif
4098
4099         return d;
4100 }
4101 #else
4102 double vm_call_double_array(methodinfo *m, uint64_t *array)
4103 {
4104         methoddesc *md;
4105         double      d;
4106
4107         md = m->parseddesc;
4108
4109         /* compile the method if not already done */
4110
4111         if (m->code == NULL)
4112                 if (!jit_compile(m))
4113                         return 0;
4114
4115         STATISTICS(count_calls_native_to_java++);
4116
4117 #if defined(ENABLE_JIT)
4118 # if defined(ENABLE_INTRP)
4119         if (opt_intrp)
4120                 d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
4121         else
4122 # endif
4123                 d = asm_vm_call_method_double(m->code->entrypoint, array, md->memuse);
4124 #else
4125         d = intrp_asm_vm_call_method_double(m, vmargscount, vmargs);
4126 #endif
4127
4128         return d;
4129 }
4130 #endif
4131
4132
4133 /* vm_call_method_double *******************************************************
4134
4135    Calls a Java method with a variable number of arguments and returns
4136    a double.
4137
4138 *******************************************************************************/
4139
4140 double vm_call_method_double(methodinfo *m, java_objectheader *o, ...)
4141 {
4142         va_list ap;
4143         double  d;
4144
4145         va_start(ap, o);
4146         d = vm_call_method_double_valist(m, o, ap);
4147         va_end(ap);
4148
4149         return d;
4150 }
4151
4152
4153 /* vm_call_method_double_valist ************************************************
4154
4155    Calls a Java method with a variable number of arguments, passed via
4156    a va_list, and returns a double.
4157
4158 *******************************************************************************/
4159
4160 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
4161 double vm_call_method_double_valist(methodinfo *m, java_objectheader *o,
4162                                                                         va_list ap)
4163 {
4164         s4      vmargscount;
4165         vm_arg *vmargs;
4166         double  d;
4167         s4      dumpsize;
4168
4169         /* mark start of dump memory area */
4170
4171         dumpsize = dump_size();
4172
4173         /* get number of Java method arguments */
4174
4175         vmargscount = m->parseddesc->paramcount;
4176
4177         /* allocate vm_arg array */
4178
4179         vmargs = DMNEW(vm_arg, vmargscount);
4180
4181         /* fill the vm_arg array from a va_list */
4182
4183         vm_vmargs_from_valist(m, o, vmargs, ap);
4184
4185         /* call the Java method */
4186
4187         d = vm_call_method_double_vmarg(m, vmargscount, vmargs);
4188
4189         /* release dump area */
4190
4191         dump_release(dumpsize);
4192
4193         return d;
4194 }
4195 #else
4196 double vm_call_method_double_valist(methodinfo *m, java_objectheader *o, va_list ap)
4197 {
4198         int32_t   dumpsize;
4199         uint64_t *array;
4200         double    d;
4201
4202         /* mark start of dump memory area */
4203
4204         dumpsize = dump_size();
4205
4206         /* fill the argument array from a va_list */
4207
4208         array = vm_array_from_valist(m, o, ap);
4209
4210         /* call the Java method */
4211
4212         d = vm_call_double_array(m, array);
4213
4214         /* release dump area */
4215
4216         dump_release(dumpsize);
4217
4218         return d;
4219 }
4220 #endif
4221
4222
4223 /* vm_call_method_double_jvalue ************************************************
4224
4225    Calls a Java method with a variable number of arguments, passed via
4226    a jvalue array, and returns a double.
4227
4228 *******************************************************************************/
4229
4230 #if !defined(__MIPS__) && !defined(__X86_64__) && !defined(__POWERPC64__) && !defined(__SPARC_64__) && !defined(__M68K__) && !defined(__ARM__) && !defined(__ALPHA__) && !defined(__I386__)
4231 double vm_call_method_double_jvalue(methodinfo *m, java_objectheader *o,
4232                                                                         const jvalue *args)
4233 {
4234         s4      vmargscount;
4235         vm_arg *vmargs;
4236         double  d;
4237         s4      dumpsize;
4238
4239         /* mark start of dump memory area */
4240
4241         dumpsize = dump_size();
4242
4243         /* get number of Java method arguments */
4244
4245         vmargscount = m->parseddesc->paramcount;
4246
4247         /* allocate vm_arg array */
4248
4249         vmargs = DMNEW(vm_arg, vmargscount);
4250
4251         /* fill the vm_arg array from a va_list */
4252
4253         vm_vmargs_from_jvalue(m, o, vmargs, args);
4254
4255         /* call the Java method */
4256
4257         d = vm_call_method_double_vmarg(m, vmargscount, vmargs);
4258
4259         /* release dump area */
4260
4261         dump_release(dumpsize);
4262
4263         return d;
4264 }
4265 #else
4266 double vm_call_method_double_jvalue(methodinfo *m, java_objectheader *o, const jvalue *args)
4267 {
4268         int32_t   dumpsize;
4269         uint64_t *array;
4270         double    d;
4271
4272         /* mark start of dump memory area */
4273
4274         dumpsize = dump_size();
4275
4276         /* fill the argument array from a va_list */
4277
4278         array = vm_array_from_jvalue(m, o, args);
4279
4280         /* call the Java method */
4281
4282         d = vm_call_double_array(m, array);
4283
4284         /* release dump area */
4285
4286         dump_release(dumpsize);
4287
4288         return d;
4289 }
4290 #endif
4291
4292 /*
4293  * These are local overrides for various environment variables in Emacs.
4294  * Please do not remove this and leave it at the end of the file, where
4295  * Emacs will automagically detect them.
4296  * ---------------------------------------------------------------------
4297  * Local variables:
4298  * mode: c
4299  * indent-tabs-mode: t
4300  * c-basic-offset: 4
4301  * tab-width: 4
4302  * End:
4303  */