codegen: make use of Functor instance
[mate.git] / Mate / X86CodeGen.hs
index 23cf7a32a348c4ba726e2093fdc734507ba026c3..af3e98d282a31e37c35e40898c70bfa487eac400 100644 (file)
@@ -63,7 +63,7 @@ emitFromBB cls method = do
   hmap = rawMapBB method
 
   getLabel :: BlockID -> [(BlockID, Label)] -> Label
-  getLabel _ [] = error "label not found!"
+  getLabel bid [] = error $ "label " ++ show bid ++ " not found"
   getLabel i ((x,l):xs) = if i==x then l else getLabel i xs
 
   efBB :: (BlockID, BasicBlock) -> TrapMap -> BBStarts -> [(BlockID, Label)] -> CodeGen e s (TrapMap, BBStarts)
@@ -99,10 +99,9 @@ emitFromBB cls method = do
 
     getCurrentOffset :: CodeGen e s Word32
     getCurrentOffset = do
-      ep <- getEntryPoint
-      let w32_ep = (fromIntegral $ ptrToIntPtr ep) :: Word32
-      offset <- getCodeOffset
-      return $ w32_ep + fromIntegral offset
+      ep <- (fromIntegral . ptrToIntPtr) <$> getEntryPoint
+      offset <- fromIntegral <$> getCodeOffset
+      return $ ep + offset
 
     emitInvoke :: Word16 -> Bool -> CodeGen e s (Maybe (Word32, TrapCause))
     emitInvoke cpidx hasThis = do
@@ -224,6 +223,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 +285,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 +371,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)