* src/vm/jit/powerpc/linux/md-os.c: Simplified signal handlers.
[cacao.git] / src / vm / jit / powerpc / md.c
index 80257ab60cc5e958d33c29123feec1cf6c70ad0d..2c9333b359d38a2a5fff55cdf66c43f18ef17603 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.
 
@@ -37,7 +38,8 @@
 #include "vm/global.h"
 #include "vm/vm.hpp"
 
-#include "vm/jit/jit.h"
+#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.