tests: calculate factorial (non-recursive)
authorBernhard Urban <lewurm@gmail.com>
Tue, 17 Apr 2012 21:57:31 +0000 (23:57 +0200)
committerBernhard Urban <lewurm@gmail.com>
Tue, 17 Apr 2012 21:57:31 +0000 (23:57 +0200)
Makefile
Mate/X86CodeGen.hs
tests/Fac.java [new file with mode: 0644]

index d82965ede391d5f81659373be083723d7bcefcfc..69884dc212cc7bcbe9257279eacb308fbb2f92ce 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -18,6 +18,7 @@ all: mate $(CLASS_FILES)
 
 test: mate $(CLASS_FILES)
        ./$< tests/Fib.class
+       ./$< tests/Fac.class
 
 %.class: %.java
        $(JAVAC) $<
index cf493f3a9b2bb6be3c8c89a6e2a516cd6b45bc97..23165ddb1bfcb4cfe74838e1d1d261b2a5ea939c 100644 (file)
@@ -192,7 +192,9 @@ emitFromBB cls hmap =  do
         let w32_calladdr = 5 + w32_ep + (fromIntegral calladdr) :: Word32
         let trapaddr = (fromIntegral getaddr :: Word32)
         call (trapaddr - w32_calladdr)
+        add esp (4 :: Word32)
     emit (BIPUSH val) = push ((fromIntegral val) :: Word32)
+    emit (ICONST_0) = push (0 :: Word32)
     emit (ICONST_1) = push (1 :: Word32)
     emit (ICONST_2) = push (2 :: Word32)
     emit (ICONST_5) = push (5 :: Word32)
@@ -203,6 +205,7 @@ emitFromBB cls hmap =  do
         mov (Disp (cArgs_ x), ebp) eax
     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 (IINC x imm) = do
         add (Disp (cArgs x), ebp) (s8_w32 imm)
 
diff --git a/tests/Fac.java b/tests/Fac.java
new file mode 100644 (file)
index 0000000..49ddbc6
--- /dev/null
@@ -0,0 +1,19 @@
+package tests;
+
+public class Fac {
+       public static void main(String args[]) {
+               for (int i = 0; i < 10; i++) {
+                       fac(i);
+                       //System.out.printf("fac(%d): 0x%08x\n", i, fac(i));
+               }
+       }
+
+       public static int fac(int a) {
+               int b = 1;
+               while (a > 0) {
+                       b *= a;
+                       a--;
+               }
+               return b;
+       }
+}