From 4efca2b4db92e78001c21d40734ce05f2977ef0b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 21 Jul 2023 10:34:26 +0100 Subject: Generate import namespace function --- ...mpiler.testing.snapshots.Snapshotter)].expected | 28 +++++++++ .../compiler/patio/PatioCodeGenNamespaces.java | 71 +++++++++++++++++++++- .../patio/ast/instructions/PatioCallPointer.java | 3 +- .../patio/ast/instructions/PatioDataPtrToInt.java | 3 +- .../patio/ast/instructions/PatioMemoryLoad.java | 3 +- 5 files changed, 104 insertions(+), 4 deletions(-) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenNamespaceTests]/[method:canDefineNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenNamespaceTests]/[method:canDefineNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index f97627a2..bd253ab8 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenNamespaceTests]/[method:canDefineNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenNamespaceTests]/[method:canDefineNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -39,4 +39,32 @@ ) ) ) + ( + function + "shed__namespaceImport__An_Example" + is-private=false + params=() + locals=(("tmp__0" bool) ("tmp__1" bool) ("tmp__2" int)) + return-type=int + body=( + ( + memory-load + (local "tmp__0") + pointer=(data-ptr "shed__namespaceIsInited__An_Example") + layout=bool + offset=() + ) + ( + if + (local "tmp__0") + ( + ) + ( + (call-pointer (local "tmp__1") (function-ptr "shed__namespaceInit__An_Example") ()) + ) + ) + (data-ptr-to-int (local "tmp__2") (data-ptr "shed__namespace__An_Example")) + (return (local "tmp__2")) + ) + ) ) \ No newline at end of file diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java index 6c2fa177..bfa23ee8 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java @@ -1,8 +1,9 @@ package org.shedlang.compiler.patio; -import org.shedlang.compiler.patio.ast.instructions.PatioMemoryStore; +import org.shedlang.compiler.patio.ast.instructions.*; import org.shedlang.compiler.patio.ast.operands.PatioBool; import org.shedlang.compiler.patio.ast.operands.PatioDataPtr; +import org.shedlang.compiler.patio.ast.operands.PatioFunctionPtr; import org.shedlang.compiler.patio.ast.types.PatioDataLayout; import org.shedlang.compiler.patio.ast.types.PatioTypes; import org.shedlang.compiler.sources.Sources; @@ -59,6 +60,74 @@ public class PatioCodeGenNamespaces { } ); + context = context.createFunction( + globalNameNamespaceImport(namespaceName), + false, + PatioCodeGenTypes.VALUE_TYPE, + source, + importContext -> { + var isInitedResult = importContext.allocateLocal( + PatioCodeGenLocalSpace.TEMPORARY, + PatioTypes.BOOL + ); + var isInited = isInitedResult.value(); + importContext = isInitedResult.context(); + + importContext = importContext.addInstruction(new PatioMemoryLoad( + isInited, + new PatioDataPtr(globalNameNamespaceIsInited(namespaceName)), + PatioTypes.scalar(PatioTypes.BOOL), + List.of(), + source + )); + + var initReturnResult = importContext.allocateLocal( + PatioCodeGenLocalSpace.TEMPORARY, + PatioTypes.BOOL + ); + var initReturn = initReturnResult.value(); + importContext = initReturnResult.context(); + + importContext = importContext.addInstruction(new PatioIf( + isInited, + List.of(), + List.of( + // TODO: use direct call + // TODO: don't require target + new PatioCallPointer( + initReturn, + new PatioFunctionPtr(globalNameNamespaceInit(namespaceName)), + List.of(), + source + ) + ), + source + )); + + var namespaceValueResult = importContext.allocateLocal( + PatioCodeGenLocalSpace.TEMPORARY, + PatioCodeGenTypes.VALUE_TYPE + ); + var namespaceValue = namespaceValueResult.value(); + importContext = namespaceValueResult.context(); + + importContext = importContext.addInstruction(new PatioDataPtrToInt( + namespaceValue, + new PatioDataPtr( + globalNameNamespaceValue(namespaceName) + ), + source + )); + + importContext = importContext.addInstruction(new PatioReturn( + namespaceValue, + source + )); + + return importContext; + } + ); + return context; } diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCallPointer.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCallPointer.java index 5373a95d..215f71f9 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCallPointer.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCallPointer.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.patio.ast.instructions; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; +import org.shedlang.compiler.patio.ast.operands.PatioOperand; import org.shedlang.compiler.sources.Source; import java.util.List; @@ -11,7 +12,7 @@ import java.util.List; */ public record PatioCallPointer( PatioLocalRef target, - PatioLocalRef functionPointer, + PatioOperand functionPointer, List args, Source source ) implements PatioInstruction { diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java index 42372cbb..481cdcd0 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java @@ -1,11 +1,12 @@ package org.shedlang.compiler.patio.ast.instructions; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; +import org.shedlang.compiler.patio.ast.operands.PatioOperand; import org.shedlang.compiler.sources.Source; public record PatioDataPtrToInt( PatioLocalRef target, - PatioLocalRef value, + PatioOperand value, Source source ) implements PatioInstruction { } diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java index 16037653..977bf6c9 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.patio.ast.instructions; 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.sources.Source; @@ -12,7 +13,7 @@ import java.util.List; */ public record PatioMemoryLoad( PatioLocalRef target, - PatioLocalRef pointer, + PatioOperand pointer, PatioDataLayout layout, List offset, Source source -- cgit v1.2.3