>From bab115db36bfe4ad4da88f09adbb9ab4b0ecb1fe Mon Sep 17 00:00:00 2001
From: Pablo de Oliveira <pablo@sifflez.org>
Date: Sun, 6 Apr 2014 02:30:00 +0200
Subject: [PATCH] Arithmetic overflow should wrap around

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     | 15 ++++++++++-----
 tests/Makefile.am   |  2 +-
 tests/overflow.lir  | 14 ++++++++++++++
 tests/overflow.out  |  1 +
 tests/overflow.test | 33 +++++++++++++++++++++++++++++++++
 5 files changed, 59 insertions(+), 6 deletions(-)
 create mode 100644 tests/overflow.lir
 create mode 100644 tests/overflow.out
 create mode 100755 tests/overflow.test

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..1a82bbb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -5,7 +5,7 @@
 XFAIL_TESTS =
 
 TESTS = \
-cmp.test trace.test
+cmp.test overflow.test trace.test
 
 EXTRA_DIST = cmp.lir cmp.out fact.lir fact.out $(TESTS)
 
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/overflow.test b/tests/overflow.test
new file mode 100755
index 0000000..bdb43d3
--- /dev/null
+++ b/tests/overflow.test
@@ -0,0 +1,33 @@
+#! /bin/sh
+# Copyright (C) 2006  LRDE
+#
+# This file is part of HAVM.
+#
+# HAVM is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# HAVM is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Texinfo; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+# Boston, MA 02110-1301, USA.
+
+. ./defs || exit 1
+
+set -e
+
+HAVM_pass --trace $srcdir/overflow.lir
+cmp stdout $srcdir/overflow.out
+test -s stderr
+
+HAVM_pass --trace=3 $srcdir/overflow.lir 3>stdtrc
+cmp stdout $srcdir/overflow.out
+test ! -s stderr
+test   -s stdtrc
+
-- 
1.9.1

