codegen: extra handling if calldisplacement doesn't fit in 8bit
[mate.git] / Mate / X86CodeGen.hs
index 893a397e8e084f897d83f924eaf7be333a5a0f6d..c45f9083858c98ce9484af1ecb7b646faa03b55e 100644 (file)
@@ -4,6 +4,7 @@
 #include "debug.h"
 module Mate.X86CodeGen where
 
+import Prelude hiding (and, div)
 import Data.Binary
 import Data.BinaryState
 import Data.Int
@@ -28,6 +29,9 @@ import Mate.Types
 import Mate.Utilities
 import Mate.ClassPool
 import Mate.Strings
+#ifdef DEBUG
+import Text.Printf
+#endif
 
 
 foreign import ccall "dynamic"
@@ -36,10 +40,6 @@ foreign import ccall "dynamic"
 foreign import ccall "getMallocObjectAddr"
   getMallocObjectAddr :: CUInt
 
-foreign import ccall "register_signal"
-  register_signal :: IO ()
-
-
 type EntryPoint = Ptr Word8
 type EntryPointOffset = Int
 type PatchInfo = (BlockID, EntryPointOffset)
@@ -49,8 +49,8 @@ type BBStarts = M.Map BlockID Int
 type CompileInfo = (EntryPoint, BBStarts, Int, TrapMap)
 
 
-emitFromBB :: B.ByteString -> Class Resolved -> MapBB -> CodeGen e s (CompileInfo, [Instruction])
-emitFromBB method cls hmap =  do
+emitFromBB :: B.ByteString -> MethodSignature -> Class Direct -> MapBB -> CodeGen e s (CompileInfo, [Instruction])
+emitFromBB method sig cls hmap =  do
         llmap <- sequence [newNamedLabel ("bb_" ++ show x) | (x,_) <- M.toList hmap]
         let lmap = zip (Prelude.fst $ unzip $ M.toList hmap) llmap
         ep <- getEntryPoint
@@ -110,7 +110,8 @@ emitFromBB method cls hmap =  do
         when (argcnt > 0) (add esp argcnt)
         -- push result on stack if method has a return value
         when (methodHaveReturnValue cls cpidx) (push eax)
-        return $ Just (calladdr, MI l)
+        -- +2 is for correcting eip in trap context
+        return $ Just (calladdr + 2, MI l)
 
     emit' :: J.Instruction -> CodeGen e s (Maybe (Word32, TrapInfo))
     emit' (INVOKESPECIAL cpidx) = emitInvoke cpidx True
@@ -138,7 +139,8 @@ emitFromBB method cls hmap =  do
         -- note, the "mi" has the wrong class reference here.
         -- we figure that out at run-time, in the methodpool,
         -- depending on the method-table-ptr
-        return $ Just (calladdr, II mi)
+        let imm8 = is8BitOffset offset
+        return $ Just (calladdr + (if imm8 then 3 else 6), II imm8 mi)
     emit' (INVOKEVIRTUAL cpidx) = do
         -- get methodInfo entry
         let mi@(MethodInfo methodname objname msig@(MethodSignature args _))  = buildMethodID cls cpidx
@@ -161,7 +163,8 @@ emitFromBB method cls hmap =  do
         -- note, the "mi" has the wrong class reference here.
         -- we figure that out at run-time, in the methodpool,
         -- depending on the method-table-ptr
-        return $ Just (calladdr, VI mi)
+        let imm8 = is8BitOffset offset
+        return $ Just (calladdr + (if imm8 then 3 else 6), VI imm8 mi)
     emit' (PUTSTATIC cpidx) = do
         pop eax
         trapaddr <- getCurrentOffset
@@ -177,6 +180,7 @@ emitFromBB method cls hmap =  do
     emit :: J.Instruction -> CodeGen e s ()
     emit POP = add esp (4 :: Word32) -- drop value
     emit DUP = push (Disp 0, esp)
+    emit DUP_X1 = do pop eax; pop ebx; push eax; push ebx; push eax
     emit AASTORE = emit IASTORE
     emit IASTORE = do
         pop eax -- value
@@ -184,12 +188,23 @@ emitFromBB method cls hmap =  do
         add ebx (1 :: Word32)
         pop ecx -- aref
         mov (ecx, ebx, S4) eax
