From 3af76a85328aa0807e47ceb77dfc7f881c82fb61 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 30 Mar 2026 22:33:27 +0100 Subject: Compile (non-void) return --- patio-to-cranelift/src/lib.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/patio-to-cranelift/src/lib.rs b/patio-to-cranelift/src/lib.rs index 12097a9a..fbb0e14f 100644 --- a/patio-to-cranelift/src/lib.rs +++ b/patio-to-cranelift/src/lib.rs @@ -2,7 +2,7 @@ use std::{path::{Path}, str::FromStr}; use cranelift::prelude::InstBuilder; -use crate::{context::{CompilerContext, CompilerFunctionContext}, errors::CompileError, patio_generated::patio::{PatioCall, PatioCompilationUnitElement, PatioCompilationUnitElementValue, PatioFunctionDefinition, PatioInstruction, PatioInstructionValue, PatioIntValue, PatioIntValueValue, PatioLocal, PatioLocalType, PatioOperand, PatioOperandValue, PatioReturnVoid, PatioType, PatioTypeValue, PatioVisibility}}; +use crate::{context::{CompilerContext, CompilerFunctionContext}, errors::CompileError, patio_generated::patio::{PatioCall, PatioCompilationUnitElement, PatioCompilationUnitElementValue, PatioFunctionDefinition, PatioInstruction, PatioInstructionValue, PatioIntValue, PatioIntValueValue, PatioLocal, PatioLocalType, PatioOperand, PatioOperandValue, PatioReturn, PatioReturnVoid, PatioType, PatioTypeValue, PatioVisibility}}; #[allow(dead_code, unused_imports)] #[path = "./gen/patio_generated.rs"] @@ -121,6 +121,11 @@ fn compile_instruction( compile_call(call, context); } + PatioInstructionValue::PatioReturn => { + let return_ = instruction.value_instruction_value_as_patio_return().unwrap(); + compile_return(return_, context); + } + PatioInstructionValue::PatioReturnVoid => { let return_void = instruction.value_instruction_value_as_patio_return_void().unwrap(); compile_return_void(return_void, context); @@ -136,14 +141,21 @@ fn compile_call( call: PatioCall<'_>, context: &mut CompilerFunctionContext<'_>, ) { - let cranelift_args: Vec<_> = call.args().iter() - .map(|arg| compile_operand(arg, context)) - .collect(); + let cranelift_args = compile_operands(call.args().iter(), context); let func_ref = context.func_id(call.function_name()); context.function_builder.ins().call(func_ref, &cranelift_args); } +fn compile_return( + return_: PatioReturn<'_>, + context: &mut CompilerFunctionContext<'_>, +) { + let cranelift_value = compile_operand(return_.value(), context); + + context.function_builder.ins().return_(&[cranelift_value]); +} + fn compile_return_void( _return_void: PatioReturnVoid<'_>, context: &mut CompilerFunctionContext<'_>, @@ -151,6 +163,15 @@ fn compile_return_void( context.function_builder.ins().return_(&[]); } +fn compile_operands<'a>( + operands: impl Iterator>, + context: &mut CompilerFunctionContext<'_>, +) -> Vec { + operands + .map(|operand| compile_operand(operand, context)) + .collect() +} + fn compile_operand( operand: PatioOperand<'_>, context: &mut CompilerFunctionContext<'_>, -- cgit v1.2.3