Skip to content

Commit 0abd076

Browse files
smarterretronym
andcommitted
Upgrade to ASM 7.0
Catch up with Scala 2.13. The corresponding scalac PRs are scala/scala#6733 and scala/scala#7384 Some of the changes from these PRs aren't currently needed in Dotty since they only affect the Scala 2 backend optimizer. This update does not change the generated bytecode (at least when compiling dotty itself). Co-Authored-By: Jason Zaugg <[email protected]>
1 parent 51853d6 commit 0abd076

File tree

9 files changed

+144
-22
lines changed

9 files changed

+144
-22
lines changed

compiler/src/dotty/tools/backend/jvm/AsmUtils.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools
22
package backend
33
package jvm
44

5-
import scala.tools.asm.tree.{AbstractInsnNode, ClassNode, MethodNode}
5+
import scala.tools.asm.tree.{AbstractInsnNode}
66
import java.io.PrintWriter
77
import scala.tools.asm.util.{TraceClassVisitor, TraceMethodVisitor, Textifier}
88
import scala.tools.asm.ClassReader
@@ -31,7 +31,7 @@ object AsmUtils {
3131
final val traceSerializedClassEnabled = false
3232
final val traceSerializedClassPattern = ""
3333

34-
def traceMethod(mnode: MethodNode): Unit = {
34+
def traceMethod(mnode: MethodNode1): Unit = {
3535
println(s"Bytecode for method ${mnode.name}")
3636
val p = new Textifier
3737
val tracer = new TraceMethodVisitor(p)
@@ -41,7 +41,7 @@ object AsmUtils {
4141
w.flush()
4242
}
4343

44-
def traceClass(cnode: ClassNode): Unit = {
44+
def traceClass(cnode: ClassNode1): Unit = {
4545
println(s"Bytecode for class ${cnode.name}")
4646
val w = new PrintWriter(System.out)
4747
cnode.accept(new TraceClassVisitor(w))
@@ -50,8 +50,8 @@ object AsmUtils {
5050

5151
def traceClass(bytes: Array[Byte]): Unit = traceClass(readClass(bytes))
5252

53-
def readClass(bytes: Array[Byte]): ClassNode = {
54-
val node = new ClassNode()
53+
def readClass(bytes: Array[Byte]): ClassNode1 = {
54+
val node = new ClassNode1()
5555
new ClassReader(bytes).accept(node, 0)
5656
node
5757
}

compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package backend
33
package jvm
44

55
import scala.tools.asm
6+
import scala.tools.asm.ClassWriter
67
import scala.collection.mutable
78
import dotty.tools.io.AbstractFile
89

@@ -126,6 +127,20 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
126127
}
127128
}
128129

130+
/*
131+
* can-multi-thread
132+
*/
133+
def createJAttribute(name: String, b: Array[Byte], offset: Int, len: Int): asm.Attribute = {
134+
new asm.Attribute(name) {
135+
override def write(classWriter: ClassWriter, code: Array[Byte],
136+
codeLength: Int, maxStack: Int, maxLocals: Int): asm.ByteVector = {
137+
val byteVector = new asm.ByteVector(len)
138+
byteVector.putByteArray(b, offset, len)
139+
byteVector
140+
}
141+
}
142+
}
143+
129144
/*
130145
* Custom attribute (JVMS 4.7.1) "ScalaSig" used as marker only
131146
* i.e., the pickle is contained in a custom annotation, see:
@@ -151,15 +166,6 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
151166
vp
152167
}
153168

154-
/*
155-
* can-multi-thread
156-
*/
157-
def createJAttribute(name: String, b: Array[Byte], offset: Int, len: Int): asm.Attribute = {
158-
val dest = new Array[Byte](len)
159-
System.arraycopy(b, offset, dest, 0, len)
160-
new asm.CustomAttr(name, dest)
161-
}
162-
163169
/*
164170
* can-multi-thread
165171
*/

compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
5353
final val MaximumJvmParameters = 254
5454

5555
// current class
56-
var cnode: asm.tree.ClassNode = null
56+
var cnode: ClassNode1 = null
5757
var thisName: String = null // the internal name of the class being emitted
5858

5959
var claszSymbol: Symbol = null
@@ -88,7 +88,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
8888
isCZStaticModule = claszSymbol.isStaticModuleClass
8989
thisName = internalName(claszSymbol)
9090

91-
cnode = new asm.tree.ClassNode()
91+
cnode = new ClassNode1()
9292

9393
initJClass(cnode)
9494

@@ -245,7 +245,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
245245
} // end of method addClassFields()
246246

247247
// current method
248-
var mnode: asm.tree.MethodNode = null
248+
var mnode: MethodNode1 = null
249249
var jMethodName: String = null
250250
var isMethSymStaticCtor = false
251251
var returnType: BType = null
@@ -524,7 +524,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
524524
mdesc,
525525
jgensig,
526526
mkArrayS(thrownExceptions)
527-
).asInstanceOf[asm.tree.MethodNode]
527+
).asInstanceOf[MethodNode1]
528528

529529
// TODO param names: (m.params map (p => javaName(p.sym)))
530530

compiler/src/dotty/tools/backend/jvm/BytecodeWriters.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ trait BytecodeWriters {
8484
private def emitAsmp(jclassBytes: Array[Byte], asmpFile: dotty.tools.io.File): Unit = {
8585
val pw = asmpFile.printWriter()
8686
try {
87-
val cnode = new asm.tree.ClassNode()
87+
val cnode = new ClassNode1()
8888
val cr = new asm.ClassReader(jclassBytes)
8989
cr.accept(cnode, 0)
9090
val trace = new scala.tools.asm.util.TraceClassVisitor(new java.io.PrintWriter(new java.io.StringWriter()))
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package dotty.tools.backend.jvm;
14+
15+
import scala.tools.asm.MethodVisitor;
16+
import scala.tools.asm.Opcodes;
17+
import scala.tools.asm.tree.ClassNode;
18+
import scala.tools.asm.tree.MethodNode;
19+
20+
/**
21+
* A subclass of {@link ClassNode} to customize the representation of
22+
* label nodes with {@link LabelNode1}.
23+
*/
24+
public class ClassNode1 extends ClassNode {
25+
public ClassNode1() {
26+
this(Opcodes.ASM6);
27+
}
28+
29+
public ClassNode1(int api) {
30+
super(api);
31+
}
32+
33+
@Override
34+
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
35+
MethodNode method = new MethodNode1(access, name, descriptor, signature, exceptions);
36+
methods.add(method);
37+
return method;
38+
}
39+
}

compiler/src/dotty/tools/backend/jvm/GenBCode.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import dotty.tools.dotc.core.Phases.Phase
77

88
import scala.collection.mutable
99
import scala.collection.JavaConverters._
10-
import scala.tools.asm.CustomAttr
1110
import dotty.tools.dotc.transform.SymUtils._
1211
import dotty.tools.dotc.interfaces
1312
import dotty.tools.dotc.util.SourceFile
@@ -240,7 +239,7 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
240239
getFileForClassfile(outF, store.name, ".hasTasty")
241240
binary
242241
}
243-
val dataAttr = new CustomAttr(nme.TASTYATTR.mangledString, tasty)
242+
val dataAttr = createJAttribute(nme.TASTYATTR.mangledString, tasty, 0, tasty.length)
244243
store.visitAttribute(dataAttr)
245244
}
246245

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package dotty.tools.backend.jvm;
14+
15+
import scala.tools.asm.Label;
16+
import scala.tools.asm.tree.ClassNode;
17+
import scala.tools.asm.tree.LabelNode;
18+
19+
/**
20+
* A subclass of {@link LabelNode} to add user-definable flags.
21+
*/
22+
public class LabelNode1 extends LabelNode {
23+
public LabelNode1() {
24+
}
25+
26+
public LabelNode1(Label label) {
27+
super(label);
28+
}
29+
30+
public int flags;
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package dotty.tools.backend.jvm;
14+
15+
import scala.tools.asm.Label;
16+
import scala.tools.asm.Opcodes;
17+
import scala.tools.asm.tree.LabelNode;
18+
import scala.tools.asm.tree.MethodNode;
19+
/**
20+
* A subclass of {@link MethodNode} to customize the representation of
21+
* label nodes with {@link LabelNode1}.
22+
*/
23+
public class MethodNode1 extends MethodNode {
24+
public MethodNode1(int api, int access, String name, String descriptor, String signature, String[] exceptions) {
25+
super(api, access, name, descriptor, signature, exceptions);
26+
}
27+
28+
public MethodNode1(int access, String name, String descriptor, String signature, String[] exceptions) {
29+
this(Opcodes.ASM6, access, name, descriptor, signature, exceptions);
30+
}
31+
32+
public MethodNode1(int api) {
33+
super(api);
34+
}
35+
36+
public MethodNode1() {
37+
this(Opcodes.ASM6);
38+
}
39+
40+
@Override
41+
protected LabelNode getLabelNode(Label label) {
42+
if (!(label.info instanceof LabelNode)) {
43+
label.info = new LabelNode1(label);
44+
}
45+
return (LabelNode) label.info;
46+
}
47+
}

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ object Build {
481481

482482
// get libraries onboard
483483
libraryDependencies ++= Seq(
484-
"org.scala-lang.modules" % "scala-asm" % "6.0.0-scala-1", // used by the backend
484+
"org.scala-lang.modules" % "scala-asm" % "7.0.0-scala-1", // used by the backend
485485
// FIXME: Not needed, but should be on the compiler CP
486486
("org.scala-lang.modules" %% "scala-xml" % "1.1.0").withDottyCompat(scalaVersion.value),
487487
"org.scala-lang" % "scala-library" % scalacVersion % "test",

0 commit comments

Comments
 (0)