]> gerrit.simantics Code Review - simantics/platform.git/blob
d8cf92332da0a035b4ce86d126a8230dabedea53
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.chr.plan;
2
3 import java.util.ArrayList;
4
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;
18
19 public abstract class PlanContext {
20     public CompilationContext context;
21     public CHRRuleset ruleset;
22     public IVal storeVar;
23     public ArrayList<PartnerFact> partnerFacts = new ArrayList<PartnerFact>();
24     public IVal currentId;
25     
26     public PlanContext(CompilationContext context, CHRRuleset ruleset, IVal storeVar) {
27         this.context = context;
28         this.ruleset = ruleset;
29         this.storeVar = storeVar;
30     }
31
32     public abstract void nextOp(CodeWriter w);
33
34     public IVal generateNewId(long location, CodeWriter w) {
35         if(currentId == null)
36             currentId = w.apply(location, ruleset.readCurrentId, storeVar);
37         IVal result = currentId;
38         currentId = w.apply(location, IncreaseByOne.INSTANCE, currentId);
39         return result;
40     }
41     
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);
45         
46         CodeWriter body = w.createBlock(Types.INTEGER);
47         ICont bodyContinuation = body.getContinuation();
48         CodeWriter end = w.createBlock();
49                 
50         w.jump(body.getContinuation(), IntegerConstant.ZERO);
51         
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));
56         nextOp(body);
57         if(body.isUnfinished())
58             body.jump(bodyContinuation, body.apply(location, IncreaseByOne.INSTANCE, index));
59         
60         w.continueAs(end);
61     }
62
63     public void check(long location, CodeWriter w, IVal booleanValue) {
64         CodeWriter end = w.createBlock();
65         w.branchAwayUnless(booleanValue, end.getContinuation());
66         nextOp(w);
67         if(w.isUnfinished())
68             w.jump(end.getContinuation());
69         w.continueAs(end);
70     }
71 }