1 package org.simantics.scl.compiler.elaboration.java;
3 import org.cojen.classfile.TypeDesc;
4 import org.objectweb.asm.Label;
5 import org.simantics.scl.compiler.constants.ComparisonFunction;
6 import org.simantics.scl.compiler.constants.FunctionValue;
7 import org.simantics.scl.compiler.internal.codegen.continuations.Cont;
8 import org.simantics.scl.compiler.internal.codegen.references.Val;
9 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
10 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
11 import org.simantics.scl.compiler.types.TVar;
12 import org.simantics.scl.compiler.types.Type;
13 import org.simantics.scl.compiler.types.Types;
14 import org.simantics.scl.compiler.types.kinds.Kinds;
16 public class EqualsFunction extends FunctionValue implements ComparisonFunction {
17 private static final TVar A = Types.var(Kinds.STAR);
18 public static final EqualsFunction INSTANCE = new EqualsFunction();
20 private EqualsFunction() {
21 super(new TVar[] {A}, Types.NO_EFFECTS, Types.BOOLEAN, A, A);
25 public Type applyExact(MethodBuilder mb, Val[] parameters) {
26 parameters[0].push(mb);
27 parameters[1].push(mb);
28 TypeDesc parameterType = mb.getJavaTypeTranslator().getTypeDesc(parameters[0]);
29 if(parameterType.isPrimitive()) {
30 if(parameterType.equals(TypeDesc.VOID)) {
31 mb.loadConstant(true);
34 Label end = mb.createLabel();
35 Label isEqual = mb.createLabel();
36 mb.ifComparisonBranch(isEqual, "==", parameterType);
37 mb.loadConstant(false);
39 mb.setLocation(isEqual);
40 mb.loadConstant(true);
45 mb.invokeStatic("java/util/Objects", "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[2]);
50 public String toString() {
55 public void generateCondition(MethodBuilder mb, Val[] parameters, Cont then_, Cont else_) {
56 parameters[0].push(mb);
57 parameters[1].push(mb);
58 TypeDesc parameterType = mb.getJavaTypeTranslator().getTypeDesc(parameters[0]);
59 if(parameterType.isPrimitive()) {
60 if(parameterType.equals(TypeDesc.VOID)) {
64 mb.ifComparisonBranch(mb.getLabel(then_), "==", parameterType);
66 mb.ensureExists(then_);
70 mb.invokeStatic("java/util/Objects", "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[2]);
71 mb.ifZeroComparisonBranch(mb.getLabel(else_), "==");
73 mb.ensureExists(else_);