From ca64b2bb8026fb4510696736d2d887e21ac2bfa3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 28 Jan 2024 21:15:53 +0000 Subject: Zero out GC children with explicit memory stores --- runtime/src/gc.c | 6 ---- ...mpiler.testing.snapshots.Snapshotter)].expected | 14 +++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 14 +++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 14 +++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 14 +++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 14 +++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 14 +++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 14 +++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 21 +++++++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 4 +-- ...mpiler.testing.snapshots.Snapshotter)].expected | 4 +-- ...mpiler.testing.snapshots.Snapshotter)].expected | 8 ++--- .../compiler/patio/types/PatioOffsets.java | 13 ++++++++ .../compiler/shidtopatio/ShidToPatioGc.java | 36 +++++++++++++++++++--- .../compiler/shidtopatio/ShidToPatioVars.java | 8 +++-- 15 files changed, 177 insertions(+), 21 deletions(-) diff --git a/runtime/src/gc.c b/runtime/src/gc.c index b15c20dc..8d2efc08 100644 --- a/runtime/src/gc.c +++ b/runtime/src/gc.c @@ -87,12 +87,6 @@ void* shed__runtime__gc_alloc(size_t size, uint32_t children_offset, uint32_t ch object->children_offset = children_offset; object->children_count = children_count; - __builtin_memset( - ((void*) object) + children_offset, - 0, - sizeof(void*) * children_count - ); - // TODO: is it possible to leave adding the root to the caller, and avoid // having to add so many roots? (Since some values will immediately become a // descendant of a root.) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineEffectTests]/[method:canGenerateCodeForDefineEffect(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineEffectTests]/[method:canGenerateCodeForDefineEffect(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index f06a5f47..5475f888 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineEffectTests]/[method:canGenerateCodeForDefineEffect(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineEffectTests]/[method:canGenerateCodeForDefineEffect(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -74,6 +74,20 @@ Instructions: ( (call (local "userDefinedEffectPointer__State__0") "shed__runtime__gc_alloc" ((int-const (byte-size-of (struct ((struct (data-ptr int int32 int32)) (struct (int int)))))) (int32-const (byte-offset-of (struct ((struct (data-ptr int int32 int32)) (struct (int int)))) (1))) (int32-const 2))) + ( + memory-store + pointer=(local "userDefinedEffectPointer__State__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int int)))) + offset=(1 0) + value=(int-const 0) + ) + ( + memory-store + pointer=(local "userDefinedEffectPointer__State__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int int)))) + offset=(1 1) + value=(int-const 0) + ) (call (local "closurePointer__read__0") "shed__runtime__gc_alloc" ((int-const (byte-size-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct ()))))))) (int32-const (byte-offset-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct ()))))) (1 2))) (int32-const 0))) ( memory-store diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:canCaptureFreeVariablesThatAreSetAfterDefinition(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:canCaptureFreeVariablesThatAreSetAfterDefinition(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 00c9a799..7ffd8bed 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:canCaptureFreeVariablesThatAreSetAfterDefinition(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:canCaptureFreeVariablesThatAreSetAfterDefinition(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -69,6 +69,20 @@ Instructions: ( (call (local "closurePointer__f__0") "shed__runtime__gc_alloc" ((int-const (byte-size-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))))) (int32-const (byte-offset-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))) (1 2))) (int32-const 2))) + ( + memory-store + pointer=(local "closurePointer__f__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))) + offset=(1 2 0) + value=(int-const 0) + ) + ( + memory-store + pointer=(local "closurePointer__f__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))) + offset=(1 2 1) + value=(int-const 0) + ) ( memory-store pointer=(local "closurePointer__f__0") diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:immutableFreeVariablesAreStoredInAndLoadedFromEnvironment(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:immutableFreeVariablesAreStoredInAndLoadedFromEnvironment(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index b2251c34..b2b20774 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:immutableFreeVariablesAreStoredInAndLoadedFromEnvironment(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:immutableFreeVariablesAreStoredInAndLoadedFromEnvironment(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -69,6 +69,20 @@ Instructions: ( (call (local "closurePointer__f__0") "shed__runtime__gc_alloc" ((int-const (byte-size-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))))) (int32-const (byte-offset-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))) (1 2))) (int32-const 2))) + ( + memory-store + pointer=(local "closurePointer__f__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))) + offset=(1 2 0) + value=(int-const 0) + ) + ( + memory-store + pointer=(local "closurePointer__f__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))) + offset=(1 2 1) + value=(int-const 0) + ) ( memory-store pointer=(local "closurePointer__f__0") diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:pointersToFreeMutableVariablesAreStoredInAndLoadedFromEnvironment(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:pointersToFreeMutableVariablesAreStoredInAndLoadedFromEnvironment(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 9df8efff..cece95b3 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:pointersToFreeMutableVariablesAreStoredInAndLoadedFromEnvironment(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:pointersToFreeMutableVariablesAreStoredInAndLoadedFromEnvironment(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -69,6 +69,20 @@ Instructions: ( (call (local "closurePointer__f__0") "shed__runtime__gc_alloc" ((int-const (byte-size-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (data-ptr data-ptr)))))))) (int32-const (byte-offset-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (data-ptr data-ptr)))))) (1 2))) (int32-const 2))) + ( + memory-store + pointer=(local "closurePointer__f__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (data-ptr data-ptr)))))) + offset=(1 2 0) + value=(data-ptr-const null) + ) + ( + memory-store + pointer=(local "closurePointer__f__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (data-ptr data-ptr)))))) + offset=(1 2 1) + value=(data-ptr-const null) + ) ( memory-store pointer=(local "closurePointer__f__0") diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index dada1615..1d906b97 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -77,6 +77,20 @@ Instructions: ( (call (local "closurePointer__f__0") "shed__runtime__gc_alloc" ((int-const (byte-size-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))))) (int32-const (byte-offset-of (struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))) (1 2))) (int32-const 2))) + ( + memory-store + pointer=(local "closurePointer__f__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))) + offset=(1 2 0) + value=(int-const 0) + ) + ( + memory-store + pointer=(local "closurePointer__f__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (function-ptr (struct ()) (struct (int int)))))) + offset=(1 2 1) + value=(int-const 0) + ) ( memory-store pointer=(local "closurePointer__f__0") diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineStructTests]/[method:canGenerateCodeForDefineStruct(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineStructTests]/[method:canGenerateCodeForDefineStruct(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 8d3e1d22..665acac6 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineStructTests]/[method:canGenerateCodeForDefineStruct(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioDefineStructTests]/[method:canGenerateCodeForDefineStruct(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -21,6 +21,20 @@ Compilation unit elements: body=( (call (local "gcRootStackIndex__0") "shed__runtime__gc_get_root_stack_index" ()) (call (local "structValuePointer__0") "shed__runtime__gc_alloc" ((int-const (byte-size-of (struct ((struct (data-ptr int int32 int32)) (struct (int (struct (int int)))))))) (int32-const (byte-offset-of (struct ((struct (data-ptr int int32 int32)) (struct (int (struct (int int)))))) (1 1))) (int32-const 2))) + ( + memory-store + pointer=(local "structValuePointer__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int (struct (int int)))))) + offset=(1 1 0) + value=(int-const 0) + ) + ( + memory-store + pointer=(local "structValuePointer__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int (struct (int int)))))) + offset=(1 1 1) + value=(int-const 0) + ) ( memory-store pointer=(local "structValuePointer__0") diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioNamespaceTests]/[method:canDefineNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioNamespaceTests]/[method:canDefineNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index ddc72f15..b2bde483 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioNamespaceTests]/[method:canDefineNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioNamespaceTests]/[method:canDefineNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -54,6 +54,20 @@ return-type=void body=( (call (local "namespaceValue__0") "shed__runtime__gc_alloc" ((int-const (byte-size-of (struct ((struct (data-ptr int int32 int32)) (struct (int int)))))) (int32-const (byte-offset-of (struct ((struct (data-ptr int int32 int32)) (struct (int int)))) (1))) (int32-const 2))) + ( + memory-store + pointer=(local "namespaceValue__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int int)))) + offset=(1 0) + value=(int-const 0) + ) + ( + memory-store + pointer=(local "namespaceValue__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int int)))) + offset=(1 1) + value=(int-const 0) + ) ( memory-store pointer=(data-ptr-const "shed__namespace__An_Example") diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioTupleCreateTests]/[method:canGenerateCodeForTupleCreate(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioTupleCreateTests]/[method:canGenerateCodeForTupleCreate(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index d6376954..f1e57c98 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioTupleCreateTests]/[method:canGenerateCodeForTupleCreate(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioTupleCreateTests]/[method:canGenerateCodeForTupleCreate(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -21,6 +21,27 @@ Instructions: (local-store (local "tupleElement__1__0") (local "c")) (local-store (local "tupleElement__0__0") (local "b")) (call (local "tuplePointer__0") "shed__runtime__gc_alloc" ((int-const (byte-size-of (struct ((struct (data-ptr int int32 int32)) (struct (int int int)))))) (int32-const (byte-offset-of (struct ((struct (data-ptr int int32 int32)) (struct (int int int)))) (1))) (int32-const 3))) + ( + memory-store + pointer=(local "tuplePointer__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int int int)))) + offset=(1 0) + value=(int-const 0) + ) + ( + memory-store + pointer=(local "tuplePointer__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int int int)))) + offset=(1 1) + value=(int-const 0) + ) + ( + memory-store + pointer=(local "tuplePointer__0") + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int int int)))) + offset=(1 2) + value=(int-const 0) + ) ( memory-store pointer=(local "tuplePointer__0") diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarLoadTests]/[method:canGenerateCodeForVarLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarLoadTests]/[method:canGenerateCodeForVarLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 6e93849c..80a5a9d5 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarLoadTests]/[method:canGenerateCodeForVarLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarLoadTests]/[method:canGenerateCodeForVarLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -12,8 +12,8 @@ Instructions: memory-load (local "varValue__x__0") pointer=(local "local__x") - layout=(struct ((struct (data-ptr int int32 int32)) int)) - offset=(1) + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int)))) + offset=(1 0) ) ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarStoreTests]/[method:canGenerateCodeForVarStore(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarStoreTests]/[method:canGenerateCodeForVarStore(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index bd2b4eaf..feea5bf2 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarStoreTests]/[method:canGenerateCodeForVarStore(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarStoreTests]/[method:canGenerateCodeForVarStore(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -10,8 +10,8 @@ Instructions: ( memory-store pointer=(local "local__x") - layout=(struct ((struct (data-ptr int int32 int32)) int)) - offset=(1) + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int)))) + offset=(1 0) value=(local "b") ) ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarStoreTests]/[method:canStoreIntoVarMultipleTimes(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarStoreTests]/[method:canStoreIntoVarMultipleTimes(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 2dd55297..08d01b4d 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarStoreTests]/[method:canStoreIntoVarMultipleTimes(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioVarStoreTests]/[method:canStoreIntoVarMultipleTimes(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -10,15 +10,15 @@ Instructions: ( memory-store pointer=(local "local__x") - layout=(struct ((struct (data-ptr int int32 int32)) int)) - offset=(1) + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int)))) + offset=(1 0) value=(local "b") ) ( memory-store pointer=(local "local__x") - layout=(struct ((struct (data-ptr int int32 int32)) int)) - offset=(1) + layout=(struct ((struct (data-ptr int int32 int32)) (struct (int)))) + offset=(1 0) value=(local "a") ) ) diff --git a/src/main/java/org/shedlang/compiler/patio/types/PatioOffsets.java b/src/main/java/org/shedlang/compiler/patio/types/PatioOffsets.java index f427bd41..5a3be3ff 100644 --- a/src/main/java/org/shedlang/compiler/patio/types/PatioOffsets.java +++ b/src/main/java/org/shedlang/compiler/patio/types/PatioOffsets.java @@ -1,11 +1,24 @@ package org.shedlang.compiler.patio.types; import org.pcollections.PVector; +import org.pcollections.TreePVector; + +import java.util.List; public class PatioOffsets { private PatioOffsets() { } + public static PatioDataLayout offsetDataLayout( + PatioDataLayout patioDataLayout, + List patioOffsets + ) { + return offsetDataLayout( + patioDataLayout, + TreePVector.from(patioOffsets) + ); + } + public static PatioDataLayout offsetDataLayout( PatioDataLayout patioDataLayout, PVector patioOffsets diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java index 351dee49..4bb7c3a3 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java @@ -3,11 +3,10 @@ package org.shedlang.compiler.shidtopatio; import org.shedlang.compiler.patio.PatioFunctionDeclaration; import org.shedlang.compiler.patio.PatioLocal; import org.shedlang.compiler.patio.instructions.PatioCall; -import org.shedlang.compiler.patio.operands.PatioInt32Const; -import org.shedlang.compiler.patio.operands.PatioIntConst; -import org.shedlang.compiler.patio.operands.PatioLocalRef; -import org.shedlang.compiler.patio.operands.PatioOperand; +import org.shedlang.compiler.patio.instructions.PatioMemoryStore; +import org.shedlang.compiler.patio.operands.*; import org.shedlang.compiler.patio.types.*; +import org.shedlang.compiler.patio.values.PatioDataPtrValueNull; import org.shedlang.compiler.patio.values.PatioIntValueByteOffsetOf; import org.shedlang.compiler.patio.values.PatioIntValueByteSizeOf; import org.shedlang.compiler.sources.Source; @@ -154,12 +153,39 @@ public class ShidToPatioGc { var offset = new PatioInt32Const(new PatioIntValueByteOffsetOf(layout, childrenOffset)); - return context.addInstruction(new PatioCall( + context = context.addInstruction(new PatioCall( Optional.of(destination), ALLOC.name(), List.of(size, offset, PatioInt32Const.of(childrenCount)), source )); + + for (var childIndex = 0; childIndex < childrenCount; childIndex++) { + var childOffset = Lists.concat(childrenOffset, List.of(childIndex)); + var childDataLayout = PatioOffsets.offsetDataLayout(layout, childOffset); + + var zero = switch (childDataLayout) { + case PatioDataLayoutDataPtr patioDataLayoutDataPtr -> + new PatioDataPtrConst(new PatioDataPtrValueNull()); + + case PatioDataLayoutInt dataLayoutInt -> + PatioIntConst.of(0); + + default -> + // TODO: better error + throw new IllegalArgumentException("unsupported child"); + }; + + context = context.addInstruction(new PatioMemoryStore( + destination, + layout, + childOffset, + zero, + source + )); + } + + return context; } // == Collection == diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioVars.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioVars.java index 37e30123..34143882 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioVars.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioVars.java @@ -16,9 +16,13 @@ public class ShidToPatioVars { } private static final PatioDataLayout VAR_DATA_LAYOUT = - ShidToPatioGc.gcObjectDataLayout(PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE)); + ShidToPatioGc.gcObjectDataLayout( + PatioDataLayouts.struct(List.of( + PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE) + )) + ); - private static final List VALUE_OFFSET = ShidToPatioGc.gcObjectOffset(List.of()); + private static final List VALUE_OFFSET = ShidToPatioGc.gcObjectOffset(List.of(0)); private static final List GC_CHILDREN_OFFSET = ShidToPatioGc.gcObjectOffset(List.of()); private static final int GC_CHILDREN_COUNT = 1; -- cgit v1.2.3