Skip to content

Commit

Permalink
Cleanup: Use BytecodeLoopLabels instead of passing the labels around …
Browse files Browse the repository at this point in the history
…individually
  • Loading branch information
kindlich committed Jun 23, 2024
1 parent 6f32190 commit 03ef6f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.openzen.zenscript.javabytecode.compiler;

import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import org.openzen.zenscript.codemodel.statement.ForeachStatement;
import org.openzen.zenscript.codemodel.type.ArrayTypeID;
import org.openzen.zenscript.codemodel.type.BasicTypeID;
import org.openzen.zenscript.codemodel.type.OptionalTypeID;
import org.openzen.zenscript.codemodel.type.RangeTypeID;
import org.openzen.zenscript.codemodel.type.TypeID;
import org.openzen.zenscript.javabytecode.BytecodeLoopLabels;
import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
import org.openzen.zenscript.javashared.JavaClass;
import org.openzen.zenscript.javashared.JavaNativeMethod;
Expand All @@ -20,19 +20,15 @@ public class JavaForeachWriter {

private final JavaWriter javaWriter;
private final ForeachStatement statement;
private final Label startOfLoopBody;
private final Label endOfLoopBody;
private final Label afterLoop;
private final JavaStatementVisitor statementVisitor;
private final JavaUnboxingTypeVisitor unboxingTypeVisitor;
private final BytecodeLoopLabels bytecodeLoopLabels;

public JavaForeachWriter(JavaStatementVisitor statementVisitor, ForeachStatement statement, Label startOfLoopBody, Label endOfLoopBody, Label afterLoop) {
public JavaForeachWriter(JavaStatementVisitor statementVisitor, ForeachStatement statement, BytecodeLoopLabels bytecodeLoopLabels) {
this.statementVisitor = statementVisitor;
this.javaWriter = statementVisitor.getJavaWriter();
this.statement = statement;
this.startOfLoopBody = startOfLoopBody;
this.endOfLoopBody = endOfLoopBody;
this.afterLoop = afterLoop;
this.bytecodeLoopLabels = bytecodeLoopLabels;
this.unboxingTypeVisitor = new JavaUnboxingTypeVisitor(this.javaWriter);
}

Expand All @@ -45,13 +41,13 @@ public void visitIntRange(RangeTypeID type) {

final int z = javaWriter.getLocalVariable(statement.loopVariables[0].variable).local;
javaWriter.storeInt(z);
javaWriter.label(startOfLoopBody);
javaWriter.label(bytecodeLoopLabels.startOfLoopBody);
javaWriter.dup();
javaWriter.loadInt(z);
javaWriter.ifICmpLE(afterLoop);
javaWriter.ifICmpLE(bytecodeLoopLabels.afterLoop);

statement.getContent().accept(statementVisitor);
javaWriter.label(endOfLoopBody);
javaWriter.label(bytecodeLoopLabels.endOfLoopBody);
javaWriter.iinc(z);
}

Expand All @@ -70,34 +66,34 @@ public void visitStringCharacterIterator() {

public void visitIteratorIterator(Type targetType) {
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.ITERABLE, "iterator", "()Ljava/lang/Iterator;", 0));
javaWriter.label(startOfLoopBody);
javaWriter.label(bytecodeLoopLabels.startOfLoopBody);
javaWriter.dup();
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.ITERATOR, "hasNext", "()Z", 0));
javaWriter.ifEQ(afterLoop);
javaWriter.ifEQ(bytecodeLoopLabels.afterLoop);
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.ITERATOR, "next", "()Ljava/lang/Object;", 0, true));
javaWriter.checkCast(targetType);
final JavaLocalVariableInfo variable = javaWriter.getLocalVariable(statement.loopVariables[0].variable);
javaWriter.store(variable.type, variable.local);

statement.getContent().accept(statementVisitor);
javaWriter.label(endOfLoopBody);
javaWriter.label(bytecodeLoopLabels.endOfLoopBody);
}

private void handleArray(final int z, final JavaLocalVariableInfo arrayTypeInfo) {
if (statement.list.type.isOptional()) {
javaWriter.dup();
javaWriter.ifNull(afterLoop);
javaWriter.ifNull(bytecodeLoopLabels.afterLoop);
}

javaWriter.iConst0();
javaWriter.storeInt(z);

javaWriter.label(startOfLoopBody);
javaWriter.label(bytecodeLoopLabels.startOfLoopBody);
javaWriter.dup();
javaWriter.arrayLength();
javaWriter.loadInt(z);

javaWriter.ifICmpLE(afterLoop);
javaWriter.ifICmpLE(bytecodeLoopLabels.afterLoop);
javaWriter.dup();
javaWriter.loadInt(z);

Expand All @@ -116,17 +112,17 @@ private void handleArray(final int z, final JavaLocalVariableInfo arrayTypeInfo)
javaWriter.store(arrayTypeInfo.type, arrayTypeInfo.local);
statement.getContent().accept(statementVisitor);

javaWriter.label(endOfLoopBody);
javaWriter.label(bytecodeLoopLabels.endOfLoopBody);
javaWriter.iinc(z);
}

