Skip to content

Commit

Permalink
🦄 refactor: Redesign logical labels 3
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 3, 2024
1 parent ab6b526 commit e4f2a53
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 18 deletions.
5 changes: 5 additions & 0 deletions scripts/ts/test/test.logical.operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ class Test {
public logicalNot_JT_IJ_Z(a: int, b: long): boolean {
return !(a < b);
}

// public logicalOr_II_Z(a: int, b: int): boolean {
// return (a > 0) || (b > 0);
// }
}

console.log(new Test().logicalEQEQ_DD_Z(1, 2));
Expand Down Expand Up @@ -306,3 +310,4 @@ console.log(new Test().logicalNot_JE_DD_Z(1, 2));
console.log(new Test().logicalNot_JE_FF_Z(1, 2));
console.log(new Test().logicalNot_JE_II_Z(1, 2));
console.log(new Test().logicalNot_JE_IJ_Z(1, 2));
// console.log(new Test().logicalOr_II_Z(1, 2));
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstUnaryExpr;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstNumber;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;
Expand Down Expand Up @@ -103,6 +104,14 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstB
stackManipulation = Ts2JavaAstBinaryOp.getLogical(
functionContext, ast.getOp(), upCaseType, logicalNot);
break;
case LogicalAnd:
stackManipulation = Ts2JavaAstBinaryOp.getLogicalAndStackManipulation(
functionContext, upCaseType);
break;
case LogicalOr:
stackManipulation = Ts2JavaAstBinaryOp.getLogicalOrStackManipulation(
functionContext, upCaseType);
break;
// case BitAnd:
// stackManipulation = Ts2JavaAstBinaryOp.getBitAndStackManipulation(functionContext);
// break;
Expand All @@ -125,6 +134,8 @@ private TypeDescription manipulateExpression(JavaFunctionContext functionContext
return new Ts2JavaAstBinExpr().manipulate(functionContext, expression.as(Swc4jAstBinExpr.class));
case Ident:
return new Ts2JavaAstIdent().manipulate(functionContext, expression.as(Swc4jAstIdent.class));
case Number:
return new Ts2JavaAstNumber().manipulate(functionContext, expression.as(Swc4jAstNumber.class));
case UnaryExpr:
return new Ts2JavaAstUnaryExpr().manipulate(functionContext, expression.as(Swc4jAstUnaryExpr.class));
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public static StackManipulation getLogical(
TypeDescription type,
boolean logicalNot) {
functionContext.increaseLogicalDepth();
Label labelFalse = functionContext.getLogicalLabels().getLabelFalse();
List<StackManipulation> stackManipulations = new ArrayList<>();
final Label labelFalse = functionContext.getLogicalLabels().getLabelFalse();
final List<StackManipulation> stackManipulations = new ArrayList<>();
if (type.represents(int.class)
|| type.represents(short.class)
|| type.represents(byte.class)
Expand Down Expand Up @@ -276,18 +276,23 @@ public static StackManipulation getLogical(
return new StackManipulation.Compound(stackManipulations);
}

public static StackManipulation getLogicalAndStackManipulation(
JavaFunctionContext functionContext,
TypeDescription type) {
return StackManipulation.Trivial.INSTANCE;
}

private static StackManipulation getLogicalEnd(JavaLogicalLabels logicalLabels) {
return new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
final int size = logicalLabels.size();
if (size >= 3) {
Label labelTrue = logicalLabels.get(2);
if (logicalLabels.hasLabelTrue()) {
Label labelTrue = logicalLabels.getLabelTrue();
methodVisitor.visitLabel(labelTrue);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
}
Label labelFalse = logicalLabels.getLabelFalse();
Label labelEnd = logicalLabels.getLabelEnd();
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
methodVisitor.visitInsn(Opcodes.ICONST_1);
methodVisitor.visitJumpInsn(Opcodes.GOTO, labelEnd);
methodVisitor.visitLabel(labelFalse);
Expand All @@ -299,6 +304,12 @@ private static StackManipulation getLogicalEnd(JavaLogicalLabels logicalLabels)
});
}

public static StackManipulation getLogicalOrStackManipulation(
JavaFunctionContext functionContext,
TypeDescription type) {
return StackManipulation.Trivial.INSTANCE;
}

public static Multiplication getMultiplication(TypeDescription type) {
if (type.represents(int.class)) {
return Multiplication.INTEGER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public JavaLogicalLabels() {
reset();
}

public Label append() {
Label label = new Label();
labels.add(label);
return label;
}

public Label get(int index) {
return labels.get(index);
}
Expand All @@ -41,13 +47,21 @@ public Label getLabelFalse() {
return labels.get(1);
}

public Label getLabelTrue() {
return labels.get(2);
}

public Label getLastLabel() {
return labels.get(labels.size() - 1);
}

public boolean hasLabelTrue() {
return labels.size() > 2;
}

public void reset() {
labels.clear();
labels.add(new Label()); // Label for Return
labels.add(new Label()); // Label for False
}

public int size() {
return labels.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,45 @@ protected void init() {
}
}

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

/*
public logicalAnd_II_Z(II)Z
L0
Expand Down Expand Up @@ -227,15 +266,13 @@ public boolean logicalNot_EQ_II_Z(int a, int b) {
}

/*
public logicalAnd_II_Z(II)Z
public logicalOr_II_Z(II)Z
L0
LINENUMBER 87 L0
LINENUMBER 294 L0
ILOAD 1
IFGT L1
ILOAD 2
IF_ICMPEQ L1
ILOAD 1
ICONST_1
IF_ICMPLE L2
IFLE L2
L1
FRAME SAME
ICONST_1
Expand All @@ -250,11 +287,11 @@ public logicalAnd_II_Z(II)Z
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
MAXSTACK = 1
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);
return (a > 0) || (b > 0);
}

@Test
Expand Down

0 comments on commit e4f2a53

Please sign in to comment.