* src/vm/jit/x86_64/codegen.h (ICONST): Fix this macro so it properly
[cacao.git] / src / vm / builtin.c
index c0ee69fd8a847b71a4fd4ad43a5b73a0911fdb46..02a80316e3456514afbb5b9501ede1407bf4001e 100644 (file)
@@ -29,6 +29,7 @@
             Mark Probst
 
    Changes: Christian Thalinger
+            Edwin Steiner
 
    Contains C functions for JavaVM Instructions that cannot be
    translated to machine language directly. Consequently, the
@@ -36,7 +37,7 @@
    calls instead of machine instructions, using the C calling
    convention.
 
-   $Id: builtin.c 4550 2006-03-01 17:00:33Z 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 "vm/types.h"
 
 #include "native/include/java_lang_Object.h"          /* required by VMObject */
 #include "native/include/java_lang_VMObject.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"
@@ -83,6 +81,8 @@
 #include "vm/stringlocal.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
 *****************************************************************************/
@@ -499,13 +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.
 
-#if !defined(NDEBUG)
-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;
@@ -520,12 +566,17 @@ java_objectheader *builtin_throw_exception(java_objectheader *xptr)
 
                if (t) {
                        logtextlen +=
-                               utf_strlen(xptr->vftbl->class->name) +
-                               strlen(": ") +
-                               javastring_strlen(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 */
 
@@ -536,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;
@@ -545,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 {
@@ -558,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;
 }
-#endif /* !defined(NDEBUG) */
 
 
 /* builtin_canstore ************************************************************
@@ -726,6 +781,15 @@ 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 */
 
@@ -747,24 +811,33 @@ java_objectheader *builtin_new(classinfo *c)
                        return NULL;
 
        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;
 }
 
@@ -785,6 +858,11 @@ 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;
@@ -809,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;
 }
 
@@ -1102,6 +1183,7 @@ java_objectheader *builtin_trace_exception(java_objectheader *xptr,
        char *logtext;
        s4    logtextlen;
        s4    dumpsize;
+       codeinfo *code;
 
        if (opt_verbosecall && indent)
                methodindent--;
@@ -1110,10 +1192,9 @@ java_objectheader *builtin_trace_exception(java_objectheader *xptr,
 
        if (xptr) {
                logtextlen =
-                       strlen("Exception ") +
-                       utf_strlen(xptr->vftbl->class->name);
-
-       } else {
+                       strlen("Exception ") + utf_bytes(xptr->vftbl->class->name);
+       } 
+       else {
                logtextlen = strlen("Some Throwable");
        }
 
@@ -1121,10 +1202,10 @@ java_objectheader *builtin_trace_exception(java_objectheader *xptr,
 
        if (m) {
                logtextlen +=
-                       utf_strlen(m->class->name) +
+                       utf_bytes(m->class->name) +
                        strlen(".") +
-                       utf_strlen(m->name) +
-                       utf_strlen(m->descriptor) +
+                       utf_bytes(m->name) +
+                       utf_bytes(m->descriptor) +
                        strlen("(NOSYNC,NATIVE");
 
 #if SIZEOF_VOID_P == 8
@@ -1137,12 +1218,14 @@ java_objectheader *builtin_trace_exception(java_objectheader *xptr,
                if (m->class->sourcefile == NULL)
                        logtextlen += strlen("<NO CLASSFILE INFORMATION>");
                else
-                       logtextlen += utf_strlen(m->class->sourcefile);
+                       logtextlen += utf_bytes(m->class->sourcefile);
 
                logtextlen += strlen(":65536)");
 
-       } else
+       } 
+       else {
                logtextlen += strlen("call_java_method");
+       }
 
        logtextlen += strlen("0");
 
@@ -1154,7 +1237,7 @@ java_objectheader *builtin_trace_exception(java_objectheader *xptr,
 
        if (xptr) {
                strcpy(logtext, "Exception ");
-               utf_strcat_classname(logtext, xptr->vftbl->class->name);
+               utf_cat_classname(logtext, xptr->vftbl->class->name);
 
        } else {
                strcpy(logtext, "Some Throwable");
@@ -1163,10 +1246,10 @@ java_objectheader *builtin_trace_exception(java_objectheader *xptr,
        strcat(logtext, " thrown in ");
 
        if (m) {
-               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_SYNCHRONIZED)
                        strcat(logtext, "(SYNC");
@@ -1176,31 +1259,38 @@ java_objectheader *builtin_trace_exception(java_objectheader *xptr,
                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);
+                                       (ptrint) code->entrypoint, (ptrint) pos);
 #else
                        sprintf(logtext + strlen(logtext),
                                        ")(0x%08x) at position 0x%08x",
-                                       (ptrint) m->entrypoint, (ptrint) pos);
+                                       (ptrint) code->entrypoint, (ptrint) pos);
 #endif
 
                } 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);
+                                       (ptrint) code->entrypoint, (ptrint) pos);
 #else
                        sprintf(logtext + strlen(logtext),
                                        ")(0x%08x) at position 0x%08x (",
-                                       (ptrint) m->entrypoint, (ptrint) pos);
+                                       (ptrint) code->entrypoint, (ptrint) pos);
 #endif
 
                        if (m->class->sourcefile == NULL)
                                strcat(logtext, "<NO CLASSFILE INFORMATION>");
                        else
-                               utf_strcat(logtext, m->class->sourcefile);
+                               utf_cat(logtext, m->class->sourcefile);
 
                        sprintf(logtext + strlen(logtext), ":%d)", 0);
                }
