]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/plan/PlanContext.java
SCL compiler generates line numbers to bytecode
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / plan / PlanContext.java
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.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;
23
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;
31     
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;
37     }
38
39     public abstract void nextOp(CodeWriter w);
40
41     public IVal generateNewId(long location, CodeWriter w) {
42         if(currentId == null)
43             currentId = w.apply(location, CHRRuleset.READ_CURRENT_ID, contextVar);
44         IVal result = currentId;
45         currentId = w.apply(location, IncreaseByOne.INSTANCE, currentId);
46         return result;
47     }
48     
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);
52         
53         CodeWriter body = w.createBlock(Types.INTEGER);
54         ICont bodyContinuation = body.getContinuation();
55         CodeWriter end = w.createBlock();
56                 
57         w.jump(location, body.getContinuation(), IntegerConstant.ZERO);
58         
59         IVal index = body.getParameters()[0];
60         body.branchAwayIf(location, body.apply(location, JavaComparisonOperation.IEQUAL, index, listLength),
61                 end.getContinuation());
62         variable.setVal(body.apply(location, ListElement.INSTANCE.createSpecialization(componentType), listValue, index));
63         nextOp(body);
64         if(body.isUnfinished())
65             body.jump(location, bodyContinuation, body.apply(location, IncreaseByOne.INSTANCE, index));
66         
67         w.continueAs(end);
68     }
69     
70     public void iterateMaybe(long location, CodeWriter w, Variable variable, IVal maybeValue) {
71         Type componentType = variable.getType();
72
73         CodeWriter end = w.createBlock();
74         CodeWriter body = w.createBlock(componentType);
75         w.switch_(location, maybeValue, new Branch[] {
76                 new Branch(JustConstant.INSTANCE, body.getContinuation()),
77                 new Branch(null, end.getContinuation())
78         });
79         
80         variable.setVal(body.getParameters()[0]);
81         nextOp(body);
82         if(body.isUnfinished())
83             body.jump(location, end.getContinuation());
84         
85         w.continueAs(end);
86     }
87
88     public void check(long location, CodeWriter w, IVal booleanValue) {
89         CodeWriter end = w.createBlock();
90         w.branchAwayUnless(location, booleanValue, end.getContinuation());
91         nextOp(w);
92         if(w.isUnfinished())
93             w.jump(location, end.getContinuation());
94         w.continueAs(end);
95     }
96
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()),
100                 a, b));
101     }
102     
103     public void checkEqualsJust(long location, CodeWriter w, IVal value, IVal maybeValue) {
104         Type componentType = value.getType();
105
106         CodeWriter end = w.createBlock();
107         CodeWriter body = w.createBlock(componentType);
108         w.switch_(location, maybeValue, new Branch[] {
109                 new Branch(JustConstant.INSTANCE, body.getContinuation()),
110                 new Branch(null, end.getContinuation())
111         });
112         body.branchAwayUnless(location, body.apply(location,
113                 EqualsFunction.INSTANCE.createSpecialization(componentType),
114                 value, body.getParameters()[0]), end.getContinuation());
115         nextOp(body);
116         if(body.isUnfinished())
117             body.jump(location, end.getContinuation());
118         
119         w.continueAs(end);
120     }
121
122     public IVal getStoreVar(CHRConstraint constraint) {
123         IncludeStatement includeStatement = ruleset.constraintSourceMap.get(constraint);
124         if(includeStatement != null)
125             return includeStatement.storeVar;
126         else
127             return mainStoreVar;
128     }
129 }