From 10445d8cbc7f45f8b447ace398eb2ff0089081a0 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 16 Dec 2023 22:03:58 +0000 Subject: Specify initial value for Patio mutable data elements --- .../backends/llvm/codegen/LlvmPatioCodeGen.java | 4 ++-- .../compiler/backends/wasm/WasmPatioCodeGen.java | 2 +- .../compiler/patio/PatioCodeGenContext.java | 6 +++--- .../compiler/patio/PatioCodeGenNamespaces.java | 25 ++++++++++------------ .../patio/ast/PatioDataElementMutable.java | 4 ++-- .../compiler/patio/ast/PatioSerializer.java | 2 +- 6 files changed, 20 insertions(+), 23 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/codegen/LlvmPatioCodeGen.java b/src/main/java/org/shedlang/compiler/backends/llvm/codegen/LlvmPatioCodeGen.java index 7fdad03e..7e98d814 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/codegen/LlvmPatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/codegen/LlvmPatioCodeGen.java @@ -168,8 +168,8 @@ public class LlvmPatioCodeGen { ) { return context.addTopLevelEntity(new LlvmGlobalDefinition( patioDataElementMutable.name(), - patioDataLayoutToLlvmType(patioDataElementMutable.layout()), - Optional.of(patioDataLayoutToZeroedLlvmOperand(patioDataElementMutable.layout())), + patioDataLayoutToLlvmType(patioDataElementMutable.initialValue().layout()), + Optional.of(patioDataValueToLlvmOperand(patioDataElementMutable.initialValue())), false, true, true diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java index a0d69f70..17b26efa 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java @@ -65,7 +65,7 @@ public class WasmPatioCodeGen { case PatioDataElementMutable patioDataElementMutable -> { wasmMemoryBuilder = wasmMemoryBuilder.add( patioDataElementMutable.name(), - new byte[sizeOf(patioDataElementMutable.layout())], + patioDataValueToBytes(patioDataElementMutable.initialValue()), WasmAlignment.ALIGN_32_BIT ).memory(); } diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java index c123b34f..6083cf75 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java @@ -10,7 +10,7 @@ import org.shedlang.compiler.patio.ast.instructions.PatioInstruction; import org.shedlang.compiler.patio.ast.instructions.PatioLocalStore; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; import org.shedlang.compiler.patio.ast.operands.PatioOperand; -import org.shedlang.compiler.patio.ast.types.PatioDataLayout; +import org.shedlang.compiler.patio.ast.types.PatioDataValue; import org.shedlang.compiler.patio.ast.types.PatioType; import org.shedlang.compiler.patio.errors.DuplicateCompilationUnitElementName; import org.shedlang.compiler.sources.Source; @@ -462,8 +462,8 @@ public record PatioCodeGenContext( } @CheckReturnValue - public PatioCodeGenContext addDataElementMutable(String name, PatioDataLayout layout, Source source) { - var dataElement = new PatioDataElementMutable(name, layout, source); + public PatioCodeGenContext addDataElementMutable(String name, PatioDataValue initialValue, Source source) { + var dataElement = new PatioDataElementMutable(name, initialValue, source); return addCompilationUnitElement(dataElement); } diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java index 280575bc..595033b1 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java @@ -7,10 +7,7 @@ import org.shedlang.compiler.patio.ast.operands.PatioBool; import org.shedlang.compiler.patio.ast.operands.PatioDataPtr; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; import org.shedlang.compiler.patio.ast.operands.PatioOperand; -import org.shedlang.compiler.patio.ast.types.PatioDataLayout; -import org.shedlang.compiler.patio.ast.types.PatioDataLayouts; -import org.shedlang.compiler.patio.ast.types.PatioType; -import org.shedlang.compiler.patio.ast.types.PatioTypes; +import org.shedlang.compiler.patio.ast.types.*; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.types.NamespaceName; @@ -35,17 +32,17 @@ public class PatioCodeGenNamespaces { var source = Sources.NULL; var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); - var layout = namespaceDataLayout(namespaceTypeInfo); + var initialValue = namespaceDataInitialValue(namespaceTypeInfo); context = context.addDataElementMutable( globalNameNamespaceValue(namespaceName), - layout, + initialValue, source ); context = context.addDataElementMutable( globalNameNamespaceIsInited(namespaceName), - PatioDataLayouts.BOOL, + PatioDataValues.bool(false), source ); @@ -174,7 +171,7 @@ public class PatioCodeGenNamespaces { PatioCodeGenContext context ) { var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); - var layout = namespaceDataLayout(namespaceTypeInfo); + var initialValue = namespaceDataInitialValue(namespaceTypeInfo); var namespacePointerResult = context.allocateVal( PatioCodeGenLocalSpace.NAMESPACE_POINTER, @@ -193,7 +190,7 @@ public class PatioCodeGenNamespaces { context = context.addInstruction(new PatioMemoryLoad( target, namespacePointer, - layout, + initialValue.layout(), List.of(namespaceTypeInfo.fieldIndex(fieldName)), source )); @@ -209,11 +206,11 @@ public class PatioCodeGenNamespaces { PatioCodeGenContext context ) { var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); - var layout = namespaceDataLayout(namespaceTypeInfo); + var initialValue = namespaceDataInitialValue(namespaceTypeInfo); context = context.addInstruction(new PatioMemoryStore( new PatioDataPtr(globalNameNamespaceValue(namespaceName)), - layout, + initialValue.layout(), List.of(namespaceTypeInfo.fieldIndex(fieldName)), fieldValue, source @@ -224,10 +221,10 @@ public class PatioCodeGenNamespaces { return context; } - private static PatioDataLayout namespaceDataLayout(NamespaceTypeInfo namespaceTypeInfo) { - return PatioDataLayouts.struct( + private static PatioDataValue namespaceDataInitialValue(NamespaceTypeInfo namespaceTypeInfo) { + return PatioDataValues.struct( Lists.repeat( - PatioDataLayouts.INT, + PatioDataValues.int_(0), namespaceTypeInfo.fieldCount() ) ); diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioDataElementMutable.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioDataElementMutable.java index ad38bbc8..568a86e9 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/PatioDataElementMutable.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioDataElementMutable.java @@ -1,11 +1,11 @@ package org.shedlang.compiler.patio.ast; -import org.shedlang.compiler.patio.ast.types.PatioDataLayout; +import org.shedlang.compiler.patio.ast.types.PatioDataValue; import org.shedlang.compiler.sources.Source; public record PatioDataElementMutable( String name, - PatioDataLayout layout, + PatioDataValue initialValue, Source source ) implements PatioCompilationUnitElement { } diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java index 05703e24..cc2903c5 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java @@ -92,7 +92,7 @@ public class PatioSerializer { treeList( treeIdentifier("data-mutable"), treeString(dataElementMutable.name()), - dataLayoutToTree(dataElementMutable.layout()) + dataValueToTree(dataElementMutable.initialValue()) ); case PatioExceptionTypeDefinition exceptionTypeDefinition -> -- cgit v1.2.3