codegen: implement `newarray' and `arraylength'
[mate.git] / Mate / X86CodeGen.hs
index 9fc57567fd963ced2185cee14d289f0ce6853472..c71494e74ecdad5ea6bee96746adbf80b9cb5a5a 100644 (file)
@@ -3,6 +3,7 @@
 module Mate.X86CodeGen where
 
 import Data.Binary
+import Data.BinaryState
 import Data.Int
 import Data.Maybe
 import qualified Data.Map as M
@@ -199,7 +200,7 @@ emitFromBB method cls hmap =  do
         mov eax (Disp 0, eax)
         -- get method offset
         let nameAndSig = methodname `B.append` (encode msig)
-        let offset = unsafePerformIO $ getMethodOffset objname nameAndSig
+        offset <- liftIO $ getMethodOffset objname nameAndSig
         -- make actual (indirect) call
         calladdr <- getCurrentOffset
         call (Disp offset, eax)
@@ -233,19 +234,34 @@ emitFromBB method cls hmap =  do
         call (trapaddr - w32_calladdr)
         add esp (4 :: Word32)
     emit DUP = push (Disp 0, esp)
+    emit ARRAYLENGTH = do
+        pop eax
+        push (Disp 0, eax)
+    emit (NEWARRAY typ) = do
+        let tsize = case decodeS (0 :: Integer) (B.pack [typ]) of
+                    T_INT -> 4
+                    _ -> error $ "newarray: type not implemented yet"
+        -- get length from stack, but leave it there
+        mov eax (Disp 0, esp)
+        mov ebx (tsize :: Word32)
+        -- multiple amount with native size of one element
+        mul ebx -- result is in eax
+        add eax (4 :: Word32) -- for "length" entry
+        -- push amount of bytes to allocate
+        push eax
+        callMalloc
+        pop eax -- ref to arraymemory
+        pop ebx -- length
+        mov (Disp 0, eax) ebx -- store length at offset 0
+        push eax -- push ref again
     emit (NEW objidx) = do
         let objname = buildClassID cls objidx
-        let amount = unsafePerformIO $ getMethodSize objname
+        amount <- liftIO $ getMethodSize objname
         push (amount :: Word32)
-        calladdr <- getCurrentOffset
-        let w32_calladdr = 5 + calladdr
-        let malloaddr = (fromIntegral getMallocAddr :: Word32)
-        call (malloaddr - w32_calladdr)
-        add esp (4 :: Word32)
-        push eax
+        callMalloc
         -- TODO(bernhard): save reference somewhere for GC
         -- set method table pointer
-        let mtable = unsafePerformIO $ getMethodTable objname
+        mtable <- liftIO $ getMethodTable objname
         mov (Disp 0, eax) mtable
     emit (CHECKCAST _) = nop -- TODO(bernhard): ...
     emit (BIPUSH val) = push ((fromIntegral val) :: Word32)
@@ -272,20 +288,20 @@ emitFromBB method cls hmap =  do
 
     emit (LDC1 x) = emit (LDC2 $ fromIntegral x)
     emit (LDC2 x) = do
-        let value = case (constsPool cls) M.! x of
-                      (CString s) -> unsafePerformIO $ getUniqueStringAddr s
+        value <- case (constsPool cls) M.! x of
+                      (CString s) -> liftIO $ getUniqueStringAddr s
                       _ -> error $ "LDCI... missing impl."
         push value
     emit (GETFIELD x) = do
         pop eax -- this pointer
         let (cname, fname) = buildFieldOffset cls x
-        let offset = unsafePerformIO $ getFieldOffset cname fname
+        offset <- liftIO $ getFieldOffset cname fname
         push (Disp (fromIntegral $ offset * 4), eax) -- get field
     emit (PUTFIELD x) = do
         pop ebx -- value to write
         pop eax -- this pointer
         let (cname, fname) = buildFieldOffset cls x
-        let offset = unsafePerformIO $ getFieldOffset cname fname
+        offset <- liftIO $ getFieldOffset cname fname
         mov (Disp (fromIntegral $ offset * 4), eax) ebx -- set field
 
     emit IADD = do pop ebx; pop eax; add eax ebx; push eax
@@ -329,6 +345,15 @@ emitFromBB method cls hmap =  do
         ret
     emit invalid = error $ "insn not implemented yet: " ++ (show invalid)
 
+    callMalloc :: CodeGen e s ()
+    callMalloc = do
+        calladdr <- getCurrentOffset
+        let w32_calladdr = 5 + calladdr
+        let malloaddr = (fromIntegral getMallocAddr :: Word32)
+        call (malloaddr - w32_calladdr)
+        add esp (4 :: Word32)
+        push eax
+
   -- for locals we use a different storage
   cArgs :: Word8 -> Word32
   cArgs x = if (x' >= thisMethodArgCnt)