summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2016-08-20 21:35:17 +0900
committerGitHub <[email protected]>2016-08-20 21:35:17 +0900
commitc313fdfed0be131b710f820b5414fb2687038feb (patch)
tree0da13c4c3d4675f558d7d2490f3ab56182c9775a
parentff3e4b91f282ade0c205b72e18c08ca22acdcdf8 (diff)
parent1751d8fd3b7632e2292cfa3c20f9fba96e74c38f (diff)
downloadmruby-c313fdfed0be131b710f820b5414fb2687038feb.tar.gz
mruby-c313fdfed0be131b710f820b5414fb2687038feb.zip
Merge pull request #3199 from miura1729/original
Add new test for optimize VM
-rwxr-xr-xtest/t/lang.rb73
1 files changed, 73 insertions, 0 deletions
diff --git a/test/t/lang.rb b/test/t/lang.rb
new file mode 100755
index 000000000..23b92771a
--- /dev/null
+++ b/test/t/lang.rb
@@ -0,0 +1,73 @@
+# The aim of these tests is to detect pitfall for optimized VM.
+
+# Test for or/and
+#
+# You may think instruction fusion(OP_EQ and OP_JMPIF) for avoiding
+# generate intermedicate boolean value.
+# But and/or is pitfall for this fusioning.
+#
+# For example. Folloging mruby code
+# if i > 0 and i < 10 then
+#
+# compilies to RITE bitecode
+#
+# 1 000 OP_LOADI R1 0 ; R1:i
+# 2 001 OP_MOVE R2 R1 ; R1:i
+# 2 002 OP_LOADI R3 0
+# 2 003 OP_GT R2 :> 1
+# 2 004 OP_JMPNOT R2 008
+# 2 005 OP_MOVE R2 R1 ; R1:i
+# 2 006 OP_LOADI R3 10
+# 2 007 OP_LT R2 :< 1
+# 2 008 OP_JMPNOT R2 (The address of end of then part)
+#
+# When the instruction fusion the OP_GT and OP_JMPNOT you fell into the pitfalls.
+# The deleted intermedicate boolean value is used in OP_JMPNOT (address 008).
+
+assert('and', '11.2.3') do
+ a = 1
+ if a > 0 and a < 10 then
+ b = 1
+ else
+ b = 0
+ end
+ assert_equal 1, b
+
+ if a < 0 and a < 10 then
+ b = 1
+ else
+ b = 0
+ end
+ assert_equal 0, b
+
+ if a < 0 and a > 10 then
+ b = 1
+ else
+ b = 0
+ end
+ assert_equal 0, b
+end
+
+assert('or','11.2.4') do
+ a = 1
+ if a > 0 or a < 10 then
+ b = 1
+ else
+ b = 0
+ end
+ assert_equal 1, b
+
+ if a < 0 or a < 10 then
+ b = 1
+ else
+ b = 0
+ end
+ assert_equal 1, b
+
+ if a < 0 or a > 10 then
+ b = 1
+ else
+ b = 0
+ end
+ assert_equal 0, b
+end