1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
package scala.issues
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Test
import scala.tools.asm.Opcodes._
import scala.tools.nsc.backend.jvm.AsmUtils
import scala.tools.nsc.backend.jvm.CodeGenTools._
import org.junit.Assert._
import scala.collection.JavaConverters._
import scala.tools.partest.ASMConverters._
import scala.tools.testing.ClearAfterClass
object BytecodeTest extends ClearAfterClass.Clearable {
var compiler = newCompiler()
def clear(): Unit = { compiler = null }
}
@RunWith(classOf[JUnit4])
class BytecodeTest extends ClearAfterClass {
ClearAfterClass.stateToClear = BytecodeTest
val compiler = BytecodeTest.compiler
@Test
def t8731(): Unit = {
val code =
"""class C {
| def f(x: Int) = (x: @annotation.switch) match {
| case 1 => 0
| case 2 => 1
| case 3 => 2
| }
| final val K = 10
| def g(x: Int) = (x: @annotation.switch) match {
| case K => 0
| case 1 => 10
| case 2 => 20
| }
|}
""".stripMargin
val List(c) = compileClasses(compiler)(code)
assertTrue(getSingleMethod(c, "f").instructions.count(_.isInstanceOf[TableSwitch]) == 1)
assertTrue(getSingleMethod(c, "g").instructions.count(_.isInstanceOf[LookupSwitch]) == 1)
}
@Test
def t8926(): Unit = {
import scala.reflect.internal.util.BatchSourceFile
// this test cannot be implemented using partest because of its mixed-mode compilation strategy:
// partest first compiles all files with scalac, then the java files, and then again the scala
// using the output classpath. this shadows the bug SI-8926.
val annotA =
"""import java.lang.annotation.Retention;
|import java.lang.annotation.RetentionPolicy;
|@Retention(RetentionPolicy.RUNTIME)
|public @interface AnnotA { }
""".stripMargin
val annotB = "public @interface AnnotB { }"
val scalaSrc =
"""@AnnotA class A
|@AnnotB class B
""".stripMargin
val run = new compiler.Run()
run.compileSources(List(new BatchSourceFile("AnnotA.java", annotA), new BatchSourceFile("AnnotB.java", annotB), new BatchSourceFile("Test.scala", scalaSrc)))
val outDir = compiler.settings.outputDirs.getSingleOutput.get
val outfiles = (for (f <- outDir.iterator if !f.isDirectory) yield (f.name, f.toByteArray)).toList
def check(classfile: String, annotName: String) = {
val f = (outfiles collect { case (`classfile`, bytes) => AsmUtils.readClass(bytes) }).head
val descs = f.visibleAnnotations.asScala.map(_.desc).toList
assertTrue(descs.toString, descs exists (_ contains annotName))
}
check("A.class", "AnnotA")
// known issue SI-8928: the visibility of AnnotB should be CLASS, but annotation classes without
// a @Retention annotation are currently emitted as RUNTIME.
check("B.class", "AnnotB")
}
@Test
def t6288bJumpPosition(): Unit = {
val code =
"""object Case3 { // 01
| def unapply(z: Any): Option[Int] = Some(-1) // 02
| def main(args: Array[String]) { // 03
| ("": Any) match { // 04
| case x : String => // 05
| println("case 0") // 06 println and jump at 6
| case _ => // 07
| println("default") // 08 println and jump at 8
| } // 09
| println("done") // 10
| }
|}
""".stripMargin
val List(mirror, module) = compileClasses(compiler)(code)
val unapplyLineNumbers = getSingleMethod(module, "unapply").instructions.filter(_.isInstanceOf[LineNumber])
assert(unapplyLineNumbers == List(LineNumber(2, Label(0))), unapplyLineNumbers)
val expected = List(
LineNumber(4, Label(0)),
LineNumber(5, Label(5)),
Jump(IFEQ, Label(20)),
LineNumber(6, Label(11)),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
Jump(GOTO, Label(33)),
LineNumber(5, Label(20)),
Jump(GOTO, Label(24)),
LineNumber(8, Label(24)),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
Jump(GOTO, Label(33)),
LineNumber(10, Label(33)),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false)
)
val mainIns = getSingleMethod(module, "main").instructions filter {
case _: LineNumber | _: Invoke | _: Jump => true
case _ => false
}
assertSameCode(mainIns, expected)
}
@Test
def bytecodeForBranches(): Unit = {
val code =
"""class C {
| def t1(b: Boolean) = if (b) 1 else 2
| def t2(x: Int) = if (x == 393) 1 else 2
| def t3(a: Array[String], b: AnyRef) = a != b && b == a
| def t4(a: AnyRef) = a == null || null != a
| def t5(a: AnyRef) = (a eq null) || (null ne a)
| def t6(a: Int, b: Boolean) = if ((a == 10) && b || a != 1) 1 else 2
| def t7(a: AnyRef, b: AnyRef) = a == b
| def t8(a: AnyRef) = Nil == a || "" != a
|}
""".stripMargin
val List(c) = compileClasses(compiler)(code)
// t1: no unnecessary GOTOs
assertSameCode(getSingleMethod(c, "t1"), List(
VarOp(ILOAD, 1), Jump(IFEQ, Label(6)),
Op(ICONST_1), Jump(GOTO, Label(9)),
Label(6), Op(ICONST_2),
Label(9), Op(IRETURN)))
// t2: no unnecessary GOTOs
assertSameCode(getSingleMethod(c, "t2"), List(
VarOp(ILOAD, 1), IntOp(SIPUSH, 393), Jump(IF_ICMPNE, Label(7)),
Op(ICONST_1), Jump(GOTO, Label(10)),
Label(7), Op(ICONST_2),
Label(10), Op(IRETURN)))
// t3: Array == is translated to reference equality, AnyRef == to null checks and equals
assertSameCode(getSingleMethod(c, "t3"), List(
// Array ==
VarOp(ALOAD, 1), VarOp(ALOAD, 2), Jump(IF_ACMPEQ, Label(23)),
// AnyRef ==
VarOp(ALOAD, 2), VarOp(ALOAD, 1), VarOp(ASTORE, 3), Op(DUP), Jump(IFNONNULL, Label(14)),
Op(POP), VarOp(ALOAD, 3), Jump(IFNULL, Label(19)), Jump(GOTO, Label(23)),
Label(14), VarOp(ALOAD, 3), Invoke(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false), Jump(IFEQ, Label(23)),
Label(19), Op(ICONST_1), Jump(GOTO, Label(26)),
Label(23), Op(ICONST_0),
Label(26), Op(IRETURN)))
val t4t5 = List(
VarOp(ALOAD, 1), Jump(IFNULL, Label(6)),
VarOp(ALOAD, 1), Jump(IFNULL, Label(10)),
Label(6), Op(ICONST_1), Jump(GOTO, Label(13)),
Label(10), Op(ICONST_0),
Label(13), Op(IRETURN))
// t4: one side is known null, so just a null check on the other
assertSameCode(getSingleMethod(c, "t4"), t4t5)
// t5: one side known null, so just a null check on the other
assertSameCode(getSingleMethod(c, "t5"), t4t5)
// t6: no unnecessary GOTOs
assertSameCode(getSingleMethod(c, "t6"), List(
VarOp(ILOAD, 1), IntOp(BIPUSH, 10), Jump(IF_ICMPNE, Label(7)),
VarOp(ILOAD, 2), Jump(IFNE, Label(12)),
Label(7), VarOp(ILOAD, 1), Op(ICONST_1), Jump(IF_ICMPEQ, Label(16)),
Label(12), Op(ICONST_1), Jump(GOTO, Label(19)),
Label(16), Op(ICONST_2),
Label(19), Op(IRETURN)))
// t7: universal equality
assertInvoke(getSingleMethod(c, "t7"), "scala/runtime/BoxesRunTime", "equals")
// t8: no null checks invoking equals on modules and constants
assertSameCode(getSingleMethod(c, "t8"), List(
Field(GETSTATIC, "scala/collection/immutable/Nil$", "MODULE$", "Lscala/collection/immutable/Nil$;"), VarOp(ALOAD, 1), Invoke(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false), Jump(IFNE, Label(10)),
Ldc(LDC, ""), VarOp(ALOAD, 1), Invoke(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false), Jump(IFNE, Label(14)),
Label(10), Op(ICONST_1), Jump(GOTO, Label(17)),
Label(14), Op(ICONST_0),
Label(17), Op(IRETURN)))
}
}
|