Skip to content

Commit ae6d185

Browse files
committed
Merge branch 'mark-box-unbox' into add-reified-values
2 parents 0a99b6b + e9fdc95 commit ae6d185

File tree

5 files changed

+61
-13
lines changed

5 files changed

+61
-13
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
826826
genLoad(expr, nativeKind)
827827
val MethodNameAndType(mname, methodType) = asmBoxTo(nativeKind)
828828
val hasExtraBoxAttach = app.getAttachment(NoBoxingNeeded).isDefined
829-
if hasExtraBoxAttach then
830-
println(s"Debug: extra box attach found on ${app.show}")
829+
// if hasExtraBoxAttach then println(s"Debug: extra box attach found on ${app.show}")
831830
bc.invokestatic(srBoxesRuntimeRef.internalName, mname, methodType.descriptor, itf = false, extraBoxingUnboxing = hasExtraBoxAttach)
832831
generatedType = boxResultType(fun.symbol) // was toTypeKind(fun.symbol.tpe.resultType)
833832

@@ -837,8 +836,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
837836
generatedType = boxType
838837
val MethodNameAndType(mname, methodType) = asmUnboxTo(boxType)
839838
val hasExtraUnboxAttach = app.getAttachment(NoUnboxingNeeded).isDefined
840-
if hasExtraUnboxAttach then
841-
println(s"Debug: extra unbox attach found on ${app.show}")
839+
// if hasExtraUnboxAttach then println(s"Debug: extra unbox attach found on ${app.show}")
842840
bc.invokestatic(srBoxesRuntimeRef.internalName, mname, methodType.descriptor, itf = false, extraBoxingUnboxing = hasExtraUnboxAttach)
843841

844842
case app @ Apply(fun, args) =>

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,8 @@ trait BCodeHelpers extends BCodeIdiomatic {
775775
* signatures, e.g. `def apply(i: Int): T`. A TyperRef to T is replaced by ObjectReference.
776776
*/
777777
def nonClassTypeRefToBType(sym: Symbol): ClassBType = {
778-
if (sym == defn.ObjectAnySymbol) ObjectRef
779-
else
780-
assert(sym.isType && compilingArray, sym)
781-
ObjectRef.asInstanceOf[ct.bTypes.ClassBType]
778+
assert(sym.isType && compilingArray, sym)
779+
ObjectRef.asInstanceOf[ct.bTypes.ClassBType]
782780
}
783781

784782
tp.widenDealias match {

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Compiler {
113113
List(new AddReifiedTypes) :: // Add reified type parameters
114114
List(new ErasurePreservation) :: //
115115
List(new Erasure) :: // Rewrite types to JVM model, erasing all type parameters, abstract types and refinements.
116-
List(new ElimErasedValueType, // Expand erased value types to their underlying implementation types
116+
List(new ElimObjectAny, // Eliminate ObjcetAny to Object
117+
new ElimErasedValueType, // Expand erased value types to their underlying implementation types
117118
new PureStats, // Remove pure stats from blocks
118119
new VCElideAllocations, // Peep-hole optimization to eliminate unnecessary value class allocations
119120
new EtaReduce, // Reduce eta expansions of pure paths to the underlying function reference
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dotty.tools
2+
package dotc
3+
package transform
4+
5+
import ast.Trees
6+
import core.Phases.*
7+
import core.Contexts.*
8+
import core.Types.*
9+
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
10+
import dotty.tools.dotc.core.Symbols.defn
11+
import dotty.tools.dotc.core.DenotTransformers.DenotTransformer
12+
import dotty.tools.dotc.core.Denotations.SingleDenotation
13+
import dotty.tools.dotc.core.DenotTransformers.InfoTransformer
14+
import dotty.tools.dotc.core.Symbols.*
15+
16+
class ElimObjectAny extends MiniPhase with InfoTransformer{
17+
import ast.tpd._
18+
19+
override def phaseName: String = ElimObjectAny.name
20+
21+
override def description: String = ElimObjectAny.description
22+
23+
def elimObjectAny(tp: Type)(using Context): Type =
24+
new TypeMap {
25+
def apply(tp: Type): Type =
26+
if tp eq defn.ObjectAnyType then
27+
// println(s"ElimObjectAny: visiting type $tp")
28+
defn.ObjectType
29+
else
30+
mapOver(tp)
31+
}.apply(tp)
32+
33+
override def transformInfo(tp: Type, sym: Symbol)(using Context): Type =
34+
elimObjectAny(tp)
35+
36+
override def transformTypeTree(tree: TypeTree)(using Context): Tree =
37+
val newType = elimObjectAny(tree.tpe)
38+
if (newType ne tree.tpe) then tree.withType(newType)
39+
else tree
40+
// def changeObjectAnyToObject(tp: Type)(using Context): Type = {
41+
// tp match
42+
// case tr: TypeRef if tr.symbol == defn.ObjectAnySymbol =>
43+
// defn.ObjectType
44+
// case _ => tp
45+
// }
46+
}
47+
48+
object ElimObjectAny {
49+
val name: String = "elimObjectAny"
50+
val description: String = "Eliminate ObjectAny to Object"
51+
}

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ object Erasure {
251251

252252
def maybeMarkBox(tree: Tree, tp: Type)(using Context): Tree =
253253
if (isObjectAny(tp)) then
254-
println(" MarkBoxing: marking box for "+ tp)
254+
// println(" MarkBoxing: marking box for "+ tp)
255255
tree.withAttachment(NoBoxingNeeded, tp.typeSymbol.name.toString)
256256
else tree
257257

258258
def maybeMarkUnbox(tree: Tree, tp: Type)(using Context): Tree =
259259
if (isObjectAny(tp)) then
260-
println(" MarkBoxing: marking unbox for "+ tp)
260+
// println(" MarkBoxing: marking unbox for "+ tp)
261261
tree.withAttachment(NoUnboxingNeeded, tp.typeSymbol.name.toString)
262262
else tree
263263

@@ -390,9 +390,9 @@ object Erasure {
390390
case _ =>
391391
if (pt.isPrimitiveValueType)
392392
primitiveConversion(tree, pt.classSymbol)
393-
// cast tree type to pt, if pt is ObjectAny, do not inject a cast
393+
// cast tree type to pt, if pt is ObjectAny and tpe of tree is not primitive, do not inject a cast
394394
else if isObjectAny(pt) && !tree.tpe.widen.isPrimitiveValueType then
395-
println(" MarkBoxing: skipping cast to ObjectAny for "+ pt + " from tree "+ tree.show)
395+
// println(" MarkBoxing: skipping cast to ObjectAny for "+ pt + " from tree "+ tree.show)
396396
tree.withType(pt)
397397
else
398398
tree.asInstance(pt)

0 commit comments

Comments
 (0)