Merged revisions 7501-7598 via svnmerge from
[cacao.git] / src / vm / jit / alpha / md.c
index 0860d115e78677f6958329c2ac687ae5bb0f4c29..1aef1bad18a3691f664747805fd5a63ce3906d91 100644 (file)
@@ -1,9 +1,9 @@
 /* src/vm/jit/alpha/md.c - machine dependent Alpha functions
 
-   Copyright (C) 1996-2005 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 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
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
-
-   Authors: Andreas Krall
-            Reinhard Grafl
-
-   Changes: Joseph Wenninger
-            Christian Thalinger
-
-   $Id: md.c 3772 2005-11-23 21:36:35Z twisti $
+   $Id: md.c 7596 2007-03-28 21:05:53Z twisti $
 
 */
 
 
+#include "config.h"
+
 #include <assert.h>
 #include <ucontext.h>
 
-#include "config.h"
+#if defined(__LINUX__)
+# include <asm/fpu.h>
+
+extern unsigned long ieee_get_fp_control();
+extern void ieee_set_fp_control(unsigned long fp_control);
+#endif
+
 #include "vm/types.h"
 
 #include "vm/jit/alpha/md-abi.h"
 
 #include "vm/exceptions.h"
 #include "vm/stringlocal.h"
+
 #include "vm/jit/asmpart.h"
 #include "vm/jit/stacktrace.h"
 
+#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
+#include "vmcore/options.h" /* XXX debug */
+#include "vm/jit/disass.h" /* XXX debug */
+#endif
+
+
+/* global variables ***********************************************************/
+
+bool has_ext_instr_set = false;             /* has instruction set extensions */
+
 
 /* md_init *********************************************************************
 
 
 void md_init(void)
 {
-       /* XXX TWISTI: do we really need this? fptest's seem to work fine */
+       /* check for extended instruction set */
 
-#if defined(__LINUX__)
-/* Linux on Digital Alpha needs an initialisation of the ieee floating point
-       control for IEEE compliant arithmetic (option -mieee of GCC). Under
-       Digital Unix this is done automatically.
-*/
-
-#include <asm/fpu.h>
+       has_ext_instr_set = !asm_md_init();
 
-extern unsigned long ieee_get_fp_control();
-extern void ieee_set_fp_control(unsigned long fp_control);
+#if defined(__LINUX__)
+       /* Linux on Digital Alpha needs an initialisation of the ieee
+          floating point control for IEEE compliant arithmetic (option
+          -mieee of GCC). Under Digital Unix this is done
+          automatically. */
 
        /* initialize floating point control */
 
@@ -78,8 +86,6 @@ extern void ieee_set_fp_control(unsigned long fp_control);
 /*                                             & ~IEEE_TRAP_ENABLE_UNF   we dont want underflow */
                                                & ~IEEE_TRAP_ENABLE_OVF);
 #endif
-
-       /* nothing to do */
 }
 
 
@@ -102,7 +108,101 @@ u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
 }
 
 
