* src/vm/jit/x86_64/codegen.h (ICONST): Fix this macro so it properly
[cacao.git] / src / vm / builtin.c
index be53a7869c301be197fcfdfee5d31e3ecec954df..02a80316e3456514afbb5b9501ede1407bf4001e 100644 (file)
@@ -1,9 +1,9 @@
 /* src/vm/builtin.c - functions for unsupported operations
 
-   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
-   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
-   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
-   Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Reinhard Grafl
             Andreas Krall
             Mark Probst
 
    Changes: Christian Thalinger
+            Edwin Steiner
 
    Contains C functions for JavaVM Instructions that cannot be
    translated to machine language directly. Consequently, the
    calls instead of machine instructions, using the C calling
    convention.
 
-   $Id: builtin.c 3562 2005-11-04 16:20:54Z twisti $
+   $Id: builtin.c 5251 2006-08-18 13:01:00Z twisti $
 
 */
 
 
+#include "config.h"
+
 #include <assert.h>
+#include <errno.h>
+#include <math.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/time.h>
 
-#include "config.h"
 #include "vm/types.h"
 
 #include "arch.h"
 #include "native/include/java_lang_Cloneable.h"
 #include "native/include/java_lang_Object.h"          /* required by VMObject */
 #include "native/include/java_lang_VMObject.h"
-#include "native/include/java_lang_Throwable.h"
-#include "native/include/java_lang_VMThrowable.h"
-
-#if defined(USE_THREADS)
-# if defined(NATIVE_THREADS)
-#  include "threads/native/threads.h"
-# else
-#  include "threads/green/threads.h"
-#  include "threads/green/locks.h"
-# endif
+
+#if defined(ENABLE_THREADS)
+# include "threads/native/threads.h"
 #endif
 
 #include "toolbox/logging.h"
 #include "toolbox/util.h"
 #include "vm/builtin.h"
+#include "vm/class.h"
 #include "vm/exceptions.h"
 #include "vm/global.h"
 #include "vm/initialize.h"
 #include "vm/loader.h"
 #include "vm/options.h"
 #include "vm/stringlocal.h"
-#include "vm/tables.h"
 #include "vm/jit/asmpart.h"
 #include "vm/jit/patcher.h"
+#include "vm/rt-timing.h"
+#include "vm/cycles-stats.h"
 
 
 /* include builtin tables *****************************************************/
@@ -90,6 +90,9 @@
 #include "vm/builtintable.inc"
 
 
+CYCLES_STATS_DECLARE(builtin_new         ,100,5)
+CYCLES_STATS_DECLARE(builtin_overhead    , 80,1)
+
 /* builtintable_init ***********************************************************
 
    Parse the descriptors of builtin functions and create the parsed
 
 static bool builtintable_init(void)
 {
-       descriptor_pool *descpool;
-       s4               dumpsize;
-       utf             *descriptor;
-       s4               entries_internal;
-       s4               entries_automatic;
-       s4               i;
+       descriptor_pool    *descpool;
+       s4                  dumpsize;
+       builtintable_entry *bte;
 
        /* mark start of dump memory area */
 
@@ -122,23 +122,14 @@ static bool builtintable_init(void)
        if (!descriptor_pool_add_class(descpool, utf_java_lang_Class))
                return false;
 
-       /* calculate table entries statically */
-
-       entries_internal =
-               sizeof(builtintable_internal) / sizeof(builtintable_entry);
-
-       entries_automatic =
-               sizeof(builtintable_automatic) / sizeof(builtintable_entry)
-               - 1; /* last filler entry (comment see builtintable.inc) */
-
        /* first add all descriptors to the pool */
 
