Skip to content

Commit 00fd210

Browse files
committed
added hints for NEW
1 parent 13b14ab commit 00fd210

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import scala.language.unsafeNulls
66

77
import scala.annotation.{switch, tailrec}
88
import scala.collection.mutable.SortedMap
9+
import scala.jdk.CollectionConverters.*
910

1011
import scala.tools.asm
1112
import scala.tools.asm.{Handle, Opcodes}
@@ -781,6 +782,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
781782
// we have to 'simulate' it by DUPlicating the freshly created
782783
// instance (on JVM, <init> methods return VOID).
783784
case Apply(fun @ DesugaredSelect(New(tpt), nme.CONSTRUCTOR), args) =>
785+
println(s"DEBUG: New(tpt = $tpt) at ${app.span}")
784786
val ctor = fun.symbol
785787
assert(ctor.isClassConstructor, s"'new' call to non-constructor: ${ctor.name}")
786788

@@ -793,13 +795,19 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
793795

794796
case rt: ClassBType =>
795797
assert(classBTypeFromSymbol(ctor.owner) == rt, s"Symbol ${ctor.owner.showFullName} is different from $rt")
796-
mnode.visitTypeInsn(asm.Opcodes.NEW, rt.internalName)
798+
val retType = app.getAttachment(InvokeReturnType)
799+
val instrType = fun.getAttachment(InstructionTypeArguments)
800+
val instrJType = instrType.getOrElse(Nil).map(bc.toJTypeA).asJava
801+
// println(s"NEW with retType = $retType and instrType = $instrType")
802+
mnode.visitTypeInsn(asm.Opcodes.NEW, rt.internalName, instrJType)
797803
bc dup generatedType
798804
stack.push(rt)
799805
stack.push(rt)
800806
genLoadArguments(args, paramTKs(app))
801807
stack.pop(2)
802-
genCallMethod(ctor, InvokeStyle.Special, app.span)
808+
genCallMethod(ctor, InvokeStyle.Special, app.span,
809+
invokeReturnType = retType,
810+
instrTypeArgs = instrType)
803811

804812
case _ =>
805813
abort(s"Cannot instantiate $tpt of kind: $generatedType")
@@ -1457,20 +1465,16 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
14571465
val staticDesc = MethodBType(ownerBType :: bmType.argumentTypes, bmType.returnType).descriptor
14581466
val staticName = traitSuperAccessorName(method)
14591467
bc.invokestatic(receiverName, staticName, staticDesc, isInterface)
1460-
// bc.invokestatic(receiverName, staticName, staticDesc, isInterface, invokeReturnType, instrTypeArgs)
14611468
} else {
14621469
bc.invokespecial(receiverName, jname, mdescr, isInterface)
1463-
// bc.invokespecial(receiverName, jname, mdescr, isInterface, invokeReturnType, instrTypeArgs)
14641470
}
14651471
} else {
14661472
val opc = style match {
14671473
case Static => Opcodes.INVOKESTATIC
14681474
case Special => Opcodes.INVOKESPECIAL
14691475
case Virtual => if (isInterface) Opcodes.INVOKEINTERFACE else Opcodes.INVOKEVIRTUAL
14701476
}
1471-
// println("genCallMethod" + method.show + " : " + method.getAnnotation(defn.ErasurePreservationAnnot))
14721477
bc.emitInvoke(opc, receiverName, jname, mdescr, isInterface, invokeReturnType, instrTypeArgs)
1473-
// bc.emitInvoke(opc, receiverName, jname, mdescr, isInterface, invokeReturnType, instrTypeArgs)
14741478
}
14751479