-/* md_codegen_findmethod *******************************************************
+/* md_get_method_patch_address *************************************************
+
+   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:
+
+   a77bffb8    ldq     pv,-72(pv)
+   6b5b4000    jsr     (pv)
+
+   INVOKEVIRTUAL:
+
+   a7900000    ldq     at,0(a0)
+   a77c0000    ldq     pv,0(at)
+   6b5b4000    jsr     (pv)
+
+   INVOKEINTERFACE:
+
+   a7900000    ldq     at,0(a0)
+   a79cff98    ldq     at,-104(at)
+   a77c0018    ldq     pv,24(at)
+   6b5b4000    jsr     (pv)
+
+*******************************************************************************/
+
+u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
+{
+       u4  mcode;
+       s4  offset;
+       u1 *pa;                             /* patch address                      */
+
+       /* go back to the actual load instruction (2 instructions on Alpha) */
+
+       ra = ra - 2 * 4;
+
+       /* get first instruction word on current PC */
+
+       mcode = *((u4 *) ra);
+
+       /* check if we have 2 instructions (lui) */
+
+       if ((mcode >> 16) == 0x3c19) {
+               /* XXX write a regression for this */
+               assert(0);
+
+               /* get displacement of first instruction (lui) */
+
+               offset = (s4) (mcode << 16);
+
+               /* get displacement of second instruction (daddiu) */
+
+               mcode = *((u4 *) (ra + 1 * 4));
+
+               assert((mcode >> 16) == 0x6739);
+
+               offset += (s2) (mcode & 0x0000ffff);
+
+       } else {
+               /* get first instruction (ldq) */
+
+               mcode = *((u4 *) ra);
+
+               /* get the offset from the instruction */
+
+               offset = (s2) (mcode & 0x0000ffff);
+
+               /* check for call with REG_METHODPTR: ldq pv,0(at) */
+
+               if ((mcode >> 16) == 0xa77c) {
+                       /* 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 {
+                       /* in the normal case we check for a `ldq pv,-72(pv)' instruction */
+
+                       assert((mcode >> 16) == 0xa77b);
+
+                       /* and get the final data segment address */
+
+                       pa = sfi->pv + offset;
+               }
+       }
+
+       return pa;
+}
+
+
+/* md_codegen_get_pv_from_pc ***************************************************
 
    Machine code:
 
@@ -112,7 +212,7 @@ u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
 
 *******************************************************************************/
 
-u1 *md_codegen_findmethod(u1 *ra)
+u1 *md_codegen_get_pv_from_pc(u1 *ra)
 {
        u1 *pv;
        u4  mcode;
@@ -136,21 +236,15 @@ u1 *md_codegen_findmethod(u1 *ra)
 
                mcode = *((u4 *) (ra + 1 * 4));
 
-               if ((mcode >> 16) != 0x237b) {
-                       log_text("No `lda pv,x(pv)' instruction found on return address!");
-                       assert(0);
-               }
+               assert((mcode >> 16) == 0x237b);
 
                offset = (s2) (mcode & 0x0000ffff);
                pv += offset;
-
-       else {
+       }
+       else {
                /* get displacement of first instruction (lda) */
 
-               if ((mcode >> 16) != 0x237a) {
-                       log_text("No `lda pv,x(ra)' instruction found on return address!");
-                       assert(0);
-               }
+               assert((mcode >> 16) == 0x237a);
 
                offset = (s2) (mcode & 0x0000ffff);
                pv += offset;
@@ -160,6 +254,77 @@ u1 *md_codegen_findmethod(u1 *ra)
 }
 
 
+/* md_cacheflush ***************************************************************
+
+   Calls the system's function to flush the instruction and data
+   cache.
+
+*******************************************************************************/
+
+void md_cacheflush(u1 *addr, s4 nbytes)
+{
+       asm_cacheflush(addr, nbytes);
+}
+
+
+/* md_icacheflush **************************************************************
+
+   Calls the system's function to flush the instruction cache.
+
+*******************************************************************************/
+
+void md_icacheflush(u1 *addr, s4 nbytes)
+{
+       asm_cacheflush(addr, nbytes);
+}
+
+
+/* md_patch_replacement_point **************************************************
+
+   Patch the given replacement point.
+
+*******************************************************************************/
+
+#if defined(ENABLE_REPLACEMENT)
+void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
+{
+       s4 disp;
+       u4 mcode;
+
+       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;
+
+               /* BR */
+        mcode = (((s4) (0x30)) << 26) | ((REG_ZERO) << 21) | ((disp) & 0x1fffff);
+
+               /* write the new machine code */
+               *(u4*)(rp->pc) = mcode;
+       }
+       
+#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
+       {
+               u1* u1ptr = rp->pc;
+               DISASSINSTR(u1ptr);
+               fflush(stdout);
+       }
+#endif
+                       
+       /* flush instruction cache */
+    md_icacheflush(rp->pc,4);
+}
+#endif /* defined(ENABLE_REPLACEMENT) */
+
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where
@@ -171,4 +336,5 @@ u1 *md_codegen_findmethod(u1 *ra)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */