* src/vm/jit/powerpc/arch.h: We have leaf-method optimization in place.
[cacao.git] / src / vm / jit / powerpc / md.c
index 3595f407a4c2688c48b44d5e280a53e974c7329f..ee362634644c1892e6b39e200a1c569b3dc79b7d 100644 (file)
@@ -2,6 +2,7 @@
 
    Copyright (C) 1996-2005, 2006, 2007, 2008
    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+   Copyright (C) 2009 Theobroma Systems Ltd.
 
    This file is part of CACAO.
 
@@ -38,6 +39,7 @@
 #include "vm/vm.hpp"
 
 #include "vm/jit/jit.hpp"
+#include "vm/jit/trap.hpp"
 
 
 /* md_init *********************************************************************
@@ -153,6 +155,72 @@ void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
 }
 
 
+/**
+ * Decode the trap instruction at the given PC.
+ *
+ * @param trp information about trap to be filled
+ * @param sig signal number
+ * @param xpc exception PC
+ * @param es execution state of the machine
+ * @return true if trap was decoded successfully, false otherwise.
+ */
+bool md_trap_decode(trapinfo_t* trp, int sig, void* xpc, executionstate_t* es)
+{
+       // Get the exception-throwing instruction.
+       uint32_t mcode = *((uint32_t*) xpc);
+
+       switch (sig) {
+       case TRAP_SIGILL:
+               // Check for valid trap instruction.
+               if (patcher_is_valid_trap_instruction_at(xpc)) {
+                       trp->type  = TRAP_PATCHER;
+                       trp->value = 0;
+                       return true;
+               }
+               return false;
+
+       case TRAP_SIGTRAP:
+       {
+               int s1 = M_OP3_GET_A(mcode);
+
+               // For now we only handle ArrayIndexOutOfBoundsException.
+               trp->type  = TRAP_ArrayIndexOutOfBoundsException;
+               trp->value = es->intregs[s1];
+               return true;
+       }
+
+       case TRAP_SIGSEGV:
+       {
+               int       s1   = M_INSTR_OP2_IMM_A(mcode);
+               uintptr_t addr = es->intregs[s1];
+
+               // Check for special-load.
+               if (s1 == REG_ZERO) {
+                       int16_t disp = M_INSTR_OP2_IMM_I(mcode);
+                       int     d    = M_INSTR_OP2_IMM_D(mcode);
+
+                       // We use the exception type as load displacement.
+                       trp->type  = disp;
+                       trp->value = es->intregs[d];
+                       return true;
+               }
+
+               // Check for implicit NullPointerException.
+               if (addr == 0) {
+                       trp->type  = TRAP_NullPointerException;
+                       trp->value = 0;
+                       return true;
+               }
+
+               return false;
+       }
+
+       default:
+               return false;
+       }
+}
+
+
 /* md_patch_replacement_point **************************************************
 
    Patch the given replacement point.
@@ -172,8 +240,8 @@ void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
                /* save the current machine code */
                *(u4*)(savedmcode) = *(u4*)(pc);
 
-               /* build the machine code for the patch */
-               assert(0); /* XXX build trap instruction below */
+               // Build the machine code for the patch. On PowerPC we use
+               // an illegal instruction which really is just 0, believe me!
                mcode = 0;
 
                /* write the new machine code */