From 5f3cd8683d8b2e7429e73c2fa7199232ea7c46ca Mon Sep 17 00:00:00 2001 From: James Iry Date: Mon, 25 Feb 2013 16:30:46 -0800 Subject: SI-7181 Eliminate unnecessary duplication of finally blocks The main body of a try and each exception handler were getting a copy of the finally block for the "normal" flow case (i.e. where they don't throw an uncaught exception or use "return" to exit early). But that's not necessary. With this commit the try body and each exception handler can all jump to the same copy of the finally block on a normal exit. A byte code test is included to ensure we're getting fewer copies of the finally block. inline-ex-handlers.check is updated because the icode is a bit different without the extra finally block copies. --- src/compiler/scala/tools/nsc/backend/icode/GenICode.scala | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala index 8881650a81..5438fd8590 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala @@ -1932,12 +1932,10 @@ abstract class GenICode extends SubComponent { * * body: * [ try body ] - * [ finally body ] * JUMP normalExit * * catch[i]: * [ handler[i] body ] - * [ finally body ] * JUMP normalExit * * catchAll: @@ -1946,6 +1944,7 @@ abstract class GenICode extends SubComponent { * THROW exception * * normalExit: + * [ finally body ] * * each catch[i] will cover body. catchAll will cover both body and each catch[i] * Additional finally copies are created on the emission of every RETURN in the try body and exception handlers. @@ -2012,9 +2011,7 @@ abstract class GenICode extends SubComponent { exhStartCtx.addFinalizer(finalizer, finalizerCtx) loadException(exhStartCtx, exh, tree.pos) val exhEndCtx = handler(exhStartCtx) - // emit finalizer - val exhEndCtx2 = emitFinalizer(exhEndCtx) - exhEndCtx2.bb.closeWith(JUMP(normalExitCtx.bb)) + exhEndCtx.bb.closeWith(JUMP(normalExitCtx.bb)) outerCtx.endHandler() } @@ -2022,14 +2019,13 @@ abstract class GenICode extends SubComponent { if (finalizer != EmptyTree) bodyCtx.addFinalizer(finalizer, finalizerCtx) - var bodyEndCtx = body(bodyCtx) - bodyEndCtx = emitFinalizer(bodyEndCtx) + val bodyEndCtx = body(bodyCtx) outerCtx.bb.closeWith(JUMP(bodyCtx.bb)) bodyEndCtx.bb.closeWith(JUMP(normalExitCtx.bb)) - normalExitCtx + emitFinalizer(normalExitCtx) } } } -- cgit v1.2.3