1 package org.simantics.scl.compiler.elaboration.chr.relations;
3 import java.util.ArrayList;
4 import java.util.Collection;
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;
32 import gnu.trove.map.hash.TIntObjectHashMap;
34 public class CHRConstraint extends Symbol implements CHRRelation {
35 public final String name;
36 public final Type[] parameterTypes;
38 public boolean implicitlyDeclared;
41 //public int firstPriorityAdded;
42 public int lastPriorityAdded;
43 //public int firstPriorityRemoved;
44 public int lastPriorityRemoved;
47 public CHRRuleset parentRuleset;
48 public String factClassName;
50 public TypeDesc factTypeDesc;
52 public TCon typeConstructor;
53 public Constant constructor;
54 public Constant accessId;
55 public Constant[] accessors;
56 public Constant addProcedure;
57 public Constant removeProcedure;
59 public String nextContainerFieldName;
61 public TIntObjectHashMap<IndexInfo> indices;
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;
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;
77 public CHRConstraint(long location, String name, Type[] parameterTypes) {
78 this.location = location;
80 this.parameterTypes = parameterTypes;
83 public void initializeCodeGeneration(CompilationContext context, CHRRuleset parentRuleset) {
84 JavaTypeTranslator jtt = context.javaTypeTranslator;
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);
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));
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),
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))
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);
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}),
123 this.indices = new TIntObjectHashMap<IndexInfo>(Math.min(10, 1 << parameterTypes.length));
125 if(context.module != null) // for unit testing
126 context.module.addTypeDescriptor(factTypeConstructor.name, new StandardTypeConstructor(factTypeConstructor, TVar.EMPTY_ARRAY, factTypeDesc));
129 if(parentRuleset.extensible) {
130 nextContainerFieldName = CHRCodeGenerationConstants.nextContainerName(name);
135 public TVar[] getTypeVariables() {
136 return TVar.EMPTY_ARRAY;
140 public Type[] getParameterTypes() {
141 return parameterTypes;
145 public String toString() {
149 public Collection<IndexInfo> getIndices() {
150 return indices.valueCollection();
153 public boolean mayBeRemoved() {
154 return removeProcedure != null;
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;
166 accessIndex = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, factType, new Type[] {parentRuleset.runtimeRulesetType},
167 null, new FieldRef(parentRuleset.runtimeRulesetClassName, name + "$" + indexName, factTypeDesc), null);
170 Type[] keyTypes = keyTypeList.toArray(new Type[keyTypeList.size()]);
171 accessIndex = new JavaMethod(true, parentRuleset.runtimeRulesetClassName, name + "$" + indexName, Types.PROC, factType, keyTypes);
173 return new IndexInfo(
177 new CallJava(TVar.EMPTY_ARRAY, Types.PROC, factType, new Type[] {factType},
178 null, new FieldRef(factClassName, indexName + "Next", factTypeDesc), null)
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);
191 public IVal fetchFromIndex(CompilationContext context, int boundMask) {
192 return getOrCreateIndex(context, boundMask).firstFact;
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);
201 return getOrCreateIndex(context, boundMask).nextFact;
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);
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}),
221 public TPred[] getTypeConstraints() {
222 return TPred.EMPTY_ARRAY;
225 public IVal accessComponent(long location, CodeWriter w, IVal fact, int i) {
226 Constant accessor = accessors[i];
228 return NoRepConstant.UNIT;
230 return w.apply(location, accessor, fact);