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