@@ -1219,9 +1309,120 @@ java_objectheader *builtin_trace_exception(java_objectheader *xptr,
 #endif /* !defined(NDEBUG) */
 
 
+/* builtin_print_argument ******************************************************
+
+   Prints arguments and return values for the call trace.
+
+*******************************************************************************/
+
+#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;
+
+       switch (paramtype->type) {
+       case TYPE_INT:
+               sprintf(logtext + strlen(logtext), "0x%x", (s4) value);
+               break;
+
+       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;
+
+       case TYPE_FLT:
+               imu.l = value;
+               sprintf(logtext + strlen(logtext), "%.8f (0x%08x)", imu.f, imu.i);
+               break;
+
+       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;
+
+       case TYPE_ADR:
+               sprintf(logtext + strlen(logtext), "%p", (void *) (ptrint) value);
+
+               /* 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("\")");
+
+                               /* 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;
+                               }
+
+                               len = strlen(" (Class = \"") + utf_bytes(u) + strlen("\")");
+
+                               /* realloc memory for string length */
+
+                               DMREALLOC(logtext, char, logtextlen, logtextlen + len);
+
+                               /* strcat to the logtext */
+
+                               strcat(logtext, " (Class = \"");
+                               utf_cat_classname(logtext, u);
+                               strcat(logtext, "\")");
+                       }
+               }
+       }
+
+       return logtext;
+}
+#endif /* !defined(NDEBUG) */
+
+
 /* builtin_trace_args **********************************************************
 
-   XXX
+   Print method call with arguments for -verbose:call.
 
 *******************************************************************************/
 
@@ -1256,10 +1457,10 @@ void builtin_trace_args(s8 a0, s8 a1,
                strlen("-2147483647-") +        /* INT_MAX should be sufficient       */
                methodindent +
                strlen("called: ") +
-               utf_strlen(m->class->name) +
+               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 */
@@ -1284,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");
@@ -1303,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;
+       if (md->paramcount >= 3) {
+               strcat(logtext, ", ");
 
-       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;
-
-
-       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;
+               logtext = builtin_print_argument(logtext, logtextlen,
+                                                                                &md->paramtypes[2], a2);
+       }
 
-       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 */
+       if (md->paramcount >= 4) {
+               strcat(logtext, ", ");
 
-       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, ")");
@@ -1487,25 +1585,19 @@ void builtin_trace_args(s8 a0, s8 a1,
 
 /* 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;
-       char              *logtext;
-       s4                 logtextlen;
-       s4                 dumpsize;
-       s4                 i;
-       s4                 pos;
-       java_objectheader *o;
-       java_lang_String  *s;
-       classinfo         *c;
-       s4                 len;
-       utf               *u;
-       imm_union          imu;
+       methoddesc *md;
+       char       *logtext;
+       s4          logtextlen;
+       s4          dumpsize;
+       s4          i;
+       s4          pos;
 
        md = m->parseddesc;
 
@@ -1516,10 +1608,10 @@ void builtin_displaymethodstop(methodinfo *m, s8 l, double d, float f)
                strlen("-2147483647-") +        /* INT_MAX should be sufficient       */
                methodindent +
                strlen("finished: ") +
-               utf_strlen(m->class->name) +
+               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 */
@@ -1550,99 +1642,15 @@ void builtin_displaymethodstop(methodinfo *m, s8 l, double d, float f)
                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);
-
-               /* check return argument for java.lang.Class or java.lang.String */
-
-               o = (java_objectheader *) (ptrint) l;
-
-               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 *) (ptrint) l;
-
-                               len = strlen(", String = \"") + javastring_strlen(s) +
-                                       strlen("\"");
-
-                               /* realloc memory for string length */
-
-                               DMREALLOC(logtext, char, logtextlen, logtextlen + len);
-
-                               /* convert to utf8 string and strcat it to the logtext */
-
-                               u = javastring_toutf(s, false);
-
-                               strcat(logtext, ", String = \"");
-                               utf_strcat(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 *) (ptrint) l;
-
-                                       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;
-                               }
+       utf_cat(logtext, m->name);
+       utf_cat(logtext, m->descriptor);
 
-                               len = strlen(", Class = \"") + utf_strlen(u) + strlen("\"");
-
-                               /* realloc memory for string length */
-
-                               DMREALLOC(logtext, char, logtextlen, logtextlen + len);
-
-                               /* strcat to the logtext */
-
-                               strcat(logtext, ", Class = \"");
-                               utf_strcat(logtext, u);
-                               strcat(logtext, "\"");
-                       }
-               }
-               break;
-
-       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);
 
@@ -1653,154 +1661,17 @@ void builtin_displaymethodstop(methodinfo *m, s8 l, double d, float f)
 #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
-
-
-#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);
-
-               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
-
-
-#if defined(USE_THREADS)
-void builtin_monitorenter(java_objectheader *o)
-{
-#if !defined(NATIVE_THREADS)
-       int hashValue;
-
-       ++blockInts;
-
-       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.
- */
-void builtin_staticmonitorenter(classinfo *c)
-{
-       builtin_monitorenter(&c->object.header);
-}
-#endif
-
-
-#if defined(USE_THREADS)
-void builtin_monitorexit(java_objectheader *o)
-{
-#if !defined(NATIVE_THREADS)
-       int hashValue;
-
-       ++blockInts;
-
-       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);
-
-       --blockInts;
-#else
-       monitorExit((threadobject *) THREADOBJECT, o);
-#endif
-}
-#endif
+#endif /* defined(ENABLE_CYCLES_STATS) */
 
 
 /*****************************************************************************
@@ -2593,37 +2464,135 @@ float builtin_d2f(double 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
 
 
 /*
@@ -2637,4 +2606,5 @@ java_objectheader **builtin_asm_get_exceptionptrptr(void)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */