* src/vm/jit/jit.c: Moved to .cpp.
[cacao.git] / src / vm / jit / alpha / md.c
index cdd5fea5ba1259bd83cf68eb6fc0498af450d865..9ad99b7474b6f982d1c66ea557be178a04f453a7 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/alpha/md.c - machine dependent Alpha 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: Andreas Krall
-            Reinhard Grafl
-
-   Changes: Joseph Wenninger
-            Christian Thalinger
-
-   $Id: md.c 4392 2006-01-31 15:35:22Z twisti $
-
 */
 
 
 #include "config.h"
 
-#include <assert.h>
+#include <stdint.h>
 #include <ucontext.h>
 
 #if defined(__LINUX__)
@@ -47,14 +35,12 @@ 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/jit/alpha/codegen.h"
+#include "vm/jit/alpha/md.h"
 
-#include "vm/exceptions.h"
-#include "vm/stringlocal.h"
 #include "vm/jit/asmpart.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/jit.hpp"
+#include "vm/jit/trap.h"
 
 
 /* global variables ***********************************************************/
@@ -70,6 +56,10 @@ bool has_ext_instr_set = false;             /* has instruction set extensions */
 
 void md_init(void)
 {
+#if defined(__LINUX__)
+       unsigned long int fpcw;
+#endif
+
        /* check for extended instruction set */
 
        has_ext_instr_set = !asm_md_init();
@@ -82,90 +72,153 @@ void md_init(void)
 
        /* initialize floating point control */
 
-       ieee_set_fp_control(ieee_get_fp_control()
-                                               & ~IEEE_TRAP_ENABLE_INV
-                                               & ~IEEE_TRAP_ENABLE_DZE
-/*                                             & ~IEEE_TRAP_ENABLE_UNF   we dont want underflow */
-                                               & ~IEEE_TRAP_ENABLE_OVF);
-#endif
-}
+       fpcw = ieee_get_fp_control();
 
+       fpcw = fpcw
+               & ~IEEE_TRAP_ENABLE_INV
+               & ~IEEE_TRAP_ENABLE_DZE
+               /* We dont want underflow. */
+/*             & ~IEEE_TRAP_ENABLE_UNF */
+               & ~IEEE_TRAP_ENABLE_OVF;
 
-/* md_stacktrace_get_returnaddress *********************************************
+/*     fpcw = fpcw */
+/*             | IEEE_TRAP_ENABLE_INV */
+/*             | IEEE_TRAP_ENABLE_DZE */
+/*             | IEEE_TRAP_ENABLE_OVF */
+/*             | IEEE_TRAP_ENABLE_UNF */
+/*             | IEEE_TRAP_ENABLE_INE */
+/*             | IEEE_TRAP_ENABLE_DNO; */
 
-   Returns the return address of the current stackframe, specified by
-   the passed stack pointer and the stack frame size.
+       ieee_set_fp_control(fpcw);
+#endif
+}
 
-*******************************************************************************/
 
-u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
-{
-       u1 *ra;
+/* md_jit_method_patch_address *************************************************
 
-       /* on Alpha the return address is located on the top of the stackframe */
+   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).
 
-       ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P));
+   INVOKESTATIC/SPECIAL:
 
-       return ra;
-}
+   a77bffb8    ldq     pv,-72(pv)
+   6b5b4000    jsr     (pv)
 
+   INVOKEVIRTUAL:
 
-/* md_codegen_findmethod *******************************************************
+   a7900000    ldq     at,0(a0)
+   a77c0000    ldq     pv,0(at)
+   6b5b4000    jsr     (pv)
 
-   Machine code:
+   INVOKEINTERFACE:
 
+   a7900000    ldq     at,0(a0)
+   a79cff98    ldq     at,-104(at)
+   a77c0018    ldq     pv,24(at)
    6b5b4000    jsr     (pv)
-   277afffe    ldah    pv,-2(ra)
-   237ba61c    lda     pv,-23012(pv)
 
 *******************************************************************************/
 
-u1 *md_codegen_findmethod(u1 *ra)
+void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
 {
-       u1 *pv;
-       u4  mcode;
-       s4  offset;
+       uint32_t *pc;
+       uint32_t  mcode;
+       int       opcode;
+       int       base;
+       int32_t   disp;
+       void     *pa;                       /* patch address                      */
 
-       pv = ra;
+       /* Go back to the load instruction (2 instructions). */
 
-       /* get first instruction word after jump */
+       pc = ((uint32_t *) ra) - 2;
 
-       mcode = *((u4 *) ra);
+       /* Get first instruction word. */
 
-       /* check if we have 2 instructions (ldah, lda) */
+       mcode = pc[0];
 
-       if ((mcode >> 16) == 0x277a) {
-               /* get displacement of first instruction (ldah) */
+       /* Get opcode, base register and displacement. */
 
-               offset = (s4) (mcode << 16);
-               pv += offset;
+       opcode = M_MEM_GET_Opcode(mcode);
+       base   = M_MEM_GET_Rb(mcode);
+       disp   = M_MEM_GET_Memory_disp(mcode);
 
-               /* get displacement of second instruction (lda) */
+       /* Check for short or long load (2 instructions). */
 
-               mcode = *((u4 *) (ra + 1 * 4));
+       switch (opcode) {
+       case 0x29: /* LDQ: TODO use define */
+               switch (base) {
+               case REG_PV:
+                       /* Calculate the data segment address. */
 
-               if ((mcode >> 16) != 0x237b) {
-                       log_text("No `lda pv,x(pv)' instruction found on return address!");
-                       assert(0);
-               }
+                       pa = ((uint8_t *) pv) + disp;
+                       break;
 
-               offset = (s2) (mcode & 0x0000ffff);
-               pv += offset;
+               case REG_METHODPTR:
+                       /* Return NULL if no mptr was specified (used for
+                          replacement). */
 
-       } else {
-               /* get displacement of first instruction (lda) */
+                       if (mptr == NULL)
+                               return NULL;
 
-               if ((mcode >> 16) != 0x237a) {
-                       log_text("No `lda pv,x(ra)' instruction found on return address!");
-                       assert(0);
+                       /* Calculate the address in the vftbl. */
+
+                       pa = ((uint8_t *) mptr) + disp;
+                       break;
+
+               default:
+                       vm_abort_disassemble(pc, 2, "md_jit_method_patch_address: unknown instruction %x", mcode);
+                       return NULL;
                }
+               break;
+
+       case 0x09: /* LDAH: TODO use define */
+               /* XXX write a regression for this */
+
+               vm_abort("md_jit_method_patch_address: IMPLEMENT ME!");
+
+               pa = NULL;
+               break;
+
+       default:
+               vm_abort_disassemble(pc, 2, "md_jit_method_patch_address: unknown instruction %x", mcode);
+               return NULL;
+       }
+
+       return pa;
+}
 
-               offset = (s2) (mcode & 0x0000ffff);
-               pv += offset;
+
+/* md_patch_replacement_point **************************************************
+
+   Patch the given replacement point.
+
+*******************************************************************************/
+
+#if defined(ENABLE_REPLACEMENT)
+void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
+{
+       u4 mcode;
+
+       if (revert) {
+               /* restore the patched-over instruction */
+               *(u4*)(pc) = *(u4*)(savedmcode);
        }
+       else {
+               /* save the current machine code */
+               *(u4*)(savedmcode) = *(u4*)(pc);
 
-       return pv;
+               /* build the machine code for the patch */
+               mcode = (0xa41f0000 | (TRAP_PATCHER));
+
+               /* write the new machine code */
+               *(u4*)(pc) = mcode;
+       }
+       
+       /* flush instruction cache */
+    md_icacheflush(pc,4);
 }
+#endif /* defined(ENABLE_REPLACEMENT) */
 
 
 /*
@@ -179,4 +232,5 @@ u1 *md_codegen_findmethod(u1 *ra)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */