Tiger specifications do not define the expected behavior of an
integer overflow. Yet, to be consistent with Nolimips, Spim,
and most current architectures, we ensure that arithmetic
operations in HAVM wrap around.
* src/StdBinop.hs (modulo32): New function.
Use it...
(binop): ...here.
* tests/overflow.lir,
* tests/overflow.out,
* tests/overflow.test:
New test.
* tests/Makefile.am (TESTS): Add overflow.test.
(EXTRA_DIST): Add overflow.lir and overflow.out.
Signed-off-by: Roland Levillain <roland(a)lrde.epita.fr>
---
src/StdBinop.hs | 15 ++++++++++-----
tests/Makefile.am | 5 +++--
tests/overflow.lir | 14 ++++++++++++++
tests/overflow.out | 1 +
tests/{cmp.test => overflow.test} | 11 +++++------
5 files changed, 33 insertions(+), 13 deletions(-)
create mode 100644 tests/overflow.lir
create mode 100644 tests/overflow.out
copy tests/{cmp.test => overflow.test} (79%)
diff --git a/src/StdBinop.hs b/src/StdBinop.hs
index 67a177c..673f26a 100644
--- a/src/StdBinop.hs
+++ b/src/StdBinop.hs
@@ -1,12 +1,17 @@
module StdBinop (binop)
where
+import
Data.Int
+
import Ir
+modulo32 op a b =
+ fromIntegral((fromIntegral a :: Int32) `op` (fromIntegral b :: Int32))
+
-- FIXME: Add all binary operators.
binop :: Op -> Int -> Int -> Int
-binop Add = (+)
-binop Sub = (-)
-binop Mul = (*)
-binop Div = quot
-binop Mod = rem
+binop Add = modulo32 (+)
+binop Sub = modulo32 (-)
+binop Mul = modulo32 (*)
+binop Div = modulo32 quot
+binop Mod = modulo32 rem
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5849e8a..c7a59a0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -5,9 +5,10 @@
XFAIL_TESTS =
TESTS = \
-cmp.test trace.test
+cmp.test overflow.test trace.test
-EXTRA_DIST = cmp.lir cmp.out fact.lir fact.out $(TESTS)
+EXTRA_DIST = cmp.lir cmp.out fact.lir fact.out overflow.lir overflow.out \
+ $(TESTS)
# Each test case depends on defs.
check_SCRIPTS = defs
diff --git a/tests/overflow.lir b/tests/overflow.lir
new file mode 100644
index 0000000..08781f1
--- /dev/null
+++ b/tests/overflow.lir
@@ -0,0 +1,14 @@
+seq
+ label main
+ sxp
+ call
+ name print_int
+ binop (*)
+ const 56182
+ const 56182
+ call end
+ move
+ temp rv
+ const 0
+ label end
+seq end
diff --git a/tests/overflow.out b/tests/overflow.out
new file mode 100644
index 0000000..a34546e
--- /dev/null
+++ b/tests/overflow.out
@@ -0,0 +1 @@
+-1138550172
\ No newline at end of file
diff --git a/tests/cmp.test b/tests/overflow.test
similarity index 79%
copy from tests/cmp.test
copy to tests/overflow.test
index 55d1015..9904ac7 100755
--- a/tests/cmp.test
+++ b/tests/overflow.test
@@ -1,5 +1,5 @@
#! /bin/sh
-# Copyright (C) 2012 EPITA Research and Development Laboratory (LRDE)
+# Copyright (C) 2014 EPITA Research and Development Laboratory (LRDE)
#
# This file is part of HAVM.
#
@@ -22,12 +22,11 @@
set -e
-HAVM_pass --trace $srcdir/cmp.lir
-cmp stdout $srcdir/cmp.out
+HAVM_pass --trace $srcdir/overflow.lir
+cmp stdout $srcdir/overflow.out
test -s stderr
-HAVM_pass --trace=3 $srcdir/cmp.lir 3>stdtrc
-cmp stdout $srcdir/cmp.out
+HAVM_pass --trace=3 $srcdir/overflow.lir 3>stdtrc
+cmp stdout $srcdir/overflow.out
test ! -s stderr
test -s stdtrc
-
--
1.7.10.4