From f4a0159dc00fd1caa7a251d7f6bcd23948cf5f7d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 16 Dec 2023 15:50:49 +0000 Subject: Store namespace fields as permanent GC roots --- .../shedlang/compiler/patio/PatioCodeGenGc.java | 29 ++++++++++++++++++++++ .../compiler/patio/PatioCodeGenNamespaces.java | 2 ++ 2 files changed, 31 insertions(+) (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 4e12e531..144cf49b 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java @@ -1,11 +1,25 @@ package org.shedlang.compiler.patio; +import org.shedlang.compiler.patio.ast.PatioFunctionDeclaration; +import org.shedlang.compiler.patio.ast.PatioLocal; +import org.shedlang.compiler.patio.ast.instructions.PatioCall; +import org.shedlang.compiler.patio.ast.operands.PatioOperand; import org.shedlang.compiler.patio.ast.types.*; +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.util.Lists; import java.util.List; +import java.util.Optional; public class PatioCodeGenGc { + private static final PatioFunctionDeclaration ADD_PERM_GC_ROOT = new PatioFunctionDeclaration( + "shed__runtime__gc_add_perm_root", + List.of(PatioLocal.param("gc_root", PatioCodeGenTypes.VALUE_TYPE)), + PatioTypes.VOID, + Sources.RUNTIME + ); + private PatioCodeGenGc() { } @@ -41,4 +55,19 @@ public class PatioCodeGenGc { value )); } + + public static PatioCodeGenContext addPermGcRoot( + PatioOperand gcRoot, + Source source, + PatioCodeGenContext context + ) { + return context + .ensureCompilationUnitElement(ADD_PERM_GC_ROOT) + .addInstruction(new PatioCall( + Optional.empty(), + ADD_PERM_GC_ROOT.name(), + List.of(gcRoot), + source + )); + } } diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java index 8bbf4e8b..280575bc 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java @@ -219,6 +219,8 @@ public class PatioCodeGenNamespaces { source )); + context = PatioCodeGenGc.addPermGcRoot(fieldValue, source, context); + return context; } -- cgit v1.2.3