diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-04-03 10:11:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-04-03 10:11:21 +0100 |
| commit | cccf9edd15ad2148dadd09fa5a2a94edf9dbbf77 (patch) | |
| tree | 4900f9eb56c8036895dc11cec89de0868710cdf3 | |
| parent | fa422910d1bdd99967016b9c1f8985ce2351fb04 (diff) | |
Compile int-sub in patio-to-cranelift
| -rw-r--r-- | patio-to-cranelift/src/lib.rs | 24 | ||||
| -rw-r--r-- | src/test/java/org/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 @@ -366,6 +366,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() .withName(FUNCTION_NAME) |
