Skip to content

Commit

Permalink
🦄 refactor: Redesign logical labels
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 1, 2024
1 parent 0c2480c commit c53da40
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstB
case EqEqEq:
case NotEq:
case NotEqEq:
stackManipulation = Ts2JavaAstBinaryOp.getLogicalStackManipulation(ast.getOp(), upCaseType);
stackManipulation = Ts2JavaAstBinaryOp.getLogicalStackManipulation(
functionContext, ast.getOp(), upCaseType);
break;
// case BitAnd:
// stackManipulation = Ts2JavaAstBinaryOp.getBitAndStackManipulation(functionContext);
// break;
// case BitOr:
// stackManipulation = Ts2JavaAstBinaryOp.getBitOrStackManipulation(functionContext);
// break;
default:
throw new Ts2JavaAstException(
ast,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaException;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
Expand Down Expand Up @@ -49,6 +50,24 @@ public static Addition getAddition(TypeDescription type) {
SimpleMap.of("type", type.getName())));
}

public static StackManipulation getBitAndStackManipulation(JavaFunctionContext functionContext) {
return new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
// TODO
return new StackManipulation.Size(-1, 0);
});
}

public static StackManipulation getBitOrStackManipulation(JavaFunctionContext functionContext) {
return new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
// TODO
return new StackManipulation.Size(-1, 0);
});
}

public static Division getDivision(TypeDescription type) {
if (type.represents(int.class)) {
return Division.INTEGER;
Expand All @@ -64,9 +83,12 @@ public static Division getDivision(TypeDescription type) {
SimpleMap.of("type", type.getName())));
}

public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp binaryOp, TypeDescription type) {
Label labelFalse = new Label();
Label labelTrue = new Label();
public static StackManipulation getLogicalStackManipulation(
JavaFunctionContext functionContext,
Swc4jAstBinaryOp binaryOp,
TypeDescription type) {
functionContext.increaseLogicalDepth();
Label labelFalse = functionContext.getLogicalLabels().get(functionContext.getLogicalLabels().size() - 2);
List<StackManipulation> stackManipulations = new ArrayList<>();
if (type.represents(int.class)
|| type.represents(short.class)
Expand Down Expand Up @@ -229,19 +251,36 @@ public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp bin
SimpleFreeMarkerFormat.format("Unsupported type ${type} in logical operation.",
SimpleMap.of("type", type.getName())));
}
stackManipulations.add(new StackManipulation.Simple((
if (functionContext.getLogicalDepth() == 1) {
stackManipulations.add(getLogicalStackManipulationEnd(functionContext.getLogicalLabels()));
}
functionContext.decreaseLogicalDepth();
return new StackManipulation.Compound(stackManipulations);
}

private static StackManipulation getLogicalStackManipulationEnd(List<Label> logicalLabels) {
// Make a copy.
final List<Label> labels = new ArrayList<>(logicalLabels);
return new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
final int size = labels.size();
if (size >= 3) {
Label labelTrue = labels.get(size - 3);
methodVisitor.visitLabel(labelTrue);
}
Label labelFalse = labels.get(size - 2);
Label labelEnd = labels.get(size - 1);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
methodVisitor.visitInsn(Opcodes.ICONST_1);
methodVisitor.visitJumpInsn(Opcodes.GOTO, labelTrue);
methodVisitor.visitJumpInsn(Opcodes.GOTO, labelEnd);
methodVisitor.visitLabel(labelFalse);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
methodVisitor.visitInsn(Opcodes.ICONST_0);
methodVisitor.visitLabel(labelTrue);
methodVisitor.visitLabel(labelEnd);
methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER});
return StackManipulation.Size.ZERO;
}));
return new StackManipulation.Compound(stackManipulations);
});
}

