Merged revisions 7501-7598 via svnmerge from
[cacao.git] / src / vm / jit / powerpc64 / md.c
index e83e58819af82a75b05d3958a3797b3551be49b6..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 5162 2006-07-19 13:07:00Z 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 *************************************************
 
@@ -88,24 +128,25 @@ u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
 
    INVOKESTATIC/SPECIAL:
 
-   81adffd4    lwz     r13,-44(r13)
-   7da903a6    mtctr   r13
+   e9ceffb8    ld      r14,-72(r14)    
+   7dc903a6    mtctr   r14             
    4e800421    bctrl
 
    INVOKEVIRTUAL:
 
-   81830000    lwz     r12,0(r3)
-   81ac0000    lwz     r13,0(r12)
-   7da903a6    mtctr   r13
-   4e800421    bctrl
+FIXME   81830000    lwz     r12,0(r3)
+   e9cc0000     ld      r14,0(r12)
+   7dc903a6     mtctr   r14
+   4e800421     bctrl
+
 
    INVOKEINTERFACE:
 
-   81830000    lwz     r12,0(r3)
-   818c0000    lwz     r12,0(r12)
-   81ac0000    lwz     r13,0(r12)
-   7da903a6    mtctr   r13
-   4e800421    bctrl
+FIXME   81830000    lwz     r12,0(r3)
+FIXME   818c0000    lwz     r12,0(r12)
+FIXME   81ac0000    lwz     r13,0(r12)
+  7dc903a6    mtctr   r14
+  4e800421    bctrl
 
 *******************************************************************************/
 
@@ -148,14 +189,19 @@ u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
 
                /* check for load from PV */
 
-               if ((mcode >> 16) == 0x81ad) {
+               if ((mcode >> 16) == 0xe9ce) {  
                        /* get the final data segment address */
 
                        pa = sfi->pv + offset;
 
-               } else if ((mcode >> 16) == 0x81ac) {
+               } 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 {
@@ -169,22 +215,22 @@ u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
 }
 
 
-/* md_codegen_findmethod *******************************************************
+/* md_codegen_get_pv_from_pc ***************************************************
 
    Machine code:
 
    7d6802a6    mflr    r11
-   39abffe0    addi    r13,r11,-32
+   39cbffe0    addi    r14,r11,-32
 
    or
 
    7d6802a6    mflr    r11
-   3dabffff    addis   r13,r11,-1
-   39ad68b0    addi    r13,r13,26800
+   3dcbffff    addis   r14,r11,-1
+   39ce68b0    addi    r14,r13,26800
 
 *******************************************************************************/
 
-u1 *md_codegen_findmethod(u1 *ra)
+u1 *md_codegen_get_pv_from_pc(u1 *ra)
 {
        u1 *pv;
        u4  mcode;
@@ -196,7 +242,7 @@ u1 *md_codegen_findmethod(u1 *ra)
 
        /* check if we have 2 instructions (addis, addi) */
 
-       if ((mcode >> 16) == 0x3dab) {
+       if ((mcode >> 16) == 0x3dcb) {
                /* get displacement of first instruction (addis) */
 
                offset = (s4) (mcode << 16);
@@ -207,14 +253,14 @@ u1 *md_codegen_findmethod(u1 *ra)
 
                /* check for addi instruction */
 
-               assert((mcode >> 16) == 0x39ad);
+               assert((mcode >> 16) == 0x39ce);
 
                offset += (s2) (mcode & 0x0000ffff);
 
        } else {
                /* check for addi instruction */
 
-               assert((mcode >> 16) == 0x39ab);
+               assert((mcode >> 16) == 0x39cb);
 
                /* get offset of first instruction (addi) */
 
@@ -272,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);
+
+               /* build the machine code for the patch */
+               disp = ((u4*)code->replacementstubs - (u4*)rp->pc)
+                          + index * REPLACEMENT_STUB_SIZE
+                          - 1;
 
-       /* write the new machine code */
-    *(u4*)(rp->pc) = (u4) rp->mcode;
+        mcode = (18 << 26) | ((((disp) * 4) + 4) & M_BMASK);
 
-       /* store saved mcode */
-       rp->mcode = mcode;
+               /* write the new machine code */
+               *(u4*)(rp->pc) = (u4) mcode;
+       }
        
 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
        {
@@ -296,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.