aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-04-04 09:11:16 +0100
committerMichael Williamson <mike@zwobble.org>2026-04-04 09:11:26 +0100
commit3736b58c8065bc96cd306d5eeb7ca3a5397fc518 (patch)
tree2dd581a9f19e79a0ce5359996bc181febc4b00e2
parent3f14376778a3ddab715e56e58fa5d9d0a39cd21b (diff)
Add rust-native PatioLocal
-rw-r--r--patio-to-cranelift/src/lib.rs43
-rw-r--r--patio-to-cranelift/src/patio/compilation_unit.rs14
-rw-r--r--patio-to-cranelift/src/patio/flatbuffers.rs30
-rw-r--r--patio-to-cranelift/src/patio/mod.rs1
4 files changed, 63 insertions, 25 deletions
diff --git a/patio-to-cranelift/src/lib.rs b/patio-to-cranelift/src/lib.rs
index 128fd459..dfe30c9c 100644
--- a/patio-to-cranelift/src/lib.rs
+++ b/patio-to-cranelift/src/lib.rs
@@ -5,14 +5,17 @@ use cranelift::prelude::InstBuilder;
use crate::{
context::{CompilerContext, CompilerFunctionContext},
errors::CompileError,
- patio::types::{PatioDataLayout, PatioType},
+ patio::{
+ compilation_unit::{PatioLocal, PatioLocalType},
+ types::{PatioDataLayout, PatioType},
+ },
patio_generated::patio::{
PatioBlock, PatioBoolToInt, PatioCall, PatioCompilationUnitElement,
PatioCompilationUnitElementValue, PatioFunctionDefinition, PatioIf, PatioInstruction,
PatioInstructionValue, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub,
- PatioIntToBool, PatioIntValue, PatioIntValueValue, PatioLocal, PatioLocalRef,
- PatioLocalStore, PatioLocalType, PatioOperand, PatioOperandValue, PatioResume, PatioReturn,
- PatioReturnVoid, PatioVarLoad, PatioVarStore, PatioVisibility,
+ PatioIntToBool, PatioIntValue, PatioIntValueValue, PatioLocalRef, PatioLocalStore,
+ PatioOperand, PatioOperandValue, PatioResume, PatioReturn, PatioReturnVoid, PatioVarLoad,
+ PatioVarStore, PatioVisibility,
},
};
@@ -107,7 +110,7 @@ fn declare_function_declaration(
context: &mut CompilerContext,
) {
let cranelift_signature = cranelift_signature(
- function_declaration.params().iter(),
+ function_declaration.params().iter().map(Into::into),
function_declaration.return_type().into(),
&context.target_triple,
);
@@ -124,7 +127,7 @@ fn declare_function_definition(
context: &mut CompilerContext,
) {
let cranelift_signature = cranelift_signature(
- function_definition.locals().iter(),
+ function_definition.locals().iter().map(Into::into),
function_definition.return_type().into(),
&context.target_triple,
);
@@ -143,7 +146,7 @@ fn define_function_definition(
context: &mut CompilerContext,
) {
let cranelift_signature = cranelift_signature(
- function_definition.locals().iter(),
+ function_definition.locals().iter().map(Into::into),
function_definition.return_type().into(),
&context.target_triple,
);
@@ -152,32 +155,24 @@ fn define_function_definition(
function_definition.name(),
cranelift_signature,
|function_context| {
- for local in function_definition.locals() {
- match local.local_type() {
+ for local in function_definition.locals().iter().map(PatioLocal::from) {
+ match local.local_type {
PatioLocalType::PARAM => {}
PatioLocalType::VAL => {
let cranelift_type = patio_type_to_cranelift_type(
- local.type_().into(),
+ local.type_,
&function_context.target_triple,
);
- function_context.declare_var(local.name(), cranelift_type);
+ function_context.declare_var(&local.name, cranelift_type);
}
PatioLocalType::VAR => {
let cranelift_type = patio_type_to_cranelift_type(
- local.type_().into(),
+ local.type_,
&function_context.target_triple,
);
- function_context.create_stack_slot(local.name(), cranelift_type);
- }
-
- local_type => {
- todo!(
- "Unhandled local type: {:?} {}",
- local_type.variant_name(),
- local_type.0
- )
+ function_context.create_stack_slot(&local.name, cranelift_type);
}
}
}
@@ -603,7 +598,7 @@ fn def_destination(
}
fn cranelift_signature<'a>(
- locals: impl Iterator<Item = PatioLocal<'a>>,
+ locals: impl Iterator<Item = PatioLocal>,
return_type: PatioType,
target_triple: &target_lexicon::Triple,
) -> cranelift::prelude::Signature {
@@ -611,8 +606,8 @@ fn cranelift_signature<'a>(
cranelift::prelude::Signature::new(cranelift_codegen::isa::CallConv::SystemV);
for local in locals {
- if local.local_type() == PatioLocalType::PARAM {
- let param_type = patio_type_to_cranelift_type(local.type_().into(), &target_triple);
+ if local.local_type == PatioLocalType::PARAM {
+ let param_type = patio_type_to_cranelift_type(local.type_, &target_triple);
let param = cranelift::prelude::AbiParam::new(param_type);
signature.params.push(param);
}
diff --git a/patio-to-cranelift/src/patio/compilation_unit.rs b/patio-to-cranelift/src/patio/compilation_unit.rs
new file mode 100644
index 00000000..cc46a0ca
--- /dev/null
+++ b/patio-to-cranelift/src/patio/compilation_unit.rs
@@ -0,0 +1,14 @@
+use crate::patio::types::PatioType;
+
+pub(crate) struct PatioLocal {
+ pub(crate) name: String,
+ pub(crate) type_: PatioType,
+ pub(crate) local_type: PatioLocalType,
+}
+
+#[derive(Eq, PartialEq)]
+pub(crate) enum PatioLocalType {
+ PARAM,
+ VAL,
+ VAR,
+}
diff --git a/patio-to-cranelift/src/patio/flatbuffers.rs b/patio-to-cranelift/src/patio/flatbuffers.rs
index 519d7560..cb99ec71 100644
--- a/patio-to-cranelift/src/patio/flatbuffers.rs
+++ b/patio-to-cranelift/src/patio/flatbuffers.rs
@@ -1,5 +1,8 @@
use crate::{
- patio::types::{PatioDataLayout, PatioType},
+ patio::{
+ compilation_unit::{PatioLocal, PatioLocalType},
+ types::{PatioDataLayout, PatioType},
+ },
patio_generated::{
self,
patio::{PatioDataLayoutValue, PatioTypeValue},
@@ -70,3 +73,28 @@ impl From<patio_generated::patio::PatioDataLayout<'_>> for PatioDataLayout {
}
}
}
+
+impl From<patio_generated::patio::PatioLocal<'_>> for PatioLocal {
+ fn from(value: patio_generated::patio::PatioLocal<'_>) -> Self {
+ Self {
+ name: value.name().to_string(),
+ type_: value.type_().into(),
+ local_type: value.local_type().into(),
+ }
+ }
+}
+
+impl From<patio_generated::patio::PatioLocalType> for PatioLocalType {
+ fn from(value: patio_generated::patio::PatioLocalType) -> Self {
+ match value {
+ patio_generated::patio::PatioLocalType::PARAM => Self::PARAM,
+ patio_generated::patio::PatioLocalType::VAL => Self::VAL,
+ patio_generated::patio::PatioLocalType::VAR => Self::VAR,
+ local_type_type => todo!(
+ "Unhandled local type type: {:?} {}",
+ local_type_type.variant_name(),
+ local_type_type.0
+ ),
+ }
+ }
+}
diff --git a/patio-to-cranelift/src/patio/mod.rs b/patio-to-cranelift/src/patio/mod.rs
index 045f33ee..2975a23f 100644
--- a/patio-to-cranelift/src/patio/mod.rs
+++ b/patio-to-cranelift/src/patio/mod.rs
@@ -1,2 +1,3 @@
+pub(crate) mod compilation_unit;
pub(crate) mod flatbuffers;
pub(crate) mod types;