diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java index f9952a28fc4..b336992d16a 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java @@ -938,8 +938,6 @@ public class FunctionCodegen { InstructionAdapter iv = new InstructionAdapter(mv); genDefaultSuperCallCheckIfNeeded(iv, functionDescriptor, defaultMethod); - loadExplicitArgumentsOnStack(OBJECT_TYPE, isStatic, signature, generator); - List mappedParameters = signature.getValueParameters(); int capturedArgumentsCount = 0; while (capturedArgumentsCount < mappedParameters.size() && @@ -968,7 +966,15 @@ public class FunctionCodegen { iv.mark(loadArg); } + } + // load arguments after defaults generation to avoid redundant stack normalization operations + loadExplicitArgumentsOnStack(OBJECT_TYPE, isStatic, signature, generator); + + for (int index = 0; index < valueParameters.size(); index++) { + ValueParameterDescriptor parameterDescriptor = valueParameters.get(index); + Type type = mappedParameters.get(capturedArgumentsCount + index).getAsmType(); + int parameterIndex = frameMap.getIndex(parameterDescriptor); generator.putValueIfNeeded(type, StackValue.local(parameterIndex, type)); } diff --git a/compiler/testData/codegen/bytecodeText/defaultMethodBody.kt b/compiler/testData/codegen/bytecodeText/defaultMethodBody.kt new file mode 100644 index 00000000000..6ce69824f90 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/defaultMethodBody.kt @@ -0,0 +1,5 @@ +fun test(a: Int, z: String = try{"1"} catch (e: Exception) {"2"}) { + "Default body" +} + +//1 ILOAD 0\s*ALOAD 1\s*INVOKESTATIC DefaultMethodBodyKt\.test \(ILjava/lang/String;\)V diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 4c019993915..7834edff265 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -120,6 +120,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { doTest(fileName); } + @TestMetadata("defaultMethodBody.kt") + public void testDefaultMethodBody() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/defaultMethodBody.kt"); + doTest(fileName); + } + @TestMetadata("falseSmartCast.kt") public void testFalseSmartCast() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/falseSmartCast.kt");