X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fconstants%2Fsingletons%2FNullCheck.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fconstants%2Fsingletons%2FNullCheck.java;h=3089a18621733ce115ab9b0c93c4692e9438d711;hb=a8758de5bc19e5adb3f618d3038743a164f09912;hp=0000000000000000000000000000000000000000;hpb=12d9af17384d960b75d58c3935d2b7b46d93e87b;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/singletons/NullCheck.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/singletons/NullCheck.java new file mode 100644 index 000000000..3089a1862 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/singletons/NullCheck.java @@ -0,0 +1,49 @@ +package org.simantics.scl.compiler.constants.singletons; + +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.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 NullCheck extends FunctionValue implements ComparisonFunction { + private static final TVar A = Types.var(Kinds.STAR); + public static final NullCheck INSTANCE = + new NullCheck(); + + private NullCheck() { + super(new TVar[] {A}, Types.NO_EFFECTS, Types.BOOLEAN, A); + } + + @Override + public Type applyExact(MethodBuilder mb, Val[] parameters) { + parameters[0].push(mb); + Label join = mb.createLabel(); + Label isNull = mb.createLabel(); + mb.ifNullBranch(isNull, true); + mb.loadConstant(false); + mb.branch(join); + mb.setLocation(isNull); + mb.loadConstant(true); + mb.setLocation(join); + return getReturnType(); + } + + @Override + public void generateCondition(MethodBuilder mb, Val[] parameters, Cont then_, Cont else_) { + parameters[0].push(mb); + mb.ifNullBranch(mb.getLabel(then_), true); + mb.jump(else_); + mb.ensureExists(then_); + } + + @Override + public String toString() { + return "nullCheck"; + } +}