From d20e8b48ffe62986ec195b1b5cf380e5526d3f8c Mon Sep 17 00:00:00 2001 From: Robbert Haarman Date: Sun, 11 Jan 2009 10:42:57 +0100 Subject: [PATCH] Added mod test and made it pass. --- src/code_generator.rb | 16 ++++++++++++++++ test/Makefile | 2 +- test/mod.out | 10 ++++++++++ test/mod.voo | 41 +++++++++++++++++++++++++++++++++++++++++ test/test | 2 ++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 test/mod.out create mode 100644 test/mod.voo diff --git a/src/code_generator.rb b/src/code_generator.rb index ddb95dd..0dcd18a 100644 --- a/src/code_generator.rb +++ b/src/code_generator.rb @@ -31,6 +31,7 @@ class CodeGenerator else # Cases that are handled specially return div(target, x, y) if op == 'div' + return mod(target, x, y) if op == 'mod' return mul(target, x, y) if op == 'mul' target_ref = load_value target, "eax" @@ -57,6 +58,7 @@ class CodeGenerator def binop2 op, target, y # Cases that are handled specially return div2(target, y) if op == 'div' + return mod2(target, y) if op == 'mod' return mul2(target, y) if op == 'mul' target_ref = load_value target, "ebx" @@ -344,6 +346,20 @@ class CodeGenerator operand[0] == ?[ end + # Divide x by y and store the remainder in target + def mod target, x, y + eval_div x, y + target_ref = load_value target, "ebx" + emit "mov #{target_ref}, edx\n" + end + + # Divide target by x and store the remainder in target + def mod2 target, x + eval_div target, x + target_ref = load_value target, "ebx" + emit "mov #{target_ref}, edx\n" + end + # Multiply x by y and store the result in target def mul target, x, y # Assumes that eval_mul does not clobber ecx diff --git a/test/Makefile b/test/Makefile index d0de800..af44a02 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,7 +1,7 @@ NASM = nasm VOODOOC = env RUBYLIB=../src:$(RUBYLIB) ../src/voodooc -TARGETS = bitwise div fact hello mul plusminus tail-calls +TARGETS = bitwise div fact hello mod mul plusminus tail-calls all : $(TARGETS) diff --git a/test/mod.out b/test/mod.out new file mode 100644 index 0000000..acc4c54 --- /dev/null +++ b/test/mod.out @@ -0,0 +1,10 @@ +0 +0 +0 +0 +4 +4 +0 +4 +2 +2 diff --git a/test/mod.voo b/test/mod.voo new file mode 100644 index 0000000..3141366 --- /dev/null +++ b/test/mod.voo @@ -0,0 +1,41 @@ +section ".data" +format: +string "%d\n\x00" + +section ".text" +import printf +export main + +main: +function argc argv + let x 24 + let y mod x 1 + call printf format y + set y mod x -1 + call printf format y + set y mod x 3 + call printf format y + set y mod x -3 + call printf format y + set y mod x 5 + call printf format y + set y mod x -5 + call printf format y + + let z 3 + set y mod x z + call printf format y + set z -5 + set y mod x z + call printf format y + + set x -24 + set z 5 + set y mod x z + call printf format y + set z -5 + set y mod x z + call printf format y + + return 0 +end function diff --git a/test/test b/test/test index e1916a8..c122eff 100755 --- a/test/test +++ b/test/test @@ -44,6 +44,8 @@ run_test1 mul run_test1 div +run_test1 mod + if [ $errors -eq 0 ] then echo All tests passed -- 2.11.4.GIT