1 package org.simantics.scl.compiler.elaboration.chr.plan;
3 import org.simantics.scl.compiler.compilation.CompilationContext;
4 import org.simantics.scl.compiler.constants.IntegerConstant;
5 import org.simantics.scl.compiler.constants.singletons.NullCheck;
6 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
7 import org.simantics.scl.compiler.internal.codegen.continuations.ICont;
8 import org.simantics.scl.compiler.internal.codegen.references.IVal;
9 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
11 public class PostCommitOp extends PlanOp {
14 public PostCommitOp(long location, int priority) {
16 this.priority = priority;
20 public void toString(StringBuilder b) {
21 b.append("POST_COMMIT " + priority);
25 public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
26 if(planContext.currentId != null) {
27 w.apply(location, planContext.ruleset.writeCurrentId, planContext.storeVar, planContext.currentId);
28 planContext.currentId = null;
29 w.apply(location, planContext.ruleset.activateProcedure, planContext.storeVar, new IntegerConstant(priority));
31 for(PartnerFact activeFact : planContext.partnerFacts) {
32 if(activeFact.killAfterMatch) {
33 if(activeFact.nextFact == null)
34 w.jump(activeFact.finishCont);
36 CodeWriter iterateAlive = w.createBlock(activeFact.constraint.factType);
37 w.jump(iterateAlive.getContinuation(), w.apply(location, activeFact.nextFact, activeFact.factVar));
38 iterateUntilLiveFactFound(iterateAlive, activeFact);
42 else if(activeFact.mayBeRemoved) {
43 if(activeFact.nextFact == null) {
44 w.branchAwayUnless(activeFact.isAlive(location, w), activeFact.finishCont);
47 CodeWriter failure = w.createBlock();
48 CodeWriter iterateAlive = w.createBlock(activeFact.constraint.factType);
49 w.branchAwayUnless(activeFact.isAlive(location, w), failure.getContinuation());
50 failure.jump(iterateAlive.getContinuation(), failure.apply(location, activeFact.nextFact, activeFact.factVar));
51 iterateUntilLiveFactFound(iterateAlive, activeFact);
55 // PostCommit does not call nextOp
58 private void iterateUntilLiveFactFound(CodeWriter w, PartnerFact activeFact) {
59 ICont initialContinuation = w.getContinuation();
60 CHRConstraint constraint = activeFact.constraint;
61 IVal fact = w.getParameters()[0];
62 CodeWriter dead = w.createBlock();
63 w.branchAwayIf(w.apply(location, NullCheck.INSTANCE.createSpecialization(constraint.factType), fact), activeFact.finishCont);
64 w.branchAwayUnless(activeFact.isAlive(location, w, fact), dead.getContinuation());
65 w.jump(activeFact.continueCont, fact);
66 dead.jump(initialContinuation, dead.apply(location, activeFact.nextFact, fact));