-       for (i = 0; i < entries_internal; i++) {
+       for (bte = builtintable_internal; bte->fp != NULL; bte++) {
                /* create a utf8 string from descriptor */
 
-               descriptor = utf_new_char(builtintable_internal[i].descriptor);
+               bte->descriptor = utf_new_char(bte->cdescriptor);
 
-               if (!descriptor_pool_add(descpool, descriptor, NULL)) {
+               if (!descriptor_pool_add(descpool, bte->descriptor, NULL)) {
                        /* release dump area */
 
                        dump_release(dumpsize);
@@ -147,16 +138,22 @@ static bool builtintable_init(void)
                }
        }
 
-       for (i = 0; i < entries_automatic; i++) {
-               /* create a utf8 string from descriptor */
+       for (bte = builtintable_automatic; bte->fp != NULL; bte++) {
+               bte->descriptor = utf_new_char(bte->cdescriptor);
 
-               descriptor = utf_new_char(builtintable_automatic[i].descriptor);
+               if (!descriptor_pool_add(descpool, bte->descriptor, NULL)) {
+                       dump_release(dumpsize);
+                       return false;
+               }
+       }
 
-               if (!descriptor_pool_add(descpool, descriptor, NULL)) {
-                       /* release dump area */
+       for (bte = builtintable_function; bte->fp != NULL; bte++) {
+               bte->classname  = utf_new_char(bte->cclassname);
+               bte->name       = utf_new_char(bte->cname);
+               bte->descriptor = utf_new_char(bte->cdescriptor);
 
+               if (!descriptor_pool_add(descpool, bte->descriptor, NULL)) {
                        dump_release(dumpsize);
-
                        return false;
                }
        }
@@ -171,28 +168,24 @@ static bool builtintable_init(void)
 
        /* now parse all descriptors */
 
-       for (i = 0; i < entries_internal; i++) {
-               /* create a utf8 string from descriptor */
-
-               descriptor = utf_new_char(builtintable_internal[i].descriptor);
-
+       for (bte = builtintable_internal; bte->fp != NULL; bte++) {
                /* parse the descriptor, builtin is always static (no `this' pointer) */
 
-               builtintable_internal[i].md =
-                       descriptor_pool_parse_method_descriptor(descpool, descriptor,
-                                                                                                       ACC_STATIC, NULL);
+               bte->md = descriptor_pool_parse_method_descriptor(descpool,
+                                                                                                                 bte->descriptor,
+                                                                                                                 ACC_STATIC, NULL);
        }
 
-       for (i = 0; i < entries_automatic; i++) {
-               /* create a utf8 string from descriptor */
-
-               descriptor = utf_new_char(builtintable_automatic[i].descriptor);
-
-               /* parse the descriptor, builtin is always static (no `this' pointer) */
+       for (bte = builtintable_automatic; bte->fp != NULL; bte++) {
+               bte->md = descriptor_pool_parse_method_descriptor(descpool,
+                                                                                                                 bte->descriptor,
+                                                                                                                 ACC_STATIC, NULL);
+       }
 
-               builtintable_automatic[i].md =
-                       descriptor_pool_parse_method_descriptor(descpool, descriptor,
-                                                                                                       ACC_STATIC, NULL);
+       for (bte = builtintable_function; bte->fp != NULL; bte++) {
+               bte->md = descriptor_pool_parse_method_descriptor(descpool,
+                                                                                                                 bte->descriptor,
+                                                                                                                 ACC_STATIC, NULL);
        }
 
        /* release dump area */
@@ -242,7 +235,7 @@ static void builtintable_sort_automatic(void)
 
 /* builtin_init ****************************************************************
 
-   XXX
+   Initialize the global table of builtin functions.
 
 *******************************************************************************/
 
@@ -270,11 +263,11 @@ bool builtin_init(void)
 
 builtintable_entry *builtintable_get_internal(functionptr fp)
 {
-       s4 i;
+       builtintable_entry *bte;
 
-       for (i = 0; builtintable_internal[i].fp != NULL; i++) {
-               if (builtintable_internal[i].fp == fp)
-                       return &builtintable_internal[i];
+       for (bte = builtintable_internal; bte->fp != NULL; bte++) {
+               if (bte->fp == fp)
+                       return bte;
        }
 
        return NULL;
@@ -318,6 +311,56 @@ builtintable_entry *builtintable_get_automatic(s4 opcode)
 }
 
 
+/* builtintable_replace_function ***********************************************
+
+   XXX
+
+*******************************************************************************/
+
+bool builtintable_replace_function(instruction *iptr)
+{
+       constant_FMIref    *mr;
+       builtintable_entry *bte;
+
+       /* get name and descriptor of the function */
+
+       switch (iptr->opc) {
+       case ICMD_INVOKESTATIC:
+               /* The instruction MUST be resolved, otherwise we run into
+                  lazy loading troubles.  Anyway, we should/can only replace
+                  very VM-close functions. */
+
+               if (INSTRUCTION_IS_UNRESOLVED(iptr))
+                       return false;
+
+               mr = INSTRUCTION_RESOLVED_FMIREF(iptr);
+               break;
+
+       default:
+               return false;
+       }
+
+       /* search the function table */
+
+       for (bte = builtintable_function; bte->fp != NULL; bte++) {
+               if ((mr->p.classref->name == bte->classname) &&
+                       (mr->name             == bte->name) &&
+                       (mr->descriptor       == bte->descriptor)) {
+
+                       /* set the values in the instruction */
+
+                       iptr->opc   = bte->opcode;
+                       iptr->op1   = bte->checkexception;
+                       iptr->val.a = bte;
+
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+
 /*****************************************************************************
                                                                TYPE CHECKS
 *****************************************************************************/
@@ -345,7 +388,7 @@ s4 builtin_isanysubclass(classinfo *sub, classinfo *super)
                        (sub->vftbl->interfacetable[-super->index] != NULL);
 
        } else {
-               asm_getclassvalues_atomic(super->vftbl, sub->vftbl, &classvalues);
+               ASM_GETCLASSVALUES_ATOMIC(super->vftbl, sub->vftbl, &classvalues);
 
                res = (u4) (classvalues.sub_baseval - classvalues.super_baseval) <=
                        (u4) classvalues.super_diffval;
@@ -364,7 +407,7 @@ s4 builtin_isanysubclass_vftbl(vftbl_t *sub, vftbl_t *super)
        if (sub == super)
                return 1;
 
-       asm_getclassvalues_atomic(super, sub, &classvalues);
+       ASM_GETCLASSVALUES_ATOMIC(super, sub, &classvalues);
 
        if ((base = classvalues.super_baseval) <= 0) {
                /* super is an interface */
@@ -499,12 +542,16 @@ s4 builtin_arrayinstanceof(java_objectheader *o, classinfo *targetclass)
 }
 
 
-/************************** exception functions *******************************
+/* builtin_throw_exception *****************************************************
 
-******************************************************************************/
+   Sets the exceptionptr with the thrown exception and prints some
+   debugging information.  Called from asm_vm_call_method.
 
-java_objectheader *builtin_throw_exception(java_objectheader *xptr)
+*******************************************************************************/
+
+void *builtin_throw_exception(java_objectheader *xptr)
 {
+#if !defined(NDEBUG)
     java_lang_Throwable *t;
        char                *logtext;
        s4                   logtextlen;
@@ -519,12 +566,17 @@ java_objectheader *builtin_throw_exception(java_objectheader *xptr)
 
                if (t) {
                        logtextlen +=
-                               utf_strlen(xptr->vftbl->class->name) +
-                               strlen(": ") +
-                               javastring_strlen((java_objectheader *) t->detailMessage);
-
-               } else
+                               utf_bytes(xptr->vftbl->class->name);
+                       if (t->detailMessage) {
+                               logtextlen += strlen(": ") +
+                                       u2_utflength(t->detailMessage->value->data 
+                                                                       + t->detailMessage->offset,
+                                                        t->detailMessage->count);
+                       }
+               } 
+               else {
                        logtextlen += strlen("(nil)");
+               }
 
                /* allocate memory */
 
@@ -535,8 +587,7 @@ java_objectheader *builtin_throw_exception(java_objectheader *xptr)
                strcpy(logtext, "Builtin exception thrown: ");
 
                if (t) {
-                       utf_sprint_classname(logtext + strlen(logtext),
-                                                                xptr->vftbl->class->name);
+                       utf_cat_classname(logtext, xptr->vftbl->class->name);
 
                        if (t->detailMessage) {
                                char *buf;
@@ -544,7 +595,7 @@ java_objectheader *builtin_throw_exception(java_objectheader *xptr)
                                buf = javastring_tochar((java_objectheader *) t->detailMessage);
                                strcat(logtext, ": ");
                                strcat(logtext, buf);
-                               MFREE(buf, char, strlen(buf));
+                               MFREE(buf, char, strlen(buf) + 1);
                        }
 
                } else {
@@ -557,12 +608,17 @@ java_objectheader *builtin_throw_exception(java_objectheader *xptr)
 
                dump_release(dumpsize);
        }
+#endif /* !defined(NDEBUG) */
+
+       /* actually set the exception */
 
        *exceptionptr = xptr;
 
-       return xptr;
-}
+       /* Return a NULL pointer.  This is required for vm_call_method to
+          check for an exception.  This is for convenience. */
 
+       return NULL;
+}
 
 
 /* builtin_canstore ************************************************************
@@ -574,60 +630,62 @@ java_objectheader *builtin_throw_exception(java_objectheader *xptr)
 
 *******************************************************************************/
 
-s4 builtin_canstore(java_objectarray *a, java_objectheader *o)
+s4 builtin_canstore(java_objectarray *oa, java_objectheader *o)
 {
        arraydescriptor *desc;
        arraydescriptor *valuedesc;
-       vftbl_t *componentvftbl;
-       vftbl_t *valuevftbl;
-       int base;
-       castinfo classvalues;
-       
+       vftbl_t         *componentvftbl;
+       vftbl_t         *valuevftbl;
+       s4               base;
+       castinfo         classvalues;
+
        if (!o)
                return 1;
 
        /* The following is guaranteed (by verifier checks):
         *
-        *     *) a->...vftbl->arraydesc != NULL
-        *     *) a->...vftbl->arraydesc->componentvftbl != NULL
+        *     *) oa->...vftbl->arraydesc != NULL
+        *     *) oa->...vftbl->arraydesc->componentvftbl != NULL
         *     *) o->vftbl is not an interface vftbl
         */
        
-       desc a->header.objheader.vftbl->arraydesc;
+       desc           = oa->header.objheader.vftbl->arraydesc;
        componentvftbl = desc->componentvftbl;
-       valuevftbl = o->vftbl;
+       valuevftbl     = o->vftbl;
 
        if ((desc->dimension - 1) == 0) {
                s4 res;
 
-               /* {a is a one-dimensional array} */
-               /* {a is an array of references} */
+               /* {oa is a one-dimensional array} */
+               /* {oa is an array of references} */
                
                if (valuevftbl == componentvftbl)
                        return 1;
 
-               asm_getclassvalues_atomic(componentvftbl, valuevftbl, &classvalues);
+               ASM_GETCLASSVALUES_ATOMIC(componentvftbl, valuevftbl, &classvalues);
 
                if ((base = classvalues.super_baseval) <= 0)
                        /* an array of interface references */
                        return (valuevftbl->interfacetablelength > -base &&
                                        valuevftbl->interfacetable[base] != NULL);
                
-               res = (unsigned) (classvalues.sub_baseval - classvalues.super_baseval)
-                       <= (unsigned) classvalues.super_diffval;
+               res = ((unsigned) (classvalues.sub_baseval - classvalues.super_baseval)
+                          <= (unsigned) classvalues.super_diffval);
 
                return res;
        }
 
-       /* {a has dimension > 1} */
+       /* {oa has dimension > 1} */
        /* {componentvftbl->arraydesc != NULL} */
 
        /* check if o is an array */
+
        if ((valuedesc = valuevftbl->arraydesc) == NULL)
                return 0;
+
        /* {o is an array} */
 
-       return builtin_descriptorscompatible(valuedesc,componentvftbl->arraydesc);
+       return builtin_descriptorscompatible(valuedesc, componentvftbl->arraydesc);
 }
 
 
@@ -660,7 +718,7 @@ s4 builtin_canstore_onedim (java_objectarray *a, java_objectheader *o)
        if (valuevftbl == elementvftbl)
                return 1;
 
-       asm_getclassvalues_atomic(elementvftbl, valuevftbl, &classvalues);
+       ASM_GETCLASSVALUES_ATOMIC(elementvftbl, valuevftbl, &classvalues);
 
        if ((base = classvalues.super_baseval) <= 0)
                /* an array of interface references */
@@ -702,7 +760,7 @@ s4 builtin_canstore_onedim_class(java_objectarray *a, java_objectheader *o)
        if (valuevftbl == elementvftbl)
                return 1;
 
-       asm_getclassvalues_atomic(elementvftbl, valuevftbl, &classvalues);
+       ASM_GETCLASSVALUES_ATOMIC(elementvftbl, valuevftbl, &classvalues);
 
        res = (unsigned) (classvalues.sub_baseval - classvalues.super_baseval)
                <= (unsigned) classvalues.super_diffval;
@@ -723,35 +781,63 @@ s4 builtin_canstore_onedim_class(java_objectarray *a, java_objectheader *o)
 java_objectheader *builtin_new(classinfo *c)
 {
        java_objectheader *o;
+#if defined(ENABLE_RT_TIMING)
+       struct timespec time_start, time_end;
+#endif
+#if defined(ENABLE_CYCLES_STATS)
+       u8 cycles_start, cycles_end;
+#endif
+
+       RT_TIMING_GET_TIME(time_start);
+       CYCLES_STATS_GET(cycles_start);
 
        /* is the class loaded */
 
-       assert(c->loaded);
+       assert(c->state & CLASS_LOADED);
+
+       /* check if we can instantiate this class */
+
+       if (c->flags & ACC_ABSTRACT) {
+               *exceptionptr =
+                       new_exception_utfmessage(string_java_lang_InstantiationError,
+                                                                        c->name);
+               return NULL;
+       }
 
        /* is the class linked */
-       if (!c->linked)
+
+       if (!(c->state & CLASS_LINKED))
                if (!link_class(c))
                        return NULL;
 
-       if (!c->initialized) {
+       if (!(c->state & CLASS_INITIALIZED)) {
+#if !defined(NDEBUG)
                if (initverbose)
                        log_message_class("Initialize class (from builtin_new): ", c);
+#endif
 
                if (!initialize_class(c))
                        return NULL;
        }
 
-       o = heap_allocate(c->instancesize, true, c->finalizer);
+       o = heap_allocate(c->instancesize, c->flags & ACC_CLASS_HAS_POINTERS,
+                                         c->finalizer);
 
        if (!o)
                return NULL;
 
        o->vftbl = c->vftbl;
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
-       initObjectLock(o);
+#if defined(ENABLE_THREADS)
+       lock_init_object_lock(o);
 #endif
 
+       CYCLES_STATS_GET(cycles_end);
+       RT_TIMING_GET_TIME(time_end);
+
+       CYCLES_STATS_COUNT(builtin_new,cycles_end - cycles_start);
+       RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_NEW_OBJECT);
+
        return o;
 }
 
@@ -772,13 +858,18 @@ java_arrayheader *builtin_newarray(s4 size, classinfo *arrayclass)
        s4                componentsize;
        s4                actualsize;
        java_arrayheader *a;
+#if defined(ENABLE_RT_TIMING)
+       struct timespec time_start, time_end;
+#endif
+
+       RT_TIMING_GET_TIME(time_start);
 
        desc          = arrayclass->vftbl->arraydesc;
        dataoffset    = desc->dataoffset;
        componentsize = desc->componentsize;
 
        if (size < 0) {
-               *exceptionptr = new_negativearraysizeexception();
+               exceptions_throw_negativearraysizeexception();
                return NULL;
        }
 
@@ -796,12 +887,15 @@ java_arrayheader *builtin_newarray(s4 size, classinfo *arrayclass)
 
        a->objheader.vftbl = arrayclass->vftbl;
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
-       initObjectLock(&a->objheader);
+#if defined(ENABLE_THREADS)
+       lock_init_object_lock(&a->objheader);
 #endif
 
        a->size = size;
 
+       RT_TIMING_GET_TIME(time_end);
+       RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_NEW_ARRAY);
+
        return a;
 }
 
@@ -821,11 +915,11 @@ java_objectarray *builtin_anewarray(s4 size, classinfo *componentclass)
        
        /* is class loaded */
 
-       assert(componentclass->loaded);
+       assert(componentclass->state & CLASS_LOADED);
 
        /* is class linked */
 
-       if (!componentclass->linked)
+       if (!(componentclass->state & CLASS_LINKED))
                if (!link_class(componentclass))
                        return NULL;
 
@@ -1058,7 +1152,7 @@ java_arrayheader *builtin_multianewarray(int n, classinfo *arrayclass,
 #endif
 
                if (size < 0) {
-                       *exceptionptr = new_negativearraysizeexception();
+                       exceptions_throw_negativearraysizeexception();
                        return NULL;
                }
        }
@@ -1077,7 +1171,9 @@ java_arrayheader *builtin_multianewarray(int n, classinfo *arrayclass,
        
 *****************************************************************************/
 
-s4 methodindent = 0;
+#if !defined(NDEBUG)
+static s4 methodindent = 0;
+static u4 callcount = 0;
 
 java_objectheader *builtin_trace_exception(java_objectheader *xptr,
                                                                                   methodinfo *m,
@@ -1087,181 +1183,251 @@ java_objectheader *builtin_trace_exception(java_objectheader *xptr,
        char *logtext;
        s4    logtextlen;
        s4    dumpsize;
+       codeinfo *code;
 
-       if (opt_verbose || runverbose || opt_verboseexception) {
-               /* when running with verbosecall we remove one indent level */
-
-               if (runverbose && indent)
-                       methodindent--;
-
-               /* calculate message length */
+       if (opt_verbosecall && indent)
+               methodindent--;
 
-               if (xptr) {
-                       logtextlen =
-                               strlen("Exception ") +
-                               utf_strlen(xptr->vftbl->class->name);
+       /* calculate message length */
 
-               } else {
-                       logtextlen = strlen("Some Throwable");
-               }
+       if (xptr) {
+               logtextlen =
+                       strlen("Exception ") + utf_bytes(xptr->vftbl->class->name);
+       } 
+       else {
+               logtextlen = strlen("Some Throwable");
+       }
 
-               logtextlen += strlen(" thrown in ");
+       logtextlen += strlen(" thrown in ");
 
-               if (m) {
-                       logtextlen +=
-                               utf_strlen(m->class->name) +
-                               strlen(".") +
-                               utf_strlen(m->name) +
-                               utf_strlen(m->descriptor) +
-                               strlen("(NOSYNC,NATIVE");
+       if (m) {
+               logtextlen +=
+                       utf_bytes(m->class->name) +
+                       strlen(".") +
+                       utf_bytes(m->name) +
+                       utf_bytes(m->descriptor) +
+                       strlen("(NOSYNC,NATIVE");
 
 #if SIZEOF_VOID_P == 8
-                       logtextlen +=
-                               strlen(")(0x123456789abcdef0) at position 0x123456789abcdef0 (");
+               logtextlen +=
+                       strlen(")(0x123456789abcdef0) at position 0x123456789abcdef0 (");
 #else
-                       logtextlen += strlen(")(0x12345678) at position 0x12345678 (");
+               logtextlen += strlen(")(0x12345678) at position 0x12345678 (");
 #endif
 
-                       if (m->class->sourcefile == NULL)
-                               logtextlen += strlen("<NO CLASSFILE INFORMATION>");
-                       else
-                               logtextlen += utf_strlen(m->class->sourcefile);
+               if (m->class->sourcefile == NULL)
+                       logtextlen += strlen("<NO CLASSFILE INFORMATION>");
+               else
+                       logtextlen += utf_bytes(m->class->sourcefile);
 
-                       logtextlen += strlen(":65536)");
+               logtextlen += strlen(":65536)");
 
-               } else
-                       logtextlen += strlen("call_java_method");
+       } 
+       else {
+               logtextlen += strlen("call_java_method");
+       }
 
-               logtextlen += strlen("0");
+       logtextlen += strlen("0");
 
-               /* allocate memory */
+       /* allocate memory */
 
-               dumpsize = dump_size();
+       dumpsize = dump_size();
 
-               logtext = DMNEW(char, logtextlen);
+       logtext = DMNEW(char, logtextlen);
 
-               if (xptr) {
-                       strcpy(logtext, "Exception ");
-                       utf_strcat_classname(logtext, xptr->vftbl->class->name);
+       if (xptr) {
+               strcpy(logtext, "Exception ");
+               utf_cat_classname(logtext, xptr->vftbl->class->name);
 
-               } else {
-                       strcpy(logtext, "Some Throwable");
-               }
+       } else {
+               strcpy(logtext, "Some Throwable");
+       }
 
-               strcat(logtext, " thrown in ");
+       strcat(logtext, " thrown in ");
 
-               if (m) {
-                       utf_strcat_classname(logtext, m->class->name);
-                       strcat(logtext, ".");
-                       utf_strcat(logtext, m->name);
-                       utf_strcat(logtext, m->descriptor);
+       if (m) {
+               utf_cat_classname(logtext, m->class->name);
+               strcat(logtext, ".");
+               utf_cat(logtext, m->name);
+               utf_cat(logtext, m->descriptor);
 
-                       if (m->flags & ACC_SYNCHRONIZED)
-                               strcat(logtext, "(SYNC");
-                       else
-                               strcat(logtext, "(NOSYNC");
+               if (m->flags & ACC_SYNCHRONIZED)
+                       strcat(logtext, "(SYNC");
+               else
+                       strcat(logtext, "(NOSYNC");
+
+               if (m->flags & ACC_NATIVE) {
+                       strcat(logtext, ",NATIVE");
 
-                       if (m->flags & ACC_NATIVE) {
-                               strcat(logtext, ",NATIVE");
+                       code = m->code;
 
 #if SIZEOF_VOID_P == 8
-                               sprintf(logtext + strlen(logtext),
-                                               ")(0x%016lx) at position 0x%016lx",
-                                               (ptrint) m->entrypoint, (ptrint) pos);
+                       sprintf(logtext + strlen(logtext),
+                                       ")(0x%016lx) at position 0x%016lx",
+                                       (ptrint) code->entrypoint, (ptrint) pos);
 #else
-                               sprintf(logtext + strlen(logtext),
-                                               ")(0x%08x) at position 0x%08x",
-                                               (ptrint) m->entrypoint, (ptrint) pos);
+                       sprintf(logtext + strlen(logtext),
+                                       ")(0x%08x) at position 0x%08x",
+                                       (ptrint) code->entrypoint, (ptrint) pos);
 #endif
 
-                       } else {
+               } else {
+
+                       /* XXX preliminary: This should get the actual codeinfo */
+                       /* in which the exception happened.                     */
+                       code = m->code;
+                       
 #if SIZEOF_VOID_P == 8
-                               sprintf(logtext + strlen(logtext),
-                                               ")(0x%016lx) at position 0x%016lx (",
-                                               (ptrint) m->entrypoint, (ptrint) pos);
+                       sprintf(logtext + strlen(logtext),
+                                       ")(0x%016lx) at position 0x%016lx (",
+                                       (ptrint) code->entrypoint, (ptrint) pos);
 #else
-                               sprintf(logtext + strlen(logtext),
-                                               ")(0x%08x) at position 0x%08x (",
-                                               (ptrint) m->entrypoint, (ptrint) pos);
+                       sprintf(logtext + strlen(logtext),
+                                       ")(0x%08x) at position 0x%08x (",
+                                       (ptrint) code->entrypoint, (ptrint) pos);
 #endif
 
-                               if (m->class->sourcefile == NULL)
-                                       strcat(logtext, "<NO CLASSFILE INFORMATION>");
-                               else
-                                       utf_strcat(logtext, m->class->sourcefile);
+                       if (m->class->sourcefile == NULL)
+                               strcat(logtext, "<NO CLASSFILE INFORMATION>");
+                       else
+                               utf_cat(logtext, m->class->sourcefile);
 
-                               sprintf(logtext + strlen(logtext), ":%d)", 0);
-                       }
+                       sprintf(logtext + strlen(logtext), ":%d)", 0);
+               }
 
-               } else
-                       strcat(logtext, "call_java_method");
+       } else
+               strcat(logtext, "call_java_method");
 
-               log_text(logtext);
+       log_text(logtext);
 
-               /* release memory */
+       /* release memory */
 
-               dump_release(dumpsize);
+       dump_release(dumpsize);
 
-               /* print stacktrace for exception */
+       return xptr;
+}
+#endif /* !defined(NDEBUG) */
 
-               if (opt_verboseexception) {
-                       java_lang_Throwable   *t;
-                       java_lang_VMThrowable *vmt;
-                       java_lang_Throwable   *cause;
-                       utf                   *u;
-                       stackTraceBuffer      *stb;
 
-                       t = (java_lang_Throwable *) xptr;
-                       cause = t->cause;
+/* builtin_print_argument ******************************************************
 
-                       /* print the root exception */
+   Prints arguments and return values for the call trace.
 
-                       utf_display_classname(t->header.vftbl->class->name);
+*******************************************************************************/
 
-                       if (t->detailMessage) {
-                               u = javastring_toutf(t->detailMessage, false);
+#if !defined(NDEBUG)
+static char *builtin_print_argument(char *logtext, s4 logtextlen,
+                                                                       typedesc *paramtype, s8 value)
+{
+       imm_union          imu;
+       java_objectheader *o;
+       java_lang_String  *s;
+       classinfo         *c;
+       utf               *u;
+       u4                 len;
 
-                               printf(": ");
-                               utf_display(u);
-                       }
+       switch (paramtype->type) {
+       case TYPE_INT:
+               sprintf(logtext + strlen(logtext), "0x%x", (s4) value);
+               break;
 
-                       putc('\n', stdout);
+       case TYPE_LNG:
+#if SIZEOF_VOID_P == 4
+               sprintf(logtext + strlen(logtext), "0x%llx", value);
+#else
+               sprintf(logtext + strlen(logtext), "0x%lx", value);
+#endif
+               break;
 
-                       /* print the cause if available */
+       case TYPE_FLT:
+               imu.l = value;
+               sprintf(logtext + strlen(logtext), "%.8f (0x%08x)", imu.f, imu.i);
+               break;
 
-                       if (cause && (cause != t)) {
-                               printf("Caused by: ");
-                               utf_display_classname(cause->header.vftbl->class->name);
+       case TYPE_DBL:
+               imu.l = value;
+#if SIZEOF_VOID_P == 4
+               sprintf(logtext + strlen(logtext), "%.16g (0x%016llx)", imu.d, imu.l);
+#else
+               sprintf(logtext + strlen(logtext), "%.16g (0x%016lx)", imu.d, imu.l);
+#endif
+               break;
 
-                               if (cause->detailMessage) {
-                                       u = javastring_toutf(cause->detailMessage, false);
+       case TYPE_ADR:
+               sprintf(logtext + strlen(logtext), "%p", (void *) (ptrint) value);
 
-                                       printf(": ");
-                                       utf_display(u);
-                               }
+               /* cast to java.lang.Object */
+
+               o = (java_objectheader *) (ptrint) value;
+
+               /* check return argument for java.lang.Class or java.lang.String */
+
+               if (o != NULL) {
+                       if (o->vftbl->class == class_java_lang_String) {
+                               /* get java.lang.String object and the length of the
+                                  string */
+
+                               s = (java_lang_String *) o;
+
+                               u = javastring_toutf(s, false);
+
+                               len = strlen(" (String = \"") + utf_bytes(u) + strlen("\")");
 
-                               putc('\n', stdout);
+                               /* realloc memory for string length */
+
+                               DMREALLOC(logtext, char, logtextlen, logtextlen + len);
+
+                               /* convert to utf8 string and strcat it to the logtext */
+
+                               strcat(logtext, " (String = \"");
+                               utf_cat(logtext, u);
+                               strcat(logtext, "\")");
                        }
+                       else {
+                               if (o->vftbl->class == class_java_lang_Class) {
+                                       /* if the object returned is a java.lang.Class
+                                          cast it to classinfo structure and get the name
+                                          of the class */
+
+                                       c = (classinfo *) o;
+
+                                       u = c->name;
+                               }
+                               else {
+                                       /* if the object returned is not a java.lang.String or
+                                          a java.lang.Class just print the name of the class */
+
+                                       u = o->vftbl->class->name;
+                               }
 
-                       /* now print the stacktrace */
+                               len = strlen(" (Class = \"") + utf_bytes(u) + strlen("\")");
 
-                       vmt = t->vmState;
-                       stb = (stackTraceBuffer *) vmt->vmData;
+                               /* realloc memory for string length */
 
-                       stacktrace_print_trace(stb);
+                               DMREALLOC(logtext, char, logtextlen, logtextlen + len);
+
+                               /* strcat to the logtext */
+
+                               strcat(logtext, " (Class = \"");
+                               utf_cat_classname(logtext, u);
+                               strcat(logtext, "\")");
+                       }
                }
        }
 
-       return xptr;
+       return logtext;
 }
+#endif /* !defined(NDEBUG) */
 
 
 /* builtin_trace_args **********************************************************
 
-   XXX
+   Print method call with arguments for -verbose:call.
 
 *******************************************************************************/
 
+#if !defined(NDEBUG)
+
 #ifdef TRACE_ARGS_NUM
 void builtin_trace_args(s8 a0, s8 a1,
 #if TRACE_ARGS_NUM >= 4
@@ -1287,11 +1453,14 @@ void builtin_trace_args(s8 a0, s8 a1,
        /* calculate message length */
 
        logtextlen =
-               6 + methodindent + strlen("called: ") +
-               utf_strlen(m->class->name) +
+               strlen("4294967295 ") +
+               strlen("-2147483647-") +        /* INT_MAX should be sufficient       */
+               methodindent +
+               strlen("called: ") +
+               utf_bytes(m->class->name) +
                strlen(".") +
-               utf_strlen(m->name) +
-               utf_strlen(m->descriptor) +
+               utf_bytes(m->name) +
+               utf_bytes(m->descriptor) +
                strlen(" SYNCHRONIZED") + strlen("(") + strlen(")");
 
        /* add maximal argument length */
@@ -1304,7 +1473,11 @@ void builtin_trace_args(s8 a0, s8 a1,
 
        logtext = DMNEW(char, logtextlen);
 
-       sprintf(logtext,"-%d-",methodindent);
+       callcount++;
+
+       sprintf(logtext, "%10d ", callcount);
+       sprintf(logtext + strlen(logtext), "-%d-", methodindent);
+
        pos = strlen(logtext);
 
        for (i = 0; i < methodindent; i++)
@@ -1312,10 +1485,10 @@ void builtin_trace_args(s8 a0, s8 a1,
 
        strcpy(logtext + pos, "called: ");
 
-       utf_strcat_classname(logtext, m->class->name);
+       utf_cat_classname(logtext, m->class->name);
        strcat(logtext, ".");
-       utf_strcat(logtext, m->name);
-       utf_strcat(logtext, m->descriptor);
+       utf_cat(logtext, m->name);
+       utf_cat(logtext, m->descriptor);
 
        if (m->flags & ACC_PUBLIC)       strcat(logtext, " PUBLIC");
        if (m->flags & ACC_PRIVATE)      strcat(logtext, " PRIVATE");
@@ -1331,172 +1504,69 @@ void builtin_trace_args(s8 a0, s8 a1,
 
        strcat(logtext, "(");
 
-       /* xxxprintf ?Bug? an PowerPc Linux (rlwinm.inso)                */
-       /* Only Arguments in integer Registers are passed correctly here */
-       /* long longs spilled on Stack have an wrong offset of +4        */
-       /* So preliminary Bugfix: Only pass 3 params at once to sprintf  */
-       /* for SIZEOG_VOID_P == 4 && TRACE_ARGS_NUM == 8                 */
-       switch (md->paramcount) {
-       case 0:
-               break;
+       if (md->paramcount >= 1) {
+               logtext = builtin_print_argument(logtext, logtextlen,
+                                                                                &md->paramtypes[0], a0);
+       }
 
-#if SIZEOF_VOID_P == 4
-       case 1:
-               sprintf(logtext + strlen(logtext),
-                               "0x%llx",
-                               a0);
-               break;
+       if (md->paramcount >= 2) {
+               strcat(logtext, ", ");
 
-       case 2:
-               sprintf(logtext + strlen(logtext),
-                               "0x%llx, 0x%llx",
-                               a0, a1);
-               break;
+               logtext = builtin_print_argument(logtext, logtextlen,
+                                                                                &md->paramtypes[1], a1);
+       }
 
 #if TRACE_ARGS_NUM >= 4
-       case 3:
-               sprintf(logtext + strlen(logtext),
-                               "0x%llx, 0x%llx, 0x%llx",
-                               a0, a1, a2);
-               break;
-
-       case 4:
-               sprintf(logtext + strlen(logtext), "0x%llx, 0x%llx, 0x%llx"
-                               , a0, a1, a2);
-               sprintf(logtext + strlen(logtext), ", 0x%llx", a3);
-
-               break;
-#endif /* TRACE_ARGS_NUM >= 4 */
-
-#if TRACE_ARGS_NUM >= 6
-       case 5:
-               sprintf(logtext + strlen(logtext), "0x%llx, 0x%llx, 0x%llx"
-                               , a0, a1, a2);
-               sprintf(logtext + strlen(logtext), ", 0x%llx, 0x%llx", a3, a4);
-               break;
+       if (md->paramcount >= 3) {
+               strcat(logtext, ", ");
 
+               logtext = builtin_print_argument(logtext, logtextlen,
+                                                                                &md->paramtypes[2], a2);
+       }
 
-       case 6:
-               sprintf(logtext + strlen(logtext), "0x%llx, 0x%llx, 0x%llx"
-                               , a0, a1, a2);
-               sprintf(logtext + strlen(logtext), ", 0x%llx, 0x%llx, 0x%llx"
-                               , a3, a4, a5);
-               break;
-#endif /* TRACE_ARGS_NUM >= 6 */
-
-#if TRACE_ARGS_NUM == 8
-       case 7:
-               sprintf(logtext + strlen(logtext), "0x%llx, 0x%llx, 0x%llx"
-                               , a0, a1, a2);
-               sprintf(logtext + strlen(logtext), ", 0x%llx, 0x%llx, 0x%llx"
-                               , a3, a4, a5);
-               sprintf(logtext + strlen(logtext), ", 0x%llx", a6);
-               break;
+       if (md->paramcount >= 4) {
+               strcat(logtext, ", ");
 
-       case 8:
-               sprintf(logtext + strlen(logtext), "0x%llx, 0x%llx, 0x%llx"
-                               , a0, a1, a2);
-               sprintf(logtext + strlen(logtext), ", 0x%llx, 0x%llx, 0x%llx"
-                               , a3, a4, a5);
-               sprintf(logtext + strlen(logtext), ", 0x%llx, 0x%llx", a6, a7);
-               break;
-#endif /* TRACE_ARGS_NUM == 8 */
-
-       default:
-#if TRACE_ARGS_NUM == 2
-               sprintf(logtext + strlen(logtext), "0x%llx, 0x%llx, ...(%d)", a0, a1, md->paramcount - 2);
-
-#elif TRACE_ARGS_NUM == 4
-               sprintf(logtext + strlen(logtext),
-                               "0x%llx, 0x%llx, 0x%llx, 0x%llx, ...(%d)",
-                               a0, a1, a2, a3, md->paramcount - 4);
-
-#elif TRACE_ARGS_NUM == 6
-               sprintf(logtext + strlen(logtext),
-                               "0x%llx, 0x%llx, 0x%llx, 0x%llx, 0x%llx, 0x%llx, ...(%d)",
-                               a0, a1, a2, a3, a4, a5, md->paramcount - 6);
-
-#elif TRACE_ARGS_NUM == 8
-               sprintf(logtext + strlen(logtext),"0x%llx, 0x%llx, 0x%llx,"
-                               , a0, a1, a2);
-               sprintf(logtext + strlen(logtext)," 0x%llx, 0x%llx, 0x%llx,"
-                               , a3, a4, a5);
-               sprintf(logtext + strlen(logtext)," 0x%llx, 0x%llx, ...(%d)"
-                               , a6, a7, md->paramcount - 8);
+               logtext = builtin_print_argument(logtext, logtextlen,
+                                                                                &md->paramtypes[3], a3);
+       }
 #endif
-               break;
-
-#else /* SIZEOF_VOID_P == 4 */
-
-       case 1:
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx",
-                               a0);
-               break;
 
-       case 2:
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx",
-                               a0, a1);
-               break;
-
-       case 3:
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx, 0x%lx", a0, a1, a2);
-               break;
+#if TRACE_ARGS_NUM >= 6
+       if (md->paramcount >= 5) {
+               strcat(logtext, ", ");
 
-       case 4:
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx, 0x%lx, 0x%lx",
-                               a0, a1, a2, a3);
-               break;
+               logtext = builtin_print_argument(logtext, logtextlen,
+                                                                                &md->paramtypes[4], a4);
+       }
 
-#if TRACE_ARGS_NUM >= 6
-       case 5:
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx",
-                               a0, a1, a2, a3, a4);
-               break;
+       if (md->paramcount >= 6) {
+               strcat(logtext, ", ");
 
-       case 6:
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx",
-                               a0, a1, a2, a3, a4, a5);
-               break;
-#endif /* TRACE_ARGS_NUM >= 6 */
+               logtext = builtin_print_argument(logtext, logtextlen,
+                                                                                &md->paramtypes[5], a5);
+       }
+#endif
 
 #if TRACE_ARGS_NUM == 8
-       case 7:
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx",
-                               a0, a1, a2, a3, a4, a5, a6);
-               break;
+       if (md->paramcount >= 7) {
+               strcat(logtext, ", ");
 
-       case 8:
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx",
-                               a0, a1, a2, a3, a4, a5, a6, a7);
-               break;
-#endif /* TRACE_ARGS_NUM == 8 */
+               logtext = builtin_print_argument(logtext, logtextlen,
+                                                                                &md->paramtypes[6], a6);
+       }
 
-       default:
-#if TRACE_ARGS_NUM == 4
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx, 0x%lx, 0x%lx, ...(%d)",
-                               a0, a1, a2, a3, md->paramcount - 4);
-
-#elif TRACE_ARGS_NUM == 6
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, ...(%d)",
-                               a0, a1, a2, a3, a4, a5, md->paramcount - 6);
-
-#elif TRACE_ARGS_NUM == 8
-               sprintf(logtext + strlen(logtext),
-                               "0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, ...(%d)",
-                               a0, a1, a2, a3, a4, a5, a6, a7, md->paramcount - 8);
+       if (md->paramcount >= 8) {
+               strcat(logtext, ", ");
+
+               logtext = builtin_print_argument(logtext, logtextlen,
+                                                                                &md->paramtypes[7], a7);
+       }
 #endif
-               break;
-#endif /* SIZEOF_VOID_P == 4 */
+
+       if (md->paramcount > 8) {
+               sprintf(logtext + strlen(logtext), ", ...(%d)",
+                               md->paramcount - TRACE_ARGS_NUM);
        }
 
        strcat(logtext, ")");
@@ -1510,14 +1580,16 @@ void builtin_trace_args(s8 a0, s8 a1,
        methodindent++;
 }
 #endif
+#endif /* !defined(NDEBUG) */
 
 
 /* builtin_displaymethodstop ***************************************************
 
-   XXX
+   Print method exit for -verbose:call.
 
 *******************************************************************************/
 
+#if !defined(NDEBUG)
 void builtin_displaymethodstop(methodinfo *m, s8 l, double d, float f)
 {
        methoddesc *md;
@@ -1526,18 +1598,20 @@ void builtin_displaymethodstop(methodinfo *m, s8 l, double d, float f)
        s4          dumpsize;
        s4          i;
        s4          pos;
-       imm_union   imu;
 
        md = m->parseddesc;
 
        /* calculate message length */
 
        logtextlen =
-               6 + methodindent + strlen("finished: ") +
-               utf_strlen(m->class->name) +
+               strlen("4294967295 ") +
+               strlen("-2147483647-") +        /* INT_MAX should be sufficient       */
+               methodindent +
+               strlen("finished: ") +
+               utf_bytes(m->class->name) +
                strlen(".") +
-               utf_strlen(m->name) +
-               utf_strlen(m->descriptor) +
+               utf_bytes(m->name) +
+               utf_bytes(m->descriptor) +
                strlen(" SYNCHRONIZED") + strlen("(") + strlen(")");
 
        /* add maximal argument length */
@@ -1559,49 +1633,24 @@ void builtin_displaymethodstop(methodinfo *m, s8 l, double d, float f)
 
        /* generate the message */
 
-       sprintf(logtext,"-%d-",methodindent);
+       sprintf(logtext, "           ");
+       sprintf(logtext + strlen(logtext), "-%d-", methodindent);
+
        pos = strlen(logtext);
 
        for (i = 0; i < methodindent; i++)
                logtext[pos++] = '\t';
 
        strcpy(logtext + pos, "finished: ");
-       utf_strcat_classname(logtext, m->class->name);
+       utf_cat_classname(logtext, m->class->name);
        strcat(logtext, ".");
-       utf_strcat(logtext, m->name);
-       utf_strcat(logtext, m->descriptor);
-
-       switch (md->returntype.type) {
-       case TYPE_INT:
-               sprintf(logtext + strlen(logtext), "->%d (0x%08x)", (s4) l, (s4) l);
-               break;
-
-       case TYPE_LNG:
-#if SIZEOF_VOID_P == 4
-               sprintf(logtext + strlen(logtext), "->%lld (0x%016llx)", (s8) l, l);
-#else
-               sprintf(logtext + strlen(logtext), "->%ld (0x%016lx)", (s8) l, l);
-#endif
-               break;
-
-       case TYPE_ADR:
-               sprintf(logtext + strlen(logtext), "->%p", (void *) (ptrint) l);
-               break;
+       utf_cat(logtext, m->name);
+       utf_cat(logtext, m->descriptor);
 
-       case TYPE_FLT:
-               imu.f = f;
-               sprintf(logtext + strlen(logtext), "->%.8f (0x%08x)", f, imu.i);
-               break;
+       if (!IS_VOID_TYPE(md->returntype.type))
+               strcat(logtext, "->");
 
-       case TYPE_DBL:
-               imu.d = d;
-#if SIZEOF_VOID_P == 4
-               sprintf(logtext + strlen(logtext), "->%.16g (0x%016llx)", d, imu.l);
-#else
-               sprintf(logtext + strlen(logtext), "->%.16g (0x%016lx)", d, imu.l);
-#endif
-               break;
-       }
+       logtext = builtin_print_argument(logtext, logtextlen, &md->returntype, l);
 
        log_text(logtext);
 
@@ -1609,186 +1658,64 @@ void builtin_displaymethodstop(methodinfo *m, s8 l, double d, float f)
 
        dump_release(dumpsize);
 }
+#endif /* !defined(NDEBUG) */
 
 
-/****************************************************************************
-                        SYNCHRONIZATION FUNCTIONS
-*****************************************************************************/
-
-#if defined(USE_THREADS) && !defined(NATIVE_THREADS)
-/*
- * Lock the mutex of an object.
- */
-void internal_lock_mutex_for_object(java_objectheader *object)
+#if defined(ENABLE_CYCLES_STATS)
+void builtin_print_cycles_stats(FILE *file)
 {
-       mutexHashEntry *entry;
-       int hashValue;
-
-       assert(object != 0);
-
-       hashValue = MUTEX_HASH_VALUE(object);
-       entry = &mutexHashTable[hashValue];
-
-       if (entry->object != 0) {
-               if (entry->mutex.count == 0 && entry->conditionCount == 0) {
-                       entry->object = 0;
-                       entry->mutex.holder = 0;
-                       entry->mutex.count = 0;
-                       entry->mutex.muxWaiters = 0;
+       fprintf(file,"builtin cylce count statistics:\n");
 
-               } else {
-                       while (entry->next != 0 && entry->object != object)
-                               entry = entry->next;
-
-                       if (entry->object != object) {
-                               entry->next = firstFreeOverflowEntry;
-                               firstFreeOverflowEntry = firstFreeOverflowEntry->next;
+       CYCLES_STATS_PRINT_OVERHEAD(builtin_overhead,file);
+       CYCLES_STATS_PRINT(builtin_new         ,file);
 
-                               entry = entry->next;
-                               entry->object = 0;
-                               entry->next = 0;
-                               assert(entry->conditionCount == 0);
-                       }
-               }
-
-       } else {
-               entry->mutex.holder = 0;
-               entry->mutex.count = 0;
-               entry->mutex.muxWaiters = 0;
-       }
-
-       if (entry->object == 0)
-               entry->object = object;
-       
-       internal_lock_mutex(&entry->mutex);
+       fprintf(file,"\n");
 }
-#endif
+#endif /* defined(ENABLE_CYCLES_STATS) */
 
 
-#if defined(USE_THREADS) && !defined(NATIVE_THREADS)
-/*
- * Unlocks the mutex of an object.
- */
-void internal_unlock_mutex_for_object (java_objectheader *object)
-{
-       int hashValue;
-       mutexHashEntry *entry;
-
-       hashValue = MUTEX_HASH_VALUE(object);
-       entry = &mutexHashTable[hashValue];
-
-       if (entry->object == object) {
-               internal_unlock_mutex(&entry->mutex);
-
-       } else {
-               while (entry->next != 0 && entry->next->object != object)
-                       entry = entry->next;
-
-               assert(entry->next != 0);
+/*****************************************************************************
+                         MISCELLANEOUS HELPER FUNCTIONS
+*****************************************************************************/
 
-               internal_unlock_mutex(&entry->next->mutex);
 
-               if (entry->next->mutex.count == 0 && entry->conditionCount == 0) {
-                       mutexHashEntry *unlinked = entry->next;
 
-                       entry->next = unlinked->next;
-                       unlinked->next = firstFreeOverflowEntry;
-                       firstFreeOverflowEntry = unlinked;
-               }
-       }
-}
-#endif
+/*********** Functions for integer divisions *****************************
+       On some systems (eg. DEC ALPHA), integer division is not supported by the
+       CPU. These helper functions implement the missing functionality.
 
+******************************************************************************/
 
-#if defined(USE_THREADS)
-void builtin_monitorenter(java_objectheader *o)
+#if !SUPPORT_DIVISION
+s4 builtin_idiv(s4 a, s4 b)
 {
-#if !defined(NATIVE_THREADS)
-       int hashValue;
+       s4 c;
 
-       ++blockInts;
+       c = a / b;
 
-       hashValue = MUTEX_HASH_VALUE(o);
-       if (mutexHashTable[hashValue].object == o 
-               && mutexHashTable[hashValue].mutex.holder == currentThread)
-               ++mutexHashTable[hashValue].mutex.count;
-       else
-               internal_lock_mutex_for_object(o);
-
-       --blockInts;
-#else
-       monitorEnter((threadobject *) THREADOBJECT, o);
-#endif
-}
-#endif
-
-
-#if defined(USE_THREADS)
-/*
- * Locks the class object - needed for static synchronized methods.
- * The use_class_as_object call is needed in order to circumvent a
- * possible deadlock with builtin_monitorenter called by another
- * thread calling use_class_as_object.
- */
-void builtin_staticmonitorenter(classinfo *c)
-{
-       use_class_as_object(c);
-       builtin_monitorenter(&c->header);
+       return c;
 }
-#endif
 
-
-#if defined(USE_THREADS)
-void builtin_monitorexit(java_objectheader *o)
+s4 builtin_irem(s4 a, s4 b)
 {
-#if !defined(NATIVE_THREADS)
-       int hashValue;
-
-       ++blockInts;
+       s4 c;
 
-       hashValue = MUTEX_HASH_VALUE(o);
-       if (mutexHashTable[hashValue].object == o) {
-               if (mutexHashTable[hashValue].mutex.count == 1
-                       && mutexHashTable[hashValue].mutex.muxWaiters != 0)
-                       internal_unlock_mutex_for_object(o);
-               else
-                       --mutexHashTable[hashValue].mutex.count;
-
-       } else
-               internal_unlock_mutex_for_object(o);
+       c = a % b;
 
-       --blockInts;
-#else
-       monitorExit((threadobject *) THREADOBJECT, o);
-#endif
+       return c;
 }
-#endif
-
-
-/*****************************************************************************
-                         MISCELLANEOUS HELPER FUNCTIONS
-*****************************************************************************/
+#endif /* !SUPPORT_DIVISION */
 
 
+/* functions for long arithmetics **********************************************
 
-/*********** Functions for integer divisions *****************************
-       On some systems (eg. DEC ALPHA), integer division is not supported by the
-       CPU. These helper functions implement the missing functionality.
-
-******************************************************************************/
-
-s4 builtin_idiv(s4 a, s4 b) { return a / b; }
-s4 builtin_irem(s4 a, s4 b) { return a % b; }
-
-
-/************** Functions for long arithmetics *******************************
-
-       On systems where 64 bit Integers are not supported by the CPU, these
-       functions are needed.
+   On systems where 64 bit Integers are not supported by the CPU,
+   these functions are needed.
 
 ******************************************************************************/
 
+#if !(SUPPORT_LONG && SUPPORT_LONG_ADD)
 s8 builtin_ladd(s8 a, s8 b)
 {
        s8 c;
@@ -1802,7 +1729,7 @@ s8 builtin_ladd(s8 a, s8 b)
        return c;
 }
 
-s8 builtin_lsub(s8 a, s8 b) 
+s8 builtin_lsub(s8 a, s8 b)
 {
        s8 c;
 
@@ -1815,7 +1742,23 @@ s8 builtin_lsub(s8 a, s8 b)
        return c;
 }
 
-s8 builtin_lmul(s8 a, s8 b) 
+s8 builtin_lneg(s8 a)
+{
+       s8 c;
+
+#if U8_AVAILABLE
+       c = -a;
+#else
+       c = builtin_i2l(0);
+#endif
+
+       return c;
+}
+#endif /* !(SUPPORT_LONG && SUPPORT_LONG_ADD) */
+
+
+#if !(SUPPORT_LONG && SUPPORT_LONG_MUL)
+s8 builtin_lmul(s8 a, s8 b)
 {
        s8 c;
 
@@ -1827,8 +1770,11 @@ s8 builtin_lmul(s8 a, s8 b)
 
        return c;
 }
+#endif /* !(SUPPORT_LONG && SUPPORT_LONG_MUL) */
+
 
-s8 builtin_ldiv(s8 a, s8 b) 
+#if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
+s8 builtin_ldiv(s8 a, s8 b)
 {
        s8 c;
 
@@ -1841,7 +1787,7 @@ s8 builtin_ldiv(s8 a, s8 b)
        return c;
 }
 
-s8 builtin_lrem(s8 a, s8 b) 
+s8 builtin_lrem(s8 a, s8 b)
 {
        s8 c;
 
@@ -1853,8 +1799,11 @@ s8 builtin_lrem(s8 a, s8 b)
 
        return c;
 }
+#endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
 
-s8 builtin_lshl(s8 a, s4 b) 
+
+#if !(SUPPORT_LONG && SUPPORT_LONG_SHIFT)
+s8 builtin_lshl(s8 a, s4 b)
 {
        s8 c;
 
@@ -1867,7 +1816,7 @@ s8 builtin_lshl(s8 a, s4 b)
        return c;
 }
 
-s8 builtin_lshr(s8 a, s4 b) 
+s8 builtin_lshr(s8 a, s4 b)
 {
        s8 c;
 
@@ -1880,7 +1829,7 @@ s8 builtin_lshr(s8 a, s4 b)
        return c;
 }
 
-s8 builtin_lushr(s8 a, s4 b) 
+s8 builtin_lushr(s8 a, s4 b)
 {
        s8 c;
 
@@ -1892,8 +1841,11 @@ s8 builtin_lushr(s8 a, s4 b)
 
        return c;
 }
+#endif /* !(SUPPORT_LONG && SUPPORT_LONG_SHIFT) */
+
 
-s8 builtin_land(s8 a, s8 b) 
+#if !(SUPPORT_LONG && SUPPORT_LONG_LOGICAL)
+s8 builtin_land(s8 a, s8 b)
 {
        s8 c;
 
@@ -1906,7 +1858,7 @@ s8 builtin_land(s8 a, s8 b)
        return c;
 }
 
-s8 builtin_lor(s8 a, s8 b) 
+s8 builtin_lor(s8 a, s8 b)
 {
        s8 c;
 
@@ -1931,36 +1883,28 @@ s8 builtin_lxor(s8 a, s8 b)
 
        return c;
 }
+#endif /* !(SUPPORT_LONG && SUPPORT_LONG_LOGICAL) */
 
-s8 builtin_lneg(s8 a) 
-{
-       s8 c;
 
+#if !(SUPPORT_LONG && SUPPORT_LONG_CMP)
+s4 builtin_lcmp(s8 a, s8 b)
+{ 
 #if U8_AVAILABLE
-       c = -a;
-#else
-       c = builtin_i2l(0);
-#endif
+       if (a < b)
+               return -1;
 
-       return c;
-}
+       if (a > b)
+               return 1;
 
-s4 builtin_lcmp(s8 a, s8 b) 
-{ 
-#if U8_AVAILABLE
-       if (a < b) return -1;
-       if (a > b) return 1;
        return 0;
 #else
        return 0;
 #endif
 }
+#endif /* !(SUPPORT_LONG && SUPPORT_LONG_CMP) */
 
 
-
-
-
-/*********** Functions for floating point operations *************************/
+/* functions for unsupported floating instructions ****************************/
 
 /* used to convert FLT_xxx defines into float values */
 
@@ -1984,6 +1928,7 @@ static inline float longBitsToDouble(s8 l)
 }
 
 
+#if !SUPPORT_FLOAT
 float builtin_fadd(float a, float b)
 {
        if (isnanf(a)) return intBitsToFloat(FLT_NAN);
@@ -2097,12 +2042,6 @@ float builtin_fdiv(float a, float b)
 }
 
 
-float builtin_frem(float a, float b)
-{
-       return fmodf(a, b);
-}
-
-
 float builtin_fneg(float a)
 {
        if (isnanf(a)) return a;
@@ -2111,8 +2050,10 @@ float builtin_fneg(float a)
                else return copysignf(a, -copysignf(1.0, a));
        }
 }
+#endif /* !SUPPORT_FLOAT */
 
 
+#if !SUPPORT_FLOAT || defined(ENABLE_INTRP)
 s4 builtin_fcmpl(float a, float b)
 {
        if (isnanf(a)) return -1;
@@ -2139,11 +2080,18 @@ s4 builtin_fcmpg(float a, float b)
        if (a == b) return 0;
        return -1;
 }
+#endif /* !SUPPORT_FLOAT || defined(ENABLE_INTRP) */
 
 
+float builtin_frem(float a, float b)
+{
+       return fmodf(a, b);
+}
+
 
-/************************* Functions for doubles ****************************/
+/* functions for unsupported double instructions ******************************/
 
+#if !SUPPORT_DOUBLE
 double builtin_dadd(double a, double b)
 {
        if (isnan(a)) return longBitsToDouble(DBL_NAN);
@@ -2252,11 +2200,6 @@ double builtin_ddiv(double a, double b)
 }
 
 
-double builtin_drem(double a, double b)
-{
-       return fmod(a, b);
-}
-
 /* builtin_dneg ****************************************************************
 
    Implemented as described in VM Spec.
@@ -2286,8 +2229,10 @@ double builtin_dneg(double a)
                }
        }
 }
+#endif /* !SUPPORT_DOUBLE */
 
 
+#if !SUPPORT_DOUBLE || defined(ENABLE_INTRP)
 s4 builtin_dcmpl(double a, double b)
 {
        if (isnan(a)) return -1;
@@ -2314,10 +2259,18 @@ s4 builtin_dcmpg(double a, double b)
        if (a == b) return 0;
        return -1;
 }
+#endif /* !SUPPORT_DOUBLE || defined(ENABLE_INTRP) */
 
 
-/*********************** Conversion operations ****************************/
+double builtin_drem(double a, double b)
+{
+       return fmod(a, b);
+}
+
+
+/* conversion operations ******************************************************/
 
+#if 0
 s8 builtin_i2l(s4 i)
 {
 #if U8_AVAILABLE
@@ -2330,31 +2283,36 @@ s8 builtin_i2l(s4 i)
 #endif
 }
 
+s4 builtin_l2i(s8 l)
+{
+#if U8_AVAILABLE
+       return (s4) l;
+#else
+       return l.low;
+#endif
+}
+#endif
+
 
+#if !(SUPPORT_FLOAT && SUPPORT_I2F)
 float builtin_i2f(s4 a)
 {
        float f = (float) a;
        return f;
 }
+#endif /* !(SUPPORT_FLOAT && SUPPORT_I2F) */
 
 
+#if !(SUPPORT_DOUBLE && SUPPORT_I2D)
 double builtin_i2d(s4 a)
 {
        double d = (double) a;
        return d;
 }
+#endif /* !(SUPPORT_DOUBLE && SUPPORT_I2D) */
 
 
-s4 builtin_l2i(s8 l)
-{
-#if U8_AVAILABLE
-       return (s4) l;
-#else
-       return l.low;
-#endif
-}
-
-
+#if !(SUPPORT_LONG && SUPPORT_FLOAT && SUPPORT_L2F)
 float builtin_l2f(s8 a)
 {
 #if U8_AVAILABLE
@@ -2364,8 +2322,10 @@ float builtin_l2f(s8 a)
        return 0.0;
 #endif
 }
+#endif /* !(SUPPORT_LONG && SUPPORT_FLOAT && SUPPORT_L2F) */
 
 
+#if !(SUPPORT_LONG && SUPPORT_DOUBLE && SUPPORT_L2D)
 double builtin_l2d(s8 a)
 {
 #if U8_AVAILABLE
@@ -2375,8 +2335,10 @@ double builtin_l2d(s8 a)
        return 0.0;
 #endif
 }
+#endif /* !(SUPPORT_LONG && SUPPORT_DOUBLE && SUPPORT_L2D) */
 
 
+#if !(SUPPORT_FLOAT && SUPPORT_F2I) || defined(ENABLE_INTRP)
 s4 builtin_f2i(float a) 
 {
        s4 i;
@@ -2401,8 +2363,10 @@ s4 builtin_f2i(float a)
                return 2147483647;
                return (-2147483648); */
 }
+#endif /* !(SUPPORT_FLOAT && SUPPORT_F2I) || defined(ENABLE_INTRP) */
 
 
+#if !(SUPPORT_FLOAT && SUPPORT_LONG && SUPPORT_F2L)
 s8 builtin_f2l(float a)
 {
        s8 l;
@@ -2427,20 +2391,10 @@ s8 builtin_f2l(float a)
                return 9223372036854775807L;
                return (-9223372036854775808L); */
 }
+#endif /* !(SUPPORT_FLOAT && SUPPORT_LONG && SUPPORT_F2L) */
 
 
-double builtin_f2d(float a)
-{
-       if (finitef(a)) return (double) a;
-       else {
-               if (isnanf(a))
-                       return longBitsToDouble(DBL_NAN);
-               else
-                       return copysign(longBitsToDouble(DBL_POSINF), (double) copysignf(1.0, a) );
-       }
-}
-
-
+#if !(SUPPORT_DOUBLE && SUPPORT_D2I) || defined(ENABLE_INTRP)
 s4 builtin_d2i(double a) 
 { 
        double d;
@@ -2459,8 +2413,10 @@ s4 builtin_d2i(double a)
                return 2147483647;
        return (-2147483647-1);
 }
+#endif /* !(SUPPORT_DOUBLE && SUPPORT_D2I) || defined(ENABLE_INTRP) */
 
 
+#if !(SUPPORT_DOUBLE && SUPPORT_LONG && SUPPORT_D2L)
 s8 builtin_d2l(double a)
 {
        double d;
@@ -2479,8 +2435,21 @@ s8 builtin_d2l(double a)
                return 9223372036854775807LL;
        return (-9223372036854775807LL-1);
 }
+#endif /* !(SUPPORT_DOUBLE && SUPPORT_LONG && SUPPORT_D2L) */
 
 
+#if !(SUPPORT_FLOAT && SUPPORT_DOUBLE)
+double builtin_f2d(float a)
+{
+       if (finitef(a)) return (double) a;
+       else {
+               if (isnanf(a))
+                       return longBitsToDouble(DBL_NAN);
+               else
+                       return copysign(longBitsToDouble(DBL_POSINF), (double) copysignf(1.0, a) );
+       }
+}
+
 float builtin_d2f(double a)
 {
        if (finite(a))
@@ -2492,39 +2461,138 @@ float builtin_d2f(double a)
                        return copysignf(intBitsToFloat(FLT_POSINF), (float) copysign(1.0, a));
        }
 }
+#endif /* !(SUPPORT_FLOAT && SUPPORT_DOUBLE) */
 
 
-/* builtin_clone_array *********************************************************
+/* builtin_arraycopy ***********************************************************
 
-   Wrapper function for cloning arrays.
+   Builtin for java.lang.System.arraycopy.
+
+   ATTENTION: This builtin function returns a boolean value to signal
+   the ICMD_BUILTIN if there was an exception.
 
 *******************************************************************************/
 
-java_arrayheader *builtin_clone_array(void *env, java_arrayheader *o)
+bool builtin_arraycopy(java_arrayheader *src, s4 srcStart,
+                                          java_arrayheader *dest, s4 destStart, s4 len)
 {
-       java_arrayheader    *ah;
-       java_lang_Cloneable *c;
+       arraydescriptor *sdesc;
+       arraydescriptor *ddesc;
+       s4               i;
 
-       c = (java_lang_Cloneable *) o;
+       if ((src == NULL) || (dest == NULL)) { 
+               exceptions_throw_nullpointerexception();
+               return false;
+       }
 
-       ah = (java_arrayheader *) Java_java_lang_VMObject_clone(0, 0, c);
+       sdesc = src->objheader.vftbl->arraydesc;
+       ddesc = dest->objheader.vftbl->arraydesc;
 
-       return ah;
+       if (!sdesc || !ddesc || (sdesc->arraytype != ddesc->arraytype)) {
+               exceptions_throw_arraystoreexception();
+               return false;
+       }
+
+       /* we try to throw exception with the same message as SUN does */
+
+       if ((len < 0) || (srcStart < 0) || (destStart < 0) ||
+               (srcStart  + len < 0) || (srcStart  + len > src->size) ||
+               (destStart + len < 0) || (destStart + len > dest->size)) {
+               exceptions_throw_arrayindexoutofboundsexception();
+               return false;
+       }
+
+       if (sdesc->componentvftbl == ddesc->componentvftbl) {
+               /* We copy primitive values or references of exactly the same type */
+
+               s4 dataoffset = sdesc->dataoffset;
+               s4 componentsize = sdesc->componentsize;
+
+               memmove(((u1 *) dest) + dataoffset + componentsize * destStart,
+                               ((u1 *) src)  + dataoffset + componentsize * srcStart,
+                               (size_t) len * componentsize);
+       }
+       else {
+               /* We copy references of different type */
+
+               java_objectarray *oas = (java_objectarray *) src;
+               java_objectarray *oad = (java_objectarray *) dest;
+                
+               if (destStart <= srcStart) {
+                       for (i = 0; i < len; i++) {
+                               java_objectheader *o = oas->data[srcStart + i];
+
+                               if (!builtin_canstore(oad, o)) {
+                                       exceptions_throw_arraystoreexception();
+                                       return false;
+                               }
+
+                               oad->data[destStart + i] = o;
+                       }
+               }
+               else {
+                       /* XXX this does not completely obey the specification!
+                          If an exception is thrown only the elements above the
+                          current index have been copied. The specification
+                          requires that only the elements *below* the current
+                          index have been copied before the throw. */
+
+                       for (i = len - 1; i >= 0; i--) {
+                               java_objectheader *o = oas->data[srcStart + i];
+
+                               if (!builtin_canstore(oad, o)) {
+                                       exceptions_throw_arraystoreexception();
+                                       return false;
+                               }
+
+                               oad->data[destStart + i] = o;
+                       }
+               }
+       }
+
+       return true;
 }
 
 
-/* builtin_asm_get_exceptionptrptr *********************************************
+/* builtin_currenttimemillis ***************************************************
 
-   this is a wrapper for calls from asmpart
+   Return the current time in milliseconds.
 
 *******************************************************************************/
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
-java_objectheader **builtin_asm_get_exceptionptrptr(void)
+s8 builtin_currenttimemillis(void)
 {
-       return builtin_get_exceptionptrptr();
+       struct timeval tv;
+       s8             result;
+
+       if (gettimeofday(&tv, NULL) == -1)
+               vm_abort("gettimeofday failed: %s", strerror(errno));
+
+       result = (s8) tv.tv_sec;
+       result *= 1000;
+       result += (tv.tv_usec / 1000);
+
+       return result;
+}
+
+
+/* builtin_clone_array *********************************************************
+
+   Wrapper function for cloning arrays.
+
+*******************************************************************************/
+
+java_arrayheader *builtin_clone_array(void *env, java_arrayheader *o)
+{
+       java_arrayheader    *ah;
+       java_lang_Cloneable *c;
+
+       c = (java_lang_Cloneable *) o;
+
+       ah = (java_arrayheader *) Java_java_lang_VMObject_clone(0, 0, c);
+
+       return ah;
 }
-#endif
 
 
 /*
@@ -2538,4 +2606,5 @@ java_objectheader **builtin_asm_get_exceptionptrptr(void)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */