From c3e4f46095658fdf331046e5812bc135e66f4844 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 17 Dec 2023 12:26:09 +0000 Subject: Allocate namespace values using GC --- .../compiler/patio/PatioCodeGenNamespaces.java | 88 +++++++++++++++++----- 1 file changed, 69 insertions(+), 19 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java index 682c6ceb..2285b408 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java @@ -32,11 +32,11 @@ public class PatioCodeGenNamespaces { var source = Sources.NULL; var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); - var initialValue = namespaceDataInitialValue(namespaceTypeInfo); + var layout = namespaceDataLayout(namespaceTypeInfo); context = context.addDataElementMutable( globalNameNamespaceValue(namespaceName), - initialValue, + PatioDataValues.dataPointerNull(), source ); @@ -52,6 +52,30 @@ public class PatioCodeGenNamespaces { PatioTypes.VOID, source, initContext -> { + var namespaceValueResult = initContext.allocateVal( + PatioCodeGenLocalSpace.NAMESPACE_VALUE, + PatioTypes.DATA_POINTER + ); + var namespaceValue = namespaceValueResult.value(); + initContext = namespaceValueResult.context(); + + initContext = PatioCodeGenGc.alloc( + namespaceValue, + layout, + GC_CHILDREN_OFFSET, + namespaceTypeInfo.fieldCount(), + source, + initContext + ); + + initContext = initContext.addInstruction(new PatioMemoryStore( + new PatioDataPtr(globalNameNamespaceValue(namespaceName)), + PatioDataLayouts.DATA_POINTER, + List.of(), + namespaceValue, + source + )); + initContext = generateInitBody.apply(initContext); initContext = initContext.addInstruction(new PatioMemoryStore( @@ -112,8 +136,23 @@ public class PatioCodeGenNamespaces { source )); + var namespaceValuePointerResult = importContext.allocateVal( + PatioCodeGenLocalSpace.NAMESPACE_POINTER, + NAMESPACE_VALUE_TYPE + ); + var namespaceValuePointer = namespaceValuePointerResult.value(); + importContext = namespaceValuePointerResult.context(); + + importContext = importContext.addInstruction(new PatioMemoryLoad( + namespaceValuePointer, + new PatioDataPtr(globalNameNamespaceValue(namespaceName)), + PatioDataLayouts.DATA_POINTER, + List.of(), + source + )); + var namespaceValueResult = importContext.allocateVal( - PatioCodeGenLocalSpace.TEMPORARY, + PatioCodeGenLocalSpace.NAMESPACE_VALUE, NAMESPACE_VALUE_TYPE ); var namespaceValue = namespaceValueResult.value(); @@ -121,9 +160,7 @@ public class PatioCodeGenNamespaces { importContext = importContext.addInstruction(new PatioDataPtrToInt( namespaceValue, - new PatioDataPtr( - globalNameNamespaceValue(namespaceName) - ), + namespaceValuePointer, source )); @@ -171,7 +208,7 @@ public class PatioCodeGenNamespaces { PatioCodeGenContext context ) { var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); - var initialValue = namespaceDataInitialValue(namespaceTypeInfo); + var layout = namespaceDataLayout(namespaceTypeInfo); var namespacePointerResult = context.allocateVal( PatioCodeGenLocalSpace.NAMESPACE_POINTER, @@ -190,7 +227,7 @@ public class PatioCodeGenNamespaces { context = context.addInstruction(new PatioMemoryLoad( target, namespacePointer, - initialValue.layout(), + layout, fieldOffset(namespaceTypeInfo.fieldIndex(fieldName)), source )); @@ -206,28 +243,38 @@ public class PatioCodeGenNamespaces { PatioCodeGenContext context ) { var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); - var initialValue = namespaceDataInitialValue(namespaceTypeInfo); + var layout = namespaceDataLayout(namespaceTypeInfo); - context = context.addInstruction(new PatioMemoryStore( + var namespaceValueResult = context.allocateVal( + PatioCodeGenLocalSpace.NAMESPACE_VALUE, + PatioTypes.DATA_POINTER + ); + var namespaceValue = namespaceValueResult.value(); + context = namespaceValueResult.context(); + + context = context.addInstruction(new PatioMemoryLoad( + namespaceValue, new PatioDataPtr(globalNameNamespaceValue(namespaceName)), - initialValue.layout(), + PatioDataLayouts.DATA_POINTER, + List.of(), + source + )); + + context = context.addInstruction(new PatioMemoryStore( + namespaceValue, + layout, fieldOffset(namespaceTypeInfo.fieldIndex(fieldName)), fieldValue, source )); - // TODO: we only have to add fields since namespace values are marked - // as GC objects without children. An (better?) alternative would be to - // have namespace values behave as ordinary GC objects with GC children. - context = PatioCodeGenGc.addPermGcRoot(fieldValue, source, context); - return context; } - private static PatioDataValue namespaceDataInitialValue(NamespaceTypeInfo namespaceTypeInfo) { - return PatioCodeGenGc.gcObjectStaticDataValue(PatioDataValues.struct( + private static PatioDataLayout namespaceDataLayout(NamespaceTypeInfo namespaceTypeInfo) { + return PatioCodeGenGc.gcObjectDataLayout(PatioDataLayouts.struct( Lists.repeat( - PatioDataValues.int_(0), + PatioDataLayouts.INT, namespaceTypeInfo.fieldCount() ) )); @@ -237,6 +284,9 @@ public class PatioCodeGenNamespaces { return PatioCodeGenGc.gcObjectOffset(List.of(fieldIndex)); } + private static final List GC_CHILDREN_OFFSET = + PatioCodeGenGc.gcObjectOffset(List.of()); + // TODO: allocate names through context private static String globalNameNamespaceValue(NamespaceName namespaceName) { return "shed__namespace__" + namespaceNameToPatioIdentifier(namespaceName); -- cgit v1.2.3