| 1 |
# The aim of these tests is to detect pitfall for optimized VM. |
| 2 |
|
| 3 |
# Test for or/and |
| 4 |
# |
| 5 |
# You may think instruction fusion(OP_EQ and OP_JMPIF) for avoiding |
| 6 |
# generate intermediate boolean value. |
| 7 |
# But and/or is pitfall for this fusioning. |
| 8 |
# |
| 9 |
# For example, the following mruby code: |
| 10 |
# |
| 11 |
# if i > 0 and i < 10 |
| 12 |
# |
| 13 |
# compiles to the following byte code: |
| 14 |
# |
| 15 |
# 1 000 OP_LOADI R1 0 ; R1:i |
| 16 |
# 2 001 OP_MOVE R2 R1 ; R1:i |
| 17 |
# 2 002 OP_LOADI R3 0 |
| 18 |
# 2 003 OP_GT R2 :> 1 |
| 19 |
# 2 004 OP_JMPNOT R2 008 |
| 20 |
# 2 005 OP_MOVE R2 R1 ; R1:i |
| 21 |
# 2 006 OP_LOADI R3 10 |
| 22 |
# 2 007 OP_LT R2 :< 1 |
| 23 |
# 2 008 OP_JMPNOT R2 (The address of end of then part) |
| 24 |
# |
| 25 |
# When the instruction fusion the OP_GT and OP_JMPNOT you fell into the pitfalls. |
| 26 |
# The deleted intermediate boolean value is used in OP_JMPNOT (address 008). |
| 27 |
|
| 28 |
assert('and', '11.2.3') do |
| 29 |
a = 1 |
| 30 |
if a > 0 and a < 10 |
| 31 |
b = 1 |
| 32 |
else |
| 33 |
b = 0 |
| 34 |
end |
| 35 |
assert_equal 1, b |
| 36 |
|
| 37 |
if a < 0 and a < 10 |
| 38 |
b = 1 |
| 39 |
else |
| 40 |
b = 0 |
| 41 |
end |
| 42 |
assert_equal 0, b |
| 43 |
|
| 44 |
if a < 0 and a > 10 |
| 45 |
b = 1 |
| 46 |
else |
| 47 |
b = 0 |
| 48 |
end |
| 49 |
assert_equal 0, b |
| 50 |
end |
| 51 |
|
| 52 |
assert('or','11.2.4') do |
| 53 |
a = 1 |
| 54 |
if a > 0 or a < 10 |
| 55 |
b = 1 |
| 56 |
else |
| 57 |
b = 0 |
| 58 |
end |
| 59 |
assert_equal 1, b |
| 60 |
|
| 61 |
if a < 0 or a < 10 |
| 62 |
b = 1 |
| 63 |
else |
| 64 |
b = 0 |
| 65 |
end |
| 66 |
assert_equal 1, b |
| 67 |
|
| 68 |
if a < 0 or a > 10 |
| 69 |
b = 1 |
| 70 |
else |
| 71 |
b = 0 |
| 72 |
end |
| 73 |
assert_equal 0, b |
| 74 |
end |