codegen/div: clear edx before use div insn
authorBernhard Urban <lewurm@gmail.com>
Sun, 20 May 2012 19:55:47 +0000 (21:55 +0200)
committerBernhard Urban <lewurm@gmail.com>
Sun, 20 May 2012 19:55:47 +0000 (21:55 +0200)
Mate/X86CodeGen.hs
tests/Div1.java [new file with mode: 0644]

index a53e704c3cd3ced6ba4b5fba4d83751f0f6560b5..a5c9a14e7a33fa580ef7404048d8bf61344ad365 100644 (file)
@@ -29,7 +29,6 @@ import Mate.Types
 import Mate.Utilities
 import Mate.ClassPool
 import Mate.Strings
-import Mate.Debug
 #ifdef DEBUG
 import Text.Printf
 #endif
@@ -268,7 +267,7 @@ emitFromBB method sig 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
@@ -281,8 +280,8 @@ emitFromBB method sig 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; div ebx; push eax
-    emit IREM = do pop ebx; pop eax; div ebx; push edx
+    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) =
diff --git a/tests/Div1.java b/tests/Div1.java
new file mode 100644 (file)
index 0000000..3e728ba
--- /dev/null
@@ -0,0 +1,12 @@
+package tests;
+
+public class Div1 {
+       public static void main(String []args) {
+               int a = 100;
+               int b = 23;
+               System.out.printf("%d\n", a / b);
+               System.out.printf("%d\n", a % b);
+               System.out.printf("%d\n", b / a);
+               System.out.printf("%d\n", b % a);
+       }
+}