X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fjava%2FEqualsFunction.java;h=d2b627c84f6dca9b9072ef2e94bde22a546c1090;hp=c2d3e6d0f1bbf666089919b305f5b6495b45967a;hb=35e73f300e2101c436dcc23c9a0f9e12ecc81d59;hpb=1b4d8b692f40d946deb5db8280eb4ca5b36a75a7 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/EqualsFunction.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/EqualsFunction.java index c2d3e6d0f..d2b627c84 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/EqualsFunction.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/EqualsFunction.java @@ -1,76 +1,76 @@ -package org.simantics.scl.compiler.elaboration.java; - -import org.cojen.classfile.TypeDesc; -import org.objectweb.asm.Label; -import org.simantics.scl.compiler.constants.ComparisonFunction; -import org.simantics.scl.compiler.constants.FunctionValue; -import org.simantics.scl.compiler.internal.codegen.continuations.Cont; -import org.simantics.scl.compiler.internal.codegen.references.Val; -import org.simantics.scl.compiler.internal.codegen.utils.Constants; -import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder; -import org.simantics.scl.compiler.types.TVar; -import org.simantics.scl.compiler.types.Type; -import org.simantics.scl.compiler.types.Types; -import org.simantics.scl.compiler.types.kinds.Kinds; - -public class EqualsFunction extends FunctionValue implements ComparisonFunction { - private static final TVar A = Types.var(Kinds.STAR); - public static final EqualsFunction INSTANCE = new EqualsFunction(); - - private EqualsFunction() { - super(new TVar[] {A}, Types.NO_EFFECTS, Types.BOOLEAN, A, A); - } - - @Override - public Type applyExact(MethodBuilder mb, Val[] parameters) { - parameters[0].push(mb); - parameters[1].push(mb); - TypeDesc parameterType = mb.getJavaTypeTranslator().getTypeDesc(parameters[0]); - if(parameterType.isPrimitive()) { - if(parameterType.equals(TypeDesc.VOID)) { - mb.loadConstant(true); - } - else { - Label end = mb.createLabel(); - Label isEqual = mb.createLabel(); - mb.ifComparisonBranch(isEqual, "==", parameterType); - mb.loadConstant(false); - mb.branch(end); - mb.setLocation(isEqual); - mb.loadConstant(true); - mb.setLocation(end); - } - } - else - mb.invokeStatic("java/util/Objects", "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[2]); - return Types.BOOLEAN; - } - - @Override - public String toString() { - return "=="; - } - - @Override - public void generateCondition(MethodBuilder mb, Val[] parameters, Cont then_, Cont else_) { - parameters[0].push(mb); - parameters[1].push(mb); - TypeDesc parameterType = mb.getJavaTypeTranslator().getTypeDesc(parameters[0]); - if(parameterType.isPrimitive()) { - if(parameterType.equals(TypeDesc.VOID)) { - mb.jump(then_); - } - else { - mb.ifComparisonBranch(mb.getLabel(then_), "==", parameterType); - mb.jump(else_); - mb.ensureExists(then_); - } - } - else { - mb.invokeStatic("java/util/Objects", "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[2]); - mb.ifZeroComparisonBranch(mb.getLabel(else_), "=="); - mb.jump(then_); - mb.ensureExists(else_); - } - } -} +package org.simantics.scl.compiler.elaboration.java; + +import org.cojen.classfile.TypeDesc; +import org.objectweb.asm.Label; +import org.simantics.scl.compiler.constants.ComparisonFunction; +import org.simantics.scl.compiler.constants.FunctionValue; +import org.simantics.scl.compiler.internal.codegen.continuations.Cont; +import org.simantics.scl.compiler.internal.codegen.references.Val; +import org.simantics.scl.compiler.internal.codegen.utils.Constants; +import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder; +import org.simantics.scl.compiler.types.TVar; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.Types; +import org.simantics.scl.compiler.types.kinds.Kinds; + +public class EqualsFunction extends FunctionValue implements ComparisonFunction { + private static final TVar A = Types.var(Kinds.STAR); + public static final EqualsFunction INSTANCE = new EqualsFunction(); + + private EqualsFunction() { + super(new TVar[] {A}, Types.NO_EFFECTS, Types.BOOLEAN, A, A); + } + + @Override + public Type applyExact(MethodBuilder mb, Val[] parameters) { + parameters[0].push(mb); + parameters[1].push(mb); + TypeDesc parameterType = mb.getJavaTypeTranslator().getTypeDesc(parameters[0]); + if(parameterType.isPrimitive()) { + if(parameterType.equals(TypeDesc.VOID)) { + mb.loadConstant(true); + } + else { + Label end = mb.createLabel(); + Label isEqual = mb.createLabel(); + mb.ifComparisonBranch(isEqual, "==", parameterType); + mb.loadConstant(false); + mb.branch(end); + mb.setLocation(isEqual); + mb.loadConstant(true); + mb.setLocation(end); + } + } + else + mb.invokeStatic("java/util/Objects", "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[2]); + return Types.BOOLEAN; + } + + @Override + public String toString() { + return "=="; + } + + @Override + public void generateCondition(MethodBuilder mb, Val[] parameters, Cont then_, Cont else_) { + parameters[0].push(mb); + parameters[1].push(mb); + TypeDesc parameterType = mb.getJavaTypeTranslator().getTypeDesc(parameters[0]); + if(parameterType.isPrimitive()) { + if(parameterType.equals(TypeDesc.VOID)) { + mb.jump(then_); + } + else { + mb.ifComparisonBranch(mb.getLabel(then_), "==", parameterType); + mb.jump(else_); + mb.ensureExists(then_); + } + } + else { + mb.invokeStatic("java/util/Objects", "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[2]); + mb.ifZeroComparisonBranch(mb.getLabel(else_), "=="); + mb.jump(then_); + mb.ensureExists(else_); + } + } +}