]> gerrit.simantics Code Review - simantics/platform.git/blob
af41f932b69f08084b6818134493606e33bb7574
[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.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;
10
11 public class PostCommitOp extends PlanOp {
12     int priority;
13     
14     public PostCommitOp(long location, int priority) {
15         super(location);
16         this.priority = priority;
17     }
18
19     @Override
20     public void toString(StringBuilder b) {
21         b.append("POST_COMMIT " + priority);
22     }
23
24     @Override
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));
30         }
31         for(PartnerFact activeFact : planContext.partnerFacts) {
32             if(activeFact.killAfterMatch) {
33                 if(activeFact.nextFact == null)
34                     w.jump(activeFact.finishCont);
35                 else {
36                     CodeWriter iterateAlive = w.createBlock(activeFact.constraint.factType);
37                     w.jump(iterateAlive.getContinuation(), w.apply(location, activeFact.nextFact, activeFact.factVar));
38                     iterateUntilLiveFactFound(iterateAlive, activeFact);                    
39                 }
40                 break;
41             }
42             else if(activeFact.mayBeRemoved) {
43                 if(activeFact.nextFact == null) {
44                     w.branchAwayUnless(activeFact.isAlive(location, w), activeFact.finishCont);
45                 }
46                 else {
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);     
52                 }
53             }
54         }
55         // PostCommit does not call nextOp
56     }
57     
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));
67     }
68
69 }