+    emit CASTORE = do
+        pop eax -- value
+        pop ebx -- offset
+        add ebx (1 :: Word32)
+        pop ecx -- aref
+        mov (ecx, ebx, S1) eax -- TODO(bernhard): char is two byte
     emit AALOAD = emit IALOAD
     emit IALOAD = do
         pop ebx -- offset
         add ebx (1 :: Word32)
         pop ecx -- aref
         push (ecx, ebx, S4)
+    emit CALOAD = do
+        pop ebx -- offset
+        add ebx (1 :: Word32)
+        pop ecx -- aref
+        push (ecx, ebx, S1) -- TODO(bernhard): char is two byte
     emit ARRAYLENGTH = do
         pop eax
         push (Disp 0, eax)
@@ -197,6 +212,7 @@ emitFromBB method cls hmap =  do
     emit (NEWARRAY typ) = do
         let tsize = case decodeS (0 :: Integer) (B.pack [typ]) of
                     T_INT -> 4
+                    T_CHAR -> 2
                     _ -> error "newarray: type not implemented yet"
         -- get length from stack, but leave it there
         mov eax (Disp 0, esp)
@@ -221,8 +237,15 @@ emitFromBB method cls hmap =  do
         mtable <- liftIO $ getMethodTable objname
         mov (Disp 0, eax) mtable
     emit (CHECKCAST _) = nop -- TODO(bernhard): ...
+    emit ATHROW = nop -- TODO(bernhard): ...
+    emit I2C = do
+      pop eax
+      and eax (0x000000ff :: Word32)
+      push eax
     emit (BIPUSH val) = push (fromIntegral val :: Word32)
     emit (SIPUSH val) = push (fromIntegral (fromIntegral val :: Int16) :: Word32)
+    emit ACONST_NULL = push (0 :: Word32)
+    emit (ICONST_M1) = push ((-1) :: Word32)
     emit (ICONST_0) = push (0 :: Word32)
     emit (ICONST_1) = push (1 :: Word32)
     emit (ICONST_2) = push (2 :: Word32)
@@ -246,7 +269,7 @@ emitFromBB method cls hmap =  do
     emit (LDC2 x) = do
         value <- case constsPool cls M.! x of
                       (CString s) -> liftIO $ getUniqueStringAddr s
-                      _ -> error "LDCI... missing impl."
+                      e -> error $ "LDCI... missing impl.: " ++ show e
         push value
     emit (GETFIELD x) = do
         offset <- emitFieldOffset x
@@ -259,10 +282,15 @@ emitFromBB method cls hmap =  do
     emit IADD = do pop ebx; pop eax; add eax ebx; push eax
     emit ISUB = do pop ebx; pop eax; sub eax ebx; push eax
     emit IMUL = do pop ebx; pop eax; mul ebx; push eax
+    emit IDIV = do pop ebx; pop eax; xor edx edx; div ebx; push eax
+    emit IREM = do pop ebx; pop eax; xor edx edx; div ebx; push edx
     emit IXOR = do pop ebx; pop eax; xor eax ebx; push eax
+    emit INEG = do pop eax; neg eax; push eax
     emit (IINC x imm) =
         add (Disp (cArgs x), ebp) (s8_w32 imm)
 
+    emit (IFNONNULL x) = emit (IF C_NE x)
+    emit (IFNULL x) = emit (IF C_EQ x)
     emit (IF_ACMP cond x) = emit (IF_ICMP cond x)
     emit (IF_ICMP cond _) = do
         pop eax -- value2
@@ -322,8 +350,8 @@ emitFromBB method cls hmap =  do
   thisMethodArgCnt :: Word32
   thisMethodArgCnt = isNonStatic + fromIntegral (length args)
     where
-    (Just m) = lookupMethod method cls
-    (MethodSignature args _) = methodSignature m
+    (Just m) = lookupMethodSig method sig cls
+    (MethodSignature args _) = sig
     isNonStatic = if S.member ACC_STATIC (methodAccessFlags m)
         then 0 else 1 -- one argument for the this pointer
 
@@ -334,3 +362,7 @@ emitFromBB method cls hmap =  do
   s8_w32 :: Word8 -> Word32
   s8_w32 w8 = fromIntegral s8
     where s8 = fromIntegral w8 :: Int8
+
+  is8BitOffset :: Word32 -> Bool
+  is8BitOffset w32 = s32 < 128 && s32 > (-127)
+    where s32 = fromIntegral w32 :: Int32