aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-04-03 15:46:29 +0100
committerMichael Williamson <mike@zwobble.org>2026-04-03 15:46:44 +0100
commitff01a1a3cde4d9ea286158d6a3d8b64c44e7fd08 (patch)
tree8fa77a3e068f692a8e5fba71ef158390fe5acb2d
parentd6bdd375fc4d893c01cb771dc63db65a26fc84a7 (diff)
Add PatioDataLayout as proper Rust type
-rw-r--r--patio-to-cranelift/src/lib.rs55
-rw-r--r--patio-to-cranelift/src/patio/flatbuffers.rs51
-rw-r--r--patio-to-cranelift/src/patio/mod.rs2
-rw-r--r--patio-to-cranelift/src/patio/types.rs15
4 files changed, 96 insertions, 27 deletions
diff --git a/patio-to-cranelift/src/lib.rs b/patio-to-cranelift/src/lib.rs
index 87054489..7cf5f9e4 100644
--- a/patio-to-cranelift/src/lib.rs
+++ b/patio-to-cranelift/src/lib.rs
@@ -5,14 +5,14 @@ use cranelift::prelude::InstBuilder;
use crate::{
context::{CompilerContext, CompilerFunctionContext},
errors::CompileError,
+ patio::types::PatioDataLayout,
patio_generated::patio::{
PatioBlock, PatioBoolToInt, PatioCall, PatioCompilationUnitElement,
- PatioCompilationUnitElementValue, PatioDataLayout, PatioDataLayoutValue,
- PatioFunctionDefinition, PatioIf, PatioInstruction, PatioInstructionValue, PatioIntAdd,
- PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntValue,
- PatioIntValueValue, PatioLocal, PatioLocalRef, PatioLocalStore, PatioLocalType,
- PatioOperand, PatioOperandValue, PatioReturn, PatioReturnVoid, PatioType, PatioTypeValue,
- PatioVisibility,
+ PatioCompilationUnitElementValue, PatioFunctionDefinition, PatioIf, PatioInstruction,
+ PatioInstructionValue, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub,
+ PatioIntToBool, PatioIntValue, PatioIntValueValue, PatioLocal, PatioLocalRef,
+ PatioLocalStore, PatioLocalType, PatioOperand, PatioOperandValue, PatioReturn,
+ PatioReturnVoid, PatioType, PatioTypeValue, PatioVisibility,
},
};
@@ -26,6 +26,7 @@ mod sources_generated;
mod context;
mod errors;
+mod patio;
pub fn compile(source_path: &Path, target_str: &str) -> Result<(), CompileError> {
let source_bytes = std::fs::read(source_path).map_err(CompileError::ReadSourceBytes)?;
@@ -488,7 +489,8 @@ fn compile_int_value(
PatioIntValueValue::PatioIntValueByteSizeOf => {
let byte_size_of = int_value.value_as_patio_int_value_byte_size_of().unwrap();
- let byte_size_of = size_of_data_layout(byte_size_of.layout(), &context.target_triple);
+ let byte_size_of =
+ size_of_data_layout(&byte_size_of.layout().into(), &context.target_triple);
context
.function_builder
@@ -609,32 +611,31 @@ fn cranelift_int_type(target_triple: &target_lexicon::Triple) -> cranelift_codeg
const CRANELIFT_INT_32_TYPE: cranelift_codegen::ir::Type = cranelift_codegen::ir::types::I32;
-fn size_of_data_layout(layout: PatioDataLayout<'_>, target_triple: &target_lexicon::Triple) -> u32 {
- match layout.value_type() {
- PatioDataLayoutValue::PatioDataLayoutArray => {
- let array = layout.value_as_patio_data_layout_array().unwrap();
- size_of_data_layout(array.element(), target_triple) * array.size()
+fn size_of_data_layout(layout: &PatioDataLayout, target_triple: &target_lexicon::Triple) -> u32 {
+ match layout {
+ PatioDataLayout::Array { element, size } => {
+ size_of_data_layout(element, target_triple) * size
}
- PatioDataLayoutValue::PatioDataLayoutBool => CRANELIFT_BOOL_TYPE.bytes(),
- PatioDataLayoutValue::PatioDataLayoutByte => CRANELIFT_BYTE_TYPE.bytes(),
- PatioDataLayoutValue::PatioDataLayoutDataPtr => {
+ PatioDataLayout::Bool => CRANELIFT_BOOL_TYPE.bytes(),
+
+ PatioDataLayout::Byte => CRANELIFT_BYTE_TYPE.bytes(),
+
+ PatioDataLayout::DataPtr => {
cranelift::prelude::Type::triple_pointer_type(&target_triple).bytes()
}
- PatioDataLayoutValue::PatioDataLayoutFunctionPtr => {
+
+ PatioDataLayout::FunctionPtr => {
cranelift::prelude::Type::triple_pointer_type(&target_triple).bytes()
}
- PatioDataLayoutValue::PatioDataLayoutInt => cranelift_int_type(target_triple).bytes(),
- PatioDataLayoutValue::PatioDataLayoutInt32 => CRANELIFT_INT_32_TYPE.bytes(),
- PatioDataLayoutValue::PatioDataLayoutStruct => {
- let struct_ = layout.value_as_patio_data_layout_struct().unwrap();
- struct_
- .elements()
- .iter()
- .map(|element| size_of_data_layout(element, target_triple))
- .sum()
- }
- layout_type => panic!("Unsupported data layout: {}", layout_type.0),
+ PatioDataLayout::Int => cranelift_int_type(target_triple).bytes(),
+
+ PatioDataLayout::Int32 => CRANELIFT_INT_32_TYPE.bytes(),
+
+ PatioDataLayout::Struct { elements } => elements
+ .iter()
+ .map(|element| size_of_data_layout(element, target_triple))
+ .sum(),
}
}
diff --git a/patio-to-cranelift/src/patio/flatbuffers.rs b/patio-to-cranelift/src/patio/flatbuffers.rs
new file mode 100644
index 00000000..7741ad93
--- /dev/null
+++ b/patio-to-cranelift/src/patio/flatbuffers.rs
@@ -0,0 +1,51 @@
+use crate::{
+ patio::types::PatioDataLayout,
+ patio_generated::{self, patio::PatioDataLayoutValue},
+};
+
+impl From<patio_generated::patio::PatioDataLayout<'_>> for PatioDataLayout {
+ fn from(value: patio_generated::patio::PatioDataLayout<'_>) -> Self {
+ match value.value_type() {
+ PatioDataLayoutValue::PatioDataLayoutArray => {
+ let array = value.value_as_patio_data_layout_array().unwrap();
+
+ PatioDataLayout::Array {
+ element: Box::new(array.element().into()),
+ size: array.size(),
+ }
+ }
+
+ PatioDataLayoutValue::PatioDataLayoutBool => PatioDataLayout::Bool,
+
+ PatioDataLayoutValue::PatioDataLayoutByte => PatioDataLayout::Byte,
+
+ PatioDataLayoutValue::PatioDataLayoutDataPtr => PatioDataLayout::DataPtr,
+
+ PatioDataLayoutValue::PatioDataLayoutFunctionPtr => PatioDataLayout::FunctionPtr,
+
+ PatioDataLayoutValue::PatioDataLayoutInt => PatioDataLayout::Int,
+
+ PatioDataLayoutValue::PatioDataLayoutInt32 => PatioDataLayout::Int32,
+
+ PatioDataLayoutValue::PatioDataLayoutStruct => {
+ let struct_ = value.value_as_patio_data_layout_struct().unwrap();
+
+ PatioDataLayout::Struct {
+ elements: struct_
+ .elements()
+ .iter()
+ .map(|element| element.into())
+ .collect(),
+ }
+ }
+
+ layout_type => {
+ todo!(
+ "Unhandled data layout type: {:?} {}",
+ layout_type.variant_name(),
+ layout_type.0
+ )
+ }
+ }
+ }
+}
diff --git a/patio-to-cranelift/src/patio/mod.rs b/patio-to-cranelift/src/patio/mod.rs
new file mode 100644
index 00000000..045f33ee
--- /dev/null
+++ b/patio-to-cranelift/src/patio/mod.rs
@@ -0,0 +1,2 @@
+pub(crate) mod flatbuffers;
+pub(crate) mod types;
diff --git a/patio-to-cranelift/src/patio/types.rs b/patio-to-cranelift/src/patio/types.rs
new file mode 100644
index 00000000..a862742e
--- /dev/null
+++ b/patio-to-cranelift/src/patio/types.rs
@@ -0,0 +1,15 @@
+pub(crate) enum PatioDataLayout {
+ Array {
+ element: Box<PatioDataLayout>,
+ size: u32,
+ },
+ Bool,
+ Byte,
+ DataPtr,
+ FunctionPtr,
+ Int,
+ Int32,
+ Struct {
+ elements: Vec<PatioDataLayout>,
+ },
+}