]> gerrit.simantics Code Review - simantics/platform.git/blob
5b3057dd9dc099da6d13121568f399fc912d4285
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.chr.plan;
2
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.CHRRuleset;
7 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
8 import org.simantics.scl.compiler.internal.codegen.continuations.ICont;
9 import org.simantics.scl.compiler.internal.codegen.references.IVal;
10 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
11
12 public class PostCommitOp extends PlanOp {
13     int priority;
14     
15     public PostCommitOp(long location, int priority) {
16         super(location);
17         this.priority = priority;
18     }
19
20     @Override
21     public void toString(StringBuilder b) {
22         b.append("POST_COMMIT " + priority);
23     }
24
25     @Override
26     public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
27         if(planContext.currentId != null) {
28             w.apply(location, planContext.ruleset.writeCurrentId, planContext.contextVar, planContext.currentId);
29             planContext.currentId = null;
30             w.apply(location, CHRRuleset.ACTIVATE, planContext.contextVar, new IntegerConstant(priority+planContext.ruleset.initialPriorityNumber));
31         }
32         for(PartnerFact activeFact : planContext.partnerFacts) {
33             if(activeFact.killAfterMatch) {
34                 if(activeFact.nextFact == null)
35                     w.jump(activeFact.finishCont);
36                 else {
37                     CodeWriter iterateAlive = w.createBlock(activeFact.constraint.factType);
38                     w.jump(iterateAlive.getContinuation(), w.apply(location, activeFact.nextFact, activeFact.factVar));
39                     iterateUntilLiveFactFound(iterateAlive, activeFact);
40                 }
41                 break;
42             }
43             else if(activeFact.mayBeRemoved) {
44                 if(activeFact.nextFact == null) {
45                     w.branchAwayUnless(activeFact.isAlive(location, w), activeFact.finishCont);
46                 }
47                 else {
48                     CodeWriter failure = w.createBlock();
49                     CodeWriter iterateAlive = w.createBlock(activeFact.constraint.factType);
50                     w.branchAwayUnless(activeFact.isAlive(location, w), failure.getContinuation());
51                     failure.jump(iterateAlive.getContinuation(), failure.apply(location, activeFact.nextFact, activeFact.factVar));
52                     iterateUntilLiveFactFound(iterateAlive, activeFact);     
53                 }
54             }
55         }
56         // PostCommit does not call nextOp
57     }
58     
59     private void iterateUntilLiveFactFound(CodeWriter w, PartnerFact activeFact) {
60         ICont initialContinuation = w.getContinuation();
61         CHRConstraint constraint = activeFact.constraint;
62         IVal fact = w.getParameters()[0];
63         CodeWriter dead = w.createBlock();
64         w.branchAwayIf(w.apply(location, NullCheck.INSTANCE.createSpecialization(constraint.factType), fact), activeFact.finishCont);
65         w.branchAwayUnless(activeFact.isAlive(location, w, fact), dead.getContinuation());
66         w.jump(activeFact.continueCont, fact);
67         dead.jump(initialContinuation, dead.apply(location, activeFact.nextFact, fact));
68     }
69
70 }