* Removed all Id tags.
[cacao.git] / src / vm / jit / mips / md.c
index 1af596c33e6346c1967fccaf69bd1abc770fbeed..d675d64ceddc7b5fc5c28c93211d639038e42855 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/jit/mips/md.c - machine dependent MIPS functions
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   Copyright (C) 1996-2005, 2006, 2007 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
    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 5929 2006-11-06 17:13:40Z twisti $
-
 */
 
 
 #include "vm/types.h"
 
 #include "toolbox/logging.h"
+
 #include "vm/global.h"
 #include "vm/vm.h"
+
+#include "vm/jit/codegen-common.h" /* REMOVEME: only for codegendata */
 #include "vm/jit/stacktrace.h"
 
 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
-#include "vm/options.h" /* XXX debug */
+#include "vmcore/options.h" /* XXX debug */
 #include "vm/jit/disass.h" /* XXX debug */
 #endif
 
 
-/* md_codegen_patch_branch *****************************************************
-
-   Back-patches a branch instruction.
-
-*******************************************************************************/
-
-void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
-{
-       s4 *mcodeptr;
-       s4  mcode;
-       s4  disp;                           /* branch displacement                */
-       s4  lo;
-       s4  hi;
-
-       /* calculate the patch position */
-
-       mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
-
-       /* get the instruction before the exception point */
-
-       mcode = mcodeptr[-1];
-
-       /* check for: ori t9,t9,0 */
-
-       if ((mcode >> 16) == 0x3739) {
-               /* Calculate the branch displacement.  For jumps we need a
-                  displacement relative to PV. */
-
-               disp = targetmpc;
-
-        lo = (short) disp;
-        hi = (short) ((disp - lo) >> 16);
-
-               /* patch the two instructions before the mcodeptr */
-
-               mcodeptr[-2] |= (hi & 0x0000ffff);
-               mcodeptr[-1] |= (lo & 0x0000ffff);
-       }
-       else {
-               /* Calculate the branch displacement.  For branches we need a
-                  displacement relative and shifted to the branch PC. */
-
-               disp = (targetmpc - branchmpc) >> 2;
-
-               /* On the MIPS we can only branch signed 16-bit instruction words
-                  (signed 18-bit = 32KB = +/- 16KB). Check this! */
-
-               if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff))
-                       vm_abort("jump displacement is out of range: %d > +/-%d", disp, 0x00007fff);
-
-               /* patch the branch instruction before the mcodeptr */
-
-               mcodeptr[-1] |= (disp & 0x0000ffff);
-       }
-}
-
-
 /* md_stacktrace_get_returnaddress *********************************************
 
    Returns the return address of the current stackframe, specified by
@@ -190,6 +128,8 @@ u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
                assert((mcode >> 16) != 0x6739);
 
                offset += (s2) (mcode & 0x0000ffff);
+
+               pa = NULL;
        }
        else {
                /* get first instruction (ld) */
@@ -209,6 +149,11 @@ u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
 #endif
                        /* in this case we use the passed method pointer */
 
+                       /* return NULL if no mptr was specified (used for replacement) */
+
+                       if (mptr == NULL)
+                               return NULL;
+
                        pa = mptr + offset;
                }
                else {
@@ -336,18 +281,30 @@ void md_dcacheflush(u1 *addr, s4 nbytes)
 
 *******************************************************************************/
 
-void md_patch_replacement_point(rplpoint *rp)
+#if defined(ENABLE_REPLACEMENT)
+void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp,
+                                                               u1 *savedmcode)
 {
-    u8 mcode;
-
-       /* save the current machine code */
-       mcode = *(u8*)rp->pc;
+       union {
+               u8 both;
+               u4 words[2];
+       } mcode;
+
+       if (index < 0) {
+               /* restore the patched-over instruction */
+               *(u8*)(rp->pc) = *(u8*)(savedmcode);
+       }
+       else {
+               /* save the current machine code */
+               *(u8*)(savedmcode) = *(u8*)(rp->pc);
 
-       /* write the new machine code */
-    *(u8*)(rp->pc) = rp->mcode;
+               /* build the machine code for the patch */
+               assert(0); /* XXX build trap instruction below */
+               mcode.both = 0;
 
-       /* store saved mcode */
-       rp->mcode = mcode;
+               /* write the new machine code */
+               *(u8*)(rp->pc) = mcode.both;
+       }
 
 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
        {
@@ -361,6 +318,7 @@ void md_patch_replacement_point(rplpoint *rp)
        /* flush instruction cache */
     md_icacheflush(rp->pc,2*4);
 }
+#endif /* defined(ENABLE_REPLACEMENT) */
 
 /*
  * These are local overrides for various environment variables in Emacs.