From b81f97cd01753eabe3722b8a9211e4f3f8d621ef Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 16 Mar 2025 14:32:10 +0000 Subject: Garbage collect arrays --- .../shidtopatio/ArrayBuilderRuntimeType.java | 35 ++++++++++++++++++++++ .../compiler/shidtopatio/ArrayRuntimeType.java | 30 +++++++++++++++++++ .../compiler/shidtopatio/ShidToPatioContext.java | 7 +++++ 3 files changed, 72 insertions(+) create mode 100644 src/main/java/org/shedlang/compiler/shidtopatio/ArrayBuilderRuntimeType.java create mode 100644 src/main/java/org/shedlang/compiler/shidtopatio/ArrayRuntimeType.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ArrayBuilderRuntimeType.java b/src/main/java/org/shedlang/compiler/shidtopatio/ArrayBuilderRuntimeType.java new file mode 100644 index 00000000..5cb8000a --- /dev/null +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ArrayBuilderRuntimeType.java @@ -0,0 +1,35 @@ +package org.shedlang.compiler.shidtopatio; + +import org.shedlang.compiler.patio.types.PatioDataLayout; +import org.shedlang.compiler.patio.types.PatioDataLayouts; + +import java.util.List; + +public class ArrayBuilderRuntimeType implements RuntimeType { + public static final ArrayBuilderRuntimeType INSTANCE = new ArrayBuilderRuntimeType(); + + private ArrayBuilderRuntimeType() { + } + + @Override + public PatioDataLayout layout() { + return ShidToPatioGc.gcObjectDataLayout( + PatioDataLayouts.struct(List.of( + PatioDataLayouts.INT, + PatioDataLayouts.INT, + PatioDataLayouts.INT, + PatioDataLayouts.BOOL + )) + ); + } + + @Override + public List gcChildrenOffset() { + return ShidToPatioGc.gcObjectOffset(List.of()); + } + + @Override + public int gcChildrenCount() { + return 1; + } +} diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ArrayRuntimeType.java b/src/main/java/org/shedlang/compiler/shidtopatio/ArrayRuntimeType.java new file mode 100644 index 00000000..bc977526 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ArrayRuntimeType.java @@ -0,0 +1,30 @@ +package org.shedlang.compiler.shidtopatio; + +import org.shedlang.compiler.patio.types.PatioDataLayout; +import org.shedlang.compiler.patio.types.PatioDataLayouts; + +import java.util.List; + +public class ArrayRuntimeType implements RuntimeType { + public static final ArrayRuntimeType INSTANCE = new ArrayRuntimeType(); + + private ArrayRuntimeType() { + } + + @Override + public PatioDataLayout layout() { + // TODO: separate out runtime type from the info needed by RTTI + return PatioDataLayouts.struct(List.of()); + } + + @Override + public List gcChildrenOffset() { + return List.of(); + } + + @Override + public int gcChildrenCount() { + // This is a lie! We special case arrays in the runtime. + return 0; + } +} diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java index 546a6ccd..87e5ff61 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java @@ -48,6 +48,13 @@ public record ShidToPatioContext( // Ensure that scalars have tag of 0: this is relied on in the // runtime (for instance, when allocating ints and strings). context = context.addRuntimeTypeTag(ScalarRuntimeType.INSTANCE); + + // Ensure that arrays have tag of 1. + context = context.addRuntimeTypeTag(ArrayRuntimeType.INSTANCE); + + // Ensure that array builders have tag of 2. + context = context.addRuntimeTypeTag(ArrayBuilderRuntimeType.INSTANCE); + context = generateStructTags(context); context = generateEffectTags(context); -- cgit v1.2.3