JVM_IR: sidestep defective getMethodAsmFlags when inlining lambdas

It uses isStaticMethod to determine whether to set ACC_STATIC, which is
not correct (see PR #2341). This results in using incorrectly typed
opcodes (as all arguments are shifted by 1) when modifying the inlined
lambda's bytecode. For example, in the test added by this commit, these
opcodes are inserted to spill the stack into locals before calling
another inline function.

Because getMethodAsmFlags is used by the non-IR backend (see PR #2341
again for why changing stuff might not be a good idea), the proposed
solution is to ditch it completely and override generateLambdaBody in
IrExpressionLambdaImpl to use FunctionCodegen's IR-based flag
computation logic.
This commit is contained in:
pyos
2019-05-24 13:57:42 +02:00
committed by max-kammerer
parent 82e4985aa0
commit 6d19eb1853
9 changed files with 59 additions and 61 deletions
@@ -174,7 +174,6 @@ class ExpressionCodegen(
val endLabel = markNewLabel()
writeLocalVariablesInTable(info, endLabel)
writeParameterInLocalVariableTable(startLabel, endLabel)
mv.visitEnd()
}
private fun generateNonNullAssertions() {
@@ -58,11 +58,12 @@ open class FunctionCodegen(
if (!state.classBuilderMode.generateBodies || flags.and(Opcodes.ACC_ABSTRACT) != 0 || irFunction.isExternal) {
generateAnnotationDefaultValueIfNeeded(methodVisitor)
methodVisitor.visitEnd()
} else {
val frameMap = createFrameMapWithReceivers(signature)
ExpressionCodegen(irFunction, frameMap, InstructionAdapter(methodVisitor), classCodegen, isInlineLambda).generate()
methodVisitor.visitMaxs(-1, -1)
}
methodVisitor.visitEnd()
return signature
}
@@ -38,7 +38,6 @@ class IrSourceCompilerForInline(
private val data: BlockInfo
) : SourceCompilerForInline {
//TODO
override val lookupLocation: LookupLocation
get() = NoLookupLocation.FROM_BACKEND
@@ -59,19 +58,24 @@ class IrSourceCompilerForInline(
override val lazySourceMapper: DefaultSourceMapper
get() = codegen.classCodegen.getOrCreateSourceMapper()
override fun generateLambdaBody(adapter: MethodVisitor, jvmMethodSignature: JvmMethodSignature, lambdaInfo: ExpressionLambda): SMAP {
lambdaInfo as? IrExpressionLambdaImpl ?: error("Expecting ir lambda, but $lambdaInfo")
val functionCodegen = object : FunctionCodegen(lambdaInfo.function, codegen.classCodegen, true) {
private fun makeInlineNode(function: IrFunction, classCodegen: ClassCodegen, marker: CallSiteMarker?): SMAPAndMethodNode {
var node: MethodNode? = null
val functionCodegen = object : FunctionCodegen(function, classCodegen, isInlineLambda = marker == null) {
override fun createMethod(flags: Int, signature: JvmMethodGenericSignature): MethodVisitor {
return adapter
val asmMethod = signature.asmMethod
node = MethodNode(Opcodes.API_VERSION, flags, asmMethod.name, asmMethod.descriptor, signature.genericsSignature, null)
return wrapWithMaxLocalCalc(node!!)
}
}
lazySourceMapper.callSiteMarker = marker
functionCodegen.generate()
return SMAP(codegen.classCodegen.getOrCreateSourceMapper().resultMappings)
lazySourceMapper.callSiteMarker = null
return SMAPAndMethodNode(node!!, SMAP(classCodegen.getOrCreateSourceMapper().resultMappings))
}
override fun generateLambdaBody(lambdaInfo: ExpressionLambda): SMAPAndMethodNode =
makeInlineNode((lambdaInfo as IrExpressionLambdaImpl).function, codegen.classCodegen, null)
override fun doCreateMethodNodeFromSource(
callableDescriptor: FunctionDescriptor,
jvmSignature: JvmMethodSignature,
@@ -99,31 +103,8 @@ class IrSourceCompilerForInline(
}
}
//ExpressionCodegen()
var node: MethodNode? = null
var maxCalcAdapter: MethodVisitor? = null
val fakeClassCodegen = FakeClassCodegen(irFunction, codegen.classCodegen)
val functionCodegen = object : FunctionCodegen(irFunction, fakeClassCodegen) {
override fun createMethod(flags: Int, signature: JvmMethodGenericSignature): MethodVisitor {
node = MethodNode(
Opcodes.API_VERSION,
flags,
signature.asmMethod.name, signature.asmMethod.descriptor,
signature.genericsSignature, null
)
maxCalcAdapter = wrapWithMaxLocalCalc(node!!)
return maxCalcAdapter!!
}
}
assert(codegen.lastLineNumber >= 0)
lazySourceMapper.callSiteMarker = CallSiteMarker(codegen.lastLineNumber)
functionCodegen.generate()
lazySourceMapper.callSiteMarker = null
maxCalcAdapter!!.visitMaxs(-1, -1)
maxCalcAdapter!!.visitEnd()
return SMAPAndMethodNode(node!!, SMAP(fakeClassCodegen.getOrCreateSourceMapper().resultMappings))
return makeInlineNode(irFunction, FakeClassCodegen(irFunction, codegen.classCodegen), CallSiteMarker(codegen.lastLineNumber))
}
override fun hasFinallyBlocks() = data.hasFinallyBlocks()