1 package org.simantics.scl.compiler.elaboration.chr.plan;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.compilation.CompilationContext;
6 import org.simantics.scl.compiler.constants.IntegerConstant;
7 import org.simantics.scl.compiler.constants.JavaComparisonOperation;
8 import org.simantics.scl.compiler.constants.singletons.IncreaseByOne;
9 import org.simantics.scl.compiler.constants.singletons.JustConstant;
10 import org.simantics.scl.compiler.constants.singletons.ListElement;
11 import org.simantics.scl.compiler.constants.singletons.ListLength;
12 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
13 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
14 import org.simantics.scl.compiler.elaboration.expressions.Variable;
15 import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement;
16 import org.simantics.scl.compiler.elaboration.java.EqualsFunction;
17 import org.simantics.scl.compiler.internal.codegen.continuations.Branch;
18 import org.simantics.scl.compiler.internal.codegen.continuations.ICont;
19 import org.simantics.scl.compiler.internal.codegen.references.IVal;
20 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
21 import org.simantics.scl.compiler.types.Type;
22 import org.simantics.scl.compiler.types.Types;
24 public abstract class PlanContext {
25 public CompilationContext context;
26 public CHRRuleset ruleset;
27 public IVal mainStoreVar;
28 public IVal contextVar;
29 public ArrayList<PartnerFact> partnerFacts = new ArrayList<PartnerFact>();
30 public IVal currentId;
32 public PlanContext(CompilationContext context, CHRRuleset ruleset, IVal mainStoreVar, IVal contextVar) {
33 this.context = context;
34 this.ruleset = ruleset;
35 this.mainStoreVar = mainStoreVar;
36 this.contextVar = contextVar;
39 public abstract void nextOp(CodeWriter w);
41 public IVal generateNewId(long location, CodeWriter w) {
43 currentId = w.apply(location, CHRRuleset.READ_CURRENT_ID, contextVar);
44 IVal result = currentId;
45 currentId = w.apply(location, IncreaseByOne.INSTANCE, currentId);
49 public void iterateList(long location, CodeWriter w, Variable variable, IVal listValue) {
50 Type componentType = variable.getType();
51 IVal listLength = w.apply(location, ListLength.INSTANCE.createSpecialization(componentType), listValue);
53 CodeWriter body = w.createBlock(Types.INTEGER);
54 ICont bodyContinuation = body.getContinuation();
55 CodeWriter end = w.createBlock();
57 w.jump(body.getContinuation(), IntegerConstant.ZERO);
59 IVal index = body.getParameters()[0];
60 body.branchAwayIf(body.apply(location, JavaComparisonOperation.IEQUAL, index, listLength),
61 end.getContinuation());
62 variable.setVal(body.apply(location, ListElement.INSTANCE.createSpecialization(componentType), listValue, index));
64 if(body.isUnfinished())
65 body.jump(bodyContinuation, body.apply(location, IncreaseByOne.INSTANCE, index));
70 public void iterateMaybe(long location, CodeWriter w, Variable variable, IVal maybeValue) {
71 Type componentType = variable.getType();
73 CodeWriter end = w.createBlock();
74 CodeWriter body = w.createBlock(componentType);
75 w.switch_(maybeValue, new Branch[] {
76 new Branch(JustConstant.INSTANCE, body.getContinuation()),
77 new Branch(null, end.getContinuation())
80 variable.setVal(body.getParameters()[0]);
82 if(body.isUnfinished())
83 body.jump(end.getContinuation());
88 public void check(long location, CodeWriter w, IVal booleanValue) {
89 CodeWriter end = w.createBlock();
90 w.branchAwayUnless(booleanValue, end.getContinuation());
93 w.jump(end.getContinuation());
97 public void checkEquals(long location, CodeWriter w, IVal a, IVal b) {
98 check(location, w, w.apply(location,
99 EqualsFunction.INSTANCE.createSpecialization(a.getType()),
103 public void checkEqualsJust(long location, CodeWriter w, IVal value, IVal maybeValue) {
104 Type componentType = value.getType();
106 CodeWriter end = w.createBlock();
107 CodeWriter body = w.createBlock(componentType);
108 w.switch_(maybeValue, new Branch[] {
109 new Branch(JustConstant.INSTANCE, body.getContinuation()),
110 new Branch(null, end.getContinuation())
112 body.branchAwayUnless(body.apply(location,
113 EqualsFunction.INSTANCE.createSpecialization(componentType),
114 value, body.getParameters()[0]), end.getContinuation());
116 if(body.isUnfinished())
117 body.jump(end.getContinuation());
122 public IVal getStoreVar(CHRConstraint constraint) {
123 IncludeStatement includeStatement = ruleset.constraintSourceMap.get(constraint);
124 if(includeStatement != null)
125 return includeStatement.storeVar;