From 2764f038f3e235bab0cab38574646481409f7deb Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Mon, 4 Nov 2024 08:58:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=B3=20chore:=20Upgrade=20swc4j=20to=20?= =?UTF-8?q?v1.2.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- .../buddy/ts2java/ast/Ts2JavaAstBinExpr.java | 70 +--- .../buddy/ts2java/ast/Ts2JavaAstBinaryOp.java | 376 ++++++++++-------- 3 files changed, 220 insertions(+), 228 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index cedd974..04d4449 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -70,7 +70,7 @@ object Config { const val JAVA_VERSION = "1.8" const val JAVET = "4.0.0" const val JAVET_BUDDY = "0.5.0" - const val JAVET_SWC4J = "1.1.0" + const val JAVET_SWC4J = "1.2.0" const val JUNIT = "5.11.3" } } diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinExpr.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinExpr.java index e62bc40..fa6df97 100644 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinExpr.java +++ b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinExpr.java @@ -19,6 +19,7 @@ import com.caoccao.javet.buddy.ts2java.compiler.JavaClassCast; import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext; import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException; +import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp; import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr; import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent; import com.caoccao.javet.swc4j.ast.expr.Swc4jAstUnaryExpr; @@ -65,64 +66,17 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstB // Add the type cast for right expression if possible. JavaClassCast.getUpCastStackManipulation(rightType, upCaseType).ifPresent(stackManipulations::add); StackManipulation stackManipulation; - switch (ast.getOp()) { - case Add: - stackManipulation = Ts2JavaAstBinaryOp.getAddition(upCaseType); - break; - case Div: - stackManipulation = Ts2JavaAstBinaryOp.getDivision(upCaseType); - break; - case LShift: - stackManipulation = Ts2JavaAstBinaryOp.getShiftLeft(upCaseType); - break; - case Mod: - stackManipulation = Ts2JavaAstBinaryOp.getRemainder(upCaseType); - break; - case Mul: - stackManipulation = Ts2JavaAstBinaryOp.getMultiplication(upCaseType); - break; - case RShift: - stackManipulation = Ts2JavaAstBinaryOp.getShiftRight(upCaseType); - break; - case Sub: - stackManipulation = Ts2JavaAstBinaryOp.getSubtraction(upCaseType); - break; - case ZeroFillRShift: - stackManipulation = Ts2JavaAstBinaryOp.getZeroFillShiftRight(upCaseType); - break; - case Exp: - stackManipulation = Ts2JavaAstBinaryOp.getExp(); - break; - case Gt: - case GtEq: - case Lt: - case LtEq: - case EqEq: - case EqEqEq: - case NotEq: - case NotEqEq: - 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; -// case BitOr: -// stackManipulation = Ts2JavaAstBinaryOp.getBitOrStackManipulation(functionContext); -// break; - default: - throw new Ts2JavaAstException( - ast, - SimpleFreeMarkerFormat.format("BinExpr op ${op} is not supported.", - SimpleMap.of("op", ast.getOp().name()))); + Swc4jAstBinaryOp binaryOp = ast.getOp(); + if (binaryOp.isArithmeticOperator()) { + stackManipulation = Ts2JavaAstBinaryOp.getArithmetic(binaryOp, upCaseType); + } else if (binaryOp.isLogicalOperator()) { + stackManipulation = Ts2JavaAstBinaryOp.getLogical(functionContext, binaryOp, upCaseType, logicalNot); +// } else if (binaryOp.isBitOperator()) { + } else { + throw new Ts2JavaAstException( + ast, + SimpleFreeMarkerFormat.format("BinExpr op ${op} is not supported.", + SimpleMap.of("op", ast.getOp().name()))); } stackManipulations.add(stackManipulation); return upCaseType; diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java index c9bd835..c5d2f3c 100644 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java +++ b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java @@ -38,7 +38,7 @@ public final class Ts2JavaAstBinaryOp { private Ts2JavaAstBinaryOp() { } - public static Addition getAddition(TypeDescription type) { + private static Addition getAddition(TypeDescription type) { if (type.represents(int.class)) { return Addition.INTEGER; } else if (type.represents(long.class)) { @@ -53,6 +53,33 @@ public static Addition getAddition(TypeDescription type) { SimpleMap.of("type", type.getName()))); } + public static StackManipulation getArithmetic(Swc4jAstBinaryOp binaryOp, TypeDescription upCaseType) { + switch (binaryOp) { + case Add: + return getAddition(upCaseType); + case Div: + return getDivision(upCaseType); + case LShift: + return getShiftLeft(upCaseType); + case Mod: + return getRemainder(upCaseType); + case Mul: + return getMultiplication(upCaseType); + case RShift: + return getShiftRight(upCaseType); + case Sub: + return getSubtraction(upCaseType); + case ZeroFillRShift: + return getZeroFillShiftRight(upCaseType); + case Exp: + return getExp(); + default: + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Binary op ${op} is not supported.", + SimpleMap.of("op", binaryOp.name()))); + } + } + public static StackManipulation getBitAndStackManipulation(JavaFunctionContext functionContext) { return new StackManipulation.Simple(( MethodVisitor methodVisitor, @@ -71,7 +98,7 @@ public static StackManipulation getBitOrStackManipulation(JavaFunctionContext fu }); } - public static Division getDivision(TypeDescription type) { + private static Division getDivision(TypeDescription type) { if (type.represents(int.class)) { return Division.INTEGER; } else if (type.represents(long.class)) { @@ -86,7 +113,7 @@ public static Division getDivision(TypeDescription type) { SimpleMap.of("type", type.getName()))); } - public static StackManipulation getExp() { + private static StackManipulation getExp() { return new StackManipulation.Simple(( MethodVisitor methodVisitor, Implementation.Context implementationContext) -> { @@ -108,169 +135,180 @@ public static StackManipulation getLogical( functionContext.increaseLogicalDepth(); final Label labelFalse = functionContext.getLogicalLabels().getLabelFalse(); final List stackManipulations = new ArrayList<>(); - if (type.represents(int.class) - || type.represents(short.class) - || type.represents(byte.class) - || type.represents(char.class)) { - int opcodeCompare; - switch (binaryOp) { - case Gt: - opcodeCompare = logicalNot ? Opcodes.IF_ICMPGT : Opcodes.IF_ICMPLE; - break; - case GtEq: - opcodeCompare = logicalNot ? Opcodes.IF_ICMPGE : Opcodes.IF_ICMPLT; - break; - case Lt: - opcodeCompare = logicalNot ? Opcodes.IF_ICMPLT : Opcodes.IF_ICMPGE; - break; - case LtEq: - opcodeCompare = logicalNot ? Opcodes.IF_ICMPLE : Opcodes.IF_ICMPGT; - break; - case EqEq: - case EqEqEq: - opcodeCompare = logicalNot ? Opcodes.IF_ICMPEQ : Opcodes.IF_ICMPNE; - break; - case NotEq: - case NotEqEq: - opcodeCompare = logicalNot ? Opcodes.IF_ICMPNE : Opcodes.IF_ICMPEQ; - break; - default: - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", - SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); - } - stackManipulations.add(new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitJumpInsn(opcodeCompare, labelFalse); - return new StackManipulation.Size(-1, 0); - })); - } else if (type.represents(long.class)) { - int opcodeCompare; - switch (binaryOp) { - case Gt: - opcodeCompare = logicalNot ? Opcodes.IFGT : Opcodes.IFLE; - break; - case GtEq: - opcodeCompare = logicalNot ? Opcodes.IFGE : Opcodes.IFLT; - break; - case Lt: - opcodeCompare = logicalNot ? Opcodes.IFLT : Opcodes.IFGE; - break; - case LtEq: - opcodeCompare = logicalNot ? Opcodes.IFLE : Opcodes.IFGT; - break; - case EqEq: - case EqEqEq: - opcodeCompare = logicalNot ? Opcodes.IFEQ : Opcodes.IFNE; - break; - case NotEq: - case NotEqEq: - opcodeCompare = logicalNot ? Opcodes.IFNE : Opcodes.IFEQ; - break; - default: - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", - SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); - } - stackManipulations.add(new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(Opcodes.LCMP); - methodVisitor.visitJumpInsn(opcodeCompare, labelFalse); - return new StackManipulation.Size(-2, 0); - })); - } else if (type.represents(float.class)) { - int opcodeCompare1; - int opcodeCompare2; - switch (binaryOp) { - case Gt: - opcodeCompare1 = Opcodes.FCMPL; - opcodeCompare2 = logicalNot ? Opcodes.IFGT : Opcodes.IFLE; - break; - case GtEq: - opcodeCompare1 = Opcodes.FCMPL; - opcodeCompare2 = logicalNot ? Opcodes.IFGE : Opcodes.IFLT; - break; - case Lt: - opcodeCompare1 = Opcodes.FCMPG; - opcodeCompare2 = logicalNot ? Opcodes.IFLT : Opcodes.IFGE; - break; - case LtEq: - opcodeCompare1 = Opcodes.FCMPG; - opcodeCompare2 = logicalNot ? Opcodes.IFLE : Opcodes.IFGT; - break; - case EqEq: - case EqEqEq: - opcodeCompare1 = Opcodes.FCMPL; - opcodeCompare2 = logicalNot ? Opcodes.IFEQ : Opcodes.IFNE; - break; - case NotEq: - case NotEqEq: - opcodeCompare1 = Opcodes.FCMPL; - opcodeCompare2 = logicalNot ? Opcodes.IFNE : Opcodes.IFEQ; - break; - default: - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", - SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); - } - stackManipulations.add(new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(opcodeCompare1); - methodVisitor.visitJumpInsn(opcodeCompare2, labelFalse); - return new StackManipulation.Size(-1, 0); - })); - } else if (type.represents(double.class)) { - int opcodeCompare1; - int opcodeCompare2; - switch (binaryOp) { - case Gt: - opcodeCompare1 = Opcodes.DCMPL; - opcodeCompare2 = logicalNot ? Opcodes.IFGT : Opcodes.IFLE; - break; - case GtEq: - opcodeCompare1 = Opcodes.DCMPL; - opcodeCompare2 = logicalNot ? Opcodes.IFGE : Opcodes.IFLT; - break; - case Lt: - opcodeCompare1 = Opcodes.DCMPG; - opcodeCompare2 = logicalNot ? Opcodes.IFLT : Opcodes.IFGE; - break; - case LtEq: - opcodeCompare1 = Opcodes.DCMPG; - opcodeCompare2 = logicalNot ? Opcodes.IFLE : Opcodes.IFGT; - break; - case EqEq: - case EqEqEq: - opcodeCompare1 = Opcodes.DCMPL; - opcodeCompare2 = logicalNot ? Opcodes.IFEQ : Opcodes.IFNE; - break; - case NotEq: - case NotEqEq: - opcodeCompare1 = Opcodes.DCMPL; - opcodeCompare2 = logicalNot ? Opcodes.IFNE : Opcodes.IFEQ; - break; - default: + switch (binaryOp) { + case LogicalAnd: + // TODO + break; + case LogicalOr: + // TODO + break; + default: { + if (type.represents(int.class) + || type.represents(short.class) + || type.represents(byte.class) + || type.represents(char.class)) { + int opcodeCompare; + switch (binaryOp) { + case Gt: + opcodeCompare = logicalNot ? Opcodes.IF_ICMPGT : Opcodes.IF_ICMPLE; + break; + case GtEq: + opcodeCompare = logicalNot ? Opcodes.IF_ICMPGE : Opcodes.IF_ICMPLT; + break; + case Lt: + opcodeCompare = logicalNot ? Opcodes.IF_ICMPLT : Opcodes.IF_ICMPGE; + break; + case LtEq: + opcodeCompare = logicalNot ? Opcodes.IF_ICMPLE : Opcodes.IF_ICMPGT; + break; + case EqEq: + case EqEqEq: + opcodeCompare = logicalNot ? Opcodes.IF_ICMPEQ : Opcodes.IF_ICMPNE; + break; + case NotEq: + case NotEqEq: + opcodeCompare = logicalNot ? Opcodes.IF_ICMPNE : Opcodes.IF_ICMPEQ; + break; + default: + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", + SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); + } + stackManipulations.add(new StackManipulation.Simple(( + MethodVisitor methodVisitor, + Implementation.Context implementationContext) -> { + methodVisitor.visitJumpInsn(opcodeCompare, labelFalse); + return new StackManipulation.Size(-1, 0); + })); + } else if (type.represents(long.class)) { + int opcodeCompare; + switch (binaryOp) { + case Gt: + opcodeCompare = logicalNot ? Opcodes.IFGT : Opcodes.IFLE; + break; + case GtEq: + opcodeCompare = logicalNot ? Opcodes.IFGE : Opcodes.IFLT; + break; + case Lt: + opcodeCompare = logicalNot ? Opcodes.IFLT : Opcodes.IFGE; + break; + case LtEq: + opcodeCompare = logicalNot ? Opcodes.IFLE : Opcodes.IFGT; + break; + case EqEq: + case EqEqEq: + opcodeCompare = logicalNot ? Opcodes.IFEQ : Opcodes.IFNE; + break; + case NotEq: + case NotEqEq: + opcodeCompare = logicalNot ? Opcodes.IFNE : Opcodes.IFEQ; + break; + default: + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", + SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); + } + stackManipulations.add(new StackManipulation.Simple(( + MethodVisitor methodVisitor, + Implementation.Context implementationContext) -> { + methodVisitor.visitInsn(Opcodes.LCMP); + methodVisitor.visitJumpInsn(opcodeCompare, labelFalse); + return new StackManipulation.Size(-2, 0); + })); + } else if (type.represents(float.class)) { + int opcodeCompare1; + int opcodeCompare2; + switch (binaryOp) { + case Gt: + opcodeCompare1 = Opcodes.FCMPL; + opcodeCompare2 = logicalNot ? Opcodes.IFGT : Opcodes.IFLE; + break; + case GtEq: + opcodeCompare1 = Opcodes.FCMPL; + opcodeCompare2 = logicalNot ? Opcodes.IFGE : Opcodes.IFLT; + break; + case Lt: + opcodeCompare1 = Opcodes.FCMPG; + opcodeCompare2 = logicalNot ? Opcodes.IFLT : Opcodes.IFGE; + break; + case LtEq: + opcodeCompare1 = Opcodes.FCMPG; + opcodeCompare2 = logicalNot ? Opcodes.IFLE : Opcodes.IFGT; + break; + case EqEq: + case EqEqEq: + opcodeCompare1 = Opcodes.FCMPL; + opcodeCompare2 = logicalNot ? Opcodes.IFEQ : Opcodes.IFNE; + break; + case NotEq: + case NotEqEq: + opcodeCompare1 = Opcodes.FCMPL; + opcodeCompare2 = logicalNot ? Opcodes.IFNE : Opcodes.IFEQ; + break; + default: + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", + SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); + } + stackManipulations.add(new StackManipulation.Simple(( + MethodVisitor methodVisitor, + Implementation.Context implementationContext) -> { + methodVisitor.visitInsn(opcodeCompare1); + methodVisitor.visitJumpInsn(opcodeCompare2, labelFalse); + return new StackManipulation.Size(-1, 0); + })); + } else if (type.represents(double.class)) { + int opcodeCompare1; + int opcodeCompare2; + switch (binaryOp) { + case Gt: + opcodeCompare1 = Opcodes.DCMPL; + opcodeCompare2 = logicalNot ? Opcodes.IFGT : Opcodes.IFLE; + break; + case GtEq: + opcodeCompare1 = Opcodes.DCMPL; + opcodeCompare2 = logicalNot ? Opcodes.IFGE : Opcodes.IFLT; + break; + case Lt: + opcodeCompare1 = Opcodes.DCMPG; + opcodeCompare2 = logicalNot ? Opcodes.IFLT : Opcodes.IFGE; + break; + case LtEq: + opcodeCompare1 = Opcodes.DCMPG; + opcodeCompare2 = logicalNot ? Opcodes.IFLE : Opcodes.IFGT; + break; + case EqEq: + case EqEqEq: + opcodeCompare1 = Opcodes.DCMPL; + opcodeCompare2 = logicalNot ? Opcodes.IFEQ : Opcodes.IFNE; + break; + case NotEq: + case NotEqEq: + opcodeCompare1 = Opcodes.DCMPL; + opcodeCompare2 = logicalNot ? Opcodes.IFNE : Opcodes.IFEQ; + break; + default: + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", + SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); + } + stackManipulations.add(new StackManipulation.Simple(( + MethodVisitor methodVisitor, + Implementation.Context implementationContext) -> { + methodVisitor.visitInsn(opcodeCompare1); + methodVisitor.visitJumpInsn(opcodeCompare2, labelFalse); + return new StackManipulation.Size(-2, 0); + })); + } else { throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", - SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); + SimpleFreeMarkerFormat.format("Unsupported type ${type} in logical operation.", + SimpleMap.of("type", type.getName()))); + } + break; } - stackManipulations.add(new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(opcodeCompare1); - methodVisitor.visitJumpInsn(opcodeCompare2, labelFalse); - return new StackManipulation.Size(-2, 0); - })); - } else { - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in logical operation.", - SimpleMap.of("type", type.getName()))); } if (functionContext.getLogicalDepth() == 1) { - stackManipulations.add(getLogicalEnd(functionContext.getLogicalLabels())); + stackManipulations.add(getLogicalClose(functionContext.getLogicalLabels())); } functionContext.decreaseLogicalDepth(); return new StackManipulation.Compound(stackManipulations); @@ -282,7 +320,7 @@ public static StackManipulation getLogicalAndStackManipulation( return StackManipulation.Trivial.INSTANCE; } - private static StackManipulation getLogicalEnd(JavaLogicalLabels logicalLabels) { + private static StackManipulation getLogicalClose(JavaLogicalLabels logicalLabels) { return new StackManipulation.Simple(( MethodVisitor methodVisitor, Implementation.Context implementationContext) -> { @@ -310,7 +348,7 @@ public static StackManipulation getLogicalOrStackManipulation( return StackManipulation.Trivial.INSTANCE; } - public static Multiplication getMultiplication(TypeDescription type) { + private static Multiplication getMultiplication(TypeDescription type) { if (type.represents(int.class)) { return Multiplication.INTEGER; } else if (type.represents(long.class)) { @@ -325,7 +363,7 @@ public static Multiplication getMultiplication(TypeDescription type) { SimpleMap.of("type", type.getName()))); } - public static Remainder getRemainder(TypeDescription type) { + private static Remainder getRemainder(TypeDescription type) { if (type.represents(int.class)) { return Remainder.INTEGER; } else if (type.represents(long.class)) { @@ -340,7 +378,7 @@ public static Remainder getRemainder(TypeDescription type) { SimpleMap.of("type", type.getName()))); } - public static ShiftLeft getShiftLeft(TypeDescription type) { + private static ShiftLeft getShiftLeft(TypeDescription type) { if (type.represents(int.class)) { return ShiftLeft.INTEGER; } else if (type.represents(long.class)) { @@ -351,7 +389,7 @@ public static ShiftLeft getShiftLeft(TypeDescription type) { SimpleMap.of("type", type.getName()))); } - public static ShiftRight getShiftRight(TypeDescription type) { + private static ShiftRight getShiftRight(TypeDescription type) { if (type.represents(int.class)) { return ShiftRight.INTEGER; } else if (type.represents(long.class)) { @@ -362,7 +400,7 @@ public static ShiftRight getShiftRight(TypeDescription type) { SimpleMap.of("type", type.getName()))); } - public static Subtraction getSubtraction(TypeDescription type) { + private static Subtraction getSubtraction(TypeDescription type) { if (type.represents(int.class)) { return Subtraction.INTEGER; } else if (type.represents(long.class)) { @@ -377,7 +415,7 @@ public static Subtraction getSubtraction(TypeDescription type) { SimpleMap.of("type", type.getName()))); } - public static StackManipulation getZeroFillShiftRight(TypeDescription type) { + private static StackManipulation getZeroFillShiftRight(TypeDescription type) { if (type.represents(int.class)) { return new StackManipulation.Simple( (MethodVisitor methodVisitor, Implementation.Context implementationContext) -> {