From c8dd5dc5b3c11bf1822c960a8ad1e7260957b3f9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 16 Dec 2023 22:13:07 +0000 Subject: Make namespaces GC objects --- .../java/org/shedlang/compiler/patio/PatioCodeGenGc.java | 5 +++-- .../shedlang/compiler/patio/PatioCodeGenNamespaces.java | 15 +++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java index cd036368..23ee6cac 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java @@ -42,8 +42,9 @@ public class PatioCodeGenGc { } /** - * Creates a data value for GC objects that should never be garbage - * collected. Such a value does not need to be added to the set of GC roots. + * Creates a data value for GC objects with no GC children that should never + * be garbage collected. Such a value does not need to be added to the set + * of GC roots. */ public static PatioDataValue gcObjectStaticDataValue(PatioDataValue value) { return PatioDataValues.struct(List.of( diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java index 595033b1..682c6ceb 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java @@ -191,7 +191,7 @@ public class PatioCodeGenNamespaces { target, namespacePointer, initialValue.layout(), - List.of(namespaceTypeInfo.fieldIndex(fieldName)), + fieldOffset(namespaceTypeInfo.fieldIndex(fieldName)), source )); @@ -211,23 +211,30 @@ public class PatioCodeGenNamespaces { context = context.addInstruction(new PatioMemoryStore( new PatioDataPtr(globalNameNamespaceValue(namespaceName)), initialValue.layout(), - List.of(namespaceTypeInfo.fieldIndex(fieldName)), + 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 PatioDataValues.struct( + return PatioCodeGenGc.gcObjectStaticDataValue(PatioDataValues.struct( Lists.repeat( PatioDataValues.int_(0), namespaceTypeInfo.fieldCount() ) - ); + )); + } + + private static List fieldOffset(int fieldIndex) { + return PatioCodeGenGc.gcObjectOffset(List.of(fieldIndex)); } // TODO: allocate names through context -- cgit v1.2.3