14761480
bmType.returnType

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ trait BCodeIdiomatic {
452452

453453
def emitInvoke(opcode: Int, owner: String, name: String, desc: String, itf: Boolean,
454454
invokeReturnType : Option[dotty.tools.dotc.transform.TypeB] = None, instrTypeArgs : Option[List[dotty.tools.dotc.transform.TypeA]] = None): Unit = {
455+
// println(s"emitInvoke $opcode $owner $name $desc itf=$itf invokeReturnType=$invokeReturnType instrTypeArgs=$instrTypeArgs")
455456
val node = new MethodInsnNode(opcode, owner, name, desc, itf)
456457
jmethod.instructions.add(node)
457458
invokeReturnType match {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ trait BCodeSkelBuilder extends BCodeHelpers {
401401
* backend emits them as static).
402402
* No code is needed for this module symbol.
403403
*/
404+
// println("for class " + claszSymbol + " adding fields:")
405+
// println("has methods:" + claszSymbol.info.decls.filter(p => p.is(Method)).toList)
404406
for (f <- claszSymbol.info.decls.filter(p => p.isTerm && !p.is(Method))) {
405407
val javagensig = getGenericSignature(f, claszSymbol)
406408
val flags = javaFieldFlags(f)
@@ -416,6 +418,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
416418
null // no initial value
417419
)
418420
cnode.fields.add(jfield)
421+
// println("Adding field:" + f + " to " + claszSymbol + "with annotations " + f.annotations)
419422
emitAnnotations(jfield, f.annotations)
420423
}
421424

@@ -714,7 +717,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
714717
* must-single-thread
715718
*/
716719
def initJMethod(flags: Int, params: List[Symbol], cnt: Int, paramType: List[dotty.tools.dotc.transform.TypeB], retType : dotty.tools.dotc.transform.TypeB): Unit = {
717-
720+
// println(s"emitting method $methSymbol for class $claszSymbol with flags $flags params $params cnt $cnt retType $retType")
718721
val jgensig = getGenericSignature(methSymbol, claszSymbol)
719722
val (excs, others) = methSymbol.annotations.partition(_.symbol eq defn.ThrowsAnnot)
720723
val thrownExceptions: List[String] = getExceptions(excs)
@@ -863,8 +866,8 @@ trait BCodeSkelBuilder extends BCodeHelpers {
863866
case Some((paramCount, paramType, returnType)) => (paramCount, paramType, returnType)
864867
case _ => (0, List.empty[dotty.tools.dotc.transform.TypeB], dotty.tools.dotc.transform.TypeB.None)
865868
}
866-
if (cnt != 0) println(s"GenBCode.genDefDef.initJMethod: ${methSymbol.show} ${methodDeclarationAttrs} " +
867-
s"${cnt} ${paramType} ${retType}")
869+
// println(s"GenBCode.genDefDef.initJMethod: ${methSymbol.show} ${methodDeclarationAttrs} " +
870+
// s"${cnt} ${paramType} ${retType}")
868871
initJMethod(flags, paramSyms, cnt, paramType, retType)
869872

870873

compiler/src/dotty/tools/backend/jvm/MethodNode1.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
import scala.tools.asm.tree.LineNumberNode;
3232
import scala.tools.asm.tree.MethodNode;
3333
import scala.tools.asm.tree.AbstractInsnNode;
34+
import scala.tools.asm.tree.TypeInsnNode;
3435
/**
3536
* A subclass of {@link MethodNode} to customize the representation of
3637
* label nodes with {@link LabelNode1}.
3738
*/
3839
public class MethodNode1 extends MethodNode {
40+
private final boolean DEBUG = false;
41+
3942
public Map<AbstractInsnNode, TypeHints.TypeB> invokeReturnTypeBs = new LinkedHashMap<>();
4043

4144
public Map<AbstractInsnNode, List<TypeHints.TypeA>> instructionTypeArgTypeAs = new LinkedHashMap<>();
@@ -389,7 +392,7 @@ public boolean hasTypeHints(){
389392
public void setAttribtues(){
390393
if (!hasTypeHints()) return;
391394
genOffsetMap();
392-
printOffsetMap();
395+
if (DEBUG) printOffsetMap();
393396
List<TypeHints.TypeBHint> typeBHintList = new ArrayList<>();
394397
for (Map.Entry<AbstractInsnNode, TypeHints.TypeB> entry : invokeReturnTypeBs.entrySet()){
395398
AbstractInsnNode insn = entry.getKey();
@@ -435,4 +438,17 @@ protected LabelNode getLabelNode(Label label) {
435438
}
436439
return (LabelNode) label.info;
437440
}
441+
442+
public void visitTypeInsn(int opcode, String type, List<TypeHints.TypeA> instrTypeA){
443+
if (opcode == Opcodes.NEW && instrTypeA.size() > 0){
444+
if (DEBUG) System.out.println("visitTypeInsn NEW with instrTypeA = " + instrTypeA);
445+
AbstractInsnNode node = new TypeInsnNode(opcode, type);
446+
instructions.add(node);
447+
if (instrTypeA != null){
448+
instructionTypeArgTypeAs.put(node, instrTypeA);
449+
}
450+
} else {
451+
visitTypeInsn(opcode, type);
452+
}
453+
}
438454
}

0 commit comments

Comments
 (0)