From: Bernhard Urban Date: Tue, 17 Apr 2012 21:57:31 +0000 (+0200) Subject: tests: calculate factorial (non-recursive) X-Git-Url: http://wien.tomnetworks.com/gitweb/?p=mate.git;a=commitdiff_plain;h=8fef0c0cd0b24e62261cdc3a63106853373b6faf tests: calculate factorial (non-recursive) --- diff --git a/Makefile b/Makefile index d82965e..69884dc 100644 --- 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) $< diff --git a/Mate/X86CodeGen.hs b/Mate/X86CodeGen.hs index cf493f3..23165dd 100644 --- a/Mate/X86CodeGen.hs +++ b/Mate/X86CodeGen.hs @@ -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 index 0000000..49ddbc6 --- /dev/null +++ b/tests/Fac.java @@ -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; + } +}