* src/vm/jit/jit.c: Moved to .cpp.
[cacao.git] / src / vm / jit / i386 / md.c
index 07328061a21feded02a0c1d7927ffed401e76587..191cddabd02cdb6d88b8b99a1b62b38dd6598c02 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/i386/md.c - machine dependent i386 functions
 
-   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
+   Copyright (C) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Christian Thalinger
-
-   Changes: Edwin Steiner
-
-   $Id: md.c 4613 2006-03-15 12:04:05Z edwin $
-
 */
 
 
 #include "config.h"
-#include "vm/types.h"
 
 #include <assert.h>
+#include <stdint.h>
+
+#include "vm/types.h"
 
 #include "vm/global.h"
+#include "vm/vm.hpp"
+
 #include "vm/jit/asmpart.h"
-#include "vm/jit/codegen-common.h"
-#include "vm/options.h" /* XXX debug */
-#include "vm/jit/disass.h" /* XXX debug */
+#include "vm/jit/jit.hpp"
 
 
 /* md_init *********************************************************************
@@ -57,78 +49,115 @@ void md_init(void)
 }
 
 
-/* md_stacktrace_get_returnaddress *********************************************
+/* md_jit_method_patch_address *************************************************
 
-   Returns the return address of the current stackframe, specified by
-   the passed stack pointer and the stack frame size.
+   Gets the patch address of the currently compiled method. The offset
+   is extracted from the load instruction(s) before the jump and added
+   to the right base address (PV or REG_METHODPTR).
+
+   INVOKESTATIC/SPECIAL:
+
+   b9 30 00 49 b7             mov    $0xb7490030,%ecx
+   ff d1                      call   *%ecx
+
+   INVOKEVIRTUAL:
+
+   8b 08                      mov    (%eax),%ecx
+   8b 91 00 00 00 00          mov    0x0(%ecx),%edx
+   ff d2                      call   *%edx
+
+   INVOKEINTERFACE:
+
+   8b 08                      mov    (%eax),%ecx
+   8b 89 00 00 00 00          mov    0x0(%ecx),%ecx
+   8b 91 00 00 00 00          mov    0x0(%ecx),%edx
+   ff d2                      call   *%edx
 
 *******************************************************************************/
 
-u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
+void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
 {
-       u1 *ra;
+       uint8_t  *pc;
+       uint16_t  opcode;
+       int32_t   disp;
+       void     *pa;                                            /* patch address */
 
-       /* on i386 the return address is above the current stack frame */
+       /* go back to the actual call instruction (2-bytes) */
 
-       ra = *((u1 **) (sp + framesize));
+       pc = ((uint8_t *) ra) - 2;
 
-       return ra;
-}
+       /* Get the opcode of the call. */
 
+       opcode = *((uint16_t *) pc);
 
-/* md_codegen_findmethod *******************************************************
+       /* check for the different calls */
 
-   On this architecture just a wrapper function to codegen_findmethod.
+       switch (opcode) {
+       case 0xd1ff:
+               /* INVOKESTATIC/SPECIAL */
 
-*******************************************************************************/
+               /* Patch address is 4-bytes before the call instruction. */
 
-u1 *md_codegen_findmethod(u1 *ra)
-{
-       u1 *pv;
+               pa = pc - 4;
+               break;
+
+       case 0xd2ff:
+               /* INVOKEVIRTUAL/INTERFACE */
+
+               /* Return NULL if no mptr was specified (used for
+                  replacement). */
 
-       /* the the start address of the function which contains this
-       address from the method table */
+               if (mptr == NULL)
+                       return NULL;
 
-       pv = codegen_findmethod(ra);
+               /* Get the displacement from the instruction (the displacement
+                  address is 4-bytes before the call instruction). */
 
-       return pv;
+               disp = *((int32_t *) (pc - 4));
+
+               /* Add the displacement to the method pointer. */
+
+               pa = ((uint8_t *) mptr) + disp;
+               break;
+
+       default:
+               vm_abort_disassemble(pc, 1, "md_jit_method_patch_address: unknown instruction %x", opcode);
+               return NULL;
+       }
+
+       return pa;
 }
 
+
 /* md_patch_replacement_point **************************************************
 
    Patch the given replacement point.
 
 *******************************************************************************/
 
-void md_patch_replacement_point(rplpoint *rp)
+#if defined(ENABLE_REPLACEMENT)
+void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
 {
-    u8 mcode;
-
-       /* XXX this is probably unsafe! */
-
-       /* save the current machine code */
-       mcode = *(u8*)rp->pc;
+       u2 mcode;
 
-       /* write spinning instruction */
-       *(u2*)(rp->pc) = 0xebfe;
-
-       /* write 5th byte */
-       rp->pc[4] = (rp->mcode >> 32);
+       if (revert) {
+               /* write saved machine code */
+               *(u2*)(pc) = *(u2*)(savedmcode);
+       }
+       else {
+               /* save the current machine code */
+               *(u2*)(savedmcode) = *(u2*)(pc);
 
-       /* write first word */
-    *(u4*)(rp->pc) = (u4) rp->mcode;
+               /* build the machine code for the patch */
+               mcode = 0x0b0f;
 
-       /* store saved mcode */
-       rp->mcode = mcode;
-       
-       {
-               u1* u1ptr = rp->pc;
-               DISASSINSTR(u1ptr);
-               fflush(stdout);
+               /* write new machine code */
+               *(u2*)(pc) = mcode;
        }
-                       
-    /* XXX if required asm_cacheflush(rp->pc,8); */
+
+    /* XXX if required asm_cacheflush(pc,8); */
 }
+#endif /* defined(ENABLE_REPLACEMENT) */
 
 /*
  * These are local overrides for various environment variables in Emacs.