Merged revisions 7501-7598 via svnmerge from
[cacao.git] / src / vm / jit / powerpc64 / md.c
index a71fd6e559370fb7866a01e2b553cfb078a51c61..6da5d7ce18373fa8f2e6ea106bf981d4255b811a 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/jit/powerpc64/md.c - machine dependent PowerPC 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 5248 2006-08-17 17:51:40Z tbfg $
+   $Id: md.c 7596 2007-03-28 21:05:53Z twisti $
 
 */
 
+
 #include "config.h"
 
 #include <assert.h>
 
 #include "md-abi.h"
 
+#include "vm/jit/powerpc64/codegen.h"
+
 #include "vm/global.h"
+
 #include "vm/jit/asmpart.h"
+#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
 
@@ -79,6 +78,47 @@ u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
        return ra;
 }
 
+/* md_codegen_patch_branch *****************************************************
+
+   Back-patches a branch instruction.
+   Changes the dispacment of the jump instruction.
+
+*******************************************************************************/
+
+void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
+{
+       s4 *mcodeptr;
+       s4  disp;                           /* branch displacement                */
+       s4  mcode;              
+
+       /* calculate the patch position */
+       mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
+
+       /* Calculate the branch displacement. */
+       disp = targetmpc - branchmpc + 4;
+       mcode = mcodeptr[-1];
+
+       if ((mcode & 0xfc000000) == 0x40000000) {
+               /* conditional jump bcx */
+               if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff))
+                       vm_abort("jump displacement is out of range: %d > +/-%d", disp, 0x00007fff);
+
+               mcode &= 0xffff0000;
+               mcode |= (((disp)& M_BCMASK));
+       } else if ((mcode & 0xfc000000) == 0x48000000) {
+               /* unconditional jump bx */
+               if ((disp < (s4) 0xfc000000) || (disp > (s4) 0x03ffffff))
+                       vm_abort("jump displacement is out of range: %d > +/-%d", disp, 0x0cffffff);
+               mcode &= 0xfc000000;
+               mcode |= (((disp)& M_BMASK));
+       } else {
+               vm_abort("md_codegen_patch_branch, patching unsupported branch: %xd", mcode);
+       }
+
+       mcodeptr[-1] = mcode;
+}
+
+
 
 /* md_get_method_patch_address *************************************************
 
@@ -157,6 +197,11 @@ u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
                } else if ((mcode >> 16) == 0xe9cc) { 
                        /* 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 {
@@ -175,12 +220,12 @@ u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
    Machine code:
 
    7d6802a6    mflr    r11
-   39cbffe0    addi    r14r11,-32
+   39cbffe0    addi    r14,r11,-32
 
    or
 
    7d6802a6    mflr    r11
-   3dcbffff    addis   r14r11,-1
+   3dcbffff    addis   r14,r11,-1
    39ce68b0    addi    r14,r13,26800
 
 *******************************************************************************/
@@ -273,18 +318,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;
+       s4 disp;
+       u4 mcode;
 
-       /* save the current machine code */
-       mcode = *(u4*)rp->pc;
+       if (index < 0) {
+               /* restore the patched-over instruction */
+               *(u4*)(rp->pc) = *(u4*)(savedmcode);
+       }
+       else {
+               /* save the current machine code */
+               *(u4*)(savedmcode) = *(u4*)(rp->pc);
 
-       /* write the new machine code */
-    *(u4*)(rp->pc) = (u4) rp->mcode;
+               /* build the machine code for the patch */
+               disp = ((u4*)code->replacementstubs - (u4*)rp->pc)
+                          + index * REPLACEMENT_STUB_SIZE
+                          - 1;
 
-       /* store saved mcode */
-       rp->mcode = mcode;
+        mcode = (18 << 26) | ((((disp) * 4) + 4) & M_BMASK);
+
+               /* write the new machine code */
+               *(u4*)(rp->pc) = (u4) mcode;
+       }
        
 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
        {
@@ -297,6 +354,7 @@ void md_patch_replacement_point(rplpoint *rp)
        /* flush instruction cache */
     md_icacheflush(rp->pc,4);
 }
+#endif /* defined(ENABLE_REPLACEMENT) */
 
 /*
  * These are local overrides for various environment variables in Emacs.