From 9ef00f06213a03bd5041cfa64486892815b7d560 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Tue, 23 Jun 2015 18:48:08 +0300 Subject: [PATCH] Code clean --- .../kotlin/codegen/ExpressionCodegen.java | 14 +++---- .../inline/CoveringTryCatchNodeProcessor.kt | 38 ++++++++----------- .../kotlin/codegen/inline/InlineCodegen.java | 14 +++---- .../codegen/inline/InlineCodegenUtil.java | 4 +- .../inline/InternalFinallyBlockInliner.java | 21 +++++----- .../kotlin/codegen/inline/InvokeCall.java | 6 +-- .../kotlin/codegen/inline/MethodInliner.java | 12 ++++-- .../codegen/inline/TryBlockClustering.kt | 8 ++-- .../src/kotlin/jvm/internal/InlineMarker.java | 4 +- 9 files changed, 58 insertions(+), 63 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 8046f0f0af3..3c93fff0063 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -129,7 +129,7 @@ public class ExpressionCodegen extends JetVisitor implem private int myLastLineNumber = -1; private boolean shouldMarkLineNumbers = true; - private int finallyDeep = 0; + private int finallyDepth = 0; public ExpressionCodegen( @NotNull MethodVisitor mv, @@ -1794,7 +1794,7 @@ public class ExpressionCodegen extends JetVisitor implem @Nullable Label afterJumpLabel ) { if (finallyBlockStackElement != null) { - finallyDeep++; + finallyDepth++; assert finallyBlockStackElement.gaps.size() % 2 == 0 : "Finally block gaps are inconsistent"; BlockStackElement topOfStack = blockStackElements.pop(); @@ -1805,13 +1805,13 @@ public class ExpressionCodegen extends JetVisitor implem v.mark(finallyStart); finallyBlockStackElement.addGapLabel(finallyStart); if (InlineCodegenUtil.isFinallyMarkerRequired(context)) { - InlineCodegenUtil.generateFinallyMarker(v, finallyDeep, true); + InlineCodegenUtil.generateFinallyMarker(v, finallyDepth, true); } //noinspection ConstantConditions gen(jetTryExpression.getFinallyBlock().getFinalExpression(), Type.VOID_TYPE); if (InlineCodegenUtil.isFinallyMarkerRequired(context)) { - InlineCodegenUtil.generateFinallyMarker(v, finallyDeep, false); + InlineCodegenUtil.generateFinallyMarker(v, finallyDepth, false); } } @@ -1820,7 +1820,7 @@ public class ExpressionCodegen extends JetVisitor implem } if (finallyBlockStackElement != null) { - finallyDeep--; + finallyDepth--; Label finallyEnd = afterJumpLabel != null ? afterJumpLabel : new Label(); if (afterJumpLabel == null) { v.mark(finallyEnd); @@ -4006,9 +4006,9 @@ The "returned" value of try expression with no finally is either the last expres return new Stack(blockStackElements); } - public void addBlockStackElementsForNonLocalReturns(@NotNull Stack elements, int finallyDeepIndex) { + public void addBlockStackElementsForNonLocalReturns(@NotNull Stack elements, int finallyDepth) { blockStackElements.addAll(elements); - this.finallyDeep = finallyDeepIndex; + this.finallyDepth = finallyDepth; } private static class NonLocalReturnInfo { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/CoveringTryCatchNodeProcessor.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/CoveringTryCatchNodeProcessor.kt index 92394ec1c8d..7320c9f2ba8 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/CoveringTryCatchNodeProcessor.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/CoveringTryCatchNodeProcessor.kt @@ -117,16 +117,14 @@ class IntervalMetaInfo> { intervalEnds.put(remapped.endLabel, remapped) } - fun splitCurrentIntervals(by : Interval, keepStart: Boolean): List> { + fun splitCurrentIntervals(by : Interval, keepStart: Boolean): List> { return currentIntervals.map { split(it, by, keepStart) } } fun processCurrent(curIns: LabelNode, directOrder: Boolean) { getInterval(curIns, directOrder).forEach { val added = currentIntervals.add(it) - if (!added) { - assert(added, "Wrong interval structure: $curIns, $it") - } + assert(added, "Wrong interval structure: $curIns, $it") } getInterval(curIns, !directOrder).forEach { @@ -135,28 +133,22 @@ class IntervalMetaInfo> { } } - fun split(interval: T, by : Interval, keepStart: Boolean): SplittedPair { - val splittedPair = interval.split(by, keepStart) + fun split(interval: T, by : Interval, keepStart: Boolean): SplitPair { + val split = interval.split(by, keepStart) if (!keepStart) { - remapStartLabel(splittedPair.newPart.startLabel, splittedPair.patchedPart) + remapStartLabel(split.newPart.startLabel, split.patchedPart) } else { - remapEndLabel(splittedPair.newPart.endLabel, splittedPair.patchedPart) + remapEndLabel(split.newPart.endLabel, split.patchedPart) } - addNewInterval(splittedPair.newPart) - return splittedPair + addNewInterval(split.newPart) + return split } - fun splitAndRemoveInterval(interval: T, by : Interval, keepStart: Boolean): SplittedPair { - val splittedPair = interval.split(by, keepStart) - if (!keepStart) { - remapStartLabel(splittedPair.newPart.startLabel, splittedPair.patchedPart) - } else { - remapEndLabel(splittedPair.newPart.endLabel, splittedPair.patchedPart) - } - val removed = currentIntervals.remove(splittedPair.patchedPart) - assert(removed, "Wrong interval structure: $splittedPair") - addNewInterval(splittedPair.newPart) - return splittedPair + fun splitAndRemoveInterval(interval: T, by : Interval, keepStart: Boolean): SplitPair { + val splitPair = split(interval, by, keepStart) + val removed = currentIntervals.remove(splitPair.patchedPart) + assert(removed, {"Wrong interval structure: $splitPair"}) + return splitPair } fun getInterval(curIns: LabelNode, isOpen: Boolean) = if (isOpen) intervalStarts.get(curIns) else intervalEnds.get(curIns) @@ -201,7 +193,7 @@ public class LocalVarNodeWrapper(val node: LocalVariableNode) : Interval, Splitt override val endLabel: LabelNode get() = node.end - override fun split(split: Interval, keepStart: Boolean): SplittedPair { + override fun split(split: Interval, keepStart: Boolean): SplitPair { val newPartInterval = if (keepStart) { val oldEnd = endLabel node.end = split.startLabel @@ -213,7 +205,7 @@ public class LocalVarNodeWrapper(val node: LocalVariableNode) : Interval, Splitt Pair(oldStart, split.startLabel) } - return SplittedPair(this, LocalVarNodeWrapper( + return SplitPair(this, LocalVarNodeWrapper( LocalVariableNode(node.name, node.desc, node.signature, newPartInterval.first, newPartInterval.second, node.index) )) } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java index 7ef0381395b..a2886f8ad0d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java @@ -664,13 +664,13 @@ public class InlineCodegen extends CallGenerator { DefaultProcessor processor = new DefaultProcessor(intoNode, offsetForFinallyLocalVar); - int curFinallyDeep = 0; + int curFinallyDepth = 0; AbstractInsnNode curInstr = intoNode.instructions.getFirst(); while (curInstr != null) { processor.processInstruction(curInstr, true); if (InlineCodegenUtil.isFinallyStart(curInstr)) { - //TODO deep index calc could be more precise - curFinallyDeep = getConstant(curInstr.getPrevious()); + //TODO depth index calc could be more precise + curFinallyDepth = getConstant(curInstr.getPrevious()); } MethodInliner.PointForExternalFinallyBlocks extension = extensionPoints.get(curInstr); @@ -683,7 +683,7 @@ public class InlineCodegen extends CallGenerator { ExpressionCodegen finallyCodegen = new ExpressionCodegen(finallyNode, codegen.getFrameMap(), codegen.getReturnType(), codegen.getContext(), codegen.getState(), codegen.getParentCodegen()); - finallyCodegen.addBlockStackElementsForNonLocalReturns(codegen.getBlockStackElements(), curFinallyDeep); + finallyCodegen.addBlockStackElementsForNonLocalReturns(codegen.getBlockStackElements(), curFinallyDepth); FrameMap frameMap = finallyCodegen.getFrameMap(); FrameMap.Mark mark = frameMap.mark(); @@ -691,12 +691,12 @@ public class InlineCodegen extends CallGenerator { frameMap.enterTemp(Type.INT_TYPE); } - finallyCodegen.generateFinallyBlocksIfNeeded(extension.returnType, extension.labelNode.getLabel()); + finallyCodegen.generateFinallyBlocksIfNeeded(extension.returnType, extension.finallyIntervalEnd.getLabel()); //Exception table for external try/catch/finally blocks will be generated in original codegen after exiting this method InlineCodegenUtil.insertNodeBefore(finallyNode, intoNode, curInstr); - SimpleInterval splitBy = new SimpleInterval((LabelNode) start.info, extension.labelNode); + SimpleInterval splitBy = new SimpleInterval((LabelNode) start.info, extension.finallyIntervalEnd); processor.getTryBlocksMetaInfo().splitCurrentIntervals(splitBy, true); processor.getLocalVarsMetaInfo().splitCurrentIntervals(splitBy, true); @@ -719,8 +719,8 @@ public class InlineCodegen extends CallGenerator { AbstractInsnNode curInstr = instructions.getFirst(); while (curInstr != null) { if (InlineCodegenUtil.isFinallyMarker(curInstr)) { - //just to assert AbstractInsnNode marker = curInstr; + //just to assert getConstant(marker.getPrevious()); curInstr = curInstr.getNext(); instructions.remove(marker.getPrevious()); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenUtil.java index 77aa9788a53..8db898e6d26 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenUtil.java @@ -404,8 +404,8 @@ public class InlineCodegenUtil { } } - public static void generateFinallyMarker(@NotNull InstructionAdapter v, int deep, boolean start) { - v.iconst(deep); + public static void generateFinallyMarker(@NotNull InstructionAdapter v, int depth, boolean start) { + v.iconst(depth); v.invokestatic(INLINE_MARKER_CLASS_NAME, start ? INLINE_MARKER_FINALLY_START : INLINE_MARKER_FINALLY_END, "(I)V", false); } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InternalFinallyBlockInliner.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InternalFinallyBlockInliner.java index daf2231a031..75e48d3b981 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InternalFinallyBlockInliner.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InternalFinallyBlockInliner.java @@ -144,6 +144,7 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { AbstractInsnNode markedReturn = curIns; AbstractInsnNode instrInsertFinallyBefore = markedReturn.getPrevious(); AbstractInsnNode nextPrev = instrInsertFinallyBefore.getPrevious().getPrevious(); + assert markedReturn.getNext() instanceof LabelNode : "Label should be occurred after non-local return"; LabelNode newFinallyEnd = (LabelNode) markedReturn.getNext(); Type nonLocalReturnType = InlineCodegenUtil.getReturnType(markedReturn.getOpcode()); @@ -158,7 +159,7 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { checkClusterInvariant(clustersFromInnermost); - int originalDeepIndex = 0; + int originalDepthIndex = 0; while (tryCatchBlockIterator.hasNext()) { TryBlockCluster clusterToFindFinally = tryCatchBlockIterator.next(); @@ -174,10 +175,8 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { FinallyBlockInfo finallyInfo = findFinallyBlockBody(nodeWithDefaultHandlerIfExists, getTryBlocksMetaInfo().getAllIntervals()); if (finallyInfo == null) continue; - TryCatchBlockNodeInfo defaultHandlerBlock = clusterToFindFinally.getDefaultHandler(); - assert defaultHandlerBlock != null; - originalDeepIndex++; + originalDepthIndex++; instructions.resetLabels(); @@ -204,7 +203,7 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { !(currentIns instanceof JumpInsnNode) || labelsInsideFinally.contains(((JumpInsnNode) currentIns).label); - copyInstruction(finallyBlockCopy, currentIns, isInsOrJumpInsideFinally, originalDeepIndex); + copyInstruction(finallyBlockCopy, currentIns, isInsOrJumpInsideFinally, originalDepthIndex); currentIns = currentIns.getNext(); } @@ -249,12 +248,12 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { @NotNull MethodNode finallyBlockCopy, @NotNull AbstractInsnNode currentIns, boolean isInsOrJumpInsideFinally, - int deep + int depthShift ) { if (isInsOrJumpInsideFinally) { if (InlineCodegenUtil.isFinallyMarker(currentIns.getNext())) { Integer constant = getConstant(currentIns); - finallyBlockCopy.visitLdcInsn(constant + deep); + finallyBlockCopy.visitLdcInsn(constant + depthShift); } else { currentIns.accept(finallyBlockCopy); //VISIT } @@ -383,7 +382,7 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { // so we should split original interval by inserted finally one for (TryCatchBlockNodeInfo block : updatingClusterBlocks) { //update exception mapping - SplittedPair split = tryBlocksMetaInfo.splitAndRemoveInterval(block, splitBy, false); + SplitPair split = tryBlocksMetaInfo.splitAndRemoveInterval(block, splitBy, false); checkFinally(split.getNewPart()); checkFinally(split.getPatchedPart()); //block patched in split method @@ -451,14 +450,14 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { AbstractInsnNode meaningful = getNextMeaningful(startFinallyChain); assert meaningful != null : "Can't find meaningful in finally block" + startFinallyChain; - Integer finallyDeep = getConstant(meaningful); + Integer finallyDepth = getConstant(meaningful); AbstractInsnNode endFinallyChainExclusive = nextIntervalWithSameDefaultHandler.getNode().start; AbstractInsnNode current = meaningful.getNext(); while (endFinallyChainExclusive != current) { current = current.getNext(); if (InlineCodegenUtil.isFinallyEnd(current)) { - Integer currentDeep = getConstant(current.getPrevious()); - if (currentDeep.equals(finallyDeep)) { + Integer currentDepth = getConstant(current.getPrevious()); + if (currentDepth.equals(finallyDepth)) { endFinallyChainExclusive = current.getNext(); break; } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InvokeCall.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InvokeCall.java index d70333fe45f..a0fc3b944ce 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InvokeCall.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InvokeCall.java @@ -21,11 +21,11 @@ import org.jetbrains.annotations.Nullable; class InvokeCall { private final int index; public final LambdaInfo lambdaInfo; - public final int finallyDeep; + public final int finallyDepthShift; - InvokeCall(int index, @Nullable LambdaInfo lambdaInfo, int finallyDeep) { + InvokeCall(int index, @Nullable LambdaInfo lambdaInfo, int finallyDepthShift) { this.index = index; this.lambdaInfo = lambdaInfo; - this.finallyDeep = finallyDeep; + this.finallyDepthShift = finallyDepthShift; } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.java index 52f86282e3d..684f3808526 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.java @@ -244,7 +244,7 @@ public class MethodInliner { mapper); LocalVarRemapper remapper = new LocalVarRemapper(lambdaParameters, valueParamShift); - InlineResult lambdaResult = inliner.doInline(this.mv, remapper, true, info, invokeCall.finallyDeep);//TODO add skipped this and receiver + InlineResult lambdaResult = inliner.doInline(this.mv, remapper, true, info, invokeCall.finallyDepthShift);//TODO add skipped this and receiver result.addAllClassesToRemove(lambdaResult); //return value boxing/unboxing @@ -756,12 +756,16 @@ public class MethodInliner { final Type returnType; - final LabelNode labelNode; + final LabelNode finallyIntervalEnd; - public PointForExternalFinallyBlocks(@NotNull AbstractInsnNode beforeIns, @NotNull Type returnType, @NotNull LabelNode labelNode) { + public PointForExternalFinallyBlocks( + @NotNull AbstractInsnNode beforeIns, + @NotNull Type returnType, + @NotNull LabelNode finallyIntervalEnd + ) { this.beforeIns = beforeIns; this.returnType = returnType; - this.labelNode = labelNode; + this.finallyIntervalEnd = finallyIntervalEnd; } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TryBlockClustering.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TryBlockClustering.kt index b74ad2935a7..8ae9ad836fb 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TryBlockClustering.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/TryBlockClustering.kt @@ -26,7 +26,7 @@ enum class TryCatchPosition { INNER } -public class SplittedPair(val patchedPart: T, val newPart: T) +public class SplitPair(val patchedPart: T, val newPart: T) class SimpleInterval(override val startLabel: LabelNode, override val endLabel: LabelNode ) : Interval @@ -40,7 +40,7 @@ trait Interval { } trait SplittableInterval : Interval { - fun split(splitBy: Interval, keepStart: Boolean): SplittedPair + fun split(splitBy: Interval, keepStart: Boolean): SplitPair } @@ -59,7 +59,7 @@ class TryCatchBlockNodeInfo(val node: TryCatchBlockNode, val onlyCopyNotProcess: override val type: String? get() = node.type - override fun split(splitBy: Interval, keepStart: Boolean): SplittedPair { + override fun split(splitBy: Interval, keepStart: Boolean): SplitPair { val newPartInterval = if (keepStart) { val oldEnd = endLabel node.end = splitBy.startLabel @@ -70,7 +70,7 @@ class TryCatchBlockNodeInfo(val node: TryCatchBlockNode, val onlyCopyNotProcess: node.start = splitBy.endLabel Pair(oldStart, splitBy.startLabel) } - return SplittedPair( + return SplitPair( this, TryCatchBlockNodeInfo(TryCatchBlockNode(newPartInterval.first, newPartInterval.second, handler, type), onlyCopyNotProcess) ) diff --git a/core/runtime.jvm/src/kotlin/jvm/internal/InlineMarker.java b/core/runtime.jvm/src/kotlin/jvm/internal/InlineMarker.java index 0b3f813737f..34d5f51317f 100644 --- a/core/runtime.jvm/src/kotlin/jvm/internal/InlineMarker.java +++ b/core/runtime.jvm/src/kotlin/jvm/internal/InlineMarker.java @@ -32,11 +32,11 @@ public class InlineMarker { } - public static void finallyStart(int finallyDeep) { + public static void finallyStart(int finallyDepth) { } - public static void finallyEnd(int finallyDeep) { + public static void finallyEnd(int finallyDepth) { } } \ No newline at end of file