Commit d3b5c93b authored by Pablo de Oliveira's avatar Pablo de Oliveira Committed by Roland Levillain

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 (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's avatarRoland Levillain <roland@lrde.epita.fr>
parent f2d7b4e2
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
......@@ -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
......
seq
label main
sxp
call
name print_int
binop (*)
const 56182
const 56182
call end
move
temp rv
const 0
label end
seq end
-1138550172
\ No newline at end of file
#! /bin/sh
# Copyright (C) 2014 EPITA Research and Development Laboratory (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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment