* patcher_builtin_newarray, patcher_builtin_multianewarray,
[cacao.git] / src / vm / jit / i386 / patcher.c
index d697d413b36a904118c31bf500ad33fe07424630..2831080f1ca10c3e8a9ceb4e567304087b137f59 100644 (file)
 
    Changes:
 
-   $Id: patcher.c 2676 2005-06-13 16:20:32Z twisti $
+   $Id: patcher.c 3569 2005-11-04 16:47:25Z twisti $
 
 */
 
 
-#include "vm/jit/i386/types.h"
+#include "config.h"
+#include "vm/types.h"
 
+#include "mm/memory.h"
 #include "native/native.h"
 #include "vm/builtin.h"
 #include "vm/field.h"
 #include "vm/initialize.h"
 #include "vm/options.h"
+#include "vm/resolve.h"
 #include "vm/references.h"
-#include "vm/jit/helper.h"
 #include "vm/jit/patcher.h"
 
 
@@ -78,7 +80,7 @@ bool patcher_get_putstatic(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(fi = helper_resolve_fieldinfo(uf))) {
+       if (!(fi = resolve_field_eager(uf))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -96,11 +98,12 @@ bool patcher_get_putstatic(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch the field value's address */
@@ -146,7 +149,7 @@ bool patcher_getfield(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(fi = helper_resolve_fieldinfo(uf))) {
+       if (!(fi = resolve_field_eager(uf))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -154,11 +157,12 @@ bool patcher_getfield(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch the field's offset */
@@ -209,7 +213,7 @@ bool patcher_putfield(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(fi = helper_resolve_fieldinfo(uf))) {
+       if (!(fi = resolve_field_eager(uf))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -217,11 +221,12 @@ bool patcher_putfield(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch the field's offset */
@@ -281,7 +286,7 @@ bool patcher_putfieldconst(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(fi = helper_resolve_fieldinfo(uf))) {
+       if (!(fi = resolve_field_eager(uf))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -289,11 +294,12 @@ bool patcher_putfieldconst(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch the field's offset */
@@ -353,7 +359,7 @@ bool patcher_builtin_new(u1 *sp)
 
        /* get the classinfo */
 
-       if (!(c = helper_resolve_classinfo(cr))) {
+       if (!(c = resolve_classref_eager_nonabstract(cr))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -361,7 +367,8 @@ bool patcher_builtin_new(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) (ra + 7)) = mcode;
+       *((u4 *) (ra + 7 + 0)) = (u4) mcode;
+       *((u1 *) (ra + 7 + 4)) = (u1) (mcode >> 32);
 
        /* patch the classinfo pointer */
 
@@ -369,7 +376,7 @@ bool patcher_builtin_new(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch new function address */
@@ -417,7 +424,7 @@ bool patcher_builtin_newarray(u1 *sp)
 
        /* get the classinfo */
 
-       if (!(c = helper_resolve_classinfo(cr))) {
+       if (!(c = resolve_classref_eager(cr))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -425,15 +432,16 @@ bool patcher_builtin_newarray(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) (ra + 8)) = mcode;
+       *((u4 *) (ra + 8 + 0)) = (u4) mcode;
+       *((u1 *) (ra + 8 + 4)) = (u1) (mcode >> 32);
 
-       /* patch the class' vftbl pointer */
+       /* patch the classinfo pointer */
 
-       *((ptrint *) (ra + 4)) = (ptrint) c->vftbl;
+       *((ptrint *) (ra + 4)) = (ptrint) c;
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch new function address */
@@ -485,7 +493,7 @@ bool patcher_builtin_multianewarray(u1 *sp)
 
        /* get the classinfo */
 
-       if (!(c = helper_resolve_classinfo(cr))) {
+       if (!(c = resolve_classref_eager(cr))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -493,16 +501,17 @@ bool patcher_builtin_multianewarray(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
-       /* patch the class' vftbl pointer */
+       /* patch the classinfo pointer */
 
-       *((ptrint *) (ra + 7 + 4)) = (ptrint) c->vftbl;
+       *((ptrint *) (ra + 7 + 4)) = (ptrint) c;
 
        /* patch new function address */
 
@@ -518,10 +527,10 @@ bool patcher_builtin_multianewarray(u1 *sp)
 
    Machine code:
 
-   c7 44 24 08 00 00 00 00    movl   $0x00000000,0x8(%esp)
    <patched call position>
-   b8 00 00 00 00             mov    $0x00000000,%eax
-   ff d0                      call   *%eax
+   c7 44 24 04 00 00 00 00    movl   $0x00000000,0x4(%esp)
+   ba 00 00 00 00             mov    $0x00000000,%edx
+   ff d2                      call   *%edx
 
 *******************************************************************************/
 
@@ -542,14 +551,14 @@ bool patcher_builtin_arraycheckcast(u1 *sp)
 
        /* calculate and set the new return address */
 
-       ra = ra - (8 + 5);
+       ra = ra - 5;
        *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
 
        PATCHER_MONITORENTER;
 
        /* get the classinfo */
 
-       if (!(c = helper_resolve_classinfo(cr))) {
+       if (!(c = resolve_classref_eager(cr))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -557,17 +566,18 @@ bool patcher_builtin_arraycheckcast(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) (ra + 8)) = mcode;
-
-       /* patch the class' vftbl pointer */
-
-       *((ptrint *) (ra + 4)) = (ptrint) c->vftbl;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
+       /* patch the classinfo pointer */
+
+       *((ptrint *) (ra + 4)) = (ptrint) c;
+
        /* patch new function address */
 
        *((ptrint *) (ra + 8 + 1)) = (ptrint) BUILTIN_arraycheckcast;
@@ -613,7 +623,7 @@ bool patcher_builtin_arrayinstanceof(u1 *sp)
 
        /* get the classinfo */
 
-       if (!(c = helper_resolve_classinfo(cr))) {
+       if (!(c = resolve_classref_eager(cr))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -621,15 +631,16 @@ bool patcher_builtin_arrayinstanceof(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) (ra + 8)) = mcode;
+       *((u4 *) (ra + 8 + 0)) = (u4) mcode;
+       *((u1 *) (ra + 8 + 4)) = (u1) (mcode >> 32);
 
-       /* patch the class' vftbl pointer */
+       /* patch the classinfo pointer */
 
-       *((ptrint *) (ra + 4)) = (ptrint) c->vftbl;
+       *((ptrint *) (ra + 4)) = (ptrint) c;
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch new function address */
@@ -676,7 +687,7 @@ bool patcher_invokestatic_special(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(m = helper_resolve_methodinfo(um))) {
+       if (!(m = resolve_method_eager(um))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -684,11 +695,12 @@ bool patcher_invokestatic_special(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch stubroutine */
@@ -736,7 +748,7 @@ bool patcher_invokevirtual(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(m = helper_resolve_methodinfo(um))) {
+       if (!(m = resolve_method_eager(um))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -744,11 +756,12 @@ bool patcher_invokevirtual(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch vftbl index */
@@ -798,7 +811,7 @@ bool patcher_invokeinterface(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(m = helper_resolve_methodinfo(um))) {
+       if (!(m = resolve_method_eager(um))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -806,11 +819,12 @@ bool patcher_invokeinterface(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch interfacetable index */
@@ -862,7 +876,7 @@ bool patcher_checkcast_instanceof_flags(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(c = helper_resolve_classinfo(cr))) {
+       if (!(c = resolve_classref_eager(cr))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -870,11 +884,12 @@ bool patcher_checkcast_instanceof_flags(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch class flags */
@@ -924,7 +939,7 @@ bool patcher_checkcast_instanceof_interface(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(c = helper_resolve_classinfo(cr))) {
+       if (!(c = resolve_classref_eager(cr))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -932,11 +947,12 @@ bool patcher_checkcast_instanceof_interface(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch super class index */
@@ -990,7 +1006,7 @@ bool patcher_checkcast_class(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(c = helper_resolve_classinfo(cr))) {
+       if (!(c = resolve_classref_eager(cr))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -998,11 +1014,12 @@ bool patcher_checkcast_class(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch super class' vftbl */
@@ -1020,6 +1037,12 @@ bool patcher_checkcast_class(u1 *sp)
 
    Machine code:
 
+   <patched call position>
+   b9 00 00 00 00             mov    $0x0,%ecx
+   8b 40 14                   mov    0x14(%eax),%eax
+   8b 51 18                   mov    0x18(%ecx),%edx
+   8b 49 14                   mov    0x14(%ecx),%ecx
+
 *******************************************************************************/
 
 bool patcher_instanceof_class(u1 *sp)
@@ -1046,7 +1069,7 @@ bool patcher_instanceof_class(u1 *sp)
 
        /* get the fieldinfo */
 
-       if (!(c = helper_resolve_classinfo(cr))) {
+       if (!(c = resolve_classref_eager(cr))) {
                PATCHER_MONITOREXIT;
 
                return false;
@@ -1054,11 +1077,12 @@ bool patcher_instanceof_class(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch super class' vftbl */
@@ -1073,7 +1097,11 @@ bool patcher_instanceof_class(u1 *sp)
 
 /* patcher_clinit **************************************************************
 
-   XXX
+   Is used int PUT/GETSTATIC and native stub.
+
+   Machine code:
+
+   <patched call position>
 
 *******************************************************************************/
 
@@ -1110,7 +1138,57 @@ bool patcher_clinit(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
+
+       PATCHER_MARK_PATCHED_MONITOREXIT;
+
+       return true;
+}
+
+
+/* patcher_athrow_areturn ******************************************************
+
+   Machine code:
+
+   <patched call position>
+
+*******************************************************************************/
+
+bool patcher_athrow_areturn(u1 *sp)
+{
+       u1                *ra;
+       java_objectheader *o;
+       u8                 mcode;
+       unresolved_class  *uc;
+       classinfo         *c;
+
+       /* get stuff from the stack */
+
+       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
+       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
+       mcode =                       *((u8 *)     (sp + 1 * 4));
+       uc    = (unresolved_class *)  *((ptrint *) (sp + 0 * 4));
+
+       /* calculate and set the new return address */
+
+       ra = ra - 5;
+       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
+
+       PATCHER_MONITORENTER;
+
+       /* resolve the class */
+
+       if (!resolve_class(uc, resolveEager, false, &c)) {
+               PATCHER_MONITOREXIT;
+
+               return false;
+       }
+
+       /* patch back original code */
+
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        PATCHER_MARK_PATCHED_MONITOREXIT;
 
@@ -1120,10 +1198,16 @@ bool patcher_clinit(u1 *sp)
 
 /* patcher_resolve_native ******************************************************
 
-   XXX
+   Is used in native stub.
+
+   Machine code:
+
+   <patched call position>
+   c7 44 24 04 28 90 01 40    movl   $0x40019028,0x4(%esp)
 
 *******************************************************************************/
 
+#if !defined(ENABLE_STATICVM)
 bool patcher_resolve_native(u1 *sp)
 {
        u1                *ra;
@@ -1156,21 +1240,23 @@ bool patcher_resolve_native(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (showdisassemble)
+       if (opt_showdisassemble)
                ra = ra + 5;
 
        /* patch native function pointer */
 
-       *((ptrint *) (ra + 1)) = (ptrint) f;
+       *((ptrint *) (ra + 4)) = (ptrint) f;
 
        PATCHER_MARK_PATCHED_MONITOREXIT;
 
        return true;
 }
+#endif /* !defined(ENABLE_STATICVM) */
 
 
 /*