From cccf9edd15ad2148dadd09fa5a2a94edf9dbbf77 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 3 Apr 2026 10:11:21 +0100 Subject: Compile int-sub in patio-to-cranelift --- patio-to-cranelift/src/lib.rs | 24 ++++++++++++++-- .../shedlang/compiler/patio/PatioExampleTests.java | 33 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/patio-to-cranelift/src/lib.rs b/patio-to-cranelift/src/lib.rs index bef7e426..d9ff6c5d 100644 --- a/patio-to-cranelift/src/lib.rs +++ b/patio-to-cranelift/src/lib.rs @@ -8,9 +8,9 @@ use crate::{ patio_generated::patio::{ PatioBlock, PatioBoolToInt, PatioCall, PatioCompilationUnitElement, PatioCompilationUnitElementValue, PatioFunctionDefinition, PatioIf, PatioInstruction, - PatioInstructionValue, PatioIntAdd, PatioIntToBool, PatioIntValue, PatioIntValueValue, - PatioLocal, PatioLocalRef, PatioLocalStore, PatioLocalType, PatioOperand, - PatioOperandValue, PatioReturn, PatioReturnVoid, PatioType, PatioTypeValue, + PatioInstructionValue, PatioIntAdd, PatioIntSub, PatioIntToBool, PatioIntValue, + PatioIntValueValue, PatioLocal, PatioLocalRef, PatioLocalStore, PatioLocalType, + PatioOperand, PatioOperandValue, PatioReturn, PatioReturnVoid, PatioType, PatioTypeValue, PatioVisibility, }, }; @@ -221,6 +221,13 @@ fn compile_instruction( compile_int_add(int_add, context); } + PatioInstructionValue::PatioIntSub => { + let int_sub = instruction + .value_instruction_value_as_patio_int_sub() + .unwrap(); + compile_int_sub(int_sub, context); + } + PatioInstructionValue::PatioIntToBool => { let int_to_bool = instruction .value_instruction_value_as_patio_int_to_bool() @@ -362,6 +369,17 @@ fn compile_int_add(int_add: PatioIntAdd<'_>, context: &mut CompilerFunctionConte def_destination(int_add.destination(), cranelift_result, context); } +fn compile_int_sub(int_sub: PatioIntSub<'_>, context: &mut CompilerFunctionContext<'_>) { + let cranelift_left = compile_operand(int_sub.left(), context); + let cranelift_right = compile_operand(int_sub.right(), context); + let cranelift_result = context + .function_builder + .ins() + .isub(cranelift_left, cranelift_right); + + def_destination(int_sub.destination(), cranelift_result, context); +} + fn compile_int_to_bool(int_to_bool: PatioIntToBool<'_>, context: &mut CompilerFunctionContext<'_>) { let cranelift_value = compile_operand(int_to_bool.value(), context); diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java index 08f4a50f..25219c9e 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -365,6 +365,39 @@ public abstract class PatioExampleTests { )); } + @Test + public void intSub() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .withReturnType(PatioTypes.VOID) + .addLocal(Patio.val("x", PatioTypes.INT)) + .addLocal(Patio.val("y", PatioTypes.INT)) + .addLocal(Patio.val("z", PatioTypes.INT)) + .addBodyInstruction(Patio.localStore( + Patio.localRef("x"), + Patio.intConst(47) + )) + .addBodyInstruction(Patio.localStore( + Patio.localRef("y"), + Patio.intConst(5) + )) + .addBodyInstruction(Patio.intSub( + Patio.localRef("z"), + Patio.localRef("x"), + Patio.localRef("y") + )) + .addBodyInstruction(emitInt(Patio.localRef("z"))) + .addBodyInstruction(Patio.returnVoid()) + .build(); + var patio = Patio.compilationUnit(List.of(patioFunction)); + + var result = run(patio); + + assertThat(result, isSequence( + isInt(42) + )); + } + @Test public void switchWithUnreachableDefault() throws IOException, InterruptedException { var patioFunction = PatioFunctionDefinition.builder() -- cgit v1.2.3