|
| 1 | +package dotty.tools.backend.jvm |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import org.junit.Assert._ |
| 5 | + |
| 6 | +import scala.tools.asm.Opcodes._ |
| 7 | + |
| 8 | +class ArrayApplyOptTest extends DottyBytecodeTest { |
| 9 | + import ASMConverters._ |
| 10 | + |
| 11 | + @Test def testArrayEmptyGenericApply= { |
| 12 | + test("Array[String]()", List(Op(ICONST_0), TypeOp(ANEWARRAY, "java/lang/String"), Op(POP), Op(RETURN))) |
| 13 | + test("Array[Unit]()", List(Op(ICONST_0), TypeOp(ANEWARRAY, "scala/runtime/BoxedUnit"), Op(POP), Op(RETURN))) |
| 14 | + test("Array[Object]()", List(Op(ICONST_0), TypeOp(ANEWARRAY, "java/lang/Object"), Op(POP), Op(RETURN))) |
| 15 | + test("Array[Boolean]()", newArray0Opcodes(T_BOOLEAN)) |
| 16 | + test("Array[Byte]()", newArray0Opcodes(T_BYTE)) |
| 17 | + test("Array[Short]()", newArray0Opcodes(T_SHORT)) |
| 18 | + test("Array[Int]()", newArray0Opcodes(T_INT)) |
| 19 | + test("Array[Long]()", newArray0Opcodes(T_LONG)) |
| 20 | + test("Array[Float]()", newArray0Opcodes(T_FLOAT)) |
| 21 | + test("Array[Double]()", newArray0Opcodes(T_DOUBLE)) |
| 22 | + test("Array[Char]()", newArray0Opcodes(T_CHAR)) |
| 23 | + test("Array[T]()", newArray0Opcodes(T_INT)) |
| 24 | + } |
| 25 | + |
| 26 | + @Test def testArrayGenericApply= { |
| 27 | + def opCodes(tpe: String) = |
| 28 | + List(Op(ICONST_2), TypeOp(ANEWARRAY, tpe), Op(DUP), Op(ICONST_0), Ldc(LDC, "a"), Op(AASTORE), Op(DUP), Op(ICONST_1), Ldc(LDC, "b"), Op(AASTORE), Op(POP), Op(RETURN)) |
| 29 | + test("""Array("a", "b")""", opCodes("java/lang/String")) |
| 30 | + test("""Array[Object]("a", "b")""", opCodes("java/lang/Object")) |
| 31 | + } |
| 32 | + |
| 33 | + @Test def testArrayApplyBoolean = |
| 34 | + test("Array(true, false)", newArray2Opcodes(T_BOOLEAN, List(Op(DUP), Op(ICONST_0), Op(ICONST_1), Op(BASTORE), Op(DUP), Op(ICONST_1), Op(ICONST_0), Op(BASTORE)))) |
| 35 | + |
| 36 | + @Test def testArrayApplyByte = |
| 37 | + test("Array[Byte](1, 2)", newArray2Opcodes(T_BYTE, List(Op(DUP), Op(ICONST_0), Op(ICONST_1), Op(BASTORE), Op(DUP), Op(ICONST_1), Op(ICONST_2), Op(BASTORE)))) |
| 38 | + |
| 39 | + @Test def testArrayApplyShort = |
| 40 | + test("Array[Short](1, 2)", newArray2Opcodes(T_SHORT, List(Op(DUP), Op(ICONST_0), Op(ICONST_1), Op(SASTORE), Op(DUP), Op(ICONST_1), Op(ICONST_2), Op(SASTORE)))) |
| 41 | + |
| 42 | + @Test def testArrayApplyInt = { |
| 43 | + test("Array(1, 2)", newArray2Opcodes(T_INT, List(Op(DUP), Op(ICONST_0), Op(ICONST_1), Op(IASTORE), Op(DUP), Op(ICONST_1), Op(ICONST_2), Op(IASTORE)))) |
| 44 | + test("""Array[T](t, t)""", newArray2Opcodes(T_INT, List(Op(DUP), Op(ICONST_0), Field(GETSTATIC, "Foo$", "MODULE$", "LFoo$;"), Invoke(INVOKEVIRTUAL, "Foo$", "t", "()I", false), Op(IASTORE), Op(DUP), Op(ICONST_1), Field(GETSTATIC, "Foo$", "MODULE$", "LFoo$;"), Invoke(INVOKEVIRTUAL, "Foo$", "t", "()I", false), Op(IASTORE)))) |
| 45 | + } |
| 46 | + |
| 47 | + @Test def testArrayApplyLong = |
| 48 | + test("Array(2L, 3L)", newArray2Opcodes(T_LONG, List(Op(DUP), Op(ICONST_0), Ldc(LDC, 2), Op(LASTORE), Op(DUP), Op(ICONST_1), Ldc(LDC, 3), Op(LASTORE)))) |
| 49 | + |
| 50 | + @Test def testArrayApplyFloat = |
| 51 | + test("Array(2.1f, 3.1f)", newArray2Opcodes(T_FLOAT, List(Op(DUP), Op(ICONST_0), Ldc(LDC, 2.1f), Op(FASTORE), Op(DUP), Op(ICONST_1), Ldc(LDC, 3.1f), Op(FASTORE)))) |
| 52 | + |
| 53 | + @Test def testArrayApplyDouble = |
| 54 | + test("Array(2.2d, 3.2d)", newArray2Opcodes(T_DOUBLE, List(Op(DUP), Op(ICONST_0), Ldc(LDC, 2.2d), Op(DASTORE), Op(DUP), Op(ICONST_1), Ldc(LDC, 3.2d), Op(DASTORE)))) |
| 55 | + |
| 56 | + @Test def testArrayApplyChar = |
| 57 | + test("Array('x', 'y')", newArray2Opcodes(T_CHAR, List(Op(DUP), Op(ICONST_0), IntOp(BIPUSH, 120), Op(CASTORE), Op(DUP), Op(ICONST_1), IntOp(BIPUSH, 121), Op(CASTORE)))) |
| 58 | + |
| 59 | + @Test def testArrayApplyUnit = |
| 60 | + test("Array[Unit]((), ())", List(Op(ICONST_2), TypeOp(ANEWARRAY, "scala/runtime/BoxedUnit"), Op(DUP), |
| 61 | + Op(ICONST_0), Field(GETSTATIC, "scala/runtime/BoxedUnit", "UNIT", "Lscala/runtime/BoxedUnit;"), Op(AASTORE), Op(DUP), |
| 62 | + Op(ICONST_1), Field(GETSTATIC, "scala/runtime/BoxedUnit", "UNIT", "Lscala/runtime/BoxedUnit;"), Op(AASTORE), Op(POP), Op(RETURN))) |
| 63 | + |
| 64 | + @Test def testArrayInlined = test( |
| 65 | + """{ |
| 66 | + | inline def array(xs: =>Int*): Array[Int] = Array(xs: _*) |
| 67 | + | array(1, 2) |
| 68 | + |}""".stripMargin, |
| 69 | + newArray2Opcodes(T_INT, List(Op(DUP), Op(ICONST_0), Op(ICONST_1), Op(IASTORE), Op(DUP), Op(ICONST_1), Op(ICONST_2), Op(IASTORE), TypeOp(CHECKCAST, "[I"))) |
| 70 | + ) |
| 71 | + |
| 72 | + @Test def testArrayInlined2 = test( |
| 73 | + """{ |
| 74 | + | inline def array(x: =>Int, xs: =>Int*): Array[Int] = Array(x, xs: _*) |
| 75 | + | array(1, 2) |
| 76 | + |}""".stripMargin, |
| 77 | + newArray2Opcodes(T_INT, List(Op(DUP), Op(ICONST_0), Op(ICONST_1), Op(IASTORE), Op(DUP), Op(ICONST_1), Op(ICONST_2), Op(IASTORE))) |
| 78 | + ) |
| 79 | + |
| 80 | + private def newArray0Opcodes(tpe: Int, init: List[Any] = Nil): List[Any] = |
| 81 | + Op(ICONST_0) :: IntOp(NEWARRAY, tpe) :: init ::: Op(POP) :: Op(RETURN) :: Nil |
| 82 | + |
| 83 | + private def newArray2Opcodes(tpe: Int, init: List[Any] = Nil): List[Any] = |
| 84 | + Op(ICONST_2) :: IntOp(NEWARRAY, tpe) :: init ::: Op(POP) :: Op(RETURN) :: Nil |
| 85 | + |
| 86 | + private def test(code: String, expectedInstructions: List[Any])= { |
| 87 | + val source = |
| 88 | + s"""class Foo { |
| 89 | + | import Foo._ |
| 90 | + | def test: Unit = $code |
| 91 | + |} |
| 92 | + |object Foo { |
| 93 | + | opaque type T = Int |
| 94 | + | def t: T = 1 |
| 95 | + |} |
| 96 | + """.stripMargin |
| 97 | + |
| 98 | + checkBCode(source) { dir => |
| 99 | + val clsIn = dir.lookupName("Foo.class", directory = false).input |
| 100 | + val clsNode = loadClassNode(clsIn) |
| 101 | + val meth = getMethod(clsNode, "test") |
| 102 | + |
| 103 | + val instructions = instructionsFromMethod(meth) |
| 104 | + |
| 105 | + assertEquals(expectedInstructions, instructions) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments