|
| 1 | +package dotty.tools.dotc |
| 2 | + |
| 3 | +import org.junit.Assert._ |
| 4 | +import org.junit.Test |
| 5 | +import dotty.tools.backend.jvm._ |
| 6 | +import dotty.tools.dotc.config.CompilerCommand |
| 7 | +import dotty.tools.dotc.core.Contexts.FreshContext |
| 8 | +import scala.tools.asm.tree.MethodNode |
| 9 | + |
| 10 | +import scala.collection.JavaConverters._ |
| 11 | + |
| 12 | +class ConstantFoldingTests extends DottyBytecodeTest { |
| 13 | + |
| 14 | + val conditionSrc = """class Test { |
| 15 | + def someCondition: Boolean = ??? |
| 16 | + final val t = true |
| 17 | + final val f = false |
| 18 | +
|
| 19 | + def whenTrue = |
| 20 | + println("hi") |
| 21 | + def whenFalse = |
| 22 | + () |
| 23 | + def whenCond = |
| 24 | + if (someCondition) |
| 25 | + println("hi") |
| 26 | +
|
| 27 | + def reduceToTrue1 = |
| 28 | + if (t) |
| 29 | + println("hi") |
| 30 | + def reduceToTrue2 = |
| 31 | + if (t || someCondition) |
| 32 | + println("hi") |
| 33 | + def reduceToTrue3 = |
| 34 | + if (f || t || someCondition) |
| 35 | + println("hi") |
| 36 | +
|
| 37 | + def reduceToFalse1 = |
| 38 | + if (f) |
| 39 | + println("hi") |
| 40 | + def reduceToFalse2 = |
| 41 | + if (f && someCondition) |
| 42 | + println("hi") |
| 43 | + def reduceToFalse3 = |
| 44 | + if (t && f && someCondition) |
| 45 | + println("hi") |
| 46 | +
|
| 47 | + def reduceToCond1 = |
| 48 | + if (t && someCondition) |
| 49 | + println("hi") |
| 50 | + def reduceToCond2 = |
| 51 | + if (f || someCondition) |
| 52 | + println("hi") |
| 53 | + def reduceToCond3 = |
| 54 | + if ((t && f) || someCondition) |
| 55 | + println("hi") |
| 56 | +} |
| 57 | +""" |
| 58 | + |
| 59 | + @Test def constantFoldConditions: Unit = { |
| 60 | + import ASMConverters._ |
| 61 | + |
| 62 | + checkBCode(conditionSrc) { dir => |
| 63 | + val clsIn = dir.lookupName(s"Test.class", directory = false).input |
| 64 | + val methods = loadClassNode(clsIn).methods.asScala |
| 65 | + |
| 66 | + val whenTrue = methods.find(_.name == "whenTrue").get |
| 67 | + val whenFalse = methods.find(_.name == "whenFalse").get |
| 68 | + val whenCond = methods.find(_.name == "whenCond").get |
| 69 | + |
| 70 | + val reduceToTrue = methods.filter(_.name.startsWith("reduceToTrue")) |
| 71 | + val reduceToFalse = methods.filter(_.name.startsWith("reduceToFalse")) |
| 72 | + val reduceToCond = methods.filter(_.name.startsWith("reduceToCond")) |
| 73 | + |
| 74 | + def compare(expected: MethodNode, actual: MethodNode) = { |
| 75 | + val expectedInstrs = instructionsFromMethod(expected) |
| 76 | + val actualInstrs = instructionsFromMethod(actual) |
| 77 | + val diff = diffInstructions(expectedInstrs, actualInstrs) |
| 78 | + assert(expectedInstrs == actualInstrs, |
| 79 | + s"Different bytecode between ${expected.name} and ${actual.name}\n$diff") |
| 80 | + } |
| 81 | + reduceToTrue.foreach(compare(whenTrue, _)) |
| 82 | + reduceToFalse.foreach(compare(whenFalse, _)) |
| 83 | + reduceToCond.foreach(compare(whenCond, _)) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments