Skip to content

Commit

Permalink
Simplify InvokeStyle
Browse files Browse the repository at this point in the history
Port of 7a12e81
  • Loading branch information
nicolasstucki committed Sep 15, 2017
1 parent cee714f commit d90cbcb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 79 deletions.
34 changes: 16 additions & 18 deletions src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import scala.annotation.switch

import scala.tools.asm
import scala.tools.asm.Label
import scala.tools.nsc.backend.jvm.BCodeHelpers.InvokeStyle

/*
*
Expand All @@ -33,7 +34,6 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
abstract class PlainBodyBuilder(cunit: CompilationUnit) extends PlainSkelBuilder(cunit) {

import Primitives.TestOp
import Opcodes.InvokeStyle

/* If the selector type has a member with the right name,
* it is the host class; otherwise the symbol's owner.
Expand Down Expand Up @@ -654,7 +654,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
// therefore, we can ignore this fact, and generate code that leaves nothing
// on the stack (contrary to what the type in the AST says).
case Apply(fun @ Select(Super(_, mix), _), args) =>
val invokeStyle = Opcodes.SuperCall(mix.mangledString)
val invokeStyle = InvokeStyle.Super
// if (fun.symbol.isConstructor) Static(true) else SuperCall(mix);
mnode.visitVarInsn(asm.Opcodes.ALOAD, 0)
genLoadArguments(args, paramTKs(app))
Expand All @@ -681,7 +681,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
mnode.visitTypeInsn(asm.Opcodes.NEW, rt.internalName)
bc dup generatedType
genLoadArguments(args, paramTKs(app))
genCallMethod(ctor, Opcodes.Static(onInstance = true))
genCallMethod(ctor, InvokeStyle.Special)

case _ =>
abort(s"Cannot instantiate $tpt of kind: $generatedType")
Expand Down Expand Up @@ -714,9 +714,9 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
def genNormalMethodCall(): Unit = {

val invokeStyle =
if (sym.isStaticMember) Opcodes.Static(onInstance = false)
else if (sym.isPrivate || sym.isClassConstructor) Opcodes.Static(onInstance = true)
else Opcodes.Dynamic;
if (sym.isStaticMember) InvokeStyle.Static
else if (sym.isPrivate || sym.isClassConstructor) InvokeStyle.Special
else InvokeStyle.Virtual

if (invokeStyle.hasInstance) {
genLoadQualifier(fun)
Expand All @@ -728,7 +728,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
var hostClass: Symbol = null

fun match {
case Select(qual, _) if isArrayClone(fun) && invokeStyle.isDynamic =>
case Select(qual, _) if isArrayClone(fun) && invokeStyle.isVirtual =>
val targetTypeKind = tpeTK(qual)
val target: String = targetTypeKind.asRefBType.classOrArrayType
bc.invokevirtual(target, "clone", "()Ljava/lang/Object;")
Expand Down Expand Up @@ -1069,7 +1069,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
// Optimization for expressions of the form "" + x. We can avoid the StringBuilder.
case List(Literal(Constant("")), arg) =>
genLoad(arg, ObjectReference)
genCallMethod(String_valueOf, Opcodes.Static(onInstance = false))
genCallMethod(String_valueOf, InvokeStyle.Static)

case concatenations =>
bc.genStartConcat
Expand Down Expand Up @@ -1101,7 +1101,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
// whether to reference the type of the receiver or
// the type of the method owner
val useMethodOwner = (
style != Opcodes.Dynamic
!style.isVirtual
|| hostSymbol.isBottomClass
|| methodOwner == ObjectClass
)
Expand All @@ -1128,11 +1128,9 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
}
}

if (style.isStatic) {
if (style.hasInstance) { bc.invokespecial (jowner, jname, mdescr) }
else { bc.invokestatic (jowner, jname, mdescr) }
}
else if (style.isDynamic) {
if (style.isStatic) { bc.invokestatic (jowner, jname, mdescr) }
else if (style.isSpecial) { bc.invokespecial (jowner, jname, mdescr) }
else if (style.isVirtual) {
if (needsInterfaceCall(receiver)) { bc.invokeinterface(jowner, jname, mdescr) }
else { bc.invokevirtual (jowner, jname, mdescr) }
}
Expand All @@ -1148,7 +1146,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
def genScalaHash(tree: Tree): BType = {
genLoadModule(ScalaRunTimeModule) // TODO why load ScalaRunTimeModule if ## has InvokeStyle of Static(false) ?
genLoad(tree, ObjectReference)
genCallMethod(hashMethodSym, Opcodes.Static(onInstance = false))
genCallMethod(hashMethodSym, InvokeStyle.Static)

INT
}
Expand Down Expand Up @@ -1334,7 +1332,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
}
genLoad(l, ObjectReference)
genLoad(r, ObjectReference)
genCallMethod(equalsMethod, Opcodes.Static(onInstance = false))
genCallMethod(equalsMethod, InvokeStyle.Static)
genCZJUMP(success, failure, Primitives.NE, BOOL)
}
else {
Expand All @@ -1350,7 +1348,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
// SI-7852 Avoid null check if L is statically non-null.
genLoad(l, ObjectReference)
genLoad(r, ObjectReference)
genCallMethod(Object_equals, Opcodes.Dynamic)
genCallMethod(Object_equals, InvokeStyle.Virtual)
genCZJUMP(success, failure, Primitives.NE, BOOL)
} else {
// l == r -> if (l eq null) r eq null else l.equals(r)
Expand All @@ -1371,7 +1369,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {

markProgramPoint(lNonNull)
locals.load(eqEqTempLocal)
genCallMethod(Object_equals, Opcodes.Dynamic)
genCallMethod(Object_equals, InvokeStyle.Virtual)
genCZJUMP(success, failure, Primitives.NE, BOOL)
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,23 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {

} // end of trait JAndroidBuilder
}

object BCodeHelpers {

class InvokeStyle(val style: Int) extends AnyVal {
import InvokeStyle._
def isVirtual: Boolean = this == Virtual
def isStatic : Boolean = this == Static
def isSpecial: Boolean = this == Special
def isSuper : Boolean = this == Super

def hasInstance = this != Static
}

object InvokeStyle {
val Virtual = new InvokeStyle(0) // InvokeVirtual or InvokeInterface
val Static = new InvokeStyle(1) // InvokeStatic
val Special = new InvokeStyle(2) // InvokeSpecial (private methods, constructors)
val Super = new InvokeStyle(3) // InvokeSpecial (super calls)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ trait BCodeIdiomatic {
def jmethod: asm.MethodVisitor

import asm.Opcodes;
import backend.jvm.Opcodes._

final def emit(opc: Int): Unit = { jmethod.visitInsn(opc) }

Expand Down
60 changes: 0 additions & 60 deletions src/compiler/scala/tools/nsc/backend/jvm/Opcodes.scala

This file was deleted.

0 comments on commit d90cbcb

Please sign in to comment.