Skip to content

Commit

Permalink
🦄 refactor: Move label true
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Dec 15, 2024
1 parent 0ab7bc5 commit 3b14864
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,15 @@ public boolean isLabelSwitched() {
return labelSwitched;
}

public abstract boolean isLabelTrueRequired();

protected Size logicalClose(MethodVisitor methodVisitor) {
if (isTopBinExpr()) {
// This is the top bin expr. Let's close it.
Label labelClose = new Label();
if (isLabelTrueRequired()) {
methodVisitor.visitLabel(labelTrue);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
}
methodVisitor.visitInsn(Opcodes.ICONST_1);
methodVisitor.visitJumpInsn(Opcodes.GOTO, labelClose);
methodVisitor.visitLabel(labelFalse);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
methodVisitor.visitInsn(Opcodes.ICONST_0);
methodVisitor.visitLabel(labelClose);
methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER});
}
Label labelClose = new Label();
methodVisitor.visitInsn(Opcodes.ICONST_1);
methodVisitor.visitJumpInsn(Opcodes.GOTO, labelClose);
methodVisitor.visitLabel(labelFalse);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
methodVisitor.visitInsn(Opcodes.ICONST_0);
methodVisitor.visitLabel(labelClose);
methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER});
return StackManipulation.Size.ZERO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,14 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
final Label label = labelSwitched ? labelTrue : labelFalse;
sizes.add(Ts2JavaAstBinaryOp.getLogicalCompareStackManipulation(ast, resolvedOp, upCastType, label)
.apply(methodVisitor, context));
sizes.add(logicalClose(methodVisitor));
if (isTopBinExpr()) {
sizes.add(logicalClose(methodVisitor));
}
return aggregateSize(sizes);
}

@Override
public Swc4jAstBinaryOp getResolvedOp() {
return bangFlipped != labelSwitched ? op.getOppositeOperator() : op;
}

@Override
public boolean isLabelTrueRequired() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,15 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
if (!isRightBool) {
sizes.add(right.apply(methodVisitor, context));
if (!isRightLogical) {
if (labelSwitched) {
methodVisitor.visitJumpInsn(opcodeCompareTrue, labelTrue);
} else {
methodVisitor.visitJumpInsn(opcodeCompareFalse, labelFalse);
}
methodVisitor.visitJumpInsn(
isTopBinExpr() ? opcodeCompareFalse : opcodeCompareTrue,
labelSwitched ? labelTrue : labelFalse);
}
}
if (isTopBinExpr()) {
methodVisitor.visitLabel(labelTrue);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
}
break;
}
default:
Expand All @@ -157,7 +159,7 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
SimpleFreeMarkerFormat.format("Bin expr op ${op} is not supported.",
SimpleMap.of("op", ast.getOp().name())));
}
if (!ignoreClose) {
if (!ignoreClose && isTopBinExpr()) {
sizes.add(logicalClose(methodVisitor));
}
return aggregateSize(sizes);
Expand Down Expand Up @@ -189,15 +191,22 @@ public void compile() {
if (isLeftLogical && isRightLogical) {
Ts2JavaAstBinExprLogical leftLogical = left.as(Ts2JavaAstBinExprLogical.class);
Ts2JavaAstBinExprLogical rightLogical = right.as(Ts2JavaAstBinExprLogical.class);
// TODO
// if (op == Swc4jAstBinaryOp.LogicalAnd) {
labelTrue = leftLogical.getLabelTrue();
labelFalse = rightLogical.getLabelFalse();
leftLogical.setLabelFalse(labelFalse);
rightLogical.setLabelTrue(labelTrue);
// } else {
// throw new Ts2JavaAstException(ast, "Logical OR (||) is not supported.");
// }
switch (op) {
case LogicalAnd:
labelTrue = leftLogical.getLabelTrue();
labelFalse = rightLogical.getLabelFalse();
leftLogical.setLabelFalse(labelFalse);
rightLogical.setLabelTrue(labelTrue);
break;
case LogicalOr:
labelTrue = leftLogical.getLabelTrue();
labelFalse = rightLogical.getLabelFalse();
leftLogical.setLabelFalse(labelFalse);
rightLogical.setLabelTrue(labelTrue);
break;
default:
throw new Ts2JavaAstException(ast, "Logical OR (||) is not supported.");
}
} else if (isLeftLogical) {
Ts2JavaAstBinExprLogical leftLogical = left.as(Ts2JavaAstBinExprLogical.class);
labelTrue = leftLogical.getLabelTrue();
Expand All @@ -213,9 +222,4 @@ public void compile() {
public Swc4jAstBinaryOp getResolvedOp() {
return bangFlipped ? op.getOppositeOperator() : op;
}

@Override
public boolean isLabelTrueRequired() {
return getResolvedOp() == Swc4jAstBinaryOp.LogicalOr;
}
}

0 comments on commit 3b14864

Please sign in to comment.