From 10d5eca372ed85d9beab8f91e0cadcd6f8e24fac Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 5 Apr 2026 21:37:17 +0100 Subject: Support null data pointer --- patio-to-cranelift/src/lib.rs | 6 ++++-- .../shedlang/compiler/patio/PatioExampleTests.java | 21 +++++++++++++++++++++ .../shedlang/compiler/patio/instructions/Patio.java | 4 ++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/patio-to-cranelift/src/lib.rs b/patio-to-cranelift/src/lib.rs index 74dd4993..85e11f7b 100644 --- a/patio-to-cranelift/src/lib.rs +++ b/patio-to-cranelift/src/lib.rs @@ -568,16 +568,18 @@ fn compile_data_ptr_value( data_ptr_value: &PatioDataPtrValue, context: &mut CompilerFunctionContext<'_>, ) -> cranelift_codegen::ir::Value { + let data_ptr_type = context.pointer_type(); + match data_ptr_value { PatioDataPtrValue::DataElement { name } => { let global_value = context.data_ptr(name); - let data_ptr_type = context.pointer_type(); context .function_builder .ins() .global_value(data_ptr_type, global_value) } - PatioDataPtrValue::Null => todo!(), + + PatioDataPtrValue::Null => context.function_builder.ins().iconst(data_ptr_type, 0), } } diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java index 9ea6aef2..0cc1574d 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -142,6 +142,27 @@ public abstract class PatioExampleTests { )); } + @Test + public void dataPtrCanBeNull() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .addLocal(PatioLocal.val("x", PatioTypes.DATA_PTR)) + .addBodyInstruction(Patio.localStore( + Patio.localRef("x"), + Patio.dataPtrConst(Patio.dataPtrValueNull()) + )) + .withReturnType(PatioTypes.VOID) + .addBodyInstruction(Patio.returnVoid()) + .build(); + var patio = Patio.compilationUnit(List.of( + patioFunction + )); + + var result = run(patio); + + assertThat(result, isSequence()); + } + @Test public void blockSingleResult() throws IOException, InterruptedException { var patioFunction = PatioFunctionDefinition.builder() diff --git a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java index f8f8dbf6..d6d236c6 100644 --- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java +++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java @@ -119,6 +119,10 @@ public class Patio { return PatioDataPtrConst.dataElement(name); } + public static PatioOperand dataPtrConst(PatioDataPtrValue value) { + return new PatioDataPtrConst(value); + } + public static PatioInstruction dataPtrToInt(PatioLocalRef destination, PatioOperand value) { return new PatioDataPtrToInt( destination, -- cgit v1.2.3