X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fchr%2FCHRRuleset.java;h=55bbcfe34f9c245968083ab39e374b9814fc8908;hp=d5f61965454e80006631d98d76621fc56ba94e50;hb=3e457239db8dd214a3a10c74c2ecefa0f0f8dd1a;hpb=78f577368ba4c71ad6fb3d9f16c03c634585cf7b diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRuleset.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRuleset.java index d5f619654..55bbcfe34 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRuleset.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRuleset.java @@ -12,6 +12,7 @@ import org.simantics.scl.compiler.constants.JavaMethod; import org.simantics.scl.compiler.constants.NoRepConstant; import org.simantics.scl.compiler.constants.generic.CallJava; import org.simantics.scl.compiler.constants.generic.MethodRef.FieldRef; +import org.simantics.scl.compiler.constants.generic.MethodRef.ObjectMethodRef; import org.simantics.scl.compiler.constants.generic.MethodRef.SetFieldRef; import org.simantics.scl.compiler.elaboration.chr.analysis.CHRConstraintGGInfo; import org.simantics.scl.compiler.elaboration.chr.analysis.UsageAnalysis; @@ -22,7 +23,6 @@ import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext; import org.simantics.scl.compiler.elaboration.contexts.TranslationContext; import org.simantics.scl.compiler.elaboration.contexts.TypingContext; import org.simantics.scl.compiler.elaboration.expressions.Variable; -import org.simantics.scl.compiler.elaboration.expressions.VariableProcedure; import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement; import org.simantics.scl.compiler.environment.AmbiguousNameException; import org.simantics.scl.compiler.errors.Locations; @@ -32,17 +32,16 @@ import org.simantics.scl.compiler.internal.codegen.references.BoundVar; import org.simantics.scl.compiler.internal.codegen.references.IVal; import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction; import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor; +import org.simantics.scl.compiler.internal.codegen.utils.Constants; import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter; import org.simantics.scl.compiler.internal.parsing.Symbol; import org.simantics.scl.compiler.types.TCon; import org.simantics.scl.compiler.types.TVar; import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; -import org.simantics.scl.runtime.chr.CHRContext; import gnu.trove.map.hash.THashMap; import gnu.trove.map.hash.TObjectIntHashMap; -import gnu.trove.set.hash.THashSet; import gnu.trove.set.hash.TIntHashSet; public class CHRRuleset extends Symbol { @@ -67,8 +66,13 @@ public class CHRRuleset extends Symbol { public TCon runtimeRulesetType; public BoundVar runtimeRulesetVariable; public TypeDesc runtimeRulesetTypeDesc; - public Constant readCurrentId; - public Constant writeCurrentId; + + public static final Constant READ_CURRENT_ID = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.INTEGER, new Type[] {Types.CHRContext}, + null, new FieldRef(CHRCodeGenerationConstants.CHRContext_name, "currentId", CHRRuntimeRulesetCodeGenerator.FACT_ID_TYPE), null); + public static final Constant WRITE_CURRENT_ID = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {Types.CHRContext, Types.INTEGER}, + null, new SetFieldRef(CHRCodeGenerationConstants.CHRContext_name, "currentId", CHRRuntimeRulesetCodeGenerator.FACT_ID_TYPE), null); + public static final Constant GENERATE_ID = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.INTEGER, new Type[] {Types.CHRContext}, + null, new ObjectMethodRef(false, CHRCodeGenerationConstants.CHRContext_name, "generateId", CHRRuntimeRulesetCodeGenerator.FACT_ID_TYPE, Constants.EMPTY_TYPEDESC_ARRAY), null); // FIXME remove and change the parameter of Expression.toVal private CompilationContext cachedContext; @@ -117,13 +121,6 @@ public class CHRRuleset extends Symbol { }*/ } - public void collectRefs(TObjectIntHashMap allRefs, TIntHashSet refs) { - for(IncludeStatement include : includes) - include.value.collectRefs(allRefs, refs); - for(CHRRule rule : rules) - rule.collectRefs(allRefs, refs); - } - public void checkType(TypingContext context) { for(IncludeStatement include : includes) include.value = include.value.checkType(context, include.ruleset.runtimeRulesetType); @@ -138,20 +135,6 @@ public class CHRRuleset extends Symbol { rule.collectVars(allVars, vars); } - public void forVariables(VariableProcedure procedure) { - for(IncludeStatement include : includes) - include.value.forVariables(procedure); - for(CHRRule rule : rules) - rule.forVariables(procedure); - } - - public void collectFreeVariables(THashSet vars) { - for(IncludeStatement include : includes) - include.value.collectFreeVariables(vars); - for(CHRRule rule : rules) - rule.collectFreeVariables(vars); - } - public void setLocationDeep(long loc) { if(location == Locations.NO_LOCATION) { this.location = loc; @@ -215,6 +198,7 @@ public class CHRRuleset extends Symbol { int max = 1 << constraint.parameterTypes.length; for(int i=0;i 0) @@ -247,10 +231,6 @@ public class CHRRuleset extends Symbol { runtimeRulesetVariable = new BoundVar(runtimeRulesetType); for(CHRConstraint constraint : constraints) constraint.initializeCodeGeneration(context, this); - readCurrentId = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.INTEGER, new Type[] {Types.CHRContext}, - null, new FieldRef(CHRCodeGenerationConstants.CHRContext_name, "currentId", CHRRuntimeRulesetCodeGenerator.FACT_ID_TYPE), null); - writeCurrentId = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {Types.CHRContext, Types.INTEGER}, - null, new SetFieldRef(CHRCodeGenerationConstants.CHRContext_name, "currentId", CHRRuntimeRulesetCodeGenerator.FACT_ID_TYPE), null); if(createTypeDesc && context.module != null) // for unit testing context.module.addTypeDescriptor(runtimeRulesetType.name, new StandardTypeConstructor(runtimeRulesetType, TVar.EMPTY_ARRAY, runtimeRulesetTypeDesc)); } @@ -260,7 +240,7 @@ public class CHRRuleset extends Symbol { public IVal generateCode(CodeWriter w) { for(IncludeStatement include : includes) { - include.storeVar = include.value.toVal(cachedContext.environment, w); + include.storeVar = include.value.toVal(cachedContext, w); initialPriorityNumber = Math.max(initialPriorityNumber, include.ruleset.initialPriorityNumber + include.ruleset.priorityCount); } CHRRulesetObject object = new CHRRulesetObject(runtimeRulesetVariable, this); @@ -280,7 +260,7 @@ public class CHRRuleset extends Symbol { methodWriter.return_(BooleanConstant.TRUE); } } - if(!includes.isEmpty()) { + if(!includes.isEmpty() || extensible) { { CodeWriter methodWriter = object.createMethod(w.getModuleWriter(), TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {Types.CHRContext}); initializer = methodWriter.getFunction(); @@ -289,6 +269,10 @@ public class CHRRuleset extends Symbol { new JavaMethod(true, runtimeRulesetClassName, "register", Types.PROC, Types.UNIT, new Type[] {runtimeRulesetType, Types.CHRContext, include.ruleset.runtimeRulesetType}), object.getTarget(), methodWriter.getParameters()[0], include.storeVar); } + if(extensible) + methodWriter.apply(Locations.NO_LOCATION, + new JavaMethod(true, runtimeRulesetClassName, "register", Types.PROC, Types.UNIT, new Type[] {runtimeRulesetType, Types.CHRContext}), + object.getTarget(), methodWriter.getParameters()[0]); methodWriter.return_(NoRepConstant.UNIT); } { @@ -299,6 +283,10 @@ public class CHRRuleset extends Symbol { new JavaMethod(true, runtimeRulesetClassName, "unregister", Types.PROC, Types.UNIT, new Type[] {runtimeRulesetType, Types.CHRContext, include.ruleset.runtimeRulesetType}), object.getTarget(), methodWriter.getParameters()[0], include.storeVar); } + if(extensible) + methodWriter.apply(Locations.NO_LOCATION, + new JavaMethod(true, runtimeRulesetClassName, "unregister", Types.PROC, Types.UNIT, new Type[] {runtimeRulesetType, Types.CHRContext}), + object.getTarget(), methodWriter.getParameters()[0]); methodWriter.return_(NoRepConstant.UNIT); } } @@ -309,7 +297,7 @@ public class CHRRuleset extends Symbol { new JavaMethod(true, runtimeRulesetClassName, "initialize", Types.PROC, Types.UNIT, new Type[] {runtimeRulesetType, Types.CHRContext}), object.getTarget(), chrContext); } - IVal initFact = w.apply(location, initConstraint.constructor, IntegerConstant.ZERO); + IVal initFact = w.apply(location, initConstraint.constructor, w.apply(location, GENERATE_ID, chrContext)); w.apply(location, initConstraint.addProcedure, runtimeRulesetVariable, chrContext, initFact); w.apply(location, ACTIVATE, chrContext, new IntegerConstant(Integer.MAX_VALUE)); if(deinitializer != null) { @@ -321,12 +309,8 @@ public class CHRRuleset extends Symbol { return runtimeRulesetVariable; } - public void collectEffects(THashSet effects) { - for(CHRRule rule : rules) { - for(CHRLiteral literal : rule.head.literals) - literal.collectQueryEffects(effects); - for(CHRLiteral literal : rule.head.literals) - literal.collectEnforceEffects(effects); - } + public void addRule(CHRRule rule) { + rules.add(rule); + rule.parentRuleset = this; } }