|
| 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 | +class SimplifyPosTests extends DottyBytecodeOptimisedTest with SimplifyEquivalences |
| 11 | +class SimplifyNegTests extends DottyBytecodeTest with SimplifyEquivalences |
| 12 | + |
| 13 | +class DottyBytecodeOptimisedTest extends DottyBytecodeTest { |
| 14 | + override protected def initializeCtx(c: FreshContext): Unit = { |
| 15 | + super.initializeCtx(c) |
| 16 | + val flags = Array("-optimise") // :+ "-Xprint:simplify" |
| 17 | + val summary = CompilerCommand.distill(flags)(c) |
| 18 | + c.setSettings(summary.sstate) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +trait SimplifyEquivalences { self: DottyBytecodeTest => |
| 23 | + def check(expr1: String, expr2: String, shared: String = ""): Unit = { |
| 24 | + import ASMConverters._ |
| 25 | + val source = |
| 26 | + s""" |
| 27 | + $shared |
| 28 | + |class A { |
| 29 | + | def main(): Unit = { |
| 30 | + $expr1 |
| 31 | + | } |
| 32 | + |} |
| 33 | + |class B { |
| 34 | + | def main(): Unit = { |
| 35 | + $expr2 |
| 36 | + | } |
| 37 | + |} |
| 38 | + """.stripMargin |
| 39 | + checkBCode(source) { dir => |
| 40 | + def instructions(clazz: String): List[Instruction] = { |
| 41 | + val clsIn = dir.lookupName(s"$clazz.class", directory = false).input |
| 42 | + val clsNode = loadClassNode(clsIn) |
| 43 | + instructionsFromMethod(getMethod(clsNode, "main")) |
| 44 | + } |
| 45 | + val A = instructions("A") |
| 46 | + val B = instructions("B") |
| 47 | + val diff = diffInstructions(A, B) |
| 48 | + if (this.isInstanceOf[DottyBytecodeOptimisedTest]) |
| 49 | + assert(A == B, s"Bytecode wasn't same:\n$diff") |
| 50 | + else |
| 51 | + assert(A != B, s"Bytecode was the same:\n$diff") |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test def inlineVals = |
| 56 | + check("println(1)", |
| 57 | + """ |
| 58 | + |val one = 1 |
| 59 | + |val anotherone = one |
| 60 | + |println(anotherone) |
| 61 | + """) |
| 62 | + |
| 63 | + @Test def inlineCaseIntrinsicsDottyApply = |
| 64 | + check( |
| 65 | + expr1 = "CC.apply(1, 2)", |
| 66 | + expr2 = "new CC(1, 2)", |
| 67 | + shared = "case class CC(i: Int, j: Int)") |
| 68 | + |
| 69 | + @Test def inlineCaseIntrinsicsScalacApply = |
| 70 | + check("::.apply(1, Nil)", "new ::(1, Nil)") |
| 71 | + |
| 72 | + @Test def inlineCaseIntrinsicsScalacUnapply = |
| 73 | + check( |
| 74 | + """ |
| 75 | + |val t = Tuple2(1, "s") |
| 76 | + |print(Tuple2.unapply(t)) |
| 77 | + """, |
| 78 | + """ |
| 79 | + |val t = Tuple2(1, "s") |
| 80 | + |print(new Some(new Tuple2(t._1, t._2))) |
| 81 | + """) |
| 82 | + |
| 83 | + @Test def constantFold = |
| 84 | + check( |
| 85 | + """ |
| 86 | + |val t = true // val needed, or typer takes care of this |
| 87 | + |if (t) print(1) |
| 88 | + |else print(2) |
| 89 | + """, |
| 90 | + """ |
| 91 | + |print(1) |
| 92 | + """) |
| 93 | + |
| 94 | + @Test def dropNoEffects = |
| 95 | + check( |
| 96 | + """ |
| 97 | + |"wow" |
| 98 | + |print(1) |
| 99 | + """, |
| 100 | + """ |
| 101 | + |print(1) |
| 102 | + """) |
| 103 | + |
| 104 | + // @Test def inlineOptions = |
| 105 | + // check( |
| 106 | + // """ |
| 107 | + // |val sum = Some("s") |
| 108 | + // |println(sum.isDefined) |
| 109 | + // """, |
| 110 | + // """ |
| 111 | + // |println(true) |
| 112 | + // """) |
| 113 | +} |
0 commit comments