diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-01-31 19:44:06 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-01-31 20:12:27 +0000 |
| commit | 920fa1fdcb3eac4348591ebaba6b42df9d5f5ae9 (patch) | |
| tree | e6bcc691605bfa0f54bbe74ceac6d0a603fd1155 /src/main/java | |
| parent | 996b383373711e6b3bbbbd6b965e019d60ddfe19 (diff) | |
Introduce RuntimeType
Diffstat (limited to 'src/main/java')
9 files changed, 322 insertions, 183 deletions
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/RuntimeType.java b/src/main/java/org/shedlang/compiler/shidtopatio/RuntimeType.java new file mode 100644 index 00000000..9375564f --- /dev/null +++ b/src/main/java/org/shedlang/compiler/shidtopatio/RuntimeType.java @@ -0,0 +1,11 @@ +package org.shedlang.compiler.shidtopatio; + +import org.shedlang.compiler.patio.types.PatioDataLayout; + +import java.util.List; + +public interface RuntimeType { + PatioDataLayout layout(); + List<Integer> gcChildrenOffset(); + int gcChildrenCount(); +} diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioClosures.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioClosures.java index 55ef675b..4a96a2bf 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioClosures.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioClosures.java @@ -29,45 +29,70 @@ class ShidToPatioClosures { private ShidToPatioClosures() {} - private static PatioDataLayout closureDataLayoutForCall() { - return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( - PatioDataLayouts.FUNCTION_PTR - ))); - } - - static PatioDataLayout closureDataLayout( + public static ClosureRuntimeType closureRuntimeType( PatioDataLayout additional, FreeVariables freeVariables, ShidToPatioContext context ) { - return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( - PatioDataLayouts.FUNCTION_PTR, + return new ClosureRuntimeType( additional, - PatioDataLayouts.struct( - freeVariables.stream() - .map(freeVariable -> PatioDataLayouts.fromType( - context.shidLocalType(freeVariable.name()) - )) - .toList() - ) - ))); + freeVariables.stream() + .map(freeVariable -> PatioDataLayouts.fromType( + context.shidLocalType(freeVariable.name()) + )) + .toList() + ); } - static final List<Integer> FUNCTION_POINTER_OFFSET - = ShidToPatioGc.gcObjectOffset(List.of(0)); + public record ClosureRuntimeType( + PatioDataLayout additional, + List<PatioDataLayout> freeVariables + ) implements RuntimeType { + @Override + public PatioDataLayout layout() { + return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( + PatioDataLayouts.FUNCTION_PTR, + additional, + PatioDataLayouts.struct(freeVariables) + ))); + } + + private static final List<Integer> FUNCTION_PTR_OFFSET + = ShidToPatioGc.gcObjectOffset(List.of(0)); - static List<Integer> additionalOffset(List<Integer> offset) { - return ShidToPatioGc.gcObjectOffset(P.vector(1).plusAll(offset)); + public List<Integer> functionPtrOffset() { + return FUNCTION_PTR_OFFSET; + } + + private static final List<Integer> GC_CHILDREN_OFFSET = + ShidToPatioGc.gcObjectOffset(List.of(2)); + + @Override + public List<Integer> gcChildrenOffset() { + return GC_CHILDREN_OFFSET; + } + + @Override + public int gcChildrenCount() { + return freeVariables.size(); + } + + public List<Integer> freeVariableOffset( + FreeVariable freeVariable + ) { + return ShidToPatioGc.gcObjectOffset(List.of(2, freeVariable.index())); + } } - private static List<Integer> freeVariableOffset( - FreeVariable freeVariable - ) { - return ShidToPatioGc.gcObjectOffset(List.of(2, freeVariable.index())); + private static PatioDataLayout closureDataLayoutForCall() { + return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( + PatioDataLayouts.FUNCTION_PTR + ))); } - private static final List<Integer> GC_CHILDREN_OFFSET = - ShidToPatioGc.gcObjectOffset(List.of(2)); + static List<Integer> additionalOffset(List<Integer> offset) { + return ShidToPatioGc.gcObjectOffset(P.vector(1).plusAll(offset)); + } static ShidToPatioContext defineParams( List<ShidDefineFunction.Param> positionalParams, @@ -90,7 +115,7 @@ class ShidToPatioClosures { String name, List<ShidDefineFunction.Param> positionalParams, List<ShidDefineFunction.Param> namedParams, - PatioDataLayout layout, + ClosureRuntimeType closureRuntimeType, BiFunction<PatioLocalRef, ShidToPatioContext, ShidToPatioContext> storeAdditional, FreeVariables freeVariables, BiFunction<PatioLocalRef, ShidToPatioContext, ShidToPatioContext> generateBody, @@ -118,7 +143,7 @@ class ShidToPatioClosures { bodyContext = loadFreeVariables( freeVariables, closurePointer, - layout, + closureRuntimeType, source, bodyContext, bodyContextParent @@ -135,7 +160,7 @@ class ShidToPatioClosures { destination, name, fullName, - layout, + closureRuntimeType, storeAdditional, freeVariables, source, @@ -149,7 +174,7 @@ class ShidToPatioClosures { PatioLocalRef destination, String name, String functionName, - PatioDataLayout layout, + ClosureRuntimeType closureRuntimeType, BiFunction<PatioLocalRef, ShidToPatioContext, ShidToPatioContext> storeAdditional, FreeVariables freeVariables, Source source, @@ -165,17 +190,15 @@ class ShidToPatioClosures { context = ShidToPatioGc.alloc( closurePointer, - layout, - GC_CHILDREN_OFFSET, - freeVariables.size(), + closureRuntimeType, source, context ); context = context.addInstruction(new PatioMemoryStore( closurePointer, - layout, - FUNCTION_POINTER_OFFSET, + closureRuntimeType.layout(), + closureRuntimeType.functionPtrOffset(), new PatioFunctionPtrConst(functionName), source )); @@ -185,7 +208,7 @@ class ShidToPatioClosures { context = storeFreeVariables( freeVariables, closurePointer, - layout, + closureRuntimeType, source, context ); @@ -202,7 +225,7 @@ class ShidToPatioClosures { static ShidToPatioContext storeFreeVariables( FreeVariables freeVariables, PatioLocalRef closurePointer, - PatioDataLayout closureLayout, + ClosureRuntimeType closureRuntimeType, Source source, ShidToPatioContext context ) { @@ -211,8 +234,8 @@ class ShidToPatioClosures { freeVariable.name(), c -> c.addInstruction(new PatioMemoryStore( closurePointer, - closureLayout, - freeVariableOffset(freeVariable), + closureRuntimeType.layout(), + closureRuntimeType.freeVariableOffset(freeVariable), c.shidLocalNameToLocal(freeVariable.name()), source )) @@ -224,7 +247,7 @@ class ShidToPatioClosures { static ShidToPatioContext loadFreeVariables( FreeVariables freeVariables, PatioLocalRef closurePointer, - PatioDataLayout closureLayout, + ClosureRuntimeType closureRuntimeType, Source source, ShidToPatioContext bodyContext, ShidToPatioContext bodyContextParent @@ -240,8 +263,8 @@ class ShidToPatioClosures { bodyContext = bodyContext.addInstruction(new PatioMemoryLoad( freeVariableLocal, closurePointer, - closureLayout, - freeVariableOffset(freeVariable), + closureRuntimeType.layout(), + closureRuntimeType.freeVariableOffset(freeVariable), source )); @@ -290,7 +313,7 @@ class ShidToPatioClosures { functionPointer, closurePointer, closureDataLayoutForCall(), - FUNCTION_POINTER_OFFSET, + ClosureRuntimeType.FUNCTION_PTR_OFFSET, source )); diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffects.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffects.java index 0bffd357..4a6baaa3 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffects.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffects.java @@ -31,37 +31,57 @@ public class ShidToPatioEffects { private static final PatioType EFFECT_HANDLER_METHOD_POINTER_TYPE = PatioTypes.DATA_PTR; private static final PatioType EFFECT_HANDLER_METHOD_TYPE = ShidToPatioTypes.VALUE_TYPE; - private static PatioDataLayout effectHandlerMethodDataLayout( + private static ShidToPatioClosures.ClosureRuntimeType effectHandlerMethodRuntimeType( FreeVariables freeVariables, ShidToPatioContext context ) { - return ShidToPatioClosures.closureDataLayout( - PatioDataLayouts.struct(List.of(PatioDataLayouts.DATA_PTR)), + return ShidToPatioClosures.closureRuntimeType( + EFFECT_HANDLER_METHOD_ADDITIONAL_DATA_LAYOUT, freeVariables, context ); } + private static final PatioDataLayout EFFECT_HANDLER_METHOD_ADDITIONAL_DATA_LAYOUT = + PatioDataLayouts.struct(List.of(PatioDataLayouts.DATA_PTR)); + private static final List<Integer> CLOSURE_EFFECT_HANDLER_OFFSET = ShidToPatioClosures.additionalOffset(List.of(0)); - private static PatioDataLayout userDefinedEffectLayout( + private static UserDefinedEffectRuntimeType userDefinedEffectRuntimeType( UserDefinedEffect effect, ShidToPatioContext context ) { var effectInfo = context.userDefinedEffectInfo(effect); - return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(Lists.repeat( - PatioDataLayouts.fromType(ShidToPatioClosures.CLOSURE_TYPE), - effectInfo.methodCount() - ))); + return new UserDefinedEffectRuntimeType(effectInfo.methodCount()); } - private static List<Integer> userDefinedEffectMethodOffset(int methodIndex) { - return ShidToPatioGc.gcObjectOffset(List.of(methodIndex)); - } + private record UserDefinedEffectRuntimeType(int methodCount) implements RuntimeType { + @Override + public PatioDataLayout layout() { + return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(Lists.repeat( + PatioDataLayouts.fromType(ShidToPatioClosures.CLOSURE_TYPE), + methodCount + ))); + } + + private static final List<Integer> GC_CHILDREN_OFFSET = + ShidToPatioGc.gcObjectOffset(List.of()); + + @Override + public List<Integer> gcChildrenOffset() { + return GC_CHILDREN_OFFSET; + } + + @Override + public int gcChildrenCount() { + return methodCount; + } - private static final List<Integer> USER_DEFINED_EFFECT_GC_CHILDREN_OFFSET = - ShidToPatioGc.gcObjectOffset(List.of()); + public List<Integer> methodOffset(int methodIndex) { + return ShidToPatioGc.gcObjectOffset(List.of(methodIndex)); + } + } private static final PatioFunctionDeclaration FIND_HANDLER_METHOD_DECLARATION = new PatioFunctionDeclaration( "shed__runtime__effects_find_handler_method", @@ -138,15 +158,13 @@ public class ShidToPatioEffects { var effectPointer = effectPointerResult.value(); context = effectPointerResult.context(); - var effectLayout = userDefinedEffectLayout(effect, context); + var effectRuntimeType = userDefinedEffectRuntimeType(effect, context); var effectInfo = context.userDefinedEffectInfo(effect); context = ShidToPatioGc.alloc( effectPointer, - effectLayout, - USER_DEFINED_EFFECT_GC_CHILDREN_OFFSET, - effectInfo.methodCount(), + effectRuntimeType, source, context ); @@ -228,8 +246,8 @@ public class ShidToPatioEffects { context = context .addInstruction(new PatioMemoryStore( effectPointer, - effectLayout, - userDefinedEffectMethodOffset(method.fieldIndex()), + effectRuntimeType.layout(), + effectRuntimeType.methodOffset(method.fieldIndex()), effectMethod, source )); @@ -265,7 +283,7 @@ public class ShidToPatioEffects { source )); - var effectDataLayout = userDefinedEffectLayout(effect, context); + var effectRuntimeType = userDefinedEffectRuntimeType(effect, context); var effectInfo = context.userDefinedEffectInfo(effect); var methodIndex = effectInfo.methodIndex(fieldName); @@ -273,8 +291,8 @@ public class ShidToPatioEffects { context = context.addInstruction(new PatioMemoryLoad( destination, effectPointer, - effectDataLayout, - userDefinedEffectMethodOffset(methodIndex), + effectRuntimeType.layout(), + effectRuntimeType.methodOffset(methodIndex), source )); @@ -383,7 +401,7 @@ public class ShidToPatioEffects { .ensureCompilationUnitElement(GET_HANDLER_DECLARATION) .ensureCompilationUnitElement(SET_HANDLER_DECLARATION); - var layout = effectHandlerMethodDataLayout(freeVariables, context); + var effectHandlerMethodRuntimeType = effectHandlerMethodRuntimeType(freeVariables, context); return ShidToPatioClosures.create( destination, @@ -391,7 +409,7 @@ public class ShidToPatioEffects { name, positionalParams, namedParams, - layout, + effectHandlerMethodRuntimeType, (closurePointer, allocContext) -> { var effectHandlerResult = allocContext.allocateVal( ShidToPatioLocalSpace.EFFECT_HANDLER_CURRENT, @@ -404,7 +422,7 @@ public class ShidToPatioEffects { allocContext = allocContext.addInstruction(new PatioMemoryStore( closurePointer, - layout, + effectHandlerMethodRuntimeType.layout(), CLOSURE_EFFECT_HANDLER_OFFSET, effectHandler, source @@ -435,7 +453,7 @@ public class ShidToPatioEffects { bodyContext = bodyContext.addInstruction(new PatioMemoryLoad( effectHandler, closurePointer, - layout, + effectHandlerMethodRuntimeType.layout(), CLOSURE_EFFECT_HANDLER_OFFSET, source )); diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioFunctions.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioFunctions.java index 633cb7b1..01527ae5 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioFunctions.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioFunctions.java @@ -14,12 +14,14 @@ public class ShidToPatioFunctions { private ShidToPatioFunctions() { } - private static PatioDataLayout functionDataLayout( + private static final PatioDataLayout ADDITIONAL_DATA_LAYOUT = PatioDataLayouts.struct(List.of()); + + private static ShidToPatioClosures.ClosureRuntimeType functionRuntimeType( FreeVariables freeVariables, ShidToPatioContext context ) { - return ShidToPatioClosures.closureDataLayout( - PatioDataLayouts.struct(List.of()), + return ShidToPatioClosures.closureRuntimeType( + ADDITIONAL_DATA_LAYOUT, freeVariables, context ); @@ -35,7 +37,7 @@ public class ShidToPatioFunctions { Source source, ShidToPatioContext context ) { - var layout = functionDataLayout(freeVariables, context); + var functionRuntimeType = functionRuntimeType(freeVariables, context); return ShidToPatioClosures.create( destination, @@ -43,7 +45,7 @@ public class ShidToPatioFunctions { name, positionalParams, namedParams, - layout, + functionRuntimeType, (closurePointer, storeContext) -> storeContext, freeVariables, (closurePointer, bodyContext) -> generateBody.apply(bodyContext), diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java index 4bb7c3a3..e2c9242a 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java @@ -138,6 +138,21 @@ public class ShidToPatioGc { Sources.RUNTIME ); + public static ShidToPatioContext alloc( + PatioLocalRef destination, + RuntimeType runtimeType, + Source source, + ShidToPatioContext context + ) { + return alloc( + destination, + runtimeType.layout(), + runtimeType.gcChildrenOffset(), + runtimeType.gcChildrenCount(), + source, + context + ); + } public static ShidToPatioContext alloc( PatioLocalRef destination, diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaces.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaces.java index c4fcb5fb..b2f1447f 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaces.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaces.java @@ -10,7 +10,6 @@ import org.shedlang.compiler.patio.operands.PatioOperand; import org.shedlang.compiler.patio.types.*; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.NamespaceTypeInfo; import org.shedlang.compiler.util.Lists; import java.util.List; @@ -22,14 +21,50 @@ public class ShidToPatioNamespaces { private ShidToPatioNamespaces() {} + private static NamespaceRuntimeType namespaceRuntimeType( + NamespaceName namespaceName, + ShidToPatioContext context + ) { + var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); + return new NamespaceRuntimeType(namespaceTypeInfo.fieldCount()); + } + + private record NamespaceRuntimeType(int fieldCount) implements RuntimeType { + @Override + public PatioDataLayout layout() { + return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct( + Lists.repeat( + PatioDataLayouts.INT, + fieldCount + ) + )); + } + + private static final List<Integer> GC_CHILDREN_OFFSET = + ShidToPatioGc.gcObjectOffset(List.of()); + + @Override + public List<Integer> gcChildrenOffset() { + return GC_CHILDREN_OFFSET; + } + + @Override + public int gcChildrenCount() { + return fieldCount; + } + + public List<Integer> fieldOffset(int fieldIndex) { + return ShidToPatioGc.gcObjectOffset(List.of(fieldIndex)); + } + } + public static ShidToPatioContext define( NamespaceName namespaceName, Function<ShidToPatioContext, ShidToPatioContext> generateInitBody, Source source, ShidToPatioContext context ) { - var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); - var layout = namespaceDataLayout(namespaceTypeInfo); + var namespaceRuntimeType = namespaceRuntimeType(namespaceName, context); context = context.addDataElementMutable( globalNameNamespaceValue(namespaceName), @@ -58,9 +93,7 @@ public class ShidToPatioNamespaces { initContext = ShidToPatioGc.alloc( namespaceValue, - layout, - GC_CHILDREN_OFFSET, - namespaceTypeInfo.fieldCount(), + namespaceRuntimeType, source, initContext ); @@ -205,7 +238,7 @@ public class ShidToPatioNamespaces { ShidToPatioContext context ) { var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); - var layout = namespaceDataLayout(namespaceTypeInfo); + var namespaceRuntimeType = namespaceRuntimeType(namespaceName, context); var namespacePointerResult = context.allocateVal( ShidToPatioLocalSpace.NAMESPACE_POINTER, @@ -224,8 +257,8 @@ public class ShidToPatioNamespaces { context = context.addInstruction(new PatioMemoryLoad( destination, namespacePointer, - layout, - fieldOffset(namespaceTypeInfo.fieldIndex(fieldName)), + namespaceRuntimeType.layout(), + namespaceRuntimeType.fieldOffset(namespaceTypeInfo.fieldIndex(fieldName)), source )); @@ -240,7 +273,7 @@ public class ShidToPatioNamespaces { ShidToPatioContext context ) { var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); - var layout = namespaceDataLayout(namespaceTypeInfo); + var namespaceRuntimeType = namespaceRuntimeType(namespaceName, context); var namespaceValueResult = context.allocateVal( ShidToPatioLocalSpace.NAMESPACE_VALUE, @@ -259,8 +292,8 @@ public class ShidToPatioNamespaces { context = context.addInstruction(new PatioMemoryStore( namespaceValue, - layout, - fieldOffset(namespaceTypeInfo.fieldIndex(fieldName)), + namespaceRuntimeType.layout(), + namespaceRuntimeType.fieldOffset(namespaceTypeInfo.fieldIndex(fieldName)), fieldValue, source )); @@ -268,22 +301,6 @@ public class ShidToPatioNamespaces { return context; } - private static PatioDataLayout namespaceDataLayout(NamespaceTypeInfo namespaceTypeInfo) { - return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct( - Lists.repeat( - PatioDataLayouts.INT, - namespaceTypeInfo.fieldCount() - ) - )); - } - - private static List<Integer> fieldOffset(int fieldIndex) { - return ShidToPatioGc.gcObjectOffset(List.of(fieldIndex)); - } - - private static final List<Integer> GC_CHILDREN_OFFSET = - ShidToPatioGc.gcObjectOffset(List.of()); - private static PatioOperand globalNameNamespaceValueOperand(NamespaceName namespaceName) { return PatioDataPtrConst.dataElement(globalNameNamespaceValue(namespaceName)); } diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java index 19f7b6f4..8cce8d9a 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java @@ -20,6 +20,57 @@ public class ShidToPatioStructs { private ShidToPatioStructs() { } + private static PatioDataLayout unknownStructDataLayout() { + return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( + PatioDataLayouts.INT + ))); + } + + private static final List<Integer> TAG_OFFSET + = ShidToPatioGc.gcObjectOffset(List.of(0)); + + private static StructRuntimeType structRuntimeType( + SimpleStructType type, + ShidToPatioContext context + ) { + var structTypeInfo = context.structTypeInfo(type); + return new StructRuntimeType(type, structTypeInfo.fieldCount()); + } + + private record StructRuntimeType(SimpleStructType type, int fieldCount) implements RuntimeType { + @Override + public PatioDataLayout layout() { + return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( + PatioDataLayouts.INT, + PatioDataLayouts.struct(Lists.repeat( + PatioDataLayouts.INT, + fieldCount + )) + ))); + } + + private static final List<Integer> GC_CHILDREN_OFFSET = + ShidToPatioGc.gcObjectOffset(List.of(1)); + + @Override + public List<Integer> gcChildrenOffset() { + return GC_CHILDREN_OFFSET; + } + + @Override + public int gcChildrenCount() { + return fieldCount; + } + + public List<Integer> tagOffset() { + return TAG_OFFSET; + } + + public List<Integer> fieldOffset(int fieldIndex) { + return ShidToPatioGc.gcObjectOffset(List.of(1, fieldIndex)); + } + } + public static ShidToPatioContext createType( PatioLocalRef destination, SimpleStructType type, @@ -43,13 +94,11 @@ public class ShidToPatioStructs { var valuePointer = valuePointerResult.value(); bodyContext = valuePointerResult.context(); - var structDataLayout = structDataLayout(type, context); + var structRuntimeType = structRuntimeType(type, context); bodyContext = ShidToPatioGc.alloc( valuePointer, - structDataLayout, - GC_CHILDREN_OFFSET, - fields.size(), + structRuntimeType, source, bodyContext ); @@ -57,8 +106,8 @@ public class ShidToPatioStructs { bodyContext = bodyContext .addInstruction(new PatioMemoryStore( valuePointer, - structDataLayout, - TAG_OFFSET, + structRuntimeType.layout(), + structRuntimeType.tagOffset(), PatioIntConst.of(context.structTag(type)), source )); @@ -69,8 +118,8 @@ public class ShidToPatioStructs { bodyContext = bodyContext .addInstruction(new PatioMemoryStore( valuePointer, - structDataLayout, - fieldOffset(fieldIndex), + structRuntimeType.layout(), + structRuntimeType.fieldOffset(fieldIndex), bodyContext.shidLocalNameToLocal(field.name()), source )); @@ -119,7 +168,7 @@ public class ShidToPatioStructs { source )); - var structDataLayout = structDataLayout(type, context); + var structRuntimeType = structRuntimeType(type, context); var structTypeInfo = context.structTypeInfo(type); var fieldIndex = structTypeInfo.fieldIndex(fieldName); @@ -127,8 +176,8 @@ public class ShidToPatioStructs { context = context.addInstruction(new PatioMemoryLoad( destination, valuePointer, - structDataLayout, - fieldOffset(fieldIndex), + structRuntimeType.layout(), + structRuntimeType.fieldOffset(fieldIndex), source )); @@ -166,35 +215,4 @@ public class ShidToPatioStructs { return context; } - - private static PatioDataLayout unknownStructDataLayout() { - return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( - PatioDataLayouts.INT - ))); - } - - private static PatioDataLayout structDataLayout( - SimpleStructType type, - ShidToPatioContext context - ) { - var structTypeInfo = context.structTypeInfo(type); - - return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(List.of( - PatioDataLayouts.INT, - PatioDataLayouts.struct(Lists.repeat( - PatioDataLayouts.INT, - structTypeInfo.fieldCount() - )) - ))); - } - - private static final List<Integer> TAG_OFFSET - = ShidToPatioGc.gcObjectOffset(List.of(0)); - - private static List<Integer> fieldOffset(int fieldIndex) { - return ShidToPatioGc.gcObjectOffset(List.of(1, fieldIndex)); - } - - private static final List<Integer> GC_CHILDREN_OFFSET = - ShidToPatioGc.gcObjectOffset(List.of(1)); } diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTuples.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTuples.java index fa6fac58..cdbe334c 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTuples.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTuples.java @@ -16,6 +16,33 @@ public class ShidToPatioTuples { private ShidToPatioTuples() { } + private record TupleRuntimeType(int elementCount) implements RuntimeType { + @Override + public PatioDataLayout layout() { + return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(Lists.repeat( + PatioDataLayouts.INT, + elementCount + ))); + } + + private static final List<Integer> GC_CHILDREN_OFFSET = + ShidToPatioGc.gcObjectOffset(List.of()); + + @Override + public List<Integer> gcChildrenOffset() { + return GC_CHILDREN_OFFSET; + } + + @Override + public int gcChildrenCount() { + return elementCount; + } + + public List<Integer> elementOffset(int elementIndex) { + return ShidToPatioGc.gcObjectOffset(List.of(elementIndex)); + } + } + public static ShidToPatioResult<PatioLocalRef> create( List<PatioOperand> elements, Source source, @@ -45,13 +72,11 @@ public class ShidToPatioTuples { var tuplePointer = tuplePointerResult.value(); context = tuplePointerResult.context(); - var tupleLayout = tupleDataLayout(elements.size()); + var tupleRuntimeType = new TupleRuntimeType(elements.size()); context = ShidToPatioGc.alloc( tuplePointer, - tupleLayout, - GC_CHILDREN_OFFSET, - elements.size(), + tupleRuntimeType, source, context ); @@ -61,8 +86,8 @@ public class ShidToPatioTuples { context = context.addInstruction(new PatioMemoryStore( tuplePointer, - tupleLayout, - elementOffset(elementIndex), + tupleRuntimeType.layout(), + tupleRuntimeType.elementOffset(elementIndex), element, source )); @@ -105,28 +130,16 @@ public class ShidToPatioTuples { var destination = destinationResult.value(); context = destinationResult.context(); + var tupleRuntimeType = new TupleRuntimeType(elementCount); + context = context.addInstruction(new PatioMemoryLoad( destination, tuplePointer, - tupleDataLayout(elementCount), - elementOffset(elementIndex), + tupleRuntimeType.layout(), + tupleRuntimeType.elementOffset(elementIndex), source )); return context.toResult(destination); } - - private static PatioDataLayout tupleDataLayout(int elementCount) { - return ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.struct(Lists.repeat( - PatioDataLayouts.INT, - elementCount - ))); - } - - private static List<Integer> elementOffset(int elementIndex) { - return ShidToPatioGc.gcObjectOffset(List.of(elementIndex)); - } - - private static final List<Integer> GC_CHILDREN_OFFSET = - ShidToPatioGc.gcObjectOffset(List.of()); } diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioVars.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioVars.java index 34143882..6aa1df90 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioVars.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioVars.java @@ -15,16 +15,40 @@ public class ShidToPatioVars { private ShidToPatioVars() { } - private static final PatioDataLayout VAR_DATA_LAYOUT = - ShidToPatioGc.gcObjectDataLayout( - PatioDataLayouts.struct(List.of( - PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE) - )) - ); + private static class VarRuntimeType implements RuntimeType { + private static final PatioDataLayout LAYOUT = + ShidToPatioGc.gcObjectDataLayout( + PatioDataLayouts.struct(List.of( + PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE) + )) + ); + + @Override + public PatioDataLayout layout() { + return LAYOUT; + } + + private static final List<Integer> GC_CHILDREN_OFFSET = + ShidToPatioGc.gcObjectOffset(List.of()); + + @Override + public List<Integer> gcChildrenOffset() { + return GC_CHILDREN_OFFSET; + } + + @Override + public int gcChildrenCount() { + return 1; + } + + private static final List<Integer> VALUE_OFFSET = ShidToPatioGc.gcObjectOffset(List.of(0)); + + public List<Integer> valueOffset() { + return VALUE_OFFSET; + } + } - private static final List<Integer> VALUE_OFFSET = ShidToPatioGc.gcObjectOffset(List.of(0)); - private static final List<Integer> GC_CHILDREN_OFFSET = ShidToPatioGc.gcObjectOffset(List.of()); - private static final int GC_CHILDREN_COUNT = 1; + private static final VarRuntimeType VAR_RUNTIME_TYPE = new VarRuntimeType(); public static ShidToPatioContext alloc( String shidName, @@ -40,9 +64,7 @@ public class ShidToPatioVars { context = ShidToPatioGc.alloc( patioLocal, - VAR_DATA_LAYOUT, - GC_CHILDREN_OFFSET, - GC_CHILDREN_COUNT, + VAR_RUNTIME_TYPE, source, context ); @@ -60,8 +82,8 @@ public class ShidToPatioVars { ) { return context.addInstruction(new PatioMemoryStore( context.shidLocalNameToLocal(shidName), - VAR_DATA_LAYOUT, - VALUE_OFFSET, + VAR_RUNTIME_TYPE.layout(), + VAR_RUNTIME_TYPE.valueOffset(), value, source )); @@ -76,8 +98,8 @@ public class ShidToPatioVars { return context.addInstruction(new PatioMemoryLoad( destination, context.shidLocalNameToLocal(shidName), - VAR_DATA_LAYOUT, - VALUE_OFFSET, + VAR_RUNTIME_TYPE.layout(), + VAR_RUNTIME_TYPE.valueOffset(), source )); } |
