]> gerrit.simantics Code Review - simantics/platform.git/blob
c0f43928877458cbb9a9241ad3e764481acd3ce5
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.chr.relations;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import org.cojen.classfile.TypeDesc;
7 import org.simantics.scl.compiler.compilation.CompilationContext;
8 import org.simantics.scl.compiler.constants.Constant;
9 import org.simantics.scl.compiler.constants.JavaMethod;
10 import org.simantics.scl.compiler.constants.NoRepConstant;
11 import org.simantics.scl.compiler.constants.generic.CallJava;
12 import org.simantics.scl.compiler.constants.generic.MethodRef.ConstructorRef;
13 import org.simantics.scl.compiler.constants.generic.MethodRef.FieldRef;
14 import org.simantics.scl.compiler.constants.generic.MethodRef.ObjectMethodRef;
15 import org.simantics.scl.compiler.constants.generic.ParameterStackItem;
16 import org.simantics.scl.compiler.constants.generic.StackItem;
17 import org.simantics.scl.compiler.elaboration.chr.CHRRelation;
18 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
19 import org.simantics.scl.compiler.internal.codegen.chr.CHRCodeGenerationConstants;
20 import org.simantics.scl.compiler.internal.codegen.chr.CHRRuntimeRulesetCodeGenerator;
21 import org.simantics.scl.compiler.internal.codegen.references.IVal;
22 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
23 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
24 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
25 import org.simantics.scl.compiler.internal.parsing.Symbol;
26 import org.simantics.scl.compiler.types.TCon;
27 import org.simantics.scl.compiler.types.TPred;
28 import org.simantics.scl.compiler.types.TVar;
29 import org.simantics.scl.compiler.types.Type;
30 import org.simantics.scl.compiler.types.Types;
31
32 import gnu.trove.map.hash.TIntObjectHashMap;
33
34 public class CHRConstraint extends Symbol implements CHRRelation {
35     public final String name;
36     public final Type[] parameterTypes;
37     
38     public boolean implicitlyDeclared;
39
40     // Analysis
41     //public int firstPriorityAdded;
42     public int lastPriorityAdded;
43     //public int firstPriorityRemoved;
44     public int lastPriorityRemoved;
45     
46     // Transient info
47     public CHRRuleset parentRuleset;
48     public String factClassName;
49     public Type factType;
50     public TypeDesc factTypeDesc;
51     
52     public TCon typeConstructor;
53     public Constant constructor;
54     public Constant accessId;
55     public Constant[] accessors;
56     public Constant addProcedure;
57     public Constant removeProcedure;
58     
59     public String nextContainerFieldName;
60     
61     public TIntObjectHashMap<IndexInfo> indices;
62     
63     public static class IndexInfo {
64         public final int indexMask;
65         public final String indexName;
66         public final Constant firstFact;
67         public final Constant nextFact;
68         
69         public IndexInfo(int indexMask, String indexName, Constant firstFact, Constant nextFact) {
70             this.indexMask = indexMask;
71             this.indexName = indexName;
72             this.firstFact = firstFact;
73             this.nextFact = nextFact;
74         }
75     }
76     
77     public CHRConstraint(long location, String name, Type[] parameterTypes) {
78         this.location = location;
79         this.name = name;
80         this.parameterTypes = parameterTypes;
81     }
82
83     public void initializeCodeGeneration(CompilationContext context, CHRRuleset parentRuleset) {
84         JavaTypeTranslator jtt = context.javaTypeTranslator;
85         
86         this.parentRuleset = parentRuleset;
87         this.factClassName = parentRuleset.runtimeRulesetClassName + "$" + name;
88         TCon factTypeConstructor = Types.con(parentRuleset.runtimeRulesetType.module, parentRuleset.runtimeRulesetType.name + "$" + name); 
89         this.factType = Types.apply(factTypeConstructor, TVar.EMPTY_ARRAY);
90         this.factTypeDesc = TypeDesc.forClass(factClassName);
91         
92         Type[] constructorTypes = new Type[parameterTypes.length+1];
93         constructorTypes[0] = Types.INTEGER;
94         ArrayList<StackItem> stackItems = new ArrayList<StackItem>(constructorTypes.length);
95         stackItems.add(new ParameterStackItem(0, Types.INTEGER));
96         for(int i=0;i<parameterTypes.length;++i) {
97             Type parameterType = parameterTypes[i];
98             constructorTypes[i+1] = parameterType;
99             if(!parameterType.equals(Types.UNIT))
100                 stackItems.add(new ParameterStackItem(stackItems.size(), parameterType));
101         }
102         TypeDesc[] constructorTypeDescs = JavaTypeTranslator.filterVoid(jtt.toTypeDescs(constructorTypes));
103         this.constructor = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, factType, constructorTypes,
104                 stackItems.toArray(new StackItem[stackItems.size()]),
105                 new ConstructorRef(factClassName, constructorTypeDescs),
106                 null);
107         //this.constructor = new JavaConstructor(factClassName, Types.PROC, factType, constructorTypes);
108         this.accessId = new CallJava(TVar.EMPTY_ARRAY, Types.NO_EFFECTS, Types.INTEGER, new Type[] {factType},
109                 null, new FieldRef(CHRCodeGenerationConstants.CHRFact_name, "id", CHRRuntimeRulesetCodeGenerator.FACT_ID_TYPE), null);
110         this.accessors = new Constant[parameterTypes.length];
111         for(int i=0;i<parameterTypes.length;++i) {
112             TypeDesc typeDesc = jtt.toTypeDesc(parameterTypes[i]);
113             if(typeDesc.equals(TypeDesc.VOID))
114                 continue;
115             this.accessors[i] = new CallJava(TVar.EMPTY_ARRAY, Types.NO_EFFECTS, parameterTypes[i], new Type[] {factType},
116                     null, new FieldRef(factClassName, CHRCodeGenerationConstants.fieldName(i), jtt.toTypeDesc(parameterTypes[i])), null);
117         }
118         this.addProcedure = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {parentRuleset.runtimeRulesetType, Types.CHRContext, factType},
119                 new StackItem[] {new ParameterStackItem(2, factType), new ParameterStackItem(0, parentRuleset.runtimeRulesetType), new ParameterStackItem(1, Types.CHRContext)},
120                 new ObjectMethodRef(false, factClassName, "add", TypeDesc.VOID, new TypeDesc[] {parentRuleset.runtimeRulesetTypeDesc, CHRCodeGenerationConstants.CHRContext}),
121                 null);
122         
123         this.indices = new TIntObjectHashMap<IndexInfo>(Math.min(10, 1 << parameterTypes.length));
124         
125         if(context.module != null) // for unit testing
126             context.module.addTypeDescriptor(factTypeConstructor.name, new StandardTypeConstructor(factTypeConstructor, TVar.EMPTY_ARRAY, factTypeDesc));
127         
128         // next container
129         if(parentRuleset.extensible) {
130             nextContainerFieldName = CHRCodeGenerationConstants.nextContainerName(name);
131         }
132     }
133
134     @Override
135     public TVar[] getTypeVariables() {
136         return TVar.EMPTY_ARRAY;
137     }
138
139     @Override
140     public Type[] getParameterTypes() {
141         return parameterTypes;
142     }
143     
144     @Override
145     public String toString() {
146         return name;
147     }
148     
149     public Collection<IndexInfo> getIndices() {
150         return indices.valueCollection();
151     }
152     
153     public boolean mayBeRemoved() {
154         return removeProcedure != null;
155     }
156
157     private IndexInfo createIndexInfo(CompilationContext context, int indexMask) {
158         ArrayList<Type> keyTypeList = new ArrayList<Type>(parameterTypes.length+1);
159         keyTypeList.add(parentRuleset.runtimeRulesetType);
160         for(int i=0;i<parameterTypes.length;++i)
161             if(((indexMask>>i)&1)==1)
162                 keyTypeList.add(parameterTypes[i]);
163         String indexName = nameOfIndex(indexMask, parameterTypes.length);
164         Constant accessIndex;
165         if(indexMask == 0) {
166             accessIndex = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, factType, new Type[] {parentRuleset.runtimeRulesetType},
167                     null, new FieldRef(parentRuleset.runtimeRulesetClassName, name + "$" + indexName, factTypeDesc), null);
168         }
169         else {
170             Type[] keyTypes = keyTypeList.toArray(new Type[keyTypeList.size()]);
171             accessIndex = new JavaMethod(true, parentRuleset.runtimeRulesetClassName, name + "$" + indexName, Types.PROC, factType, keyTypes);
172         }
173         return new IndexInfo(
174                 indexMask,
175                 indexName,
176                 accessIndex,
177                 new CallJava(TVar.EMPTY_ARRAY, Types.PROC, factType, new Type[] {factType},
178                         null, new FieldRef(factClassName, indexName + "Next", factTypeDesc), null)
179                 );
180     }
181     
182     public IndexInfo getOrCreateIndex(CompilationContext context, int boundMask) {
183         IndexInfo indexInfo = indices.get(boundMask);
184         if(indexInfo == null) {
185             indexInfo = createIndexInfo(context, boundMask);
186             indices.put(boundMask, indexInfo);
187         }
188         return indexInfo;
189     }
190     
191     public IVal fetchFromIndex(CompilationContext context, int boundMask) {
192         return getOrCreateIndex(context, boundMask).firstFact;
193     }
194
195     public Constant nextElement(CompilationContext context, int boundMask) {
196         IndexInfo indexInfo = indices.get(boundMask);
197         if(indexInfo == null) {
198             indexInfo = createIndexInfo(context, boundMask);
199             indices.put(boundMask, indexInfo);
200         }
201         return getOrCreateIndex(context, boundMask).nextFact;
202     }
203
204     
205     public static String nameOfIndex(int indexMask, int length) {
206         char[] chars = new char[length];
207         for(int i=0;i<length;++i)
208             chars[i] = ((indexMask>>i)&1) == 1 ? 'b' : 'f';
209         return new String(chars);
210     }
211
212     public void setMayBeRemoved() {
213         if(removeProcedure == null) {
214             removeProcedure = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {parentRuleset.runtimeRulesetType, factType},
215                     new StackItem[] {new ParameterStackItem(1, factType), new ParameterStackItem(0, parentRuleset.runtimeRulesetType)},
216                     new ObjectMethodRef(false, factClassName, "remove", TypeDesc.VOID, new TypeDesc[] {parentRuleset.runtimeRulesetTypeDesc}),
217                     null);
218         }
219     }
220
221     public TPred[] getTypeConstraints() {
222         return TPred.EMPTY_ARRAY;
223     }
224
225     public IVal accessComponent(long location, CodeWriter w, IVal fact, int i) {
226         Constant accessor = accessors[i];
227         if(accessor == null)
228             return NoRepConstant.UNIT;
229         else
230             return w.apply(location, accessor, fact);
231     }
232 }