test/Simplify: don't crash on divide by zero
authorMichal Terepeta <michal.terepeta@gmail.com>
Sun, 5 Jul 2015 12:43:42 +0000 (14:43 +0200)
committerMichal Terepeta <michal.terepeta@gmail.com>
Sun, 12 Jul 2015 15:02:52 +0000 (17:02 +0200)
Currently test/Simplify would not check for 0 before trying to do
constant folding, which is quite bad since it simply crashes the
program.

The fix is to simply avoid evaluating the expression if there is a
division by zero.

testing/Main.hs
testing/Simplify.hs
testing/tests/test7 [new file with mode: 0644]
testing/tests/test7.expected [new file with mode: 0644]

index 000c7ac..47dbcd5 100644 (file)
@@ -32,6 +32,7 @@ goldensTests = Framework.testGroup "Goldens tests"
         , "test4"
         , "test5"
         , "test6"
+        , "test7"
         , "if-test"
         , "if-test2"
         , "if-test3"
index 20b4ab2..bb00dc6 100644 (file)
@@ -27,6 +27,8 @@ simplify = deepFwdRw simp
     = Just $ Lit $ Int $ n1 + n2
     -- ... more cases for constant folding
 -- @ end cprop.tex
+  s_exp (Binop Div _lhs (Lit (Int 0)))
+    = Nothing
   s_exp (Binop opr e1 e2)
     | (Just op, Lit (Int i1), Lit (Int i2)) <- (intOp opr, e1, e2) =
         Just $ Lit $ Int  $ op i1 i2
diff --git a/testing/tests/test7 b/testing/tests/test7
new file mode 100644 (file)
index 0000000..9558250
--- /dev/null
@@ -0,0 +1,6 @@
+-- Test that we don't crash on divide by zero.
+procName() {
+L0:
+  reg1 = 42 / 0
+  ret(reg1)
+}
diff --git a/testing/tests/test7.expected b/testing/tests/test7.expected
new file mode 100644 (file)
index 0000000..e9f7ffd
--- /dev/null
@@ -0,0 +1,5 @@
+procName() {
+L0:
+  reg1 = 42 / 0
+  ret(reg1)
+}