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.expressions.Variable;
14 import org.simantics.scl.compiler.elaboration.java.EqualsFunction;
15 import org.simantics.scl.compiler.internal.codegen.continuations.Branch;
16 import org.simantics.scl.compiler.internal.codegen.continuations.ICont;
17 import org.simantics.scl.compiler.internal.codegen.references.IVal;
18 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
19 import org.simantics.scl.compiler.types.Type;
20 import org.simantics.scl.compiler.types.Types;
22 public abstract class PlanContext {
23 public CompilationContext context;
24 public CHRRuleset ruleset;
26 public IVal contextVar;
27 public ArrayList<PartnerFact> partnerFacts = new ArrayList<PartnerFact>();
28 public IVal currentId;
30 public PlanContext(CompilationContext context, CHRRuleset ruleset, IVal storeVar, IVal contextVar) {
31 this.context = context;
32 this.ruleset = ruleset;
33 this.storeVar = storeVar;
34 this.contextVar = contextVar;
37 public abstract void nextOp(CodeWriter w);
39 public IVal generateNewId(long location, CodeWriter w) {
41 currentId = w.apply(location, ruleset.readCurrentId, contextVar);
42 IVal result = currentId;
43 currentId = w.apply(location, IncreaseByOne.INSTANCE, currentId);
47 public void iterateList(long location, CodeWriter w, Variable variable, IVal listValue) {
48 Type componentType = variable.getType();
49 IVal listLength = w.apply(location, ListLength.INSTANCE.createSpecialization(componentType), listValue);
51 CodeWriter body = w.createBlock(Types.INTEGER);
52 ICont bodyContinuation = body.getContinuation();
53 CodeWriter end = w.createBlock();
55 w.jump(body.getContinuation(), IntegerConstant.ZERO);
57 IVal index = body.getParameters()[0];
58 body.branchAwayIf(body.apply(location, JavaComparisonOperation.IEQUAL, index, listLength),
59 end.getContinuation());
60 variable.setVal(body.apply(location, ListElement.INSTANCE.createSpecialization(componentType), listValue, index));
62 if(body.isUnfinished())
63 body.jump(bodyContinuation, body.apply(location, IncreaseByOne.INSTANCE, index));
68 public void iterateMaybe(long location, CodeWriter w, Variable variable, IVal maybeValue) {
69 Type componentType = variable.getType();
71 CodeWriter end = w.createBlock();
72 CodeWriter body = w.createBlock(componentType);
73 w.switch_(maybeValue, new Branch[] {
74 new Branch(JustConstant.INSTANCE, body.getContinuation()),
75 new Branch(null, end.getContinuation())
78 variable.setVal(body.getParameters()[0]);
80 if(body.isUnfinished())
81 body.jump(end.getContinuation());
86 public void check(long location, CodeWriter w, IVal booleanValue) {
87 CodeWriter end = w.createBlock();
88 w.branchAwayUnless(booleanValue, end.getContinuation());
91 w.jump(end.getContinuation());
95 public void checkEquals(long location, CodeWriter w, IVal a, IVal b) {
96 check(location, w, w.apply(location,
97 EqualsFunction.INSTANCE.createSpecialization(a.getType()),
101 public void checkEqualsJust(long location, CodeWriter w, IVal value, IVal maybeValue) {
102 Type componentType = value.getType();
104 CodeWriter end = w.createBlock();
105 CodeWriter body = w.createBlock(componentType);
106 w.switch_(maybeValue, new Branch[] {
107 new Branch(JustConstant.INSTANCE, body.getContinuation()),
108 new Branch(null, end.getContinuation())
110 body.branchAwayUnless(body.apply(location,
111 EqualsFunction.INSTANCE.createSpecialization(componentType),
112 value, body.getParameters()[0]), end.getContinuation());
114 if(body.isUnfinished())
115 body.jump(end.getContinuation());