athrow: add trapskeleton for it
authorBernhard Urban <lewurm@gmail.com>
Sun, 2 Sep 2012 10:12:01 +0000 (12:12 +0200)
committerBernhard Urban <lewurm@gmail.com>
Sun, 2 Sep 2012 10:12:01 +0000 (12:12 +0200)
Mate/Types.hs
Mate/X86CodeGen.hs
Mate/X86TrapHandling.hs
ffi/trap.c

index 7de8493a8523dddf9c8b6084e42e238a26d0fe22..67ac2a52b1d016cab35d6fa625287344d0eaba9e 100644 (file)
@@ -68,11 +68,13 @@ type TrapMap = M.Map NativeWord TrapCause
 
 type TrapPatcher = CPtrdiff -> CodeGen () () CPtrdiff
 type TrapPatcherEax = CPtrdiff -> CPtrdiff -> CodeGen () () CPtrdiff
+type TrapPatcherEsp = TrapPatcherEax
 
 data TrapCause
   = StaticMethod TrapPatcher -- for static calls
   | VirtualCall Bool MethodInfo (IO NativeWord) -- for invoke{interface,virtual}
   | InstanceOf TrapPatcherEax
+  | ThrowException TrapPatcherEsp
   | NewObject TrapPatcher
   | StaticField StaticFieldInfo
   | ObjectField TrapPatcher
index 23cf7a32a348c4ba726e2093fdc734507ba026c3..7d17a9d7314d0f18929f124fdca250909381f9c7 100644 (file)
@@ -224,6 +224,13 @@ emitFromBB cls method = do
             return reip
       return $ Just (trapaddr, NewObject patcher)
 
+    emit' ATHROW = do
+      trapaddr <- emitSigIllTrap 2
+      let patcher resp reip = do
+            emitSigIllTrap 2
+            return reip
+      return $ Just (trapaddr, ThrowException patcher)
+
     emit' insn = emit insn >> return Nothing
 
     emit :: J.Instruction -> CodeGen e s ()
@@ -279,8 +286,6 @@ emitFromBB cls method = do
       push eax -- push ref again
 
     emit (CHECKCAST _) = nop -- TODO(bernhard): ...
-    emit ATHROW = -- TODO(bernhard): ...
-        emit32 (0xffffffff :: Word32)
     emit I2C = do
       pop eax
       and eax (0x000000ff :: Word32)
@@ -367,6 +372,7 @@ emitFromBB cls method = do
 
     emitSigIllTrap :: Int -> CodeGen e s NativeWord
     emitSigIllTrap traplen = do
+      when (traplen < 2) (error "emitSigIllTrap: trap len too short")
       trapaddr <- getCurrentOffset
       -- 0xffff causes SIGILL
       emit8 (0xff :: Word8); emit8 (0xff :: Word8)
index 5f993a6203304533635681149baa6ed09655d574..b7b81d679e2081f8a241e3aabf25dd27fe613fec 100644 (file)
@@ -26,9 +26,9 @@ import Harpy.X86Disassembler
 foreign import ccall "register_signal"
   register_signal :: IO ()
 
-foreign export ccall mateHandler :: CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> IO CPtrdiff
-mateHandler :: CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> IO CPtrdiff
-mateHandler reip reax rebx resi = do
+foreign export ccall mateHandler :: CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> IO CPtrdiff
+mateHandler :: CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> IO CPtrdiff
+mateHandler reip reax rebx resi resp = do
   tmap <- getTrapMap
   let reipw32 = fromIntegral reip
   (deleteMe, ret_nreip) <- case M.lookup reipw32 tmap of
@@ -40,6 +40,8 @@ mateHandler reip reax rebx resi = do
         patchWithHarpy patcher reip >>= delTrue
     (Just (InstanceOf patcher))  ->
         patchWithHarpy (patcher reax) reip >>= delFalse
+    (Just (ThrowException patcher)) ->
+        patchWithHarpy (patcher resp) reip >>= delFalse
     (Just (NewObject patcher))   ->
         patchWithHarpy patcher reip >>= delTrue
     (Just (VirtualCall False mi io_offset)) ->
index c550658d5f5920d0cdc8a35f3520054ccf24163f..f6281d013ad770c7a23aebdebcaa284049dbca98 100644 (file)
@@ -20,7 +20,7 @@
 
 #include <sys/ucontext.h>
 
-ptrdiff_t mateHandler(ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t);
+ptrdiff_t mateHandler(ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t);
 
 #ifdef DBG_TRAP
 #define dprintf(args...) do { printf (args); } while (0);
@@ -43,7 +43,7 @@ void chandler(int nSignal, siginfo_t *info, void *ctx)
                        "esp 0x%08x, *esp 0x%08x, *(ebp+8) 0x%08x\n", nSignal, eip,
                        eax, ebx, esp, *(ptrdiff_t*) esp, *(ptrdiff_t *) (ebp + 8));
 
-       ptrdiff_t ret = mateHandler(eip, eax, ebx, esi);
+       ptrdiff_t ret = mateHandler(eip, eax, ebx, esi, esp);
        if (ret == -1) {
                dprintf("regdump @ EIP: 0x%08x\n", regs[REG_EIP]);
                dprintf("\tEAX: 0x%08lx EBX: 0x%08lx ECX: 0x%08lx EDX: 0x%08lx\n",