aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-03-30 22:33:27 +0100
committerMichael Williamson <mike@zwobble.org>2026-03-30 22:33:27 +0100
commit3af76a85328aa0807e47ceb77dfc7f881c82fb61 (patch)
treecb4c5a4b4e3a9b9eadf9856476f31c73ddba64bf
parentfa83dd5c628a93fc3129f5c6f21b617fdd3fffc8 (diff)
Compile (non-void) return
-rw-r--r--patio-to-cranelift/src/lib.rs29
1 files 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<Item = PatioOperand<'a>>,
+ context: &mut CompilerFunctionContext<'_>,
+) -> Vec<cranelift::prelude::Value> {
+ operands
+ .map(|operand| compile_operand(operand, context))
+ .collect()
+}
+
fn compile_operand(
operand: PatioOperand<'_>,
context: &mut CompilerFunctionContext<'_>,