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.ListElement;
10 import org.simantics.scl.compiler.constants.singletons.ListLength;
11 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
12 import org.simantics.scl.compiler.elaboration.expressions.Variable;
13 import org.simantics.scl.compiler.internal.codegen.continuations.ICont;
14 import org.simantics.scl.compiler.internal.codegen.references.IVal;
15 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
16 import org.simantics.scl.compiler.types.Type;
17 import org.simantics.scl.compiler.types.Types;
19 public abstract class PlanContext {
20 public CompilationContext context;
21 public CHRRuleset ruleset;
23 public ArrayList<PartnerFact> partnerFacts = new ArrayList<PartnerFact>();
24 public IVal currentId;
26 public PlanContext(CompilationContext context, CHRRuleset ruleset, IVal storeVar) {
27 this.context = context;
28 this.ruleset = ruleset;
29 this.storeVar = storeVar;
32 public abstract void nextOp(CodeWriter w);
34 public IVal generateNewId(long location, CodeWriter w) {
36 currentId = w.apply(location, ruleset.readCurrentId, storeVar);
37 IVal result = currentId;
38 currentId = w.apply(location, IncreaseByOne.INSTANCE, currentId);
42 public void iterateList(long location, CodeWriter w, Variable variable, IVal listValue) {
43 Type componentType = variable.getType();
44 IVal listLength = w.apply(location, ListLength.INSTANCE.createSpecialization(componentType), listValue);
46 CodeWriter body = w.createBlock(Types.INTEGER);
47 ICont bodyContinuation = body.getContinuation();
48 CodeWriter end = w.createBlock();
50 w.jump(body.getContinuation(), IntegerConstant.ZERO);
52 IVal index = body.getParameters()[0];
53 body.branchAwayIf(body.apply(location, JavaComparisonOperation.IEQUAL, index, listLength),
54 end.getContinuation());
55 variable.setVal(body.apply(location, ListElement.INSTANCE.createSpecialization(componentType), listValue, index));
57 if(body.isUnfinished())
58 body.jump(bodyContinuation, body.apply(location, IncreaseByOne.INSTANCE, index));
63 public void check(long location, CodeWriter w, IVal booleanValue) {
64 CodeWriter end = w.createBlock();
65 w.branchAwayUnless(booleanValue, end.getContinuation());
68 w.jump(end.getContinuation());