You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/09/30 17:16:55 UTC
[groovy] branch GROOVY_3_0_X updated: Minor refactoring: remove
duplicated code for bytecode generation of loop statement
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new 731573a Minor refactoring: remove duplicated code for bytecode generation of loop statement
731573a is described below
commit 731573a00975e2269c4a75a5dc650d4029f5b2b8
Author: Daniel Sun <su...@apache.org>
AuthorDate: Thu Oct 1 00:28:54 2020 +0800
Minor refactoring: remove duplicated code for bytecode generation of loop statement
(cherry picked from commit b499fbebd16ab025292629ca62a89047d52e9109)
---
.../groovy/classgen/asm/StatementWriter.java | 18 ++++++--
.../asm/sc/StaticTypesStatementWriter.java | 54 ++++++----------------
2 files changed, 26 insertions(+), 46 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
index e452b9e..7714d0a 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
@@ -127,19 +127,28 @@ public class StatementWriter {
writeStatementLabel(statement);
CompileStack compileStack = controller.getCompileStack();
- MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
compileStack.pushLoop(statement.getVariableScope(), statement.getStatementLabels());
- // declare the loop counter
- BytecodeVariable variable = compileStack.defineVariable(statement.getVariable(), false);
-
// then get the iterator and generate the loop control
MethodCallExpression iterator = new MethodCallExpression(statement.getCollectionExpression(), "iterator", new ArgumentListExpression());
iterator.visit(controller.getAcg());
operandStack.doGroovyCast(ClassHelper.Iterator_TYPE);
+ writeForInLoopControlAndBlock(statement);
+ compileStack.pop();
+ }
+
+ protected void writeForInLoopControlAndBlock(ForStatement statement) {
+ CompileStack compileStack = controller.getCompileStack();
+ MethodVisitor mv = controller.getMethodVisitor();
+ OperandStack operandStack = controller.getOperandStack();
+
+ // declare the loop counter
+ BytecodeVariable variable = compileStack.defineVariable(statement.getVariable(), false);
+
+ // get the iterator and generate the loop control
int iteratorIndex = compileStack.defineTemporaryVariable("iterator", ClassHelper.Iterator_TYPE, true);
Label continueLabel = compileStack.getContinueLabel();
Label breakLabel = compileStack.getBreakLabel();
@@ -162,7 +171,6 @@ public class StatementWriter {
mv.visitLabel(breakLabel);
compileStack.removeVar(iteratorIndex);
- compileStack.pop();
}
protected void writeIteratorHasNext(final MethodVisitor mv) {
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java
index 8165224..cac69f7 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java
@@ -99,9 +99,9 @@ public class StaticTypesStatementWriter extends StatementWriter {
if (collectionType.isArray() && loopVariable.getOriginType().equals(collectionType.getComponentType())) {
writeOptimizedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
} else if (ENUMERATION_CLASSNODE.equals(collectionType)) {
- writeEnumerationBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
+ writeEnumerationBasedForEachLoop(loop, collectionExpression, collectionType);
} else {
- writeIteratorBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
+ writeIteratorBasedForEachLoop(loop, collectionExpression, collectionType);
}
operandStack.popDownTo(size);
compileStack.pop();
@@ -206,15 +206,9 @@ public class StaticTypesStatementWriter extends StatementWriter {
}
private void writeIteratorBasedForEachLoop(
- CompileStack compileStack,
- OperandStack operandStack,
- MethodVisitor mv,
ForStatement loop,
Expression collectionExpression,
- ClassNode collectionType,
- Parameter loopVariable) {
- // Declare the loop counter.
- BytecodeVariable variable = compileStack.defineVariable(loopVariable, false);
+ ClassNode collectionType) {
if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(collectionType, ITERABLE_CLASSNODE)) {
MethodCallExpression iterator = new MethodCallExpression(collectionExpression, "iterator", new ArgumentListExpression());
@@ -223,46 +217,24 @@ public class StaticTypesStatementWriter extends StatementWriter {
iterator.visit(controller.getAcg());
} else {
collectionExpression.visit(controller.getAcg());
- mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/DefaultGroovyMethods", "iterator", "(Ljava/lang/Object;)Ljava/util/Iterator;", false);
- operandStack.replace(ClassHelper.Iterator_TYPE);
+ controller.getMethodVisitor().visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/DefaultGroovyMethods", "iterator", "(Ljava/lang/Object;)Ljava/util/Iterator;", false);
+ controller.getOperandStack().replace(ClassHelper.Iterator_TYPE);
}
- // Then get the iterator and generate the loop control
-
- int iteratorIdx = compileStack.defineTemporaryVariable("iterator", ClassHelper.Iterator_TYPE, true);
-
- Label continueLabel = compileStack.getContinueLabel();
- Label breakLabel = compileStack.getBreakLabel();
-
- mv.visitLabel(continueLabel);
- mv.visitVarInsn(ALOAD, iteratorIdx);
- writeIteratorHasNext(mv);
- // note: ifeq tests for ==0, a boolean is 0 if it is false
- mv.visitJumpInsn(IFEQ, breakLabel);
-
- mv.visitVarInsn(ALOAD, iteratorIdx);
- writeIteratorNext(mv);
- operandStack.push(ClassHelper.OBJECT_TYPE);
- operandStack.storeVar(variable);
-
- // Generate the loop body
- loop.getLoopBlock().visit(controller.getAcg());
-
- mv.visitJumpInsn(GOTO, continueLabel);
- mv.visitLabel(breakLabel);
- compileStack.removeVar(iteratorIdx);
+ writeForInLoopControlAndBlock(loop);
}
private void writeEnumerationBasedForEachLoop(
- CompileStack compileStack,
- OperandStack operandStack,
- MethodVisitor mv,
ForStatement loop,
Expression collectionExpression,
- ClassNode collectionType,
- Parameter loopVariable) {
+ ClassNode collectionType) {
+
+ CompileStack compileStack = controller.getCompileStack();
+ MethodVisitor mv = controller.getMethodVisitor();
+ OperandStack operandStack = controller.getOperandStack();
+
// Declare the loop counter.
- BytecodeVariable variable = compileStack.defineVariable(loopVariable, false);
+ BytecodeVariable variable = compileStack.defineVariable(loop.getVariable(), false);
collectionExpression.visit(controller.getAcg());