From 4f6628825d40a8a77dc55e11781738f89978db35 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Tue, 16 May 2017 09:55:57 +0300 Subject: [PATCH] (refs #6178) Module serialization code from scl repository Registered also some new classes Change-Id: I914c2b79ac08e6046507f2c02c33dfe534b72814 --- .../META-INF/MANIFEST.MF | 7 +- .../internal/codegen/ssa/SSAFunction.java | 7 + .../serialization/KryoRegistration.java | 406 ++++++++++++++++++ .../serializers/FiniteClassSerializer.java | 49 +++ .../serializers/NameSerializer.java | 25 ++ .../serializers/SSABlockSerializer.java | 52 +++ .../serializers/SSAFunctionSerializer.java | 44 ++ .../serializers/SerializationUtils.java | 79 ++++ .../serializers/SingletonSerializer.java | 26 ++ .../serializers/TConSerializer.java | 26 ++ .../serializers/THashMapSerializer.java | 43 ++ .../serializers/TPredSerializer.java | 28 ++ .../serializers/TUnionSerializer.java | 23 + .../serializers/TypeDescSerializer.java | 23 + .../serializers/ValRefSerializer.java | 28 ++ .../scl/compiler/module/ConcreteModule.java | 2 +- .../org.simantics.scl.feature/feature.xml | 28 ++ .../META-INF/MANIFEST.MF | 6 +- .../serialization/TestSerialization.java | 61 +++ 19 files changed, 960 insertions(+), 3 deletions(-) create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/KryoRegistration.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/FiniteClassSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/NameSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/SSABlockSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/SSAFunctionSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/SerializationUtils.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/SingletonSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TConSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/THashMapSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TPredSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TUnionSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TypeDescSerializer.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/ValRefSerializer.java create mode 100644 tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/serialization/TestSerialization.java diff --git a/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF b/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF index 1007cf675..2bd500d06 100644 --- a/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF @@ -10,7 +10,11 @@ Require-Bundle: gnu.trove3;bundle-version="3.0.0", org.objectweb.asm;bundle-version="[5.0.0,6.0.0)", org.objectweb.asm.commons;bundle-version="[5.0.0,6.0.0)", org.objectweb.asm.util;bundle-version="[5.0.0,6.0.0)", - org.slf4j.api;bundle-version="1.7.2" + org.slf4j.api;bundle-version="1.7.2", + com.esotericsoftware.kryo;bundle-version="3.0.3", + org.objenesis;bundle-version="2.2.0", + com.esotericsoftware.minlog;bundle-version="1.3.0", + com.esotericsoftware.reflectasm;bundle-version="1.11.3" Export-Package: org.cojen.classfile, org.simantics.scl.compiler.commands, org.simantics.scl.compiler.common.datatypes, @@ -55,6 +59,7 @@ Export-Package: org.cojen.classfile, org.simantics.scl.compiler.internal.parsing, org.simantics.scl.compiler.internal.parsing.exceptions, org.simantics.scl.compiler.internal.parsing.parser, + org.simantics.scl.compiler.internal.serialization, org.simantics.scl.compiler.markdown.html, org.simantics.scl.compiler.markdown.inlines, org.simantics.scl.compiler.markdown.internal, diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAFunction.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAFunction.java index 209c92a64..948a876fe 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAFunction.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/ssa/SSAFunction.java @@ -466,4 +466,11 @@ public final class SSAFunction extends SSAClosure { for(SSABlock block = firstBlock; block != null; block = block.next) block.cleanup(); } + + public int getBlockCount() { + int count = 0; + for(SSABlock block = firstBlock;block != null;block = block.next, ++count); + return count; + } + } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/KryoRegistration.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/KryoRegistration.java new file mode 100644 index 000000000..4f3f42d13 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/KryoRegistration.java @@ -0,0 +1,406 @@ +package org.simantics.scl.compiler.internal.serialization; + +import java.util.ArrayList; + +import org.cojen.classfile.TypeDesc; +import org.objenesis.strategy.StdInstantiatorStrategy; +import org.simantics.scl.compiler.common.datatypes.Constructor; +import org.simantics.scl.compiler.common.names.Name; +import org.simantics.scl.compiler.common.precedence.Associativity; +import org.simantics.scl.compiler.common.precedence.Precedence; +import org.simantics.scl.compiler.constants.BooleanConstant; +import org.simantics.scl.compiler.constants.ByteConstant; +import org.simantics.scl.compiler.constants.CharacterConstant; +import org.simantics.scl.compiler.constants.ClassConstant; +import org.simantics.scl.compiler.constants.Constant; +import org.simantics.scl.compiler.constants.DoubleConstant; +import org.simantics.scl.compiler.constants.FloatConstant; +import org.simantics.scl.compiler.constants.GetPrimitiveConstant; +import org.simantics.scl.compiler.constants.IntegerConstant; +import org.simantics.scl.compiler.constants.JavaComparisonOperation; +import org.simantics.scl.compiler.constants.JavaComparisonToZeroOperation; +import org.simantics.scl.compiler.constants.JavaConstructor; +import org.simantics.scl.compiler.constants.JavaConversionOperation; +import org.simantics.scl.compiler.constants.JavaMathOperation; +import org.simantics.scl.compiler.constants.JavaMethod; +import org.simantics.scl.compiler.constants.JavaStaticField; +import org.simantics.scl.compiler.constants.JavaStaticMethod; +import org.simantics.scl.compiler.constants.JavaTypeClassMethod; +import org.simantics.scl.compiler.constants.JavaTypeClassSuper; +import org.simantics.scl.compiler.constants.JavaTypeInstanceConstructor; +import org.simantics.scl.compiler.constants.LocalBoxedArrayElementConstant; +import org.simantics.scl.compiler.constants.LocalFieldConstant; +import org.simantics.scl.compiler.constants.LocalVariableConstant; +import org.simantics.scl.compiler.constants.LongConstant; +import org.simantics.scl.compiler.constants.NoRepConstant; +import org.simantics.scl.compiler.constants.SCLConstant; +import org.simantics.scl.compiler.constants.SCLConstructor; +import org.simantics.scl.compiler.constants.ShortConstant; +import org.simantics.scl.compiler.constants.StringConstant; +import org.simantics.scl.compiler.constants.StringInterpolation; +import org.simantics.scl.compiler.constants.ThisConstant; +import org.simantics.scl.compiler.constants.generic.CallJava; +import org.simantics.scl.compiler.constants.generic.ConvertToListFilter; +import org.simantics.scl.compiler.constants.generic.MethodRef; +import org.simantics.scl.compiler.constants.generic.OutputFilter; +import org.simantics.scl.compiler.constants.generic.ParameterStackItem; +import org.simantics.scl.compiler.constants.generic.PopOutputFilter; +import org.simantics.scl.compiler.constants.generic.StackItem; +import org.simantics.scl.compiler.constants.generic.ThreadLocalStackItem; +import org.simantics.scl.compiler.constants.singletons.BindingConstant; +import org.simantics.scl.compiler.constants.singletons.FailFunction; +import org.simantics.scl.compiler.constants.singletons.JustConstant; +import org.simantics.scl.compiler.constants.singletons.NothingConstant; +import org.simantics.scl.compiler.constants.singletons.TypeOfConstant; +import org.simantics.scl.compiler.constants.singletons.UnsafeCoerce; +import org.simantics.scl.compiler.elaboration.expressions.Assignment; +import org.simantics.scl.compiler.elaboration.expressions.Case; +import org.simantics.scl.compiler.elaboration.expressions.EApply; +import org.simantics.scl.compiler.elaboration.expressions.EApplyType; +import org.simantics.scl.compiler.elaboration.expressions.EAsPattern; +import org.simantics.scl.compiler.elaboration.expressions.EBinary; +import org.simantics.scl.compiler.elaboration.expressions.EBinaryRightSide; +import org.simantics.scl.compiler.elaboration.expressions.EBind; +import org.simantics.scl.compiler.elaboration.expressions.EBlock; +import org.simantics.scl.compiler.elaboration.expressions.EConstant; +import org.simantics.scl.compiler.elaboration.expressions.EEnforce; +import org.simantics.scl.compiler.elaboration.expressions.EError; +import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant; +import org.simantics.scl.compiler.elaboration.expressions.EFieldAccess; +import org.simantics.scl.compiler.elaboration.expressions.EGetConstraint; +import org.simantics.scl.compiler.elaboration.expressions.EIf; +import org.simantics.scl.compiler.elaboration.expressions.EIntegerLiteral; +import org.simantics.scl.compiler.elaboration.expressions.ELambda; +import org.simantics.scl.compiler.elaboration.expressions.ELambdaType; +import org.simantics.scl.compiler.elaboration.expressions.ELet; +import org.simantics.scl.compiler.elaboration.expressions.EListComprehension; +import org.simantics.scl.compiler.elaboration.expressions.EListLiteral; +import org.simantics.scl.compiler.elaboration.expressions.ELiteral; +import org.simantics.scl.compiler.elaboration.expressions.EMatch; +import org.simantics.scl.compiler.elaboration.expressions.EPlaceholder; +import org.simantics.scl.compiler.elaboration.expressions.EPreLet; +import org.simantics.scl.compiler.elaboration.expressions.EPreRuleset; +import org.simantics.scl.compiler.elaboration.expressions.ERange; +import org.simantics.scl.compiler.elaboration.expressions.ERealLiteral; +import org.simantics.scl.compiler.elaboration.expressions.ERecord; +import org.simantics.scl.compiler.elaboration.expressions.ERuleset; +import org.simantics.scl.compiler.elaboration.expressions.ESelect; +import org.simantics.scl.compiler.elaboration.expressions.ESimpleLambda; +import org.simantics.scl.compiler.elaboration.expressions.ESimpleLet; +import org.simantics.scl.compiler.elaboration.expressions.EStringLiteral; +import org.simantics.scl.compiler.elaboration.expressions.ETransformation; +import org.simantics.scl.compiler.elaboration.expressions.ETypeAnnotation; +import org.simantics.scl.compiler.elaboration.expressions.EVar; +import org.simantics.scl.compiler.elaboration.expressions.EVariable; +import org.simantics.scl.compiler.elaboration.expressions.EWhen; +import org.simantics.scl.compiler.elaboration.expressions.Expression; +import org.simantics.scl.compiler.elaboration.expressions.GuardedExpression; +import org.simantics.scl.compiler.elaboration.expressions.GuardedExpressionGroup; +import org.simantics.scl.compiler.elaboration.expressions.Variable; +import org.simantics.scl.compiler.elaboration.expressions.accessor.ExpressionAccessor; +import org.simantics.scl.compiler.elaboration.expressions.accessor.IdAccessor; +import org.simantics.scl.compiler.elaboration.expressions.accessor.StringAccessor; +import org.simantics.scl.compiler.elaboration.expressions.block.BindStatement; +import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement; +import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement; +import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement; +import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment; +import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator; +import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard; +import org.simantics.scl.compiler.elaboration.expressions.list.ListSeq; +import org.simantics.scl.compiler.elaboration.expressions.list.ListThen; +import org.simantics.scl.compiler.elaboration.fundeps.Fundep; +import org.simantics.scl.compiler.elaboration.java.EmptyListConstructor; +import org.simantics.scl.compiler.elaboration.java.EqualsFunction; +import org.simantics.scl.compiler.elaboration.java.HashCodeFunction; +import org.simantics.scl.compiler.elaboration.java.ListConstructor; +import org.simantics.scl.compiler.elaboration.macros.StandardMacroRule; +import org.simantics.scl.compiler.elaboration.modules.DeprecatedProperty; +import org.simantics.scl.compiler.elaboration.modules.DerivedProperty; +import org.simantics.scl.compiler.elaboration.modules.InlineProperty; +import org.simantics.scl.compiler.elaboration.modules.MethodImplementation; +import org.simantics.scl.compiler.elaboration.modules.PrivateProperty; +import org.simantics.scl.compiler.elaboration.modules.SCLValue; +import org.simantics.scl.compiler.elaboration.modules.TypeClass; +import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance; +import org.simantics.scl.compiler.elaboration.modules.TypeClassMethod; +import org.simantics.scl.compiler.elaboration.query.QAlternative; +import org.simantics.scl.compiler.elaboration.query.QAtom; +import org.simantics.scl.compiler.elaboration.query.QConjunction; +import org.simantics.scl.compiler.elaboration.query.QDisjunction; +import org.simantics.scl.compiler.elaboration.query.QExists; +import org.simantics.scl.compiler.elaboration.query.QIf; +import org.simantics.scl.compiler.elaboration.query.QMapping; +import org.simantics.scl.compiler.elaboration.query.QNegation; +import org.simantics.scl.compiler.elaboration.query.Query; +import org.simantics.scl.compiler.errors.CompilationError; +import org.simantics.scl.compiler.internal.codegen.continuations.BranchRef; +import org.simantics.scl.compiler.internal.codegen.continuations.ContRef; +import org.simantics.scl.compiler.internal.codegen.continuations.ReturnCont; +import org.simantics.scl.compiler.internal.codegen.references.BoundVar; +import org.simantics.scl.compiler.internal.codegen.references.ValRef; +import org.simantics.scl.compiler.internal.codegen.references.ValSpecialization; +import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock; +import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction; +import org.simantics.scl.compiler.internal.codegen.ssa.exits.If; +import org.simantics.scl.compiler.internal.codegen.ssa.exits.Jump; +import org.simantics.scl.compiler.internal.codegen.ssa.exits.Switch; +import org.simantics.scl.compiler.internal.codegen.ssa.exits.Throw; +import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply; +import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetFunctions; +import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor; +import org.simantics.scl.compiler.internal.serialization.serializers.FiniteClassSerializer; +import org.simantics.scl.compiler.internal.serialization.serializers.NameSerializer; +import org.simantics.scl.compiler.internal.serialization.serializers.SSABlockSerializer; +import org.simantics.scl.compiler.internal.serialization.serializers.SSAFunctionSerializer; +import org.simantics.scl.compiler.internal.serialization.serializers.SingletonSerializer; +import org.simantics.scl.compiler.internal.serialization.serializers.TConSerializer; +import org.simantics.scl.compiler.internal.serialization.serializers.TPredSerializer; +import org.simantics.scl.compiler.internal.serialization.serializers.TUnionSerializer; +import org.simantics.scl.compiler.internal.serialization.serializers.TypeDescSerializer; +import org.simantics.scl.compiler.internal.serialization.serializers.ValRefSerializer; +import org.simantics.scl.compiler.module.ConcreteModule; +import org.simantics.scl.compiler.module.ImportDeclaration; +import org.simantics.scl.compiler.types.TApply; +import org.simantics.scl.compiler.types.TCon; +import org.simantics.scl.compiler.types.TForAll; +import org.simantics.scl.compiler.types.TFun; +import org.simantics.scl.compiler.types.TMetaVar; +import org.simantics.scl.compiler.types.TPred; +import org.simantics.scl.compiler.types.TUnion; +import org.simantics.scl.compiler.types.TVar; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.kinds.KArrow; +import org.simantics.scl.compiler.types.kinds.KCon; +import org.simantics.scl.compiler.types.kinds.KMetaVar; +import org.simantics.scl.compiler.types.kinds.Kinds; +import org.simantics.scl.compiler.types.util.Polarity; + +import com.esotericsoftware.kryo.Kryo; + +import gnu.trove.map.hash.THashMap; + +public class KryoRegistration { + + public static void registerClasses(Kryo kryo) { + kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); + kryo.setRegistrationRequired(true); + + // External types + kryo.register(String[].class); + kryo.register(byte[].class); + kryo.register(ArrayList.class); + kryo.register(THashMap.class); + + kryo.register(TypeDesc.OBJECT.getClass(), TypeDescSerializer.INSTANCE); + kryo.register(TypeDesc.INT.getClass(), TypeDescSerializer.INSTANCE); + kryo.register(TypeDesc.forClass(Object[].class).getClass(), TypeDescSerializer.INSTANCE); + kryo.register(TypeDesc[].class); + + // Types + kryo.register(KArrow.class); + kryo.register(KCon.class, new FiniteClassSerializer(Kinds.STAR, Kinds.EFFECT)); + kryo.register(KMetaVar.class); + + kryo.register(TApply.class); + kryo.register(TCon.class, TConSerializer.INSTANCE); + kryo.register(TForAll.class); + kryo.register(TFun.class); + kryo.register(TMetaVar.class); + kryo.register(TPred.class, TPredSerializer.INSTANCE); + kryo.register(TPred[].class); + kryo.register(TUnion.class, TUnionSerializer.INSTANCE); + kryo.register(TVar.class); + kryo.register(TVar[].class); + kryo.register(Type[].class); + + kryo.register(Polarity.class); + + // SSA + kryo.register(ValRef.class, ValRefSerializer.INSTANCE); + kryo.register(ValRef[].class); + kryo.register(BoundVar.class); + kryo.register(BoundVar[].class); + kryo.register(ValSpecialization.class); + kryo.register(ContRef.class); + kryo.register(BranchRef.class); + kryo.register(BranchRef[].class); + kryo.register(ReturnCont.class); + kryo.register(If.class); + kryo.register(Jump.class); + kryo.register(Switch.class); + kryo.register(Throw.class); + kryo.register(LetApply.class); + kryo.register(LetFunctions.class); + kryo.register(LetApply.class); + kryo.register(SSABlock.class, SSABlockSerializer.INSTANCE); + kryo.register(SSAFunction.class, SSAFunctionSerializer.INSTANCE); + + // Expression + kryo.register(EApply.class); + kryo.register(EApplyType.class); + kryo.register(EAsPattern.class); + kryo.register(EBinary.class); + kryo.register(EBinaryRightSide.class); + kryo.register(EBind.class); + kryo.register(EBlock.class); + kryo.register(EConstant.class); + kryo.register(EEnforce.class); + kryo.register(EError.class); + kryo.register(EExternalConstant.class); + kryo.register(EFieldAccess.class); + kryo.register(EGetConstraint.class); + kryo.register(EIf.class); + kryo.register(EIntegerLiteral.class); + kryo.register(ELambda.class); + kryo.register(ELambdaType.class); + kryo.register(ELet.class); + kryo.register(EListComprehension.class); + kryo.register(EListLiteral.class); + kryo.register(ELiteral.class); + kryo.register(EMatch.class); + kryo.register(EPlaceholder.class); + kryo.register(EPreLet.class); + kryo.register(EPreRuleset.class); + kryo.register(ERange.class); + kryo.register(ERealLiteral.class); + kryo.register(ERecord.class); + kryo.register(ERuleset.class); + kryo.register(ESelect.class); + kryo.register(ESimpleLambda.class); + kryo.register(ESimpleLet.class); + kryo.register(EStringLiteral.class); + kryo.register(ETransformation.class); + kryo.register(ETypeAnnotation.class); + kryo.register(EVar.class); + kryo.register(EVar.class); + kryo.register(EVar[].class); + kryo.register(EVariable.class); + kryo.register(EWhen.class); + kryo.register(GuardedExpression.class); + kryo.register(GuardedExpression[].class); + kryo.register(GuardedExpressionGroup.class); + kryo.register(Expression[].class); + + kryo.register(Variable.class); + kryo.register(Variable[].class); + + kryo.register(ExpressionAccessor.class); + kryo.register(IdAccessor.class); + kryo.register(StringAccessor.class); + + kryo.register(BindStatement.class); + kryo.register(GuardStatement.class); + kryo.register(LetStatement.class); + kryo.register(RuleStatement.class); + + kryo.register(ListAssignment.class); + kryo.register(ListGenerator.class); + kryo.register(ListGuard.class); + kryo.register(ListSeq.class); + kryo.register(ListThen.class); + + kryo.register(Assignment.class); + kryo.register(Assignment[].class); + kryo.register(Case.class); + kryo.register(Case[].class); + + // Queries + kryo.register(QAlternative.class); + kryo.register(QAtom.class); + kryo.register(QConjunction.class); + kryo.register(QDisjunction.class); + kryo.register(QExists.class); + kryo.register(QIf.class); + kryo.register(QMapping.class); + kryo.register(QNegation.class); + kryo.register(Query[].class); + + // + kryo.register(Name.class, NameSerializer.INSTANCE); + + kryo.register(ConcreteModule.class); + kryo.register(ImportDeclaration.class); + kryo.register(ImportDeclaration.ImportSpec.class); + kryo.register(TypeClassInstance.class); + kryo.register(TypeClassMethod.class); + kryo.register(MethodImplementation.class); + kryo.register(Constructor.class); + kryo.register(Constructor[].class); + kryo.register(TypeClass.class); + kryo.register(SCLValue.class); + kryo.register(SCLValue[].class); + kryo.register(Fundep.class); + kryo.register(Fundep[].class); + kryo.register(Precedence.class); + kryo.register(Associativity.class); + kryo.register(StandardTypeConstructor.class); + kryo.register(PrivateProperty.class); + kryo.register(InlineProperty.class); + kryo.register(DerivedProperty.class); + kryo.register(StandardMacroRule.class); + kryo.register(DeprecatedProperty.class); + kryo.register(CompilationError.class); + kryo.register(CompilationError[].class); + + // Constants + kryo.register(BooleanConstant.class); + kryo.register(ByteConstant.class); + kryo.register(CharacterConstant.class); + kryo.register(ClassConstant.class); + kryo.register(DoubleConstant.class); + kryo.register(EmptyListConstructor.class); + kryo.register(EqualsFunction.class); + kryo.register(FloatConstant.class); + kryo.register(GetPrimitiveConstant.class); + kryo.register(HashCodeFunction.class); + kryo.register(IntegerConstant.class); + kryo.register(JavaComparisonOperation.class); + kryo.register(JavaComparisonToZeroOperation.class); + kryo.register(JavaConstructor.class); + kryo.register(JavaConversionOperation.class); + kryo.register(JavaMathOperation.class); + kryo.register(JavaMethod.class); + kryo.register(JavaStaticField.class); + kryo.register(JavaStaticMethod.class); + kryo.register(JavaTypeClassMethod.class); + kryo.register(JavaTypeClassSuper.class); + kryo.register(JavaTypeInstanceConstructor.class); + kryo.register(ListConstructor.class); + kryo.register(LocalBoxedArrayElementConstant.class); + kryo.register(LocalFieldConstant.class); + kryo.register(LocalVariableConstant.class); + kryo.register(LongConstant.class); + kryo.register(NoRepConstant.class); + kryo.register(SCLConstant.class); + kryo.register(SCLConstructor.class); + kryo.register(ShortConstant.class); + kryo.register(StringConstant.class); + kryo.register(StringInterpolation.class); + kryo.register(ThisConstant.class); + kryo.register(BindingConstant.class, new SingletonSerializer(BindingConstant.INSTANCE)); + kryo.register(FailFunction.class, new SingletonSerializer(FailFunction.INSTANCE)); + kryo.register(JustConstant.class, new SingletonSerializer(JustConstant.INSTANCE)); + kryo.register(NothingConstant.class, new SingletonSerializer(NothingConstant.INSTANCE)); + kryo.register(TypeOfConstant.class, new SingletonSerializer(TypeOfConstant.INSTANCE)); + kryo.register(UnsafeCoerce.class, new SingletonSerializer(UnsafeCoerce.INSTANCE)); + + kryo.register(CallJava.class); + kryo.register(ParameterStackItem.class); + kryo.register(ThreadLocalStackItem.class); + kryo.register(StackItem[].class); + kryo.register(MethodRef.ConstructorRef.class); + kryo.register(MethodRef.FieldRef.class); + kryo.register(MethodRef.ObjectMethodRef.class); + kryo.register(MethodRef.SetFieldRef.class); + kryo.register(MethodRef.StaticFieldRef.class); + kryo.register(MethodRef.StaticMethodRef.class); + kryo.register(MethodRef[].class); + kryo.register(ConvertToListFilter.class); + kryo.register(PopOutputFilter.class); + kryo.register(OutputFilter[].class); + + kryo.register(Constant[].class); + } + +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/FiniteClassSerializer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/FiniteClassSerializer.java new file mode 100644 index 000000000..2f956909e --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/FiniteClassSerializer.java @@ -0,0 +1,49 @@ +package org.simantics.scl.compiler.internal.serialization.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +import gnu.trove.impl.Constants; +import gnu.trove.map.hash.TObjectIntHashMap; + +public class FiniteClassSerializer extends Serializer { + + private final T[] items; + private final TObjectIntHashMap map; + + public FiniteClassSerializer(T... items) { + this.items = items; + this.map = new TObjectIntHashMap(items.length, Constants.DEFAULT_LOAD_FACTOR, -1); + for(int i=0;i type) { + int id; + if(items.length < 0x100) + id = input.readByteUnsigned(); + else if(items.length < 0x10000) + id = input.readShortUnsigned(); + else + id = input.readInt(); + return items[id]; + } + +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/NameSerializer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/NameSerializer.java new file mode 100644 index 000000000..bf510f44e --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/NameSerializer.java @@ -0,0 +1,25 @@ +package org.simantics.scl.compiler.internal.serialization.serializers; + +import org.simantics.scl.compiler.common.names.Name; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +public class NameSerializer extends Serializer { + public static final NameSerializer INSTANCE = new NameSerializer(); + + @Override + public void write(Kryo kryo, Output output, Name object) { + SerializationUtils.writeModuleName(kryo, output, object.module); + output.writeString(object.name); + } + + @Override + public Name read(Kryo kryo, Input input, Class type) { + String module = SerializationUtils.readModuleName(kryo, input); + String name = input.readString(); + return Name.create(module, name); + } +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/SSABlockSerializer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/SSABlockSerializer.java new file mode 100644 index 000000000..bc79fe6eb --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/SSABlockSerializer.java @@ -0,0 +1,52 @@ +package org.simantics.scl.compiler.internal.serialization.serializers; + +import org.simantics.scl.compiler.internal.codegen.references.BoundVar; +import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock; +import org.simantics.scl.compiler.internal.codegen.ssa.SSAExit; +import org.simantics.scl.compiler.internal.codegen.ssa.SSAStatement; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +public class SSABlockSerializer extends Serializer { + public static final SSABlockSerializer INSTANCE = new SSABlockSerializer(); + + @Override + public void write(Kryo kryo, Output output, SSABlock object) { + { + BoundVar[] parameters = object.getParameters(); + output.writeVarInt(parameters.length, true); + for(BoundVar parameter : parameters) + kryo.writeObject(output, parameter); + } + { + output.writeVarInt(object.getStatementCount(), true); + for(SSAStatement stat = object.getFirstStatement();stat != null;stat = stat.getNext()) + kryo.writeClassAndObject(output, stat); + } + kryo.writeClassAndObject(output, object.getExit()); + } + + @Override + public SSABlock read(Kryo kryo, Input input, Class type) { + BoundVar[] parameters; + { + int parametersLength = input.readVarInt(true); + parameters = new BoundVar[parametersLength]; + for(int i=0;i { + public static final SSAFunctionSerializer INSTANCE = new SSAFunctionSerializer(); + + @Override + public void write(Kryo kryo, Output output, SSAFunction object) { + SerializationUtils.writeTVars(kryo, output, object.getTypeParameters()); + kryo.writeClassAndObject(output, object.getEffect()); + kryo.writeClassAndObject(output, object.getTarget()); + kryo.writeObject(output, object.getReturnCont()); + output.writeVarInt(object.getBlockCount(), true); + for(SSABlock block = object.getFirstBlock();block != null;block = block.getNext()) + kryo.writeObject(output, block); + } + + @Override + public SSAFunction read(Kryo kryo, Input input, Class type) { + TVar[] typeParameters = SerializationUtils.readTVars(kryo, input); + Type effect = (Type)kryo.readClassAndObject(input); + ReturnCont returnCont = kryo.readObject(input, ReturnCont.class); + SSAFunction result = new SSAFunction(typeParameters, effect, returnCont); + kryo.reference(result); + Val target = (Val)kryo.readClassAndObject(input); + result.setTarget(target); + int blockCount = input.readVarInt(true); + for(int i=0;i(); + kryo.getGraphContext().put(MODULE_NAME_CACHE, cache_); + } + TObjectIntHashMap cache = (TObjectIntHashMap)cache_; + int id = cache.get(moduleName); + output.writeVarInt(id, true); + if(id == 0) { + output.writeString(moduleName); + cache.put(moduleName, cache.size()+1); + } + } + + @SuppressWarnings("unchecked") + public static String readModuleName(Kryo kryo, Input input) { + Object cache_ = kryo.getGraphContext().get(MODULE_NAME_CACHE); + if(cache_ == null) { + cache_ = new ArrayList(); + kryo.getGraphContext().put(MODULE_NAME_CACHE, cache_); + } + ArrayList cache = (ArrayList)cache_; + int id = input.readVarInt(true); + if(id == 0) { + String moduleName = input.readString(); + cache.add(moduleName); + return moduleName; + } + else + return cache.get(id-1); + } + + public static void writeTypes(Kryo kryo, Output output, Type[] types) { + output.writeVarInt(types.length, true); + for(Type type : types) + kryo.writeClassAndObject(output, type); + } + + public static Type[] readTypes(Kryo kryo, Input input) { + int length = input.readVarInt(true); + Type[] result = new Type[length]; + for(int i=0;i extends Serializer { + + private final T singleton; + + public SingletonSerializer(T singleton) { + this.singleton = singleton; + setImmutable(true); + } + + @Override + public void write(Kryo kryo, Output output, T object) { + } + + @Override + public T read(Kryo kryo, Input input, Class type) { + return singleton; + } + +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TConSerializer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TConSerializer.java new file mode 100644 index 000000000..a0177b47e --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TConSerializer.java @@ -0,0 +1,26 @@ +package org.simantics.scl.compiler.internal.serialization.serializers; + +import org.simantics.scl.compiler.types.TCon; +import org.simantics.scl.compiler.types.Types; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +public class TConSerializer extends Serializer { + public static final TConSerializer INSTANCE = new TConSerializer(); + + @Override + public void write(Kryo kryo, Output output, TCon object) { + SerializationUtils.writeModuleName(kryo, output, object.module); + output.writeString(object.name); + } + + @Override + public TCon read(Kryo kryo, Input input, Class type) { + String module = SerializationUtils.readModuleName(kryo, input); + String name = input.readString(); + return Types.con(module, name); + } +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/THashMapSerializer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/THashMapSerializer.java new file mode 100644 index 000000000..4fbda44f2 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/THashMapSerializer.java @@ -0,0 +1,43 @@ +package org.simantics.scl.compiler.internal.serialization.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +import gnu.trove.map.hash.THashMap; +import gnu.trove.procedure.TObjectObjectProcedure; + + +@SuppressWarnings("rawtypes") +public class THashMapSerializer extends Serializer { + + @SuppressWarnings("unchecked") + @Override + public void write(final Kryo kryo, final Output output, THashMap object) { + output.writeVarInt(object.size(), true); + object.forEachEntry(new TObjectObjectProcedure() { + @Override + public boolean execute(Object key, Object value) { + kryo.writeClassAndObject(output, key); + kryo.writeClassAndObject(output, value); + return true; + } + }); + } + + @SuppressWarnings("unchecked") + @Override + public THashMap read(Kryo kryo, Input input, + Class type) { + int size = input.readVarInt(true); + THashMap result = new THashMap(size); + while((size--) > 0) { + Object key = kryo.readClassAndObject(input); + Object value = kryo.readClassAndObject(input); + result.put(key, value); + } + return result; + } + +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TPredSerializer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TPredSerializer.java new file mode 100644 index 000000000..f0679b2c5 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TPredSerializer.java @@ -0,0 +1,28 @@ +package org.simantics.scl.compiler.internal.serialization.serializers; + +import org.simantics.scl.compiler.types.TCon; +import org.simantics.scl.compiler.types.TPred; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.Types; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +public class TPredSerializer extends Serializer { + public static final TPredSerializer INSTANCE = new TPredSerializer(); + + @Override + public void write(Kryo kryo, Output output, TPred object) { + kryo.writeObject(output, object.typeClass); + SerializationUtils.writeTypes(kryo, output, object.parameters); + } + + @Override + public TPred read(Kryo kryo, Input input, Class type) { + TCon typeClass = kryo.readObject(input, TCon.class); + Type[] parameters = SerializationUtils.readTypes(kryo, input); + return Types.pred(typeClass, parameters); + } +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TUnionSerializer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TUnionSerializer.java new file mode 100644 index 000000000..484c6e14a --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TUnionSerializer.java @@ -0,0 +1,23 @@ +package org.simantics.scl.compiler.internal.serialization.serializers; + +import org.simantics.scl.compiler.types.TUnion; +import org.simantics.scl.compiler.types.Types; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +public class TUnionSerializer extends Serializer { + public static final TUnionSerializer INSTANCE = new TUnionSerializer(); + + @Override + public void write(Kryo kryo, Output output, TUnion object) { + SerializationUtils.writeTypes(kryo, output, object.effects); + } + + @Override + public TUnion read(Kryo kryo, Input input, Class type) { + return (TUnion)Types.union(SerializationUtils.readTypes(kryo, input)); + } +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TypeDescSerializer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TypeDescSerializer.java new file mode 100644 index 000000000..e7c4b3657 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/TypeDescSerializer.java @@ -0,0 +1,23 @@ +package org.simantics.scl.compiler.internal.serialization.serializers; + +import org.cojen.classfile.TypeDesc; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +public class TypeDescSerializer extends Serializer { + public static final TypeDescSerializer INSTANCE = new TypeDescSerializer(); + + @Override + public void write(Kryo kryo, Output output, TypeDesc object) { + output.writeString(object.getDescriptor()); + } + + @Override + public TypeDesc read(Kryo kryo, Input input, Class type) { + return TypeDesc.forDescriptor(input.readString()); + } + +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/ValRefSerializer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/ValRefSerializer.java new file mode 100644 index 000000000..e81f069e6 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/serialization/serializers/ValRefSerializer.java @@ -0,0 +1,28 @@ +package org.simantics.scl.compiler.internal.serialization.serializers; + +import org.simantics.scl.compiler.internal.codegen.references.Val; +import org.simantics.scl.compiler.internal.codegen.references.ValRef; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +public class ValRefSerializer extends Serializer { + public static final ValRefSerializer INSTANCE = new ValRefSerializer(); + + @Override + public void write(Kryo kryo, Output output, ValRef object) { + SerializationUtils.writeTypes(kryo, output, object.getTypeParameters()); + kryo.writeClassAndObject(output, object.getBinding()); + } + + @Override + public ValRef read(Kryo kryo, Input input, Class type) { + ValRef result = new ValRef(SerializationUtils.readTypes(kryo, input)); + kryo.reference(result); + result.setBinding((Val)kryo.readClassAndObject(input)); + return result; + } + +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java index 73311e99a..b973edd5a 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java @@ -49,7 +49,7 @@ public class ConcreteModule implements Module { CompilationError[] warnings = CompilationError.EMPTY_ARRAY; Map classes = Collections.emptyMap(); - ClassLoader parentClassLoader; + transient ClassLoader parentClassLoader; ModuleInitializer moduleInitializer; protected Documentation documentation; diff --git a/features/org.simantics.scl.feature/feature.xml b/features/org.simantics.scl.feature/feature.xml index c0ee33b4e..8a58a540b 100644 --- a/features/org.simantics.scl.feature/feature.xml +++ b/features/org.simantics.scl.feature/feature.xml @@ -241,4 +241,32 @@ This Agreement is governed by the laws of the State of New York and the intellec version="0.0.0" unpack="false"/> + + + + + + + + diff --git a/tests/org.simantics.scl.compiler.tests/META-INF/MANIFEST.MF b/tests/org.simantics.scl.compiler.tests/META-INF/MANIFEST.MF index 710abbf56..3a791c7d9 100644 --- a/tests/org.simantics.scl.compiler.tests/META-INF/MANIFEST.MF +++ b/tests/org.simantics.scl.compiler.tests/META-INF/MANIFEST.MF @@ -12,4 +12,8 @@ Require-Bundle: org.junit;bundle-version="4.12.0", ch.qos.logback.classic;bundle-version="1.1.7", ch.qos.logback.core;bundle-version="1.1.7", org.apache.commons.collections;bundle-version="3.2.2", - org.eclipse.equinox.ds;bundle-version="1.4.300" + org.eclipse.equinox.ds;bundle-version="1.4.300", + com.esotericsoftware.kryo, + com.esotericsoftware.minlog;bundle-version="1.3.0", + com.esotericsoftware.reflectasm;bundle-version="1.11.3", + org.objenesis;bundle-version="2.2.0" diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/serialization/TestSerialization.java b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/serialization/TestSerialization.java new file mode 100644 index 000000000..751dbf423 --- /dev/null +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/serialization/TestSerialization.java @@ -0,0 +1,61 @@ +package org.simantics.scl.compiler.tests.serialization; + +import java.io.IOException; + +import org.junit.Ignore; +import org.junit.Test; +import org.simantics.scl.compiler.internal.serialization.KryoRegistration; +import org.simantics.scl.compiler.module.ConcreteModule; +import org.simantics.scl.compiler.module.repository.ImportFailureException; +import org.simantics.scl.compiler.tests.TestBase; +import org.simantics.scl.compiler.top.SCLExpressionCompilationException; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Output; + +import gnu.trove.map.hash.THashMap; + +public class TestSerialization { + + @Ignore + @Test + public void typeSerialization() throws IOException, ImportFailureException, SCLExpressionCompilationException { + final Kryo kryo = new Kryo(); + KryoRegistration.registerClasses(kryo); + + Output output = new Output(1024, -1); + ConcreteModule module = (ConcreteModule)TestBase.PRELUDE_MODULE_REPOSITORY.getModule("Prelude").getResult(); + module.setClasses(new THashMap()); + kryo.writeClassAndObject(output, module); + //kryo.writeClassAndObject(output, Environments.getType(env, "forall a. Integer -> [Maybe a]")); + byte[] bytes = output.toBytes(); + /*Class[] keys = kryo.classCounter.keys(new Class[kryo.classCounter.size()]); + Arrays.sort(keys, new Comparator() { + @Override + public int compare(Class o1, Class o2) { + int c1 = kryo.classCounter.get(o1); + int c2 = kryo.classCounter.get(o2); + return Integer.compare(c2, c1); + } + });*/ + //for(Class key : keys) + // System.out.println((key == null ? "null" : key.getSimpleName()) + " " + kryo.classCounter.get(key)); + + System.out.println(bytes.length); + /*for(byte b : bytes) { + int i = (int)b; + if(i < 0) + i += 256; + System.out.print(i); + if(i >= 128) + i -= 128; + if(i >= 20 && i < 128) + System.out.print(" (" + (char)i + ")"); + System.out.println(); + }*/ + + /*Input input = new Input(bytes); + Type type = (Type)kryo.readClassAndObject(input); + System.out.println(type);*/ + } +} -- 2.47.1