public void visitCustomIterator() {
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(new JavaClass("java.lang", "Iterable", JavaClass.Kind.INTERFACE), "iterator", "()Ljava/util/Iterator;", 0));

javaWriter.label(startOfLoopBody);
javaWriter.label(bytecodeLoopLabels.startOfLoopBody);
javaWriter.dup();
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.ITERATOR, "hasNext", "()Z", 0));
javaWriter.ifEQ(afterLoop);
javaWriter.ifEQ(bytecodeLoopLabels.afterLoop);
javaWriter.dup();
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.ITERATOR, "next", "()Ljava/lang/Object;", 0, true));

Expand All @@ -135,17 +131,17 @@ public void visitCustomIterator() {
javaWriter.store(keyVariable.type, keyVariable.local);

statement.getContent().accept(statementVisitor);
javaWriter.label(endOfLoopBody);
javaWriter.label(bytecodeLoopLabels.endOfLoopBody);
}

public void visitAssocKeyIterator() {
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.MAP, "keySet", "()Ljava/util/Set;", 0));
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.COLLECTION, "iterator", "()Ljava/util/Iterator;", 0));

javaWriter.label(startOfLoopBody);
javaWriter.label(bytecodeLoopLabels.startOfLoopBody);
javaWriter.dup();
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.ITERATOR, "hasNext", "()Z", 0));
javaWriter.ifEQ(afterLoop);
javaWriter.ifEQ(bytecodeLoopLabels.afterLoop);
javaWriter.dup();
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.ITERATOR, "next", "()Ljava/lang/Object;", 0, true));

Expand All @@ -154,17 +150,17 @@ public void visitAssocKeyIterator() {
javaWriter.store(keyVariable.type, keyVariable.local);

statement.getContent().accept(statementVisitor);
javaWriter.label(endOfLoopBody);
javaWriter.label(bytecodeLoopLabels.endOfLoopBody);
}

public void visitAssocKeyValueIterator() {
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.MAP, "entrySet", "()Ljava/util/Set;", 0));
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.COLLECTION, "iterator", "()Ljava/util/Iterator;", 0));

javaWriter.label(startOfLoopBody);
javaWriter.label(bytecodeLoopLabels.startOfLoopBody);
javaWriter.dup();
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.ITERATOR, "hasNext", "()Z", 0));
javaWriter.ifEQ(afterLoop);
javaWriter.ifEQ(bytecodeLoopLabels.afterLoop);
javaWriter.dup();
javaWriter.invokeInterface(JavaNativeMethod.getVirtual(JavaClass.ITERATOR, "next", "()Ljava/lang/Object;", 0, true));
javaWriter.checkCast(Type.getType(Map.Entry.class));
Expand All @@ -183,7 +179,7 @@ public void visitAssocKeyValueIterator() {
javaWriter.store(valueVariable.type, valueVariable.local);

statement.getContent().accept(statementVisitor);
javaWriter.label(endOfLoopBody);
javaWriter.label(bytecodeLoopLabels.endOfLoopBody);
}

private void downCast(int typeNumber, Type t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public Boolean visitForeach(ForeachStatement statement) {
Label startOfLoopBody = new Label();
Label endOfLoopBody = new Label();
Label afterLoop = new Label();
context.setLoopLabels(statement, new BytecodeLoopLabels(startOfLoopBody, endOfLoopBody, afterLoop));
BytecodeLoopLabels bytecodeLoopLabels = new BytecodeLoopLabels(startOfLoopBody, endOfLoopBody, afterLoop);
context.setLoopLabels(statement, bytecodeLoopLabels);


//Compile Array/Collection
Expand All @@ -125,7 +126,7 @@ public Boolean visitForeach(ForeachStatement statement) {
}

//javaWriter.label(min);
JavaForeachWriter iteratorWriter = new JavaForeachWriter(this, statement, startOfLoopBody, endOfLoopBody, afterLoop);
JavaForeachWriter iteratorWriter = new JavaForeachWriter(this, statement, bytecodeLoopLabels);
if (statement.iterator.method.method instanceof BuiltinMethodSymbol) {
switch ((BuiltinMethodSymbol) statement.iterator.method.method) {
case ITERATOR_INT_RANGE:
Expand Down

0 comments on commit 03ef6f7

Please sign in to comment.