summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala3
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala1
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala4
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala1
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala5
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala3
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/opt/ByteCodeRepository.scala11
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala125
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/opt/Inliner.scala203
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/opt/LocalOpt.scala27
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/opt/OptimizerReporting.scala10
-rw-r--r--test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala193
12 files changed, 534 insertions, 52 deletions
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
index 062daa4eac..1b3f124dd8 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
@@ -12,6 +12,7 @@ package jvm
import scala.annotation.switch
import scala.tools.asm
+import GenBCode._
/*
*
@@ -613,7 +614,7 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
*/
for (i <- args.length until dims) elemKind = ArrayBType(elemKind)
}
- (argsSize : @switch) match {
+ argsSize match {
case 1 => bc newarray elemKind
case _ =>
val descr = ('[' * argsSize) + elemKind.descriptor // denotes the same as: arrayN(elemKind, argsSize).descriptor
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
index ccee230191..e366bbabb8 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
@@ -10,6 +10,7 @@ package backend.jvm
import scala.tools.asm
import scala.collection.mutable
import scala.tools.nsc.io.AbstractFile
+import GenBCode._
/*
* Traits encapsulating functionality to convert Scala AST Trees into ASM ClassNodes.
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala
index c3db28151b..c743ebd16f 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala
@@ -10,6 +10,7 @@ package backend.jvm
import scala.tools.asm
import scala.annotation.switch
import scala.collection.mutable
+import GenBCode._
/*
* A high-level facade to the ASM API for bytecode generation.
@@ -42,9 +43,6 @@ abstract class BCodeIdiomatic extends SubComponent {
val StringBuilderClassName = "scala/collection/mutable/StringBuilder"
- val CLASS_CONSTRUCTOR_NAME = "<clinit>"
- val INSTANCE_CONSTRUCTOR_NAME = "<init>"
-
val EMPTY_STRING_ARRAY = Array.empty[String]
val EMPTY_INT_ARRAY = Array.empty[Int]
val EMPTY_LABEL_ARRAY = Array.empty[asm.Label]
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala
index 142c901c21..48df4e1121 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala
@@ -16,6 +16,7 @@ import scala.annotation.switch
import scala.tools.asm
import scala.tools.asm.util.{TraceMethodVisitor, ASMifier}
import java.io.PrintWriter
+import GenBCode._
/*
*
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala b/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala
index ff30631c10..fe4c4794a9 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala
@@ -11,6 +11,7 @@ import scala.tools.asm
import asm.Opcodes
import scala.tools.asm.tree.{InnerClassNode, ClassNode}
import opt.{ByteCodeRepository, Inliner}
+import OptimizerReporting._
import scala.collection.convert.decorateAsScala._
/**
@@ -273,7 +274,7 @@ abstract class BTypes {
ObjectReference
case _: MethodBType =>
- throw new AssertionError(s"unexpected method type when computing maxType: $this")
+ assertionError(s"unexpected method type when computing maxType: $this")
}
/**
@@ -364,7 +365,7 @@ abstract class BTypes {
*/
final def maxValueType(other: BType): BType = {
- def uncomparable: Nothing = throw new AssertionError(s"Cannot compute maxValueType: $this, $other")
+ def uncomparable: Nothing = assertionError(s"Cannot compute maxValueType: $this, $other")
if (!other.isPrimitive && !other.isNothingType) uncomparable
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala
index d5e95c47cf..9b3bd7648d 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala
@@ -410,4 +410,7 @@ object GenBCode {
final val PublicStatic = asm.Opcodes.ACC_PUBLIC | asm.Opcodes.ACC_STATIC
final val PublicStaticFinal = asm.Opcodes.ACC_PUBLIC | asm.Opcodes.ACC_STATIC | asm.Opcodes.ACC_FINAL
+
+ val CLASS_CONSTRUCTOR_NAME = "<clinit>"
+ val INSTANCE_CONSTRUCTOR_NAME = "<init>"
}
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/ByteCodeRepository.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/ByteCodeRepository.scala
index 9e56f25888..b3ac06877b 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/opt/ByteCodeRepository.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/ByteCodeRepository.scala
@@ -13,6 +13,7 @@ import scala.collection.convert.decorateAsScala._
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.util.ClassFileLookup
import OptimizerReporting._
+import BytecodeUtils._
import ByteCodeRepository._
import BTypes.InternalName
@@ -93,16 +94,6 @@ class ByteCodeRepository(val classPath: ClassFileLookup[AbstractFile], val class
inlineFailure(s"Class file for class $fullName not found.")
}
}
-
- private def removeLineNumberNodes(classNode: ClassNode): Unit = {
- for (method <- classNode.methods.asScala) {
- val iter = method.instructions.iterator()
- while (iter.hasNext) iter.next() match {
- case _: LineNumberNode => iter.remove()
- case _ =>
- }
- }
- }
}
object ByteCodeRepository {
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala
index 6b4047c0a7..4cff92d38b 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala
@@ -10,9 +10,14 @@ package opt
import scala.annotation.{tailrec, switch}
import scala.collection.mutable
import scala.reflect.internal.util.Collections._
-import scala.tools.asm.Opcodes
+import scala.tools.asm.tree.analysis._
+import scala.tools.asm.{MethodWriter, ClassWriter, Label, Opcodes}
import scala.tools.asm.tree._
import scala.collection.convert.decorateAsScala._
+import GenBCode._
+import scala.collection.convert.decorateAsScala._
+import scala.collection.convert.decorateAsJava._
+import scala.tools.nsc.backend.jvm.BTypes._
object BytecodeUtils {
@@ -68,6 +73,16 @@ object BytecodeUtils {
def isExecutable(instruction: AbstractInsnNode): Boolean = instruction.getOpcode >= 0
+ def isConstructor(methodNode: MethodNode): Boolean = {
+ methodNode.name == INSTANCE_CONSTRUCTOR_NAME || methodNode.name == CLASS_CONSTRUCTOR_NAME
+ }
+
+ def isStaticMethod(methodNode: MethodNode): Boolean = (methodNode.access & Opcodes.ACC_STATIC) != 0
+
+ def isAbstractMethod(methodNode: MethodNode): Boolean = (methodNode.access & Opcodes.ACC_ABSTRACT) != 0
+
+ def isSynchronizedMethod(methodNode: MethodNode): Boolean = (methodNode.access & Opcodes.ACC_SYNCHRONIZED) != 0
+
def nextExecutableInstruction(instruction: AbstractInsnNode, alsoKeep: AbstractInsnNode => Boolean = Set()): Option[AbstractInsnNode] = {
var result = instruction
do { result = result.getNext }
@@ -181,4 +196,112 @@ object BytecodeUtils {
if (handler.end == from) handler.end = to
}
}
+
+ /**
+ * In order to run an Analyzer, the maxLocals / maxStack fields need to be available. The ASM
+ * framework only computes these values during bytecode generation.
+ *
+ * Since there's currently no better way, we run a bytecode generator on the method and extract
+ * the computed values. This required changes to the ASM codebase:
+ * - the [[MethodWriter]] class was made public
+ * - accessors for maxLocals / maxStack were added to the MethodWriter class
+ *
+ * We could probably make this faster (and allocate less memory) by hacking the ASM framework
+ * more: create a subclass of MethodWriter with a /dev/null byteVector. Another option would be
+ * to create a separate visitor for computing those values, duplicating the functionality from the
+ * MethodWriter.
+ */
+ def computeMaxLocalsMaxStack(method: MethodNode) {
+ val cw = new ClassWriter(ClassWriter.COMPUTE_MAXS)
+ val excs = method.exceptions.asScala.toArray
+ val mw = cw.visitMethod(method.access, method.name, method.desc, method.signature, excs).asInstanceOf[MethodWriter]
+ method.accept(mw)
+ method.maxLocals = mw.getMaxLocals
+ method.maxStack = mw.getMaxStack
+ }
+
+ def removeLineNumberNodes(classNode: ClassNode): Unit = {
+ for (m <- classNode.methods.asScala) removeLineNumberNodes(m.instructions)
+ }
+
+ def removeLineNumberNodes(instructions: InsnList): Unit = {
+ val iter = instructions.iterator()
+ while (iter.hasNext) iter.next() match {
+ case _: LineNumberNode => iter.remove()
+ case _ =>
+ }
+ }
+
+ def cloneLabels(methodNode: MethodNode): Map[LabelNode, LabelNode] = {
+ methodNode.instructions.iterator().asScala.collect({
+ case labelNode: LabelNode => (labelNode, newLabelNode)
+ }).toMap
+ }
+
+ /**
+ * Create a new [[LabelNode]] with a correctly associated [[Label]].
+ */
+ def newLabelNode: LabelNode = {
+ val label = new Label
+ val labelNode = new LabelNode(label)
+ label.info = labelNode
+ labelNode
+ }
+
+ /**
+ * Clone the instructions in `methodNode` into a new [[InsnList]], mapping labels according to
+ * the `labelMap`. Returns the new instruction list and a map from old to new instructions.
+ */
+ def cloneInstructions(methodNode: MethodNode, labelMap: Map[LabelNode, LabelNode]): (InsnList, Map[AbstractInsnNode, AbstractInsnNode]) = {
+ val javaLabelMap = labelMap.asJava
+ val result = new InsnList
+ var map = Map.empty[AbstractInsnNode, AbstractInsnNode]
+ for (ins <- methodNode.instructions.iterator.asScala) {
+ val cloned = ins.clone(javaLabelMap)
+ result add cloned
+ map += ((ins, cloned))
+ }
+ (result, map)
+ }
+
+ /**
+ * Clone the local variable descriptors of `methodNode` and map their `start` and `end` labels
+ * according to the `labelMap`.
+ */
+ def cloneLocalVariableNodes(methodNode: MethodNode, labelMap: Map[LabelNode, LabelNode], prefix: String): List[LocalVariableNode] = {
+ methodNode.localVariables.iterator().asScala.map(localVariable => new LocalVariableNode(
+ prefix + localVariable.name,
+ localVariable.desc,
+ localVariable.signature,
+ labelMap(localVariable.start),
+ labelMap(localVariable.end),
+ localVariable.index
+ )).toList
+ }
+
+ /**
+ * Clone the local try/catch blocks of `methodNode` and map their `start` and `end` and `handler`
+ * labels according to the `labelMap`.
+ */
+ def cloneTryCatchBlockNodes(methodNode: MethodNode, labelMap: Map[LabelNode, LabelNode]): List[TryCatchBlockNode] = {
+ methodNode.tryCatchBlocks.iterator().asScala.map(tryCatch => new TryCatchBlockNode(
+ labelMap(tryCatch.start),
+ labelMap(tryCatch.end),
+ labelMap(tryCatch.handler),
+ tryCatch.`type`
+ )).toList
+ }
+
+ class BasicAnalyzer(methodNode: MethodNode, classInternalName: InternalName) {
+ val analyzer = new Analyzer[BasicValue](new BasicInterpreter)
+ analyzer.analyze(classInternalName, methodNode)
+ def frameAt(instruction: AbstractInsnNode): Frame[BasicValue] = analyzer.getFrames()(methodNode.instructions.indexOf(instruction))
+ }
+
+ implicit class `frame extensions`[V <: Value](val frame: Frame[V]) extends AnyVal {
+ def peekDown(n: Int): V = {
+ val topIndex = frame.getStackSize - 1
+ frame.getStack(topIndex - n)
+ }
+ }
}
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/Inliner.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/Inliner.scala
index 6e5e03f730..f964b5b25d 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/opt/Inliner.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/Inliner.scala
@@ -8,15 +8,206 @@ package backend.jvm
package opt
import scala.tools.asm
-import asm.Opcodes
+import asm.Opcodes._
import asm.tree._
import scala.collection.convert.decorateAsScala._
+import scala.collection.convert.decorateAsJava._
+import AsmUtils._
+import BytecodeUtils._
import OptimizerReporting._
+import scala.tools.asm.tree.analysis._
class Inliner[BT <: BTypes](val btypes: BT) {
import btypes._
import btypes.byteCodeRepository
+ /**
+ * Copy and adapt the instructions of a method to a callsite.
+ *
+ * Preconditions:
+ * - The maxLocals and maxStack values of the callsite method are correctly computed
+ * - The callsite method contains no unreachable basic blocks, i.e., running an [[Analyzer]]
+ * does not produce any `null` frames
+ *
+ * @param callsiteInstruction The invocation instruction
+ * @param callsiteStackHeight The stack height at the callsite
+ * @param callsiteMethod The method in which the invocation occurs
+ * @param callsiteClass The class in which the callsite method is defined
+ * @param callee The invoked method
+ * @param calleeDeclarationClass The class in which the invoked method is defined
+ * @param receiverKnownNotNull `true` if the receiver is known to be non-null
+ * @param keepLineNumbers `true` if LineNumberNodes should be copied to the call site
+ * @return `Some(message)` if inlining cannot be performed, `None` otherwise
+ */
+ def inline(callsiteInstruction: MethodInsnNode, callsiteStackHeight: Int, callsiteMethod: MethodNode, callsiteClass: ClassBType,
+ callee: MethodNode, calleeDeclarationClass: ClassBType,
+ receiverKnownNotNull: Boolean, keepLineNumbers: Boolean): Option[String] = {
+ canInline(callsiteInstruction, callsiteStackHeight, callsiteMethod, callsiteClass, callee, calleeDeclarationClass) orElse {
+ // New labels for the cloned instructions
+ val labelsMap = cloneLabels(callee)
+ val (clonedInstructions, instructionMap) = cloneInstructions(callee, labelsMap)
+ if (!keepLineNumbers) {
+ removeLineNumberNodes(clonedInstructions)
+ }
+
+ // local vars in the callee are shifted by the number of locals at the callsite
+ val localVarShift = callsiteMethod.maxLocals
+ clonedInstructions.iterator.asScala foreach {
+ case varInstruction: VarInsnNode => varInstruction.`var` += localVarShift
+ case _ => ()
+ }
+
+ // add a STORE instruction for each expected argument, including for THIS instance if any
+ val argStores = new InsnList
+ var nextLocalIndex = callsiteMethod.maxLocals
+ if (!isStaticMethod(callee)) {
+ if (!receiverKnownNotNull) {
+ argStores.add(new InsnNode(DUP))
+ val nonNullLabel = newLabelNode
+ argStores.add(new JumpInsnNode(IFNONNULL, nonNullLabel))
+ argStores.add(new InsnNode(ACONST_NULL))
+ argStores.add(new InsnNode(ATHROW))
+ argStores.add(nonNullLabel)
+ }
+ argStores.add(new VarInsnNode(ASTORE, nextLocalIndex))
+ nextLocalIndex += 1
+ }
+
+ // We just use an asm.Type here, no need to create the MethodBType.
+ val calleAsmType = asm.Type.getMethodType(callee.desc)
+
+ for(argTp <- calleAsmType.getArgumentTypes) {
+ val opc = argTp.getOpcode(ISTORE) // returns the correct xSTORE instruction for argTp
+ argStores.insert(new VarInsnNode(opc, nextLocalIndex)) // "insert" is "prepend" - the last argument is on the top of the stack
+ nextLocalIndex += argTp.getSize
+ }
+
+ clonedInstructions.insert(argStores)
+
+ // label for the exit of the inlined functions. xRETURNs are rplaced by GOTOs to this label.
+ val postCallLabel = newLabelNode
+ clonedInstructions.add(postCallLabel)
+
+ // replace xRETURNs:
+ // - store the return value (if any)
+ // - clear the stack of the inlined method (insert DROPs)
+ // - load the return value
+ // - GOTO postCallLabel
+
+ val returnType = calleAsmType.getReturnType
+ val hasReturnValue = returnType.getSort != asm.Type.VOID
+ val returnValueIndex = callsiteMethod.maxLocals + callee.maxLocals
+ nextLocalIndex += returnType.getSize
+
+ def returnValueStore(returnInstruction: AbstractInsnNode) = {
+ val opc = returnInstruction.getOpcode match {
+ case IRETURN => ISTORE
+ case LRETURN => LSTORE
+ case FRETURN => FSTORE
+ case DRETURN => DSTORE
+ case ARETURN => ASTORE
+ }
+ new VarInsnNode(opc, returnValueIndex)
+ }
+
+ // We run an interpreter to know the stack height at each xRETURN instruction and the sizes
+ // of the values on the stack.
+ val analyzer = new BasicAnalyzer(callee, calleeDeclarationClass.internalName)
+
+ for (originalReturn <- callee.instructions.iterator().asScala if isReturn(originalReturn)) {
+ val frame = analyzer.frameAt(originalReturn)
+ var stackHeight = frame.getStackSize
+
+ val inlinedReturn = instructionMap(originalReturn)
+ val returnReplacement = new InsnList
+
+ def drop(slot: Int) = returnReplacement add getPop(frame.peekDown(slot).getSize)
+
+ // for non-void methods, store the stack top into the return local variable
+ if (hasReturnValue) {
+ returnReplacement add returnValueStore(originalReturn)
+ stackHeight -= 1
+ }
+
+ // drop the rest of the stack
+ for (i <- 0 until stackHeight) drop(i)
+
+ returnReplacement add new JumpInsnNode(GOTO, postCallLabel)
+ clonedInstructions.insert(inlinedReturn, returnReplacement)
+ clonedInstructions.remove(inlinedReturn)
+ }
+
+ // Load instruction for the return value
+ if (hasReturnValue) {
+ val retVarLoad = {
+ val opc = returnType.getOpcode(ILOAD)
+ new VarInsnNode(opc, returnValueIndex)
+ }
+ clonedInstructions.insert(postCallLabel, retVarLoad)
+ }
+
+ callsiteMethod.instructions.insert(callsiteInstruction, clonedInstructions)
+ callsiteMethod.instructions.remove(callsiteInstruction)
+
+ callsiteMethod.localVariables.addAll(cloneLocalVariableNodes(callee, labelsMap, callee.name + "_").asJava)
+ callsiteMethod.tryCatchBlocks.addAll(cloneTryCatchBlockNodes(callee, labelsMap).asJava)
+
+ callsiteMethod.maxLocals += returnType.getSize + callee.maxLocals
+ callsiteMethod.maxStack = math.max(callsiteMethod.maxStack, callee.maxStack + callsiteStackHeight)
+
+ None
+ }
+ }
+
+ /**
+ * Check whether an inling can be performed. Parmeters are described in method [[inline]].
+ * @return `Some(message)` if inlining cannot be performed, `None` otherwise
+ */
+ def canInline(callsiteInstruction: MethodInsnNode, callsiteStackHeight: Int, callsiteMethod: MethodNode, callsiteClass: ClassBType,
+ callee: MethodNode, calleeDeclarationClass: ClassBType): Option[String] = {
+
+ def calleeDesc = s"${callee.name} of type ${callee.desc} in ${calleeDeclarationClass.internalName}"
+ def methodMismatch = s"Wrong method node for inlining ${textify(callsiteInstruction)}: $calleeDesc"
+ assert(callsiteInstruction.name == callee.name, methodMismatch)
+ assert(callsiteInstruction.desc == callee.desc, methodMismatch)
+ assert(!isConstructor(callee), s"Constructors cannot be inlined: $calleeDesc")
+ assert(!BytecodeUtils.isAbstractMethod(callee), s"Callee is abstract: $calleeDesc")
+ assert(callsiteMethod.instructions.contains(callsiteInstruction), s"Callsite ${textify(callsiteInstruction)} is not an instruction of $calleeDesc")
+
+ // When an exception is thrown, the stack is cleared before jumping to the handler. When
+ // inlining a method that catches an exception, all values that were on the stack before the
+ // call (in addition to the arguments) would be cleared (SI-6157). So we don't inline methods
+ // with handlers in case there are values on the stack.
+ // Alternatively, we could save all stack values below the method arguments into locals, but
+ // that would be inefficient: we'd need to pop all parameters, save the values, and push the
+ // parameters back for the (inlined) invocation. Similarly for the result after the call.
+ def stackHasNonParameters: Boolean = {
+ val expectedArgs = asm.Type.getArgumentTypes(callsiteInstruction.desc).length + (callsiteInstruction.getOpcode match {
+ case INVOKEVIRTUAL | INVOKESPECIAL | INVOKEINTERFACE => 1
+ case INVOKESTATIC => 0
+ case INVOKEDYNAMIC =>
+ assertionError(s"Unexpected opcode, cannot inline ${textify(callsiteInstruction)}")
+ })
+ callsiteStackHeight > expectedArgs
+ }
+
+ if (isSynchronizedMethod(callee)) {
+ // Could be done by locking on the receiver, wrapping the inlined code in a try and unlocking
+ // in finally. But it's probably not worth the effort, scala never emits synchronized methods.
+ Some(s"Method ${methodSignature(calleeDeclarationClass.internalName, callee)} is not inlined because it is synchronized")
+ } else if (!callee.tryCatchBlocks.isEmpty && stackHasNonParameters) {
+ Some(
+ s"""The operand stack at the callsite in ${methodSignature(callsiteClass.internalName, callsiteMethod)} contains more values than the
+ |arguments expected by the callee ${methodSignature(calleeDeclarationClass.internalName, callee)}. These values would be discarded
+ |when entering an exception handler declared in the inlined method.""".stripMargin
+ )
+ } else findIllegalAccess(callee.instructions, callsiteClass) map {
+ case illegalAccessIns =>
+ s"""The callee ${methodSignature(calleeDeclarationClass.internalName, callee)} contains the instruction ${AsmUtils.textify(illegalAccessIns)}
+ |that would cause an IllegalAccessError when inlined into class ${callsiteClass.internalName}""".stripMargin
+ }
+ }
+
def findIllegalAccess(instructions: InsnList, destinationClass: ClassBType): Option[AbstractInsnNode] = {
/**
@@ -57,14 +248,14 @@ class Inliner[BT <: BTypes](val btypes: BT) {
// TODO: B3 requires "same run-time package", which seems to be package + classloader (JMVS 5.3.). is the below ok?
def samePackageAsDestination = memberDeclClass.packageInternalName == destinationClass.packageInternalName
- val key = (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE) & memberFlags
+ val key = (ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE) & memberFlags
key match {
- case Opcodes.ACC_PUBLIC => // B1
+ case ACC_PUBLIC => // B1
true
- case Opcodes.ACC_PROTECTED => // B2
+ case ACC_PROTECTED => // B2
val condB2 = destinationClass.isSubtypeOf(memberDeclClass) && {
- val isStatic = (Opcodes.ACC_STATIC & memberFlags) != 0
+ val isStatic = (ACC_STATIC & memberFlags) != 0
isStatic || memberRefClass.isSubtypeOf(destinationClass) || destinationClass.isSubtypeOf(memberRefClass)
}
condB2 || samePackageAsDestination // B3 (protected)
@@ -72,7 +263,7 @@ class Inliner[BT <: BTypes](val btypes: BT) {
case 0 => // B3 (default access)
samePackageAsDestination
- case Opcodes.ACC_PRIVATE => // B4
+ case ACC_PRIVATE => // B4
memberDeclClass == destinationClass
}
}
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/LocalOpt.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/LocalOpt.scala
index 87ad715e4d..3a7250031a 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/opt/LocalOpt.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/LocalOpt.scala
@@ -8,7 +8,7 @@ package backend.jvm
package opt
import scala.annotation.switch
-import scala.tools.asm.{Opcodes, MethodWriter, ClassWriter}
+import scala.tools.asm.Opcodes
import scala.tools.asm.tree.analysis.{Analyzer, BasicValue, BasicInterpreter}
import scala.tools.asm.tree._
import scala.collection.convert.decorateAsScala._
@@ -73,7 +73,7 @@ class LocalOpt(settings: ScalaSettings) {
*
* Returns `true` if the bytecode of `method` was changed.
*/
- private def methodOptimizations(method: MethodNode, ownerClassName: String): Boolean = {
+ def methodOptimizations(method: MethodNode, ownerClassName: String): Boolean = {
if (method.instructions.size == 0) return false // fast path for abstract methods
// unreachable-code also removes unused local variable nodes and empty exception handlers.
@@ -319,29 +319,6 @@ class LocalOpt(settings: ScalaSettings) {
}
/**
- * In order to run an Analyzer, the maxLocals / maxStack fields need to be available. The ASM
- * framework only computes these values during bytecode generation.
- *
- * Since there's currently no better way, we run a bytecode generator on the method and extract
- * the computed values. This required changes to the ASM codebase:
- * - the [[MethodWriter]] class was made public
- * - accessors for maxLocals / maxStack were added to the MethodWriter class
- *
- * We could probably make this faster (and allocate less memory) by hacking the ASM framework
- * more: create a subclass of MethodWriter with a /dev/null byteVector. Another option would be
- * to create a separate visitor for computing those values, duplicating the functionality from the
- * MethodWriter.
- */
- private def computeMaxLocalsMaxStack(method: MethodNode) {
- val cw = new ClassWriter(ClassWriter.COMPUTE_MAXS)
- val excs = method.exceptions.asScala.toArray
- val mw = cw.visitMethod(method.access, method.name, method.desc, method.signature, excs).asInstanceOf[MethodWriter]
- method.accept(mw)
- method.maxLocals = mw.getMaxLocals
- method.maxStack = mw.getMaxStack
- }
-
- /**
* Removes LineNumberNodes that don't describe any executable instructions.
*
* This method expects (and asserts) that the `start` label of each LineNumberNode is the
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/OptimizerReporting.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/OptimizerReporting.scala
index 7002e43d98..a918e13534 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/opt/OptimizerReporting.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/OptimizerReporting.scala
@@ -8,17 +8,19 @@ package backend.jvm
import scala.tools.asm
import asm.tree._
+import scala.tools.nsc.backend.jvm.BTypes.InternalName
/**
* Reporting utilities used in the optimizer.
+ *
+ * TODO: move out of opt package, rename: it's already used outside the optimizer.
+ * Centralize backend reporting here.
*/
object OptimizerReporting {
- def methodSignature(className: String, methodName: String, methodDescriptor: String): String = {
- className + "::" + methodName + methodDescriptor
+ def methodSignature(classInternalName: InternalName, method: MethodNode): String = {
+ classInternalName + "::" + method.name + method.desc
}
- def methodSignature(className: String, method: MethodNode): String = methodSignature(className, method.name, method.desc)
-
def inlineFailure(reason: String): Nothing = MissingRequirementError.signal(reason)
def assertionError(message: String): Nothing = throw new AssertionError(message)
}
diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala
new file mode 100644
index 0000000000..1aa7bd1391
--- /dev/null
+++ b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala
@@ -0,0 +1,193 @@
+package scala.tools.nsc
+package backend.jvm
+package opt
+
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+import scala.tools.asm.Opcodes._
+import org.junit.Assert._
+
+import scala.tools.asm.tree._
+import scala.tools.asm.tree.analysis._
+import scala.tools.nsc.backend.jvm.opt.BytecodeUtils.BasicAnalyzer
+import scala.tools.testing.AssertUtil._
+
+import CodeGenTools._
+import scala.tools.partest.ASMConverters
+import ASMConverters._
+import AsmUtils._
+
+import scala.collection.convert.decorateAsScala._
+
+@RunWith(classOf[JUnit4])
+class InlinerTest {
+ val compiler = newCompiler(extraArgs = "-Ybackend:GenBCode -Yopt:l:none")
+ import compiler.genBCode.bTypes._
+ def addToRepo(cls: List[ClassNode]): List[ClassNode] = {
+ for (c <- cls) byteCodeRepository.classes(c.name) = (c, ByteCodeRepository.Classfile)
+ cls
+ }
+
+ // inline first invocation of f into g in class C
+ def inlineTest(code: String, mod: ClassNode => Unit = _ => ()): (MethodNode, Option[String]) = {
+ val List(cls) = addToRepo(compileClasses(compiler)(code))
+ mod(cls)
+ val clsBType = classBTypeFromParsedClassfile(cls.name)
+
+ val List(f, g) = cls.methods.asScala.filter(m => Set("f", "g")(m.name)).toList.sortBy(_.name)
+ val fCall = g.instructions.iterator.asScala.collect({ case i: MethodInsnNode if i.name == "f" => i }).next()
+
+ val analyzer = new BasicAnalyzer(g, clsBType.internalName)
+
+ val r = inliner.inline(
+ fCall,
+ analyzer.frameAt(fCall).getStackSize,
+ g,
+ clsBType,
+ f,
+ clsBType,
+ receiverKnownNotNull = true,
+ keepLineNumbers = true)
+ (g, r)
+ }
+
+ @Test
+ def simpleInlineOK(): Unit = {
+ val code =
+ """class C {
+ | def f = 1
+ | def g = f + f
+ |}
+ """.stripMargin
+
+ val (g, _) = inlineTest(code)
+
+ val gConv = convertMethod(g)
+ assertSameCode(gConv.instructions.dropNonOp,
+ List(
+ VarOp(ALOAD, 0), VarOp(ASTORE, 1), // store this
+ Op(ICONST_1), VarOp(ISTORE, 2), Jump(GOTO, Label(10)), // store return value
+ Label(10), VarOp(ILOAD, 2), // load return value
+ VarOp(ALOAD, 0), Invoke(INVOKEVIRTUAL, "C", "f", "()I", false), Op(IADD), Op(IRETURN)))
+
+ // line numbers are kept, so there's a line 2 (from the inlined f)
+ assert(gConv.instructions exists {
+ case LineNumber(2, _) => true
+ case _ => false
+ }, gConv.instructions.filter(_.isInstanceOf[LineNumber]))
+
+ assert(gConv.localVars.map(_.name).sorted == List("f_this", "this"), gConv.localVars)
+ assert(g.maxStack == 2 && g.maxLocals == 3, s"${g.maxLocals} - ${g.maxStack}")
+ }
+
+ @Test
+ def nothingTypedOK(): Unit = {
+ val code =
+ """class C {
+ | def f: Nothing = ???
+ | def g: Int = { f; 1 }
+ |}
+ """.stripMargin
+
+ // On the bytecode level, methods of type Nothing have return type Nothing$.
+ // This can be treated like any other result object.
+
+ // See also discussion around ATHROW in BCodeBodyBuilder
+
+ val (g, _) = inlineTest(code)
+ val expectedInlined = List(
+ VarOp(ALOAD, 0), VarOp(ASTORE, 1), // store this
+ Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"), Invoke(INVOKEVIRTUAL, "scala/Predef$", "$qmark$qmark$qmark", "()Lscala/runtime/Nothing$;", false)) // inlined call to ???
+
+ assertSameCode(convertMethod(g).instructions.dropNonOp.take(4), expectedInlined)
+
+ localOpt.methodOptimizations(g, "C")
+ assertSameCode(convertMethod(g).instructions.dropNonOp,
+ expectedInlined ++ List(VarOp(ASTORE, 2), VarOp(ALOAD, 2), Op(ATHROW)))
+ }
+
+ @Test
+ def synchronizedNoInline(): Unit = {
+ val code =
+ """class C {
+ | def f: Int = 0
+ | def g: Int = f
+ |}
+ """.stripMargin
+
+ val (_, can) = inlineTest(code, cls => {
+ val f = cls.methods.asScala.find(_.name == "f").get
+ f.access |= ACC_SYNCHRONIZED
+ })
+ assert(can.get contains "synchronized", can)
+ }
+
+ @Test
+ def tryCatchOK(): Unit = {
+ val code =
+ """class C {
+ | def f: Int = try { 1 } catch { case _: Exception => 2 }
+ | def g = f + 1
+ |}
+ """.stripMargin
+ val (_, r) = inlineTest(code)
+ assert(r.isEmpty, r)
+ }
+
+ @Test
+ def tryCatchNoInline(): Unit = {
+ // cannot inline f: there's a value on g's stack. if f throws and enters the handler, all values
+ // on the stack are removed, including the one of g's stack that we still need.
+ val code =
+ """class C {
+ | def f: Int = try { 1 } catch { case _: Exception => 2 }
+ | def g = println(f)
+ |}
+ """.stripMargin
+ val (_, r) = inlineTest(code)
+ assert(r.get contains "operand stack at the callsite", r)
+ }
+
+ @Test
+ def illegalAccessNoInline(): Unit = {
+ val code =
+ """package a {
+ | class C {
+ | private def f: Int = 0
+ | def g: Int = f
+ | }
+ |}
+ |package b {
+ | class D {
+ | def h(c: a.C): Int = c.g + 1
+ | }
+ |}
+ """.stripMargin
+
+ val List(c, d) = addToRepo(compileClasses(compiler)(code))
+
+ val cTp = classBTypeFromParsedClassfile(c.name)
+ val dTp = classBTypeFromParsedClassfile(d.name)
+
+ val g = c.methods.asScala.find(_.name == "g").get
+ val h = d.methods.asScala.find(_.name == "h").get
+ val gCall = h.instructions.iterator.asScala.collect({
+ case m: MethodInsnNode if m.name == "g" => m
+ }).next()
+
+ val analyzer = new BasicAnalyzer(h, dTp.internalName)
+
+ val r = inliner.inline(
+ gCall,
+ analyzer.frameAt(gCall).getStackSize,
+ h,
+ dTp,
+ g,
+ cTp,
+ receiverKnownNotNull = true,
+ keepLineNumbers = true)
+
+ assert(r.get contains "would cause an IllegalAccessError", r)
+ }
+}