public static Multiplication getMultiplication(TypeDescription type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.jar.asm.Label;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -32,8 +33,10 @@
public final class JavaFunctionContext {
private final boolean _static;
private final List<JavaLexicalScope> lexicalScopes;
private final List<Label> logicalLabels;
private final TypeDescription returnType;
private final List<StackManipulation> stackManipulations;
private int logicalDepth;
private int maxOffset;
private int nextOffset;

Expand All @@ -42,6 +45,8 @@ public JavaFunctionContext(boolean _static, TypeDescription returnType) {
nextOffset = _static ? 0 : 1;
maxOffset = nextOffset;
this.lexicalScopes = SimpleList.of(new JavaLexicalScope(0));
logicalDepth = 0;
logicalLabels = new ArrayList<>();
this.returnType = Objects.requireNonNull(returnType);
this.stackManipulations = new ArrayList<>();
}
Expand All @@ -56,6 +61,14 @@ public void addLocalVariable(JavaLocalVariable localVariable) {
}
}

public void decreaseLogicalDepth() {
logicalDepth--;
if (logicalDepth <= 0) {
logicalDepth = 0;
logicalLabels.clear();
}
}

public JavaLocalVariable getLocalVariable(String name) {
for (int lexicalScopeIndex = lexicalScopes.size() - 1; lexicalScopeIndex >= 0; lexicalScopeIndex--) {
JavaLexicalScope lexicalScope = lexicalScopes.get(lexicalScopeIndex);
Expand All @@ -69,6 +82,14 @@ public JavaLocalVariable getLocalVariable(String name) {
SimpleMap.of("name", name)));
}

public int getLogicalDepth() {
return logicalDepth;
}

public List<Label> getLogicalLabels() {
return logicalLabels;
}

public int getMaxOffset() {
return maxOffset;
}
Expand All @@ -92,6 +113,14 @@ public List<StackManipulation> getStackManipulations() {
return stackManipulations;
}

public void increaseLogicalDepth() {
logicalDepth++;
if (logicalDepth == 1) {
logicalLabels.add(new Label()); // Label for False
logicalLabels.add(new Label()); // Label for Return
}
}

public boolean isStatic() {
return _static;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ protected void init() {
}
}

/*
public logicalAnd_II_Z(II)Z
L0
LINENUMBER 86 L0
ILOAD 1
ILOAD 2
IF_ICMPNE L1
ILOAD 1
ICONST_1
IF_ICMPLE L1
ICONST_1
GOTO L2
L1
FRAME SAME
ICONST_0
L2
FRAME SAME1 I
IRETURN
L3
LOCALVARIABLE this Lcom/caoccao/javet/buddy/ts2java/ast/TestLogicalOperations; L0 L3 0
LOCALVARIABLE a I L0 L3 1
LOCALVARIABLE b I L0 L3 2
MAXSTACK = 2
MAXLOCALS = 3
*/
public boolean logicalAnd_II_Z(int a, int b) {
return a == b && a > 1;
}

/*
public logicalEQ_II_Z(II)Z
L0
Expand Down Expand Up @@ -171,6 +200,37 @@ public boolean logicalGT_II_Z(int a, int b) {
return a > b;
}

/*
public logicalAnd_II_Z(II)Z
L0
LINENUMBER 87 L0
ILOAD 1
ILOAD 2
IF_ICMPEQ L1
ILOAD 1
ICONST_1
IF_ICMPLE L2
L1
FRAME SAME
ICONST_1
GOTO L3
L2
FRAME SAME
ICONST_0
L3
FRAME SAME1 I
IRETURN
L4
LOCALVARIABLE this Lcom/caoccao/javet/buddy/ts2java/ast/TestLogicalOperations; L0 L4 0
LOCALVARIABLE a I L0 L4 1
LOCALVARIABLE b I L0 L4 2
MAXSTACK = 2
MAXLOCALS = 3
*/
public boolean logicalOr_II_Z(int a, int b) {
return (a == b || a > 1 || (b > 1 && a == 1)) && (a * 3 == b * 2 || a / 4 == b * 5);
}

@Test
public void testLogicalEQEQ_DD_Z() throws Exception {
Method method = clazz.getMethod("logicalEQEQ_DD_Z", double.class, double.class);
Expand Down

0 comments on commit c53da40

Please sign in to comment.