codegen: implement IF_ACMP
[mate.git] / Mate / X86CodeGen.hs
index 7ddcb6a8b0cd257068f9477092db51d9f731f803..9fc57567fd963ced2185cee14d289f0ce6853472 100644 (file)
@@ -26,6 +26,8 @@ import Mate.BasicBlocks
 import Mate.Types
 import Mate.Utilities
 import Mate.ClassPool
+import Mate.Strings
+
 
 foreign import ccall "dynamic"
    code_int :: FunPtr (CInt -> CInt -> IO CInt) -> (CInt -> CInt -> IO CInt)
@@ -193,7 +195,7 @@ emitFromBB method cls hmap =  do
         newNamedLabel (show mi) >>= defineLabel
         -- objref lives somewhere on the argument stack
         mov eax (Disp ((*4) $ fromIntegral $ length args), esp)
-        -- get methodtable ref
+        -- get method-table-ptr
         mov eax (Disp 0, eax)
         -- get method offset
         let nameAndSig = methodname `B.append` (encode msig)
@@ -206,7 +208,10 @@ 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 mi)
+        -- 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)
     emit' (PUTSTATIC cpidx) = do
         pop eax
         trapaddr <- getCurrentOffset
@@ -242,6 +247,7 @@ emitFromBB method cls hmap =  do
         -- set method table pointer
         let mtable = unsafePerformIO $ getMethodTable objname
         mov (Disp 0, eax) mtable
+    emit (CHECKCAST _) = nop -- TODO(bernhard): ...
     emit (BIPUSH val) = push ((fromIntegral val) :: Word32)
     emit (SIPUSH val) = push ((fromIntegral $ ((fromIntegral val) :: Int16)) :: Word32)
     emit (ICONST_0) = push (0 :: Word32)
@@ -264,6 +270,12 @@ emitFromBB method cls hmap =  do
         pop eax
         mov (Disp (cArgs x), ebp) eax
 
+    emit (LDC1 x) = emit (LDC2 $ fromIntegral x)
+    emit (LDC2 x) = do
+        let value = case (constsPool cls) M.! x of
+                      (CString s) -> unsafePerformIO $ getUniqueStringAddr s
+                      _ -> error $ "LDCI... missing impl."
+        push value
     emit (GETFIELD x) = do
         pop eax -- this pointer
         let (cname, fname) = buildFieldOffset cls x
@@ -282,6 +294,7 @@ emitFromBB method cls hmap =  do
     emit (IINC x imm) = do
         add (Disp (cArgs x), ebp) (s8_w32 imm)
 
+    emit (IF_ACMP cond x) = emit (IF_ICMP cond x)
     emit (IF_ICMP cond _) = do
         pop eax -- value2
         pop ebx -- value1
@@ -308,6 +321,7 @@ emitFromBB method cls hmap =  do
         jmp $ getLabel sid lmap
 
     emit RETURN = do mov esp ebp; pop ebp; ret
+    emit ARETURN = emit IRETURN
     emit IRETURN = do
         pop eax
         mov esp ebp