summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@gmail.com>2015-02-21 16:04:21 +0100
committerLukas Rytz <lukas.rytz@gmail.com>2015-03-11 12:53:34 -0700
commit0d8b32469ec655f35a31843e1843b8a580e772d1 (patch)
tree0a58379ab346045b890105dd0e134fcd8b8c7640 /src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
parent4ffe9345ca6e611ff5a83a3fdabeb7658a2fce50 (diff)
downloadscala-0d8b32469ec655f35a31843e1843b8a580e772d1.tar.gz
scala-0d8b32469ec655f35a31843e1843b8a580e772d1.tar.bz2
scala-0d8b32469ec655f35a31843e1843b8a580e772d1.zip
Build a call graph for inlining decisions
Inlining decisions will be taken by analyzing the ASM bytecode. This commit adds tools to build a call graph representation that can be used for these decisions. The call graph is currently built by considering method descriptors of callsite instructions. It will become more precise by using data flow analyses.
Diffstat (limited to 'src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala')
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala127
1 files changed, 22 insertions, 105 deletions
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
index e366bbabb8..246d565987 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
@@ -332,125 +332,42 @@ abstract class BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
getClassBTypeAndRegisterInnerClass(classSym).internalName
}
- private def assertClassNotArray(sym: Symbol): Unit = {
- assert(sym.isClass, sym)
- assert(sym != definitions.ArrayClass || isCompilingArray, sym)
- }
-
- private def assertClassNotArrayNotPrimitive(sym: Symbol): Unit = {
- assertClassNotArray(sym)
- assert(!primitiveTypeMap.contains(sym) || isCompilingPrimitive, sym)
- }
-
/**
* The ClassBType for a class symbol. If the class is nested, the ClassBType is added to the
* innerClassBufferASM.
*
- * The class symbol scala.Nothing is mapped to the class scala.runtime.Nothing$. Similarly,
- * scala.Null is mapped to scala.runtime.Null$. This is because there exist no class files
- * for the Nothing / Null. If used for example as a parameter type, we use the runtime classes
- * in the classfile method signature.
- *
- * Note that the referenced class symbol may be an implementation class. For example when
- * compiling a mixed-in method that forwards to the static method in the implementation class,
- * the class descriptor of the receiver (the implementation class) is obtained by creating the
- * ClassBType.
+ * TODO: clean up the way we track referenced inner classes.
+ * doing it during code generation is not correct when the optimizer changes the code.
*/
final def getClassBTypeAndRegisterInnerClass(sym: Symbol): ClassBType = {
- assertClassNotArrayNotPrimitive(sym)
-
- if (sym == definitions.NothingClass) RT_NOTHING
- else if (sym == definitions.NullClass) RT_NULL
- else {
- val r = classBTypeFromSymbol(sym)
- if (r.isNestedClass) innerClassBufferASM += r
- r
- }
+ val r = classBTypeFromSymbol(sym)
+ if (r.isNestedClass) innerClassBufferASM += r
+ r
}
/**
- * This method returns the BType for a type reference, for example a parameter type.
- *
- * If the result is a ClassBType for a nested class, it is added to the innerClassBufferASM.
- *
- * If `t` references a class, toTypeKind ensures that the class is not an implementation class.
- * See also comment on getClassBTypeAndRegisterInnerClass, which is invoked for implementation
- * classes.
+ * The BType for a type reference. If the result is a ClassBType for a nested class, it is added
+ * to the innerClassBufferASM.
+ * TODO: clean up the way we track referenced inner classes.
*/
- final def toTypeKind(t: Type): BType = {
- import definitions.ArrayClass
-
- /**
- * Primitive types are represented as TypeRefs to the class symbol of, for example, scala.Int.
- * The `primitiveTypeMap` maps those class symbols to the corresponding PrimitiveBType.
- */
- def primitiveOrClassToBType(sym: Symbol): BType = {
- assertClassNotArray(sym)
- assert(!sym.isImplClass, sym)
- primitiveTypeMap.getOrElse(sym, getClassBTypeAndRegisterInnerClass(sym))
- }
-
- /**
- * When compiling Array.scala, the type parameter T is not erased and shows up in method
- * signatures, e.g. `def apply(i: Int): T`. A TyperRef to T is replaced by ObjectReference.
- */
- def nonClassTypeRefToBType(sym: Symbol): ClassBType = {
- assert(sym.isType && isCompilingArray, sym)
- ObjectReference
- }
-
- t.dealiasWiden match {
- case TypeRef(_, ArrayClass, List(arg)) => ArrayBType(toTypeKind(arg)) // Array type such as Array[Int] (kept by erasure)
- case TypeRef(_, sym, _) if !sym.isClass => nonClassTypeRefToBType(sym) // See comment on nonClassTypeRefToBType
- case TypeRef(_, sym, _) => primitiveOrClassToBType(sym) // Common reference to a type such as scala.Int or java.lang.String
- case ClassInfoType(_, _, sym) => primitiveOrClassToBType(sym) // We get here, for example, for genLoadModule, which invokes toTypeKind(moduleClassSymbol.info)
-
- /* AnnotatedType should (probably) be eliminated by erasure. However we know it happens for
- * meta-annotated annotations (@(ann @getter) val x = 0), so we don't emit a warning.
- * The type in the AnnotationInfo is an AnnotatedTpe. Tested in jvm/annotations.scala.
- */
- case a @ AnnotatedType(_, t) =>
- debuglog(s"typeKind of annotated type $a")
- toTypeKind(t)
-
- /* ExistentialType should (probably) be eliminated by erasure. We know they get here for
- * classOf constants:
- * class C[T]
- * class T { final val k = classOf[C[_]] }
- */
- case e @ ExistentialType(_, t) =>
- debuglog(s"typeKind of existential type $e")
- toTypeKind(t)
-
- /* The cases below should probably never occur. They are kept for now to avoid introducing
- * new compiler crashes, but we added a warning. The compiler / library bootstrap and the
- * test suite don't produce any warning.
- */
-
- case tp =>
- currentUnit.warning(tp.typeSymbol.pos,
- s"an unexpected type representation reached the compiler backend while compiling $currentUnit: $tp. " +
- "If possible, please file a bug on issues.scala-lang.org.")
-
- tp match {
- case ThisType(ArrayClass) => ObjectReference // was introduced in 9b17332f11 to fix SI-999, but this code is not reached in its test, or any other test
- case ThisType(sym) => getClassBTypeAndRegisterInnerClass(sym)
- case SingleType(_, sym) => primitiveOrClassToBType(sym)
- case ConstantType(_) => toTypeKind(t.underlying)
- case RefinedType(parents, _) => parents.map(toTypeKind(_).asClassBType).reduceLeft((a, b) => a.jvmWiseLUB(b))
- }
- }
+ final def toTypeKind(t: Type): BType = typeToBType(t) match {
+ case c: ClassBType if c.isNestedClass =>
+ innerClassBufferASM += c
+ c
+ case r => r
}
- /*
- * must-single-thread
+ /**
+ * Class components that are nested classes are added to the innerClassBufferASM.
+ * TODO: clean up the way we track referenced inner classes.
*/
final def asmMethodType(msym: Symbol): MethodBType = {
- assert(msym.isMethod, s"not a method-symbol: $msym")
- val resT: BType =
- if (msym.isClassConstructor || msym.isConstructor) UNIT
- else toTypeKind(msym.tpe.resultType)
- MethodBType(msym.tpe.paramTypes map toTypeKind, resT)
+ val r = methodBTypeFromSymbol(msym)
+ (r.returnType :: r.argumentTypes) foreach {
+ case c: ClassBType if c.isNestedClass => innerClassBufferASM += c
+ case _ =>
+ }
+ r
}
/**