aboutsummaryrefslogtreecommitdiff
path: root/patio-to-cranelift/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-31 14:45:04 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-31 15:22:40 +0100
commit91860485774924e82da9b0774179d4c3c8da810a (patch)
treee06f52f147502e80aea139c20adac706479cb7ea /patio-to-cranelift/src
parent410fff7b7ecf6f36a0a46ab1d90553169e3728f1 (diff)
Use transient-0 instead of flatbuffersmain
Diffstat (limited to 'patio-to-cranelift/src')
-rw-r--r--patio-to-cranelift/src/codegen/mod.rs159
-rw-r--r--patio-to-cranelift/src/codegen/types.rs321
-rw-r--r--patio-to-cranelift/src/codegen/values.rs157
-rw-r--r--patio-to-cranelift/src/errors.rs4
-rw-r--r--patio-to-cranelift/src/lib.rs71
-rw-r--r--patio-to-cranelift/src/patio/compilation_unit.rs67
-rw-r--r--patio-to-cranelift/src/patio/flatbuffers.rs619
-rw-r--r--patio-to-cranelift/src/patio/instructions.rs153
-rw-r--r--patio-to-cranelift/src/patio/mod.rs73
-rw-r--r--patio-to-cranelift/src/patio/operands.rs17
-rw-r--r--patio-to-cranelift/src/patio/types.rs39
-rw-r--r--patio-to-cranelift/src/patio/values.rs55
12 files changed, 525 insertions, 1210 deletions
diff --git a/patio-to-cranelift/src/codegen/mod.rs b/patio-to-cranelift/src/codegen/mod.rs
index be98a0a3..a9cfab5b 100644
--- a/patio-to-cranelift/src/codegen/mod.rs
+++ b/patio-to-cranelift/src/codegen/mod.rs
@@ -9,12 +9,10 @@ use crate::{
},
errors::CompileError,
patio::{
- compilation_unit::{
- PatioCompilationUnit, PatioCompilationUnitElement, PatioDataElementImmutableDefinition,
- PatioDataElementMutableDeclaration, PatioDataElementMutableDefinition,
- PatioFunctionDeclaration, PatioFunctionDefinition, PatioLocal, PatioLocalType,
- PatioVisibility,
- },
+ PatioCompilationUnit, PatioCompilationUnitElement, PatioDataElementImmutableDefinition,
+ PatioDataElementMutableDeclaration, PatioDataElementMutableDefinition,
+ PatioFunctionDeclaration, PatioFunctionDefinition, PatioLocal, PatioLocalType,
+ PatioVisibility,
instructions::{
PatioBlock, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf,
PatioInstruction, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub,
@@ -23,7 +21,10 @@ use crate::{
PatioUnreachable, PatioVarLoad, PatioVarStore,
},
operands::{PatioLocalRef, PatioOperand},
- types::PatioType,
+ types::{
+ PatioBoolType, PatioByteType, PatioDataPtrType, PatioFunctionPtrType, PatioInt32Type,
+ PatioIntType, PatioType,
+ },
values::{PatioDataPtrValue, PatioIntValue},
},
};
@@ -84,29 +85,31 @@ fn declare_compilation_unit_element(
context: &mut CompilerContext,
) {
match element {
- PatioCompilationUnitElement::DataElementImmutableDefinition(
+ PatioCompilationUnitElement::PatioDataElementImmutableDefinition(
data_element_immutable_definition,
) => {
declare_data_element_immutable_definition(data_element_immutable_definition, context);
}
- PatioCompilationUnitElement::DataElementMutableDeclaration(
+ PatioCompilationUnitElement::PatioDataElementMutableDeclaration(
data_element_mutable_declaration,
) => {
declare_data_element_mutable_declaration(data_element_mutable_declaration, context);
}
- PatioCompilationUnitElement::DataElementMutableDefinition(
+ PatioCompilationUnitElement::PatioDataElementMutableDefinition(
data_element_mutable_definition,
) => {
declare_data_element_mutable_definition(data_element_mutable_definition, context);
}
- PatioCompilationUnitElement::FunctionDeclaration(function_declaration) => {
+ PatioCompilationUnitElement::PatioExceptionTypeDefinition(_) => todo!(),
+
+ PatioCompilationUnitElement::PatioFunctionDeclaration(function_declaration) => {
declare_function_declaration(function_declaration, context);
}
- PatioCompilationUnitElement::FunctionDefinition(function_definition) => {
+ PatioCompilationUnitElement::PatioFunctionDefinition(function_definition) => {
declare_function_definition(function_definition, context);
}
}
@@ -117,23 +120,25 @@ fn define_compilation_unit_element(
context: &mut CompilerContext,
) {
match element {
- PatioCompilationUnitElement::DataElementImmutableDefinition(
+ PatioCompilationUnitElement::PatioDataElementImmutableDefinition(
data_element_immutable_definition,
) => {
define_data_element_immutable_definition(data_element_immutable_definition, context);
}
- PatioCompilationUnitElement::DataElementMutableDeclaration(_) => {}
+ PatioCompilationUnitElement::PatioDataElementMutableDeclaration(_) => {}
- PatioCompilationUnitElement::DataElementMutableDefinition(
+ PatioCompilationUnitElement::PatioDataElementMutableDefinition(
data_element_mutable_definition,
) => {
define_data_element_mutable_definition(data_element_mutable_definition, context);
}
- PatioCompilationUnitElement::FunctionDeclaration(_) => {}
+ PatioCompilationUnitElement::PatioExceptionTypeDefinition(_) => todo!(),
- PatioCompilationUnitElement::FunctionDefinition(function_definition) => {
+ PatioCompilationUnitElement::PatioFunctionDeclaration(_) => {}
+
+ PatioCompilationUnitElement::PatioFunctionDefinition(function_definition) => {
define_function_definition(&function_definition, context);
}
}
@@ -254,7 +259,7 @@ fn define_function_definition(
for local in &function_definition.locals {
let cranelift_type = types::patio_type_to_cranelift_type(
- local.type_,
+ &local.r#type,
&function_context.target_triple,
);
@@ -284,95 +289,103 @@ fn define_function_definition(
fn compile_instruction(instruction: &PatioInstruction, context: &mut CompilerFunctionContext<'_>) {
match instruction {
- PatioInstruction::Block(block) => {
+ PatioInstruction::PatioBlock(block) => {
compile_block(block, context);
}
- PatioInstruction::BoolToInt(bool_to_int) => {
+ PatioInstruction::PatioBoolToInt(bool_to_int) => {
compile_bool_to_int(bool_to_int, context);
}
- PatioInstruction::Call(call) => {
+ PatioInstruction::PatioCall(call) => {
compile_call(call, context);
}
- PatioInstruction::CallPointer(call_pointer) => {
+ PatioInstruction::PatioCallPointer(call_pointer) => {
compile_call_pointer(call_pointer, context);
}
- PatioInstruction::DataPtrToInt(data_ptr_to_int) => {
+ PatioInstruction::PatioDataPtrToInt(data_ptr_to_int) => {
compile_data_ptr_to_int(data_ptr_to_int, context);
}
- PatioInstruction::If(if_) => {
+ PatioInstruction::PatioIf(if_) => {
compile_if(if_, context);
}
- PatioInstruction::IntAdd(int_add) => {
+ PatioInstruction::PatioIntAdd(int_add) => {
compile_int_add(int_add, context);
}
- PatioInstruction::IntEquals(int_equals) => {
+ PatioInstruction::PatioIntEquals(int_equals) => {
compile_int_equals(int_equals, context);
}
- PatioInstruction::IntNotEqual(int_not_equal) => {
+ PatioInstruction::PatioIntNotEqual(int_not_equal) => {
compile_int_not_equal(int_not_equal, context);
}
- PatioInstruction::IntSub(int_sub) => {
+ PatioInstruction::PatioIntSub(int_sub) => {
compile_int_sub(int_sub, context);
}
- PatioInstruction::IntToBool(int_to_bool) => {
+ PatioInstruction::PatioIntToBool(int_to_bool) => {
compile_int_to_bool(int_to_bool, context);
}
- PatioInstruction::IntToDataPtr(int_to_data_ptr) => {
+ PatioInstruction::PatioIntToDataPtr(int_to_data_ptr) => {
compile_int_to_data_ptr(int_to_data_ptr, context);
}
- PatioInstruction::LocalStore(local_store) => {
+ PatioInstruction::PatioLocalStore(local_store) => {
compile_local_store(local_store, context);
}
- PatioInstruction::Loop(loop_) => {
+ PatioInstruction::PatioLoop(loop_) => {
compile_loop(loop_, context);
}
- PatioInstruction::LoopBreak(loop_break) => {
+ PatioInstruction::PatioLoopBreak(loop_break) => {
compile_loop_break(loop_break, context);
}
- PatioInstruction::LoopContinue(loop_continue) => {
+ PatioInstruction::PatioLoopContinue(loop_continue) => {
compile_loop_continue(loop_continue, context);
}
- PatioInstruction::MemoryLoad(memory_load) => {
+ PatioInstruction::PatioMemoryLoad(memory_load) => {
compile_memory_load(memory_load, context);
}
- PatioInstruction::MemoryStore(memory_store) => {
+ PatioInstruction::PatioMemoryStore(memory_store) => {
compile_memory_store(memory_store, context);
}
- PatioInstruction::Return(return_) => {
+ PatioInstruction::PatioReturn(return_) => {
compile_return(return_, context);
}
- PatioInstruction::Switch(switch) => {
+ PatioInstruction::PatioSwitch(switch) => {
compile_switch(switch, context);
}
- PatioInstruction::Unreachable(unreachable) => {
+ PatioInstruction::PatioThrow(_) => {
+ todo!();
+ }
+
+ PatioInstruction::PatioTry(_) => {
+ todo!();
+ }
+
+ PatioInstruction::PatioUnreachable(unreachable) => {
compile_unreachable(unreachable, context);
}
- PatioInstruction::VarLoad(var_load) => {
+ PatioInstruction::PatioVarLoad(var_load) => {
compile_var_load(var_load, context);
}
- PatioInstruction::VarStore(var_store) => {
+ PatioInstruction::PatioVarStore(var_store) => {
compile_var_store(var_store, context);
}
}
@@ -640,7 +653,7 @@ fn compile_memory_load(memory_load: &PatioMemoryLoad, context: &mut CompilerFunc
let value_layout =
types::data_layout_element_at(&memory_load.layout, &memory_load.element_offsets);
let value_type = types::data_layout_to_type(value_layout);
- let cranelift_type = types::patio_type_to_cranelift_type(value_type, &context.target_triple);
+ let cranelift_type = types::patio_type_to_cranelift_type(&value_type, &context.target_triple);
let cranelift_pointer = compile_operand(&memory_load.pointer, context);
let cranelift_flags = cranelift_codegen::ir::MemFlags::new();
@@ -719,7 +732,7 @@ fn compile_switch(switch: &PatioSwitch, context: &mut CompilerFunctionContext<'_
// Create the jump itself.
let mut cranelift_switch = cranelift::frontend::Switch::new();
for (case, case_block) in switch.cases.iter().zip(&case_blocks) {
- cranelift_switch.set_entry(case.value.into(), *case_block);
+ cranelift_switch.set_entry(case.value.try_into().unwrap(), *case_block);
}
cranelift_switch.emit(
&mut context.function_builder,
@@ -796,8 +809,8 @@ fn compile_operand(
context: &mut CompilerFunctionContext<'_>,
) -> cranelift::prelude::Value {
match operand {
- PatioOperand::BoolConst(value) => {
- let integer_value = if *value { 1 } else { 0 };
+ PatioOperand::PatioBoolConst(bool_const) => {
+ let integer_value = if bool_const.value { 1 } else { 0 };
context
.function_builder
@@ -805,10 +818,12 @@ fn compile_operand(
.iconst(types::CRANELIFT_BOOL_TYPE, integer_value)
}
- PatioOperand::DataPtrConst(value) => compile_data_ptr_value(&value, context),
+ PatioOperand::PatioDataPtrConst(data_ptr_const) => {
+ compile_data_ptr_value(&data_ptr_const.value, context)
+ }
- PatioOperand::FunctionPtrConst(name) => {
- let func_ref = context.func_id(name);
+ PatioOperand::PatioFunctionPtrConst(function_ptr_const) => {
+ let func_ref = context.func_id(&function_ptr_const.name);
let function_ptr_type = context.pointer_type();
context
@@ -817,16 +832,16 @@ fn compile_operand(
.func_addr(function_ptr_type, func_ref)
}
- PatioOperand::IntConst(value) => {
+ PatioOperand::PatioIntConst(int_const) => {
let cranelift_int_type = types::cranelift_int_type(&context.target_triple);
- compile_int_value(&value, cranelift_int_type, context)
+ compile_int_value(&int_const.value, cranelift_int_type, context)
}
- PatioOperand::Int32Const(value) => {
- compile_int_value(&value, types::CRANELIFT_INT_32_TYPE, context)
+ PatioOperand::PatioInt32Const(int_32_const) => {
+ compile_int_value(&int_32_const.value, types::CRANELIFT_INT_32_TYPE, context)
}
- PatioOperand::LocalRef(local_ref) => context.local_load(&local_ref.name),
+ PatioOperand::PatioLocalRef(local_ref) => context.local_load(&local_ref.name),
_ => {
todo!("Unhandled operand: {operand:?}",)
@@ -841,15 +856,17 @@ fn compile_data_ptr_value(
let data_ptr_type = context.pointer_type();
match data_ptr_value {
- PatioDataPtrValue::DataElement { name } => {
- let global_value = context.data_ptr(name);
+ PatioDataPtrValue::PatioDataPtrValueDataElement(data_element) => {
+ let global_value = context.data_ptr(&data_element.name);
context
.function_builder
.ins()
.global_value(data_ptr_type, global_value)
}
- PatioDataPtrValue::Null => context.function_builder.ins().iconst(data_ptr_type, 0),
+ PatioDataPtrValue::PatioDataPtrValueNull(_) => {
+ context.function_builder.ins().iconst(data_ptr_type, 0)
+ }
}
}
@@ -896,7 +913,7 @@ fn cranelift_signature<'a>(
for local in locals {
if local.local_type == PatioLocalType::Param {
- let param_type = types::patio_type_to_cranelift_type(local.type_, &target_triple);
+ let param_type = types::patio_type_to_cranelift_type(&local.r#type, &target_triple);
let param = cranelift::prelude::AbiParam::new(param_type);
signature.params.push(param);
}
@@ -904,7 +921,7 @@ fn cranelift_signature<'a>(
for return_type in return_types {
let cranelift_return_type =
- types::patio_type_to_cranelift_type(*return_type, &target_triple);
+ types::patio_type_to_cranelift_type(return_type, &target_triple);
let returns = cranelift::prelude::AbiParam::new(cranelift_return_type);
signature.returns.push(returns);
}
@@ -924,25 +941,25 @@ fn patio_operand_to_cranelift_type(
context: &mut CompilerFunctionContext<'_>,
) -> cranelift_codegen::ir::Type {
match operand {
- PatioOperand::BoolConst(_) => {
- patio_type_to_cranelift_type(PatioType::Bool, &context.target_triple)
+ PatioOperand::PatioBoolConst(_) => {
+ patio_type_to_cranelift_type(&PatioBoolType.into(), &context.target_triple)
}
- PatioOperand::ByteConst(_) => {
- patio_type_to_cranelift_type(PatioType::Byte, &context.target_triple)
+ PatioOperand::PatioByteConst(_) => {
+ patio_type_to_cranelift_type(&PatioByteType.into(), &context.target_triple)
}
- PatioOperand::DataPtrConst(_) => {
- patio_type_to_cranelift_type(PatioType::DataPtr, &context.target_triple)
+ PatioOperand::PatioDataPtrConst(_) => {
+ patio_type_to_cranelift_type(&PatioDataPtrType.into(), &context.target_triple)
}
- PatioOperand::FunctionPtrConst(_) => {
- patio_type_to_cranelift_type(PatioType::FunctionPtr, &context.target_triple)
+ PatioOperand::PatioFunctionPtrConst(_) => {
+ patio_type_to_cranelift_type(&PatioFunctionPtrType.into(), &context.target_triple)
}
- PatioOperand::IntConst(_) => {
- patio_type_to_cranelift_type(PatioType::Int, &context.target_triple)
+ PatioOperand::PatioIntConst(_) => {
+ patio_type_to_cranelift_type(&PatioIntType.into(), &context.target_triple)
}
- PatioOperand::Int32Const(_) => {
- patio_type_to_cranelift_type(PatioType::Int32, &context.target_triple)
+ PatioOperand::PatioInt32Const(_) => {
+ patio_type_to_cranelift_type(&PatioInt32Type.into(), &context.target_triple)
}
- PatioOperand::LocalRef(local_ref) => context.local_type(&local_ref.name),
+ PatioOperand::PatioLocalRef(local_ref) => context.local_type(&local_ref.name),
}
}
diff --git a/patio-to-cranelift/src/codegen/types.rs b/patio-to-cranelift/src/codegen/types.rs
index 21833f3f..616520f7 100644
--- a/patio-to-cranelift/src/codegen/types.rs
+++ b/patio-to-cranelift/src/codegen/types.rs
@@ -1,21 +1,28 @@
-use crate::patio::types::{PatioDataLayout, PatioType};
+use crate::patio::types::{
+ PatioBoolType, PatioByteType, PatioDataLayout, PatioDataPtrType, PatioFunctionPtrType,
+ PatioInt32Type, PatioIntType, PatioType,
+};
pub(crate) fn patio_type_to_cranelift_type(
- patio_type: PatioType,
+ patio_type: &PatioType,
target_triple: &target_lexicon::Triple,
) -> cranelift::prelude::Type {
match patio_type {
- PatioType::Bool => CRANELIFT_BOOL_TYPE,
+ PatioType::PatioBoolType(_) => CRANELIFT_BOOL_TYPE,
- PatioType::Byte => CRANELIFT_BYTE_TYPE,
+ PatioType::PatioByteType(_) => CRANELIFT_BYTE_TYPE,
- PatioType::DataPtr => cranelift::prelude::Type::triple_pointer_type(&target_triple),
+ PatioType::PatioDataPtrType(_) => {
+ cranelift::prelude::Type::triple_pointer_type(&target_triple)
+ }
- PatioType::FunctionPtr => cranelift::prelude::Type::triple_pointer_type(&target_triple),
+ PatioType::PatioFunctionPtrType(_) => {
+ cranelift::prelude::Type::triple_pointer_type(&target_triple)
+ }
- PatioType::Int => cranelift_int_type(target_triple),
+ PatioType::PatioIntType(_) => cranelift_int_type(target_triple),
- PatioType::Int32 => CRANELIFT_INT_32_TYPE,
+ PatioType::PatioInt32Type(_) => CRANELIFT_INT_32_TYPE,
}
}
@@ -41,27 +48,29 @@ pub(crate) fn data_layout_byte_size(
target_triple: &target_lexicon::Triple,
) -> u32 {
match layout {
- PatioDataLayout::Array { element, size } => {
- data_layout_byte_size(element, target_triple) * size
+ PatioDataLayout::PatioDataLayoutArray(array) => {
+ data_layout_byte_size(&array.element, target_triple)
+ * u32::try_from(array.size).unwrap()
}
- PatioDataLayout::Bool => CRANELIFT_BOOL_TYPE.bytes(),
+ PatioDataLayout::PatioDataLayoutBool(_) => CRANELIFT_BOOL_TYPE.bytes(),
- PatioDataLayout::Byte => CRANELIFT_BYTE_TYPE.bytes(),
+ PatioDataLayout::PatioDataLayoutByte(_) => CRANELIFT_BYTE_TYPE.bytes(),
- PatioDataLayout::DataPtr => {
+ PatioDataLayout::PatioDataLayoutDataPtr(_) => {
cranelift::prelude::Type::triple_pointer_type(&target_triple).bytes()
}
- PatioDataLayout::FunctionPtr => {
+ PatioDataLayout::PatioDataLayoutFunctionPtr(_) => {
cranelift::prelude::Type::triple_pointer_type(&target_triple).bytes()
}
- PatioDataLayout::Int => cranelift_int_type(target_triple).bytes(),
+ PatioDataLayout::PatioDataLayoutInt(_) => cranelift_int_type(target_triple).bytes(),
- PatioDataLayout::Int32 => CRANELIFT_INT_32_TYPE.bytes(),
+ PatioDataLayout::PatioDataLayoutInt32(_) => CRANELIFT_INT_32_TYPE.bytes(),
- PatioDataLayout::Struct { elements } => elements
+ PatioDataLayout::PatioDataLayoutStruct(layout_struct) => layout_struct
+ .elements
.iter()
.map(|element| data_layout_byte_size(element, target_triple))
.sum(),
@@ -72,7 +81,11 @@ pub(crate) fn data_layout_byte_size(
mod data_layout_byte_size_test {
use std::str::FromStr;
- use crate::patio::types::PatioDataLayout;
+ use crate::patio::types::{
+ PatioDataLayout, PatioDataLayoutArray, PatioDataLayoutBool, PatioDataLayoutByte,
+ PatioDataLayoutDataPtr, PatioDataLayoutFunctionPtr, PatioDataLayoutInt,
+ PatioDataLayoutInt32, PatioDataLayoutStruct,
+ };
fn data_layout_byte_size(layout: &PatioDataLayout) -> u32 {
super::data_layout_byte_size(
@@ -83,38 +96,44 @@ mod data_layout_byte_size_test {
#[test]
fn bool_is_one_byte() {
- assert_eq!(data_layout_byte_size(&PatioDataLayout::Bool), 1);
+ assert_eq!(data_layout_byte_size(&PatioDataLayoutBool.into()), 1);
}
#[test]
fn byte_is_one_byte() {
- assert_eq!(data_layout_byte_size(&PatioDataLayout::Byte), 1);
+ assert_eq!(data_layout_byte_size(&PatioDataLayoutByte.into()), 1);
}
#[test]
fn data_ptr_is_width_of_pointer() {
- assert_eq!(data_layout_byte_size(&PatioDataLayout::DataPtr), 8);
+ assert_eq!(data_layout_byte_size(&PatioDataLayoutDataPtr.into()), 8);
}
#[test]
fn function_ptr_is_width_of_pointer() {
- assert_eq!(data_layout_byte_size(&PatioDataLayout::FunctionPtr), 8);
+ assert_eq!(data_layout_byte_size(&PatioDataLayoutFunctionPtr.into()), 8);
}
#[test]
fn int_is_width_of_pointer_and_at_least_32_bits() {
- assert_eq!(data_layout_byte_size(&PatioDataLayout::Int), 8);
+ assert_eq!(data_layout_byte_size(&PatioDataLayoutInt.into()), 8);
}
#[test]
fn int32_is_four_bytes() {
- assert_eq!(data_layout_byte_size(&PatioDataLayout::Int32), 4);
+ assert_eq!(data_layout_byte_size(&PatioDataLayoutInt32.into()), 4);
}
#[test]
fn array_size_is_size_of_each_element_multiplied_by_number_of_elements() {
assert_eq!(
- data_layout_byte_size(&PatioDataLayout::array(PatioDataLayout::Int32, 7)),
+ data_layout_byte_size(
+ &PatioDataLayoutArray {
+ element: PatioDataLayoutInt32.into(),
+ size: 7
+ }
+ .into()
+ ),
28
);
}
@@ -122,10 +141,15 @@ mod data_layout_byte_size_test {
#[test]
fn struct_size_is_sum_of_element_sizes() {
assert_eq!(
- data_layout_byte_size(&PatioDataLayout::struct_(vec![
- PatioDataLayout::Int32,
- PatioDataLayout::Byte
- ])),
+ data_layout_byte_size(
+ &PatioDataLayoutStruct {
+ elements: vec![
+ PatioDataLayout::PatioDataLayoutInt32(PatioDataLayoutInt32),
+ PatioDataLayout::PatioDataLayoutByte(PatioDataLayoutByte)
+ ]
+ }
+ .into()
+ ),
5
);
}
@@ -133,7 +157,7 @@ mod data_layout_byte_size_test {
pub(crate) fn data_layout_byte_offset(
layout: &PatioDataLayout,
- element_offsets: &[u32],
+ element_offsets: &[i32],
target_triple: &target_lexicon::Triple,
) -> u32 {
let mut byte_offset = 0;
@@ -141,24 +165,32 @@ pub(crate) fn data_layout_byte_offset(
for element_offset in element_offsets {
match current_element {
- PatioDataLayout::Array { element, size: _ } => {
- byte_offset += data_layout_byte_size(element, target_triple) * element_offset;
- current_element = element;
+ PatioDataLayout::PatioDataLayoutArray(array) => {
+ byte_offset += data_layout_byte_size(&array.element, target_triple)
+ * u32::try_from(*element_offset).unwrap();
+ current_element = &array.element;
}
- PatioDataLayout::Bool => panic!("offset not allowed for bool"),
- PatioDataLayout::Byte => panic!("offset not allowed for byte"),
- PatioDataLayout::DataPtr => panic!("offset not allowed for data ptr"),
- PatioDataLayout::FunctionPtr => panic!("offset not allowed for function ptr"),
- PatioDataLayout::Int => panic!("offset not allowed for int"),
- PatioDataLayout::Int32 => panic!("offset not allowed for int32"),
-
- PatioDataLayout::Struct { elements } => {
- for element in elements.iter().take((*element_offset).try_into().unwrap()) {
+ PatioDataLayout::PatioDataLayoutBool(_) => panic!("offset not allowed for bool"),
+ PatioDataLayout::PatioDataLayoutByte(_) => panic!("offset not allowed for byte"),
+ PatioDataLayout::PatioDataLayoutDataPtr(_) => panic!("offset not allowed for data ptr"),
+ PatioDataLayout::PatioDataLayoutFunctionPtr(_) => {
+ panic!("offset not allowed for function ptr")
+ }
+ PatioDataLayout::PatioDataLayoutInt(_) => panic!("offset not allowed for int"),
+ PatioDataLayout::PatioDataLayoutInt32(_) => panic!("offset not allowed for int32"),
+
+ PatioDataLayout::PatioDataLayoutStruct(layout_struct) => {
+ for element in layout_struct
+ .elements
+ .iter()
+ .take((*element_offset).try_into().unwrap())
+ {
byte_offset += data_layout_byte_size(element, target_triple);
}
- current_element = &elements[usize::try_from(*element_offset).unwrap()];
+ current_element =
+ &layout_struct.elements[usize::try_from(*element_offset).unwrap()];
}
}
}
@@ -170,9 +202,12 @@ pub(crate) fn data_layout_byte_offset(
mod data_layout_byte_offset_test {
use std::str::FromStr;
- use crate::patio::types::PatioDataLayout;
+ use crate::patio::types::{
+ PatioDataLayout, PatioDataLayoutArray, PatioDataLayoutByte, PatioDataLayoutDataPtr,
+ PatioDataLayoutInt32, PatioDataLayoutStruct,
+ };
- fn data_layout_byte_offset(layout: &PatioDataLayout, element_offsets: Vec<u32>) -> u32 {
+ fn data_layout_byte_offset(layout: &PatioDataLayout, element_offsets: Vec<i32>) -> u32 {
super::data_layout_byte_offset(
layout,
&element_offsets,
@@ -182,7 +217,11 @@ mod data_layout_byte_offset_test {
#[test]
fn byte_offset_of_first_element_of_array_is_zero() {
- let layout = PatioDataLayout::array(PatioDataLayout::Int32, 2);
+ let layout = PatioDataLayoutArray {
+ element: PatioDataLayoutInt32.into(),
+ size: 2,
+ }
+ .into();
let byte_offset = data_layout_byte_offset(&layout, vec![0]);
@@ -191,7 +230,11 @@ mod data_layout_byte_offset_test {
#[test]
fn byte_offset_of_element_of_array_is_size_of_skipped_elements() {
- let layout = PatioDataLayout::array(PatioDataLayout::Int32, 5);
+ let layout = PatioDataLayoutArray {
+ element: PatioDataLayoutInt32.into(),
+ size: 5,
+ }
+ .into();
let byte_offset = data_layout_byte_offset(&layout, vec![2]);
@@ -200,7 +243,15 @@ mod data_layout_byte_offset_test {
#[test]
fn byte_offset_of_element_of_element_of_array_is_size_of_skipped_elements() {
- let layout = PatioDataLayout::array(PatioDataLayout::array(PatioDataLayout::Byte, 7), 5);
+ let layout = PatioDataLayoutArray {
+ element: PatioDataLayoutArray {
+ element: PatioDataLayoutByte.into(),
+ size: 7,
+ }
+ .into(),
+ size: 5,
+ }
+ .into();
let byte_offset = data_layout_byte_offset(&layout, vec![2, 3]);
@@ -209,11 +260,14 @@ mod data_layout_byte_offset_test {
#[test]
fn byte_offset_of_first_element_of_struct_is_zero() {
- let layout = PatioDataLayout::struct_(vec![
- PatioDataLayout::Int32,
- PatioDataLayout::Byte,
- PatioDataLayout::DataPtr,
- ]);
+ let layout = PatioDataLayoutStruct {
+ elements: vec![
+ PatioDataLayoutInt32.into(),
+ PatioDataLayoutByte.into(),
+ PatioDataLayoutDataPtr.into(),
+ ],
+ }
+ .into();
let byte_offset = data_layout_byte_offset(&layout, vec![0]);
@@ -222,11 +276,14 @@ mod data_layout_byte_offset_test {
#[test]
fn byte_offset_of_element_of_struct_is_size_of_skipped_elements() {
- let layout = PatioDataLayout::struct_(vec![
- PatioDataLayout::Int32,
- PatioDataLayout::Byte,
- PatioDataLayout::DataPtr,
- ]);
+ let layout = PatioDataLayoutStruct {
+ elements: vec![
+ PatioDataLayoutInt32.into(),
+ PatioDataLayoutByte.into(),
+ PatioDataLayoutDataPtr.into(),
+ ],
+ }
+ .into();
let byte_offset = data_layout_byte_offset(&layout, vec![2]);
@@ -235,15 +292,21 @@ mod data_layout_byte_offset_test {
#[test]
fn byte_offset_of_element_of_element_of_struct_is_size_of_skipped_elements() {
- let layout = PatioDataLayout::struct_(vec![
- PatioDataLayout::Int32,
- PatioDataLayout::struct_(vec![
- PatioDataLayout::Byte,
- PatioDataLayout::DataPtr,
- PatioDataLayout::Byte,
- ]),
- PatioDataLayout::Int32,
- ]);
+ let layout = PatioDataLayoutStruct {
+ elements: vec![
+ PatioDataLayoutInt32.into(),
+ PatioDataLayoutStruct {
+ elements: vec![
+ PatioDataLayoutByte.into(),
+ PatioDataLayoutDataPtr.into(),
+ PatioDataLayoutByte.into(),
+ ],
+ }
+ .into(),
+ PatioDataLayoutInt32.into(),
+ ],
+ }
+ .into();
let byte_offset = data_layout_byte_offset(&layout, vec![1, 2]);
@@ -253,25 +316,28 @@ mod data_layout_byte_offset_test {
pub(crate) fn data_layout_element_at<'a>(
layout: &'a PatioDataLayout,
- element_offsets: &[u32],
+ element_offsets: &[i32],
) -> &'a PatioDataLayout {
let mut current_element = layout;
for element_offset in element_offsets {
match current_element {
- PatioDataLayout::Array { element, size: _ } => {
- current_element = element;
+ PatioDataLayout::PatioDataLayoutArray(array) => {
+ current_element = &array.element;
}
- PatioDataLayout::Bool => panic!("offset not allowed for bool"),
- PatioDataLayout::Byte => panic!("offset not allowed for byte"),
- PatioDataLayout::DataPtr => panic!("offset not allowed for data ptr"),
- PatioDataLayout::FunctionPtr => panic!("offset not allowed for function ptr"),
- PatioDataLayout::Int => panic!("offset not allowed for int"),
- PatioDataLayout::Int32 => panic!("offset not allowed for int32"),
+ PatioDataLayout::PatioDataLayoutBool(_) => panic!("offset not allowed for bool"),
+ PatioDataLayout::PatioDataLayoutByte(_) => panic!("offset not allowed for byte"),
+ PatioDataLayout::PatioDataLayoutDataPtr(_) => panic!("offset not allowed for data ptr"),
+ PatioDataLayout::PatioDataLayoutFunctionPtr(_) => {
+ panic!("offset not allowed for function ptr")
+ }
+ PatioDataLayout::PatioDataLayoutInt(_) => panic!("offset not allowed for int"),
+ PatioDataLayout::PatioDataLayoutInt32(_) => panic!("offset not allowed for int32"),
- PatioDataLayout::Struct { elements } => {
- current_element = &elements[usize::try_from(*element_offset).unwrap()];
+ PatioDataLayout::PatioDataLayoutStruct(layout_struct) => {
+ current_element =
+ &layout_struct.elements[usize::try_from(*element_offset).unwrap()];
}
}
}
@@ -281,86 +347,113 @@ pub(crate) fn data_layout_element_at<'a>(
#[cfg(test)]
mod data_layout_element_at_test {
- use crate::patio::types::PatioDataLayout;
+ use crate::patio::types::{
+ PatioDataLayout, PatioDataLayoutArray, PatioDataLayoutByte, PatioDataLayoutDataPtr,
+ PatioDataLayoutInt32, PatioDataLayoutStruct,
+ };
fn data_layout_element_at(
layout: &PatioDataLayout,
- element_offsets: Vec<u32>,
+ element_offsets: Vec<i32>,
) -> &PatioDataLayout {
super::data_layout_element_at(layout, &element_offsets)
}
#[test]
fn element_of_array_is_element_layout() {
- let layout = PatioDataLayout::array(PatioDataLayout::Int32, 2);
+ let layout = PatioDataLayoutArray {
+ element: PatioDataLayoutInt32.into(),
+ size: 2,
+ }
+ .into();
let element = data_layout_element_at(&layout, vec![0]);
- assert_eq!(element, &PatioDataLayout::Int32);
+ assert_eq!(element, &PatioDataLayoutInt32.into());
}
#[test]
fn element_of_element_of_array() {
- let layout = PatioDataLayout::array(PatioDataLayout::array(PatioDataLayout::Byte, 7), 5);
+ let layout = PatioDataLayoutArray {
+ element: PatioDataLayoutArray {
+ element: PatioDataLayoutByte.into(),
+ size: 7,
+ }
+ .into(),
+ size: 5,
+ }
+ .into();
let element = data_layout_element_at(&layout, vec![2, 3]);
- assert_eq!(element, &PatioDataLayout::Byte);
+ assert_eq!(element, &PatioDataLayoutByte.into());
}
#[test]
fn first_element_of_struct() {
- let layout = PatioDataLayout::struct_(vec![
- PatioDataLayout::Int32,
- PatioDataLayout::Byte,
- PatioDataLayout::DataPtr,
- ]);
+ let layout = PatioDataLayoutStruct {
+ elements: vec![
+ PatioDataLayoutInt32.into(),
+ PatioDataLayoutByte.into(),
+ PatioDataLayoutDataPtr.into(),
+ ],
+ }
+ .into();
let element = data_layout_element_at(&layout, vec![0]);
- assert_eq!(element, &PatioDataLayout::Int32);
+ assert_eq!(element, &PatioDataLayoutInt32.into());
}
#[test]
fn non_first_element_of_struct() {
- let layout = PatioDataLayout::struct_(vec![
- PatioDataLayout::Int32,
- PatioDataLayout::Byte,
- PatioDataLayout::DataPtr,
- ]);
+ let layout = PatioDataLayoutStruct {
+ elements: vec![
+ PatioDataLayoutInt32.into(),
+ PatioDataLayoutByte.into(),
+ PatioDataLayoutDataPtr.into(),
+ ],
+ }
+ .into();
let element = data_layout_element_at(&layout, vec![2]);
- assert_eq!(element, &PatioDataLayout::DataPtr);
+ assert_eq!(element, &PatioDataLayoutDataPtr.into());
}
#[test]
fn element_of_element_of_struct() {
- let layout = PatioDataLayout::struct_(vec![
- PatioDataLayout::Int32,
- PatioDataLayout::struct_(vec![
- PatioDataLayout::Byte,
- PatioDataLayout::DataPtr,
- PatioDataLayout::Byte,
- ]),
- PatioDataLayout::Int32,
- ]);
+ let layout = PatioDataLayoutStruct {
+ elements: vec![
+ PatioDataLayoutInt32.into(),
+ PatioDataLayoutStruct {
+ elements: vec![
+ PatioDataLayoutByte.into(),
+ PatioDataLayoutDataPtr.into(),
+ PatioDataLayoutByte.into(),
+ ],
+ }
+ .into(),
+ PatioDataLayoutInt32.into(),
+ ],
+ }
+ .into();
let element = data_layout_element_at(&layout, vec![1, 2]);
- assert_eq!(element, &PatioDataLayout::Byte);
+ assert_eq!(element, &PatioDataLayoutByte.into());
}
}
pub(crate) fn data_layout_to_type(layout: &PatioDataLayout) -> PatioType {
match layout {
- PatioDataLayout::Array { .. } => panic!("array cannot be represented as type"),
- PatioDataLayout::Bool => PatioType::Bool,
- PatioDataLayout::Byte => PatioType::Byte,
- PatioDataLayout::DataPtr => PatioType::DataPtr,
- PatioDataLayout::FunctionPtr => PatioType::FunctionPtr,
- PatioDataLayout::Int => PatioType::Int,
- PatioDataLayout::Int32 => PatioType::Int32,
- PatioDataLayout::Struct { .. } => panic!("struct cannot be represented as type"),
+ PatioDataLayout::PatioDataLayoutArray(_) => panic!("array cannot be represented as type"),
+ PatioDataLayout::PatioDataLayoutBool(_) => PatioBoolType.into(),
+ PatioDataLayout::PatioDataLayoutByte(_) => PatioByteType.into(),
+ PatioDataLayout::PatioDataLayoutDataPtr(_) => PatioDataPtrType.into(),
+ PatioDataLayout::PatioDataLayoutFunctionPtr(_) => PatioFunctionPtrType.into(),
+ PatioDataLayout::PatioDataLayoutInt(_) => PatioIntType.into(),
+ PatioDataLayout::PatioDataLayoutInt32(_) => PatioInt32Type.into(),
+ PatioDataLayout::PatioDataLayoutStruct(_) => panic!("struct cannot be represented as type"),
}
}
diff --git a/patio-to-cranelift/src/codegen/values.rs b/patio-to-cranelift/src/codegen/values.rs
index 229dbc6f..e4bcec2b 100644
--- a/patio-to-cranelift/src/codegen/values.rs
+++ b/patio-to-cranelift/src/codegen/values.rs
@@ -1,6 +1,9 @@
use crate::{
codegen::types,
- patio::values::{PatioDataPtrValue, PatioDataValue, PatioIntValue},
+ patio::{
+ types::{PatioDataValue, PatioDataValueDataPtr},
+ values::{PatioDataPtrValue, PatioIntValue},
+ },
};
pub(crate) fn data_value_to_bytes(
@@ -24,37 +27,38 @@ fn write_data_value(
target_triple: &target_lexicon::Triple,
) {
match value {
- PatioDataValue::Array {
- element_layout: _,
- elements,
- } => {
- for element in elements {
+ PatioDataValue::PatioDataValueArray(array) => {
+ for element in &array.elements {
write_data_value(element, bytes, target_triple);
}
}
- PatioDataValue::Bool(value) => bytes.push(if *value { 1 } else { 0 }),
+ PatioDataValue::PatioDataValueBool(bool) => bytes.push(if bool.value { 1 } else { 0 }),
- PatioDataValue::Byte(value) => bytes.push(*value),
+ PatioDataValue::PatioDataValueByte(byte) => bytes.push(byte.value as u8),
- PatioDataValue::DataPtr(PatioDataPtrValue::DataElement { name: _ }) => todo!(),
+ PatioDataValue::PatioDataValueDataPtr(PatioDataValueDataPtr {
+ value: PatioDataPtrValue::PatioDataPtrValueDataElement(_),
+ }) => todo!(),
- PatioDataValue::DataPtr(PatioDataPtrValue::Null) => {
+ PatioDataValue::PatioDataValueDataPtr(PatioDataValueDataPtr {
+ value: PatioDataPtrValue::PatioDataPtrValueNull(_),
+ }) => {
write_patio_int(0, bytes, target_triple);
}
- PatioDataValue::Int(int_value) => {
- let value = eval_int_value(int_value, target_triple);
+ PatioDataValue::PatioDataValueInt(int_value) => {
+ let value = eval_int_value(&int_value.value, target_triple);
write_patio_int(value, bytes, target_triple);
}
- PatioDataValue::Int32(int_value) => {
- let value: i32 = eval_int_value(int_value, target_triple);
+ PatioDataValue::PatioDataValueInt32(int_value) => {
+ let value: i32 = eval_int_value(&int_value.value, target_triple);
bytes.extend_from_slice(&value.to_le_bytes());
}
- PatioDataValue::Struct { elements } => {
- for element in elements {
+ PatioDataValue::PatioDataValueStruct(value_struct) => {
+ for element in &value_struct.elements {
write_data_value(element, bytes, target_triple);
}
}
@@ -80,18 +84,21 @@ fn write_patio_int(value: i32, bytes: &mut Vec<u8>, target_triple: &target_lexic
pub(crate) fn eval_int_value(value: &PatioIntValue, target_triple: &target_lexicon::Triple) -> i32 {
match value {
- PatioIntValue::ByteOffsetOf {
- layout,
- element_offsets,
- } => types::data_layout_byte_offset(layout, element_offsets, target_triple)
- .try_into()
- .unwrap(),
+ PatioIntValue::PatioIntValueByteOffsetOf(byte_offset_of) => types::data_layout_byte_offset(
+ &byte_offset_of.layout,
+ &byte_offset_of.element_offsets,
+ target_triple,
+ )
+ .try_into()
+ .unwrap(),
- PatioIntValue::ByteSizeOf { layout } => types::data_layout_byte_size(layout, target_triple)
- .try_into()
- .unwrap(),
+ PatioIntValue::PatioIntValueByteSizeOf(byte_size_of) => {
+ types::data_layout_byte_size(&byte_size_of.layout, target_triple)
+ .try_into()
+ .unwrap()
+ }
- PatioIntValue::Literal { value } => *value,
+ PatioIntValue::PatioIntValueLiteral(literal) => literal.value,
}
}
@@ -100,8 +107,12 @@ mod data_value_to_bytes_test {
use std::str::FromStr;
use crate::patio::{
- types::PatioDataLayout,
- values::{PatioDataPtrValue, PatioDataValue, PatioIntValue},
+ types::{
+ PatioDataLayoutInt32, PatioDataValue, PatioDataValueArray, PatioDataValueBool,
+ PatioDataValueByte, PatioDataValueDataPtr, PatioDataValueInt, PatioDataValueInt32,
+ PatioDataValueStruct,
+ },
+ values::{PatioDataPtrValueNull, PatioIntValueLiteral},
};
fn data_value_to_bytes(data_value: PatioDataValue) -> Box<[u8]> {
@@ -113,13 +124,20 @@ mod data_value_to_bytes_test {
#[test]
fn array_is_written_as_elements() {
- let data_value = PatioDataValue::Array {
- element_layout: PatioDataLayout::Int32,
+ let data_value = PatioDataValueArray {
+ element_layout: PatioDataLayoutInt32.into(),
elements: vec![
- PatioDataValue::Int32(PatioIntValue::Literal { value: 0x12345678 }),
- PatioDataValue::Int32(PatioIntValue::Literal { value: 0x23456789 }),
+ PatioDataValueInt32 {
+ value: PatioIntValueLiteral { value: 0x12345678 }.into(),
+ }
+ .into(),
+ PatioDataValueInt32 {
+ value: PatioIntValueLiteral { value: 0x23456789 }.into(),
+ }
+ .into(),
],
- };
+ }
+ .into();
let bytes = data_value_to_bytes(data_value);
@@ -131,7 +149,7 @@ mod data_value_to_bytes_test {
#[test]
fn false_is_single_byte_0() {
- let data_value = PatioDataValue::Bool(false);
+ let data_value = PatioDataValueBool { value: false }.into();
let bytes = data_value_to_bytes(data_value);
@@ -140,7 +158,7 @@ mod data_value_to_bytes_test {
#[test]
fn true_is_single_byte_1() {
- let data_value = PatioDataValue::Bool(true);
+ let data_value = PatioDataValueBool { value: true }.into();
let bytes = data_value_to_bytes(data_value);
@@ -149,7 +167,7 @@ mod data_value_to_bytes_test {
#[test]
fn byte_is_written_as_single_byte() {
- let data_value = PatioDataValue::Byte(42);
+ let data_value = PatioDataValueByte { value: 42 }.into();
let bytes = data_value_to_bytes(data_value);
@@ -159,7 +177,10 @@ mod data_value_to_bytes_test {
#[test]
fn given_pointer_is_64_bits_wide_then_null_data_ptr_is_written_as_64_bit_0() {
let target_triple = target_lexicon::Triple::from_str("x86_64-unknown-linux-gnu").unwrap();
- let data_value = PatioDataValue::DataPtr(PatioDataPtrValue::Null);
+ let data_value = PatioDataValueDataPtr {
+ value: PatioDataPtrValueNull {}.into(),
+ }
+ .into();
let bytes = super::data_value_to_bytes(&data_value, &target_triple);
@@ -169,7 +190,10 @@ mod data_value_to_bytes_test {
#[test]
fn given_pointer_is_32_bits_wide_then_null_data_ptr_is_written_as_32_bit_0() {
let target_triple = target_lexicon::Triple::from_str("i686-unknown-linux-gnu").unwrap();
- let data_value = PatioDataValue::DataPtr(PatioDataPtrValue::Null);
+ let data_value = PatioDataValueDataPtr {
+ value: PatioDataPtrValueNull {}.into(),
+ }
+ .into();
let bytes = super::data_value_to_bytes(&data_value, &target_triple);
@@ -179,7 +203,10 @@ mod data_value_to_bytes_test {
#[test]
fn given_pointer_is_64_bits_wide_then_int_is_written_with_pointer_width() {
let target_triple = target_lexicon::Triple::from_str("x86_64-unknown-linux-gnu").unwrap();
- let data_value = PatioDataValue::Int(PatioIntValue::Literal { value: 0x12345678 });
+ let data_value = PatioDataValueInt {
+ value: PatioIntValueLiteral { value: 0x12345678 }.into(),
+ }
+ .into();
let bytes = super::data_value_to_bytes(&data_value, &target_triple);
@@ -189,7 +216,10 @@ mod data_value_to_bytes_test {
#[test]
fn given_pointer_is_32_bits_wide_then_int_is_written_with_pointer_width() {
let target_triple = target_lexicon::Triple::from_str("i686-unknown-linux-gnu").unwrap();
- let data_value = PatioDataValue::Int(PatioIntValue::Literal { value: 0x12345678 });
+ let data_value = PatioDataValueInt {
+ value: PatioIntValueLiteral { value: 0x12345678 }.into(),
+ }
+ .into();
let bytes = super::data_value_to_bytes(&data_value, &target_triple);
@@ -198,7 +228,10 @@ mod data_value_to_bytes_test {
#[test]
fn int_32_is_written_as_four_bytes() {
- let data_value = PatioDataValue::Int32(PatioIntValue::Literal { value: 0x12345678 });
+ let data_value = PatioDataValueInt32 {
+ value: PatioIntValueLiteral { value: 0x12345678 }.into(),
+ }
+ .into();
let bytes = data_value_to_bytes(data_value);
@@ -207,12 +240,19 @@ mod data_value_to_bytes_test {
#[test]
fn struct_is_written_as_elements() {
- let data_value = PatioDataValue::Struct {
+ let data_value = PatioDataValueStruct {
elements: vec![
- PatioDataValue::Int32(PatioIntValue::Literal { value: 0x12345678 }),
- PatioDataValue::Int32(PatioIntValue::Literal { value: 0x23456789 }),
+ PatioDataValueInt32 {
+ value: PatioIntValueLiteral { value: 0x12345678 }.into(),
+ }
+ .into(),
+ PatioDataValueInt32 {
+ value: PatioIntValueLiteral { value: 0x23456789 }.into(),
+ }
+ .into(),
],
- };
+ }
+ .into();
let bytes = data_value_to_bytes(data_value);
@@ -227,7 +267,12 @@ mod data_value_to_bytes_test {
mod eval_int_value_test {
use std::str::FromStr;
- use crate::patio::{types::PatioDataLayout, values::PatioIntValue};
+ use crate::patio::{
+ types::{PatioDataLayoutArray, PatioDataLayoutInt32},
+ values::{
+ PatioIntValue, PatioIntValueByteOffsetOf, PatioIntValueByteSizeOf, PatioIntValueLiteral,
+ },
+ };
fn eval_int_value(int_value: PatioIntValue) -> i32 {
super::eval_int_value(
@@ -238,7 +283,7 @@ mod eval_int_value_test {
#[test]
fn int_value_literal_is_used_verbatim() {
- let int_value = PatioIntValue::Literal { value: 0x12345678 };
+ let int_value = PatioIntValueLiteral { value: 0x12345678 }.into();
let value = eval_int_value(int_value);
@@ -247,9 +292,10 @@ mod eval_int_value_test {
#[test]
fn int_can_have_size_of_layout() {
- let int_value = PatioIntValue::ByteSizeOf {
- layout: PatioDataLayout::Int32,
- };
+ let int_value = PatioIntValueByteSizeOf {
+ layout: PatioDataLayoutInt32.into(),
+ }
+ .into();
let value = eval_int_value(int_value);
@@ -258,10 +304,15 @@ mod eval_int_value_test {
#[test]
fn int_can_have_byte_offset() {
- let int_value = PatioIntValue::ByteOffsetOf {
- layout: PatioDataLayout::array(PatioDataLayout::Int32, 7),
+ let int_value = PatioIntValueByteOffsetOf {
+ layout: PatioDataLayoutArray {
+ element: PatioDataLayoutInt32.into(),
+ size: 7,
+ }
+ .into(),
element_offsets: vec![5],
- };
+ }
+ .into();
let value = eval_int_value(int_value);
diff --git a/patio-to-cranelift/src/errors.rs b/patio-to-cranelift/src/errors.rs
index 09e9a2a4..0bffe856 100644
--- a/patio-to-cranelift/src/errors.rs
+++ b/patio-to-cranelift/src/errors.rs
@@ -3,8 +3,8 @@ pub enum CompileError {
/// Reading the bytes of the patio source file failed.
ReadSourceBytes(#[allow(dead_code)] std::io::Error),
- /// The bytes of the patio source were an invalid flatbuffer.
- InvalidFlatbuffer(#[allow(dead_code)] flatbuffers::InvalidFlatbuffer),
+ /// The Patio source file was invalid.
+ InvalidPatioSource(#[allow(dead_code)] std::io::Error),
/// Enabling PIC failed.
EnablePicFailed(#[allow(dead_code)] cranelift_codegen::settings::SetError),
diff --git a/patio-to-cranelift/src/lib.rs b/patio-to-cranelift/src/lib.rs
index 80d22840..2c7e8d8f 100644
--- a/patio-to-cranelift/src/lib.rs
+++ b/patio-to-cranelift/src/lib.rs
@@ -1,19 +1,61 @@
-use std::{path::Path, str::FromStr};
+use std::{io::Cursor, path::Path, str::FromStr};
-use crate::{errors::CompileError, patio::compilation_unit::PatioCompilationUnit};
-
-#[allow(dead_code, unused_imports)]
-#[path = "./gen/patio_generated.rs"]
-mod patio_generated;
-
-#[allow(dead_code, unused_imports)]
-#[path = "./gen/sources_generated.rs"]
-mod sources_generated;
+use crate::errors::CompileError;
mod codegen;
mod errors;
mod patio;
+mod sources {
+ include!("./gen/sources.rs");
+
+ #[allow(unused)]
+ pub(crate) mod transient_0 {
+ include!("./gen/sources/transient_0.rs");
+ }
+}
+
+mod sourcetext {
+ include!("./gen/sourcetext.rs");
+
+ #[derive(Clone, Debug, Hash, PartialEq)]
+ pub struct SourceText {
+ name: String,
+ contents: String,
+ }
+
+ #[allow(unused)]
+ pub(crate) mod transient_0 {
+ use std::{any::Any, collections::HashMap, sync::Arc};
+
+ use crate::sourcetext::SourceText;
+
+ include!("./gen/sourcetext/transient_0.rs");
+
+ fn encode_source_text(
+ value: &SourceText,
+ writer: &mut impl std::io::Write,
+ shared_values: &mut HashMap<usize, i64>,
+ ) -> std::io::Result<()> {
+ todo!();
+ }
+
+ fn decode_source_text(
+ reader: &mut impl std::io::Read,
+ shared_values: &mut Vec<Arc<dyn Any + Sync + Send>>,
+ ) -> std::io::Result<SourceText> {
+ let name = crate::transient_0::decode_string(reader, shared_values)?;
+ let contents = crate::transient_0::decode_string(reader, shared_values)?;
+ Ok(SourceText { name, contents })
+ }
+ }
+}
+
+#[allow(unused)]
+mod transient_0 {
+ include!("./gen/transient_0.rs");
+}
+
pub fn compile(
source_path: &Path,
target_str: &str,
@@ -21,10 +63,11 @@ pub fn compile(
) -> Result<(), CompileError> {
let source_bytes = std::fs::read(source_path).map_err(CompileError::ReadSourceBytes)?;
- let compilation_unit: PatioCompilationUnit =
- patio_generated::patio::root_as_patio_compilation_unit(&source_bytes)
- .map_err(CompileError::InvalidFlatbuffer)?
- .into();
+ let compilation_unit = patio::transient_0::decode_patio_compilation_unit(
+ &mut Cursor::new(source_bytes),
+ &mut Vec::new(),
+ )
+ .map_err(CompileError::InvalidPatioSource)?;
let target_triple =
target_lexicon::Triple::from_str(target_str).map_err(CompileError::ParseTargetTriple)?;
diff --git a/patio-to-cranelift/src/patio/compilation_unit.rs b/patio-to-cranelift/src/patio/compilation_unit.rs
deleted file mode 100644
index af8b4d3f..00000000
--- a/patio-to-cranelift/src/patio/compilation_unit.rs
+++ /dev/null
@@ -1,67 +0,0 @@
-use crate::patio::{
- instructions::PatioInstruction,
- types::{PatioDataLayout, PatioType},
- values::PatioDataValue,
-};
-
-pub(crate) struct PatioCompilationUnit {
- pub(crate) elements: Vec<PatioCompilationUnitElement>,
-}
-
-pub(crate) enum PatioCompilationUnitElement {
- DataElementImmutableDefinition(PatioDataElementImmutableDefinition),
- DataElementMutableDefinition(PatioDataElementMutableDefinition),
- DataElementMutableDeclaration(PatioDataElementMutableDeclaration),
- FunctionDeclaration(PatioFunctionDeclaration),
- FunctionDefinition(PatioFunctionDefinition),
-}
-
-pub(crate) struct PatioDataElementImmutableDefinition {
- pub(crate) name: String,
- pub(crate) visibility: PatioVisibility,
- pub(crate) value: PatioDataValue,
-}
-
-pub(crate) struct PatioDataElementMutableDefinition {
- pub(crate) name: String,
- pub(crate) visibility: PatioVisibility,
- pub(crate) initial_value: PatioDataValue,
-}
-
-pub(crate) struct PatioDataElementMutableDeclaration {
- pub(crate) name: String,
- pub(crate) layout: PatioDataLayout,
-}
-
-pub(crate) struct PatioFunctionDeclaration {
- pub(crate) name: String,
- pub(crate) params: Vec<PatioLocal>,
- pub(crate) return_types: Vec<PatioType>,
-}
-
-pub(crate) struct PatioFunctionDefinition {
- pub(crate) name: String,
- pub(crate) visibility: PatioVisibility,
- pub(crate) locals: Vec<PatioLocal>,
- pub(crate) return_types: Vec<PatioType>,
- pub(crate) body: Vec<PatioInstruction>,
-}
-
-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,
-}
-
-#[derive(Clone, Copy)]
-pub(crate) enum PatioVisibility {
- Public,
- Private,
-}
diff --git a/patio-to-cranelift/src/patio/flatbuffers.rs b/patio-to-cranelift/src/patio/flatbuffers.rs
deleted file mode 100644
index e35b2c8a..00000000
--- a/patio-to-cranelift/src/patio/flatbuffers.rs
+++ /dev/null
@@ -1,619 +0,0 @@
-use crate::{
- patio::{
- compilation_unit::{
- PatioCompilationUnit, PatioCompilationUnitElement, PatioDataElementImmutableDefinition,
- PatioDataElementMutableDeclaration, PatioDataElementMutableDefinition,
- PatioFunctionDeclaration, PatioFunctionDefinition, PatioLocal, PatioLocalType,
- PatioVisibility,
- },
- instructions::{
- PatioBlock, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf,
- PatioInstruction, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub,
- PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak,
- PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioReturn, PatioSwitch,
- PatioSwitchCase, PatioUnreachable, PatioVarLoad, PatioVarStore,
- },
- operands::{PatioLocalRef, PatioOperand},
- types::{PatioDataLayout, PatioType},
- values::{PatioDataPtrValue, PatioDataValue, PatioIntValue},
- },
- patio_generated::{
- self,
- patio::{PatioDataLayoutValue, PatioDataValueValue, PatioIntValueValue, PatioTypeValue},
- },
-};
-
-// == Compilation unit ==
-
-impl From<patio_generated::patio::PatioCompilationUnit<'_>> for PatioCompilationUnit {
- fn from(value: patio_generated::patio::PatioCompilationUnit<'_>) -> Self {
- Self {
- elements: value.elements().iter().map(Into::into).collect(),
- }
- }
-}
-
-// == Compilation unit elements ==
-
-impl From<patio_generated::patio::PatioCompilationUnitElement<'_>> for PatioCompilationUnitElement {
- fn from(value: patio_generated::patio::PatioCompilationUnitElement<'_>) -> Self {
- match value.value_type() {
- patio_generated::patio::PatioCompilationUnitElementValue::PatioDataElementImmutableDefinition => {
- let data_element_immutable_definition = value.value_as_patio_data_element_immutable_definition().unwrap();
-
- Self::DataElementImmutableDefinition(PatioDataElementImmutableDefinition {
- name: data_element_immutable_definition.name().to_string(),
- visibility: data_element_immutable_definition.visibility().into(),
- value: data_element_immutable_definition.value().into(),
- })
- }
-
- patio_generated::patio::PatioCompilationUnitElementValue::PatioDataElementMutableDeclaration => {
- let data_element_mutable_declaration = value.value_as_patio_data_element_mutable_declaration().unwrap();
-
- Self::DataElementMutableDeclaration(PatioDataElementMutableDeclaration {
- name: data_element_mutable_declaration.name().to_string(),
- layout: data_element_mutable_declaration.layout().into(),
- })
- }
-
- patio_generated::patio::PatioCompilationUnitElementValue::PatioDataElementMutableDefinition => {
- let data_element_mutable_definition = value.value_as_patio_data_element_mutable_definition().unwrap();
-
- Self::DataElementMutableDefinition(PatioDataElementMutableDefinition {
- name: data_element_mutable_definition.name().to_string(),
- visibility: data_element_mutable_definition.visibility().into(),
- initial_value: data_element_mutable_definition.initial_value().into(),
- })
- }
-
- patio_generated::patio::PatioCompilationUnitElementValue::PatioFunctionDeclaration => {
- let function_declaration = value.value_as_patio_function_declaration().unwrap();
-
- Self::FunctionDeclaration(PatioFunctionDeclaration {
- name: function_declaration.name().to_string(),
- params: function_declaration
- .params()
- .iter()
- .map(Into::into)
- .collect(),
- return_types: function_declaration.return_types().iter().map(Into::into).collect(),
- })
- }
-
- patio_generated::patio::PatioCompilationUnitElementValue::PatioFunctionDefinition => {
- let function_definition = value.value_as_patio_function_definition().unwrap();
-
- Self::FunctionDefinition(PatioFunctionDefinition {
- name: function_definition.name().to_string(),
- visibility: function_definition.visibility().into(),
- locals: function_definition
- .locals()
- .iter()
- .map(Into::into)
- .collect(),
- return_types: function_definition.return_types().iter().map(Into::into).collect(),
- body: function_definition.body().iter().map(Into::into).collect(),
- })
- }
-
- element_type => todo!(
- "Unhandled compilation unit element type: {:?} {}",
- element_type.variant_name(),
- element_type.0
- ),
- }
- }
-}
-
-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
- ),
- }
- }
-}
-
-impl From<patio_generated::patio::PatioVisibility> for PatioVisibility {
- fn from(value: patio_generated::patio::PatioVisibility) -> Self {
- match value {
- patio_generated::patio::PatioVisibility::PRIVATE => Self::Private,
- patio_generated::patio::PatioVisibility::PUBLIC => Self::Public,
- visibility_type => todo!(
- "Unhandled visibility type: {:?} {}",
- visibility_type.variant_name(),
- visibility_type.0
- ),
- }
- }
-}
-
-// == Instructions ==
-
-impl From<patio_generated::patio::PatioInstruction<'_>> for PatioInstruction {
- fn from(value: patio_generated::patio::PatioInstruction<'_>) -> Self {
- match value.value_type() {
- patio_generated::patio::PatioInstructionValue::PatioBlock => {
- let block = value.value_as_patio_block().unwrap();
-
- Self::Block(block.into())
- }
-
- patio_generated::patio::PatioInstructionValue::PatioBoolToInt => {
- let bool_to_int = value.value_as_patio_bool_to_int().unwrap();
-
- Self::BoolToInt(PatioBoolToInt {
- destination: bool_to_int.destination().into(),
- value: bool_to_int.value().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioCall => {
- let call = value.value_as_patio_call().unwrap();
-
- Self::Call(PatioCall {
- destinations: call.destinations().iter().map(Into::into).collect(),
- function_name: call.function_name().into(),
- args: call.args().iter().map(Into::into).collect(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioCallPointer => {
- let call_pointer = value.value_as_patio_call_pointer().unwrap();
-
- Self::CallPointer(PatioCallPointer {
- destinations: call_pointer.destinations().iter().map(Into::into).collect(),
- function_pointer: call_pointer.function_pointer().into(),
- args: call_pointer.args().iter().map(Into::into).collect(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioDataPtrToInt => {
- let data_ptr_to_int = value.value_as_patio_data_ptr_to_int().unwrap();
-
- Self::DataPtrToInt(PatioDataPtrToInt {
- destination: data_ptr_to_int.destination().into(),
- value: data_ptr_to_int.value().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioIf => {
- let if_ = value.value_as_patio_if().unwrap();
-
- Self::If(PatioIf {
- destinations: if_.destinations().iter().map(Into::into).collect(),
- condition: if_.condition().into(),
- true_body: if_.true_body().into(),
- false_body: if_.false_body().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioIntAdd => {
- let int_add = value.value_as_patio_int_add().unwrap();
-
- Self::IntAdd(PatioIntAdd {
- destination: int_add.destination().into(),
- left: int_add.left().into(),
- right: int_add.right().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioIntEquals => {
- let int_add = value.value_as_patio_int_equals().unwrap();
-
- Self::IntEquals(PatioIntEquals {
- destination: int_add.destination().into(),
- left: int_add.left().into(),
- right: int_add.right().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioIntNotEqual => {
- let int_not_equal = value.value_as_patio_int_not_equal().unwrap();
-
- Self::IntNotEqual(PatioIntNotEqual {
- destination: int_not_equal.destination().into(),
- left: int_not_equal.left().into(),
- right: int_not_equal.right().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioIntSub => {
- let int_sub = value.value_as_patio_int_sub().unwrap();
-
- Self::IntSub(PatioIntSub {
- destination: int_sub.destination().into(),
- left: int_sub.left().into(),
- right: int_sub.right().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioIntToBool => {
- let int_to_bool = value.value_as_patio_int_to_bool().unwrap();
-
- Self::IntToBool(PatioIntToBool {
- destination: int_to_bool.destination().into(),
- value: int_to_bool.value().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioIntToDataPtr => {
- let int_to_data_ptr = value.value_as_patio_int_to_data_ptr().unwrap();
-
- Self::IntToDataPtr(PatioIntToDataPtr {
- destination: int_to_data_ptr.destination().into(),
- value: int_to_data_ptr.value().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioLocalStore => {
- let local_store = value.value_as_patio_local_store().unwrap();
-
- Self::LocalStore(PatioLocalStore {
- destination: local_store.destination().into(),
- value: local_store.value().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioLoop => {
- let loop_ = value.value_as_patio_loop().unwrap();
-
- Self::Loop(PatioLoop {
- body: loop_.body().iter().map(Into::into).collect(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioLoopBreak => {
- Self::LoopBreak(PatioLoopBreak)
- }
-
- patio_generated::patio::PatioInstructionValue::PatioLoopContinue => {
- Self::LoopContinue(PatioLoopContinue)
- }
-
- patio_generated::patio::PatioInstructionValue::PatioMemoryLoad => {
- let memory_load = value.value_as_patio_memory_load().unwrap();
-
- Self::MemoryLoad(PatioMemoryLoad {
- destination: memory_load.destination().into(),
- pointer: memory_load.pointer().into(),
- layout: memory_load.layout().into(),
- element_offsets: memory_load.element_offsets().iter().collect(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioMemoryStore => {
- let memory_store = value.value_as_patio_memory_store().unwrap();
-
- Self::MemoryStore(PatioMemoryStore {
- pointer: memory_store.pointer().into(),
- layout: memory_store.layout().into(),
- element_offsets: memory_store.element_offsets().iter().collect(),
- value: memory_store.value().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioReturn => {
- let return_ = value.value_as_patio_return().unwrap();
-
- Self::Return(PatioReturn {
- values: return_.values().iter().map(Into::into).collect(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioSwitch => {
- let switch = value.value_as_patio_switch().unwrap();
-
- Self::Switch(PatioSwitch {
- destinations: switch.destinations().iter().map(Into::into).collect(),
- value: switch.value().into(),
- cases: switch.cases().iter().map(Into::into).collect(),
- default_body: switch.default_body().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioUnreachable => {
- Self::Unreachable(PatioUnreachable)
- }
-
- patio_generated::patio::PatioInstructionValue::PatioVarLoad => {
- let var_load = value.value_as_patio_var_load().unwrap();
-
- Self::VarLoad(PatioVarLoad {
- destination: var_load.destination().into(),
- var: var_load.var().into(),
- })
- }
-
- patio_generated::patio::PatioInstructionValue::PatioVarStore => {
- let var_store = value.value_as_patio_var_store().unwrap();
-
- Self::VarStore(PatioVarStore {
- var: var_store.var().into(),
- value: var_store.value().into(),
- })
- }
-
- instruction_type => todo!(
- "Unhandled instruction type: {:?} {}",
- instruction_type.variant_name(),
- instruction_type.0
- ),
- }
- }
-}
-
-impl From<patio_generated::patio::PatioBlock<'_>> for PatioBlock {
- fn from(value: patio_generated::patio::PatioBlock<'_>) -> Self {
- Self {
- body: value.body().iter().map(Into::into).collect(),
- results: value.results().iter().map(Into::into).collect(),
- }
- }
-}
-
-impl From<patio_generated::patio::PatioSwitchCase<'_>> for PatioSwitchCase {
- fn from(value: patio_generated::patio::PatioSwitchCase<'_>) -> Self {
- Self {
- value: value.value(),
- body: value.body().into(),
- }
- }
-}
-
-// == Operands ==
-
-impl From<patio_generated::patio::PatioOperand<'_>> for PatioOperand {
- fn from(value: patio_generated::patio::PatioOperand<'_>) -> Self {
- match value.value_type() {
- patio_generated::patio::PatioOperandValue::PatioBoolConst => {
- let bool_const = value.value_as_patio_bool_const().unwrap();
- Self::BoolConst(bool_const.value())
- }
-
- patio_generated::patio::PatioOperandValue::PatioByteConst => {
- let byte_const = value.value_as_patio_byte_const().unwrap();
- Self::ByteConst(byte_const.value().try_into().unwrap())
- }
-
- patio_generated::patio::PatioOperandValue::PatioDataPtrConst => {
- let data_ptr_const = value.value_as_patio_data_ptr_const().unwrap();
- Self::DataPtrConst(data_ptr_const.value().into())
- }
-
- patio_generated::patio::PatioOperandValue::PatioFunctionPtrConst => {
- let function_ptr_const = value.value_as_patio_function_ptr_const().unwrap();
- Self::FunctionPtrConst(function_ptr_const.name().to_string())
- }
-
- patio_generated::patio::PatioOperandValue::PatioIntConst => {
- let int_const = value.value_as_patio_int_const().unwrap();
- Self::IntConst(int_const.value().into())
- }
-
- patio_generated::patio::PatioOperandValue::PatioInt32Const => {
- let int32_const = value.value_as_patio_int_32_const().unwrap();
- Self::Int32Const(int32_const.value().into())
- }
-
- patio_generated::patio::PatioOperandValue::PatioLocalRef => {
- let local_ref = value.value_as_patio_local_ref().unwrap();
- Self::LocalRef(local_ref.into())
- }
-
- operand_type => todo!(
- "Unhandled operand type: {:?} {}",
- operand_type.variant_name(),
- operand_type.0
- ),
- }
- }
-}
-
-impl From<patio_generated::patio::PatioLocalRef<'_>> for PatioLocalRef {
- fn from(value: patio_generated::patio::PatioLocalRef<'_>) -> Self {
- Self {
- name: value.name().to_string(),
- }
- }
-}
-
-// == Data ptr values ==
-
-impl From<patio_generated::patio::PatioDataPtrValue<'_>> for PatioDataPtrValue {
- fn from(value: patio_generated::patio::PatioDataPtrValue<'_>) -> Self {
- match value.value_type() {
- patio_generated::patio::PatioDataPtrValueValue::PatioDataPtrValueDataElement => {
- let data_element = value.value_as_patio_data_ptr_value_data_element().unwrap();
- Self::DataElement {
- name: data_element.name().to_string(),
- }
- }
-
- patio_generated::patio::PatioDataPtrValueValue::PatioDataPtrValueNull => Self::Null,
-
- data_ptr_value_type => todo!(
- "Unhandled data ptr value type: {:?} {}",
- data_ptr_value_type.variant_name(),
- data_ptr_value_type.0
- ),
- }
- }
-}
-
-// == Int values ==
-
-impl From<patio_generated::patio::PatioIntValue<'_>> for PatioIntValue {
- fn from(value: patio_generated::patio::PatioIntValue<'_>) -> Self {
- match value.value_type() {
- PatioIntValueValue::PatioIntValueByteOffsetOf => {
- let byte_offset_of = value.value_as_patio_int_value_byte_offset_of().unwrap();
- Self::ByteOffsetOf {
- layout: byte_offset_of.layout().into(),
- element_offsets: byte_offset_of.element_offsets().iter().collect(),
- }
- }
-
- PatioIntValueValue::PatioIntValueByteSizeOf => {
- let byte_size_of = value.value_as_patio_int_value_byte_size_of().unwrap();
- Self::ByteSizeOf {
- layout: byte_size_of.layout().into(),
- }
- }
-
- PatioIntValueValue::PatioIntValueLiteral => {
- let literal = value.value_as_patio_int_value_literal().unwrap();
- Self::Literal {
- value: literal.value(),
- }
- }
-
- int_value_type => todo!(
- "Unhandled int value type: {:?} {}",
- int_value_type.variant_name(),
- int_value_type.0
- ),
- }
- }
-}
-
-// == Types ==
-
-impl From<patio_generated::patio::PatioType<'_>> for PatioType {
- fn from(value: patio_generated::patio::PatioType<'_>) -> Self {
- match value.value_type() {
- PatioTypeValue::PatioBoolType => Self::Bool,
- PatioTypeValue::PatioByteType => Self::Byte,
- PatioTypeValue::PatioDataPtrType => Self::DataPtr,
- PatioTypeValue::PatioFunctionPtrType => Self::FunctionPtr,
- PatioTypeValue::PatioIntType => Self::Int,
- PatioTypeValue::PatioInt32Type => Self::Int32,
-
- type_type => {
- todo!(
- "Unhandled type type: {:?} {}",
- type_type.variant_name(),
- type_type.0
- )
- }
- }
- }
-}
-
-// == Data layouts ==
-
-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(array.element().into(), 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_(
- struct_
- .elements()
- .iter()
- .map(|element| element.into())
- .collect(),
- )
- }
-
- layout_type => {
- todo!(
- "Unhandled data layout type: {:?} {}",
- layout_type.variant_name(),
- layout_type.0
- )
- }
- }
- }
-}
-
-// == Data values ==
-
-impl From<patio_generated::patio::PatioDataValue<'_>> for PatioDataValue {
- fn from(value: patio_generated::patio::PatioDataValue<'_>) -> Self {
- match value.value_type() {
- PatioDataValueValue::PatioDataValueArray => {
- let array = value.value_as_patio_data_value_array().unwrap();
- Self::Array {
- element_layout: array.element_layout().into(),
- elements: array.elements().iter().map(Into::into).collect(),
- }
- }
-
- PatioDataValueValue::PatioDataValueBool => {
- let bool = value.value_as_patio_data_value_bool().unwrap();
- Self::Bool(bool.value())
- }
-
- PatioDataValueValue::PatioDataValueByte => {
- let byte = value.value_as_patio_data_value_byte().unwrap();
- Self::Byte(byte.value().try_into().unwrap())
- }
-
- PatioDataValueValue::PatioDataValueDataPtr => {
- let data_ptr = value.value_as_patio_data_value_data_ptr().unwrap();
- Self::DataPtr(data_ptr.value().into())
- }
-
- PatioDataValueValue::PatioDataValueInt => {
- let int = value.value_as_patio_data_value_int().unwrap();
- Self::Int(int.value().into())
- }
-
- PatioDataValueValue::PatioDataValueInt32 => {
- let int32 = value.value_as_patio_data_value_int_32().unwrap();
- Self::Int32(int32.value().into())
- }
-
- PatioDataValueValue::PatioDataValueStruct => {
- let struct_ = value.value_as_patio_data_value_struct().unwrap();
- Self::Struct {
- elements: struct_.elements().iter().map(Into::into).collect(),
- }
- }
-
- value_type => {
- todo!(
- "Unhandled data value type: {:?} {}",
- value_type.variant_name(),
- value_type.0
- )
- }
- }
- }
-}
diff --git a/patio-to-cranelift/src/patio/instructions.rs b/patio-to-cranelift/src/patio/instructions.rs
deleted file mode 100644
index bdfcc28e..00000000
--- a/patio-to-cranelift/src/patio/instructions.rs
+++ /dev/null
@@ -1,153 +0,0 @@
-use crate::patio::{
- operands::{PatioLocalRef, PatioOperand},
- types::PatioDataLayout,
-};
-
-pub(crate) enum PatioInstruction {
- Block(PatioBlock),
- BoolToInt(PatioBoolToInt),
- Call(PatioCall),
- CallPointer(PatioCallPointer),
- DataPtrToInt(PatioDataPtrToInt),
- If(PatioIf),
- IntAdd(PatioIntAdd),
- IntEquals(PatioIntEquals),
- IntNotEqual(PatioIntNotEqual),
- IntSub(PatioIntSub),
- IntToBool(PatioIntToBool),
- IntToDataPtr(PatioIntToDataPtr),
- LocalStore(PatioLocalStore),
- Loop(PatioLoop),
- LoopBreak(PatioLoopBreak),
- LoopContinue(PatioLoopContinue),
- MemoryLoad(PatioMemoryLoad),
- MemoryStore(PatioMemoryStore),
- Return(PatioReturn),
- Switch(PatioSwitch),
- Unreachable(PatioUnreachable),
- VarLoad(PatioVarLoad),
- VarStore(PatioVarStore),
-}
-
-pub(crate) struct PatioBlock {
- pub(crate) body: Vec<PatioInstruction>,
- pub(crate) results: Vec<PatioOperand>,
-}
-
-pub(crate) struct PatioBoolToInt {
- pub(crate) destination: PatioLocalRef,
- pub(crate) value: PatioOperand,
-}
-
-pub(crate) struct PatioCall {
- pub(crate) destinations: Vec<PatioLocalRef>,
- pub(crate) function_name: String,
- pub(crate) args: Vec<PatioOperand>,
-}
-
-pub(crate) struct PatioCallPointer {
- pub(crate) destinations: Vec<PatioLocalRef>,
- pub(crate) function_pointer: PatioOperand,
- pub(crate) args: Vec<PatioOperand>,
-}
-
-pub(crate) struct PatioDataPtrToInt {
- pub(crate) destination: PatioLocalRef,
- pub(crate) value: PatioOperand,
-}
-
-pub(crate) struct PatioIf {
- pub(crate) destinations: Vec<PatioLocalRef>,
- pub(crate) condition: PatioOperand,
- pub(crate) true_body: PatioBlock,
- pub(crate) false_body: PatioBlock,
-}
-
-pub(crate) struct PatioIntAdd {
- pub(crate) destination: PatioLocalRef,
- pub(crate) left: PatioOperand,
- pub(crate) right: PatioOperand,
-}
-
-pub(crate) struct PatioIntEquals {
- pub(crate) destination: PatioLocalRef,
- pub(crate) left: PatioOperand,
- pub(crate) right: PatioOperand,
-}
-
-pub(crate) struct PatioIntNotEqual {
- pub(crate) destination: PatioLocalRef,
- pub(crate) left: PatioOperand,
- pub(crate) right: PatioOperand,
-}
-
-pub(crate) struct PatioIntSub {
- pub(crate) destination: PatioLocalRef,
- pub(crate) left: PatioOperand,
- pub(crate) right: PatioOperand,
-}
-
-pub(crate) struct PatioIntToBool {
- pub(crate) destination: PatioLocalRef,
- pub(crate) value: PatioOperand,
-}
-
-pub(crate) struct PatioIntToDataPtr {
- pub(crate) destination: PatioLocalRef,
- pub(crate) value: PatioOperand,
-}
-
-pub(crate) struct PatioLocalStore {
- pub(crate) destination: PatioLocalRef,
- pub(crate) value: PatioOperand,
-}
-
-pub(crate) struct PatioLoop {
- pub(crate) body: Vec<PatioInstruction>,
-}
-
-pub(crate) struct PatioLoopBreak;
-
-pub(crate) struct PatioLoopContinue;
-
-pub(crate) struct PatioMemoryLoad {
- pub(crate) destination: PatioLocalRef,
- pub(crate) pointer: PatioOperand,
- pub(crate) layout: PatioDataLayout,
- pub(crate) element_offsets: Vec<u32>,
-}
-
-pub(crate) struct PatioMemoryStore {
- pub(crate) pointer: PatioOperand,
- pub(crate) layout: PatioDataLayout,
- pub(crate) element_offsets: Vec<u32>,
- pub(crate) value: PatioOperand,
-}
-
-pub(crate) struct PatioReturn {
- pub(crate) values: Vec<PatioOperand>,
-}
-
-pub(crate) struct PatioSwitch {
- pub(crate) destinations: Vec<PatioLocalRef>,
- pub(crate) value: PatioOperand,
- pub(crate) cases: Vec<PatioSwitchCase>,
- pub(crate) default_body: PatioBlock,
-}
-
-pub(crate) struct PatioSwitchCase {
- pub(crate) value: u32,
- pub(crate) body: PatioBlock,
-}
-
-pub(crate) struct PatioUnreachable;
-
-pub(crate) struct PatioVarLoad {
- pub(crate) destination: PatioLocalRef,
- pub(crate) var: PatioLocalRef,
-}
-
-pub(crate) struct PatioVarStore {
- pub(crate) var: PatioLocalRef,
- pub(crate) value: PatioOperand,
-}
diff --git a/patio-to-cranelift/src/patio/mod.rs b/patio-to-cranelift/src/patio/mod.rs
index 4316b519..3027cda4 100644
--- a/patio-to-cranelift/src/patio/mod.rs
+++ b/patio-to-cranelift/src/patio/mod.rs
@@ -1,6 +1,67 @@
-pub(crate) mod compilation_unit;
-pub(crate) mod flatbuffers;
-pub(crate) mod instructions;
-pub(crate) mod operands;
-pub(crate) mod types;
-pub(crate) mod values;
+include!("../gen/patio.rs");
+
+pub(crate) mod instructions {
+ include!("../gen/patio/instructions.rs");
+
+ #[allow(unused)]
+ pub(crate) mod transient_0 {
+ include!("../gen/patio/instructions/transient_0.rs");
+ }
+}
+
+pub(crate) mod operands {
+ include!("../gen/patio/operands.rs");
+
+ #[allow(unused)]
+ pub(crate) mod transient_0 {
+ include!("../gen/patio/operands/transient_0.rs");
+ }
+}
+
+pub(crate) mod types {
+ include!("../gen/patio/types.rs");
+
+ impl PatioDataValue {
+ pub(crate) fn layout(&self) -> PatioDataLayout {
+ match self {
+ PatioDataValue::PatioDataValueArray(array) => PatioDataLayoutArray {
+ element: array.element_layout.clone(),
+ size: array.elements.len().try_into().unwrap(),
+ }
+ .into(),
+ PatioDataValue::PatioDataValueBool(_) => PatioDataLayoutBool.into(),
+ PatioDataValue::PatioDataValueByte(_) => PatioDataLayoutByte.into(),
+ PatioDataValue::PatioDataValueDataPtr(_) => PatioDataLayoutDataPtr.into(),
+ PatioDataValue::PatioDataValueInt(_) => PatioDataLayoutInt.into(),
+ PatioDataValue::PatioDataValueInt32(_) => PatioDataLayoutInt32.into(),
+ PatioDataValue::PatioDataValueStruct(value_struct) => PatioDataLayoutStruct {
+ elements: value_struct
+ .elements
+ .iter()
+ .map(|element| element.layout())
+ .collect(),
+ }
+ .into(),
+ }
+ }
+ }
+
+ #[allow(unused)]
+ pub(crate) mod transient_0 {
+ include!("../gen/patio/types/transient_0.rs");
+ }
+}
+
+pub(crate) mod values {
+ include!("../gen/patio/values.rs");
+
+ #[allow(unused)]
+ pub(crate) mod transient_0 {
+ include!("../gen/patio/values/transient_0.rs");
+ }
+}
+
+#[allow(unused)]
+pub(crate) mod transient_0 {
+ include!("../gen/patio/transient_0.rs");
+}
diff --git a/patio-to-cranelift/src/patio/operands.rs b/patio-to-cranelift/src/patio/operands.rs
deleted file mode 100644
index ca8eb3bc..00000000
--- a/patio-to-cranelift/src/patio/operands.rs
+++ /dev/null
@@ -1,17 +0,0 @@
-use crate::patio::values::{PatioDataPtrValue, PatioIntValue};
-
-#[derive(Debug)]
-pub(crate) enum PatioOperand {
- BoolConst(bool),
- ByteConst(u8),
- DataPtrConst(PatioDataPtrValue),
- FunctionPtrConst(String),
- IntConst(PatioIntValue),
- Int32Const(PatioIntValue),
- LocalRef(PatioLocalRef),
-}
-
-#[derive(Debug)]
-pub(crate) struct PatioLocalRef {
- pub(crate) name: String,
-}
diff --git a/patio-to-cranelift/src/patio/types.rs b/patio-to-cranelift/src/patio/types.rs
deleted file mode 100644
index 49bce09a..00000000
--- a/patio-to-cranelift/src/patio/types.rs
+++ /dev/null
@@ -1,39 +0,0 @@
-#[derive(Clone, Copy, Eq, PartialEq)]
-pub(crate) enum PatioType {
- Bool,
- Byte,
- DataPtr,
- FunctionPtr,
- Int,
- Int32,
-}
-
-#[derive(Clone, Debug, PartialEq)]
-pub(crate) enum PatioDataLayout {
- Array {
- element: Box<PatioDataLayout>,
- size: u32,
- },
- Bool,
- Byte,
- DataPtr,
- FunctionPtr,
- Int,
- Int32,
- Struct {
- elements: Vec<PatioDataLayout>,
- },
-}
-
-impl PatioDataLayout {
- pub(crate) fn array(element: PatioDataLayout, size: u32) -> Self {
- Self::Array {
- element: Box::new(element),
- size,
- }
- }
-
- pub(crate) fn struct_(elements: Vec<PatioDataLayout>) -> Self {
- Self::Struct { elements }
- }
-}
diff --git a/patio-to-cranelift/src/patio/values.rs b/patio-to-cranelift/src/patio/values.rs
deleted file mode 100644
index fe511437..00000000
--- a/patio-to-cranelift/src/patio/values.rs
+++ /dev/null
@@ -1,55 +0,0 @@
-use crate::patio::types::PatioDataLayout;
-
-pub(crate) enum PatioDataValue {
- Array {
- element_layout: PatioDataLayout,
- elements: Vec<PatioDataValue>,
- },
- Bool(bool),
- Byte(u8),
- DataPtr(PatioDataPtrValue),
- Int(PatioIntValue),
- Int32(PatioIntValue),
- Struct {
- elements: Vec<PatioDataValue>,
- },
-}
-
-impl PatioDataValue {
- pub(crate) fn layout(&self) -> PatioDataLayout {
- match self {
- PatioDataValue::Array {
- element_layout,
- elements,
- } => PatioDataLayout::array(element_layout.clone(), elements.len().try_into().unwrap()),
- PatioDataValue::Bool(_) => PatioDataLayout::Bool,
- PatioDataValue::Byte(_) => PatioDataLayout::Byte,
- PatioDataValue::DataPtr(_) => PatioDataLayout::DataPtr,
- PatioDataValue::Int(_) => PatioDataLayout::Int,
- PatioDataValue::Int32(_) => PatioDataLayout::Int32,
- PatioDataValue::Struct { elements } => {
- PatioDataLayout::struct_(elements.iter().map(|element| element.layout()).collect())
- }
- }
- }
-}
-
-#[derive(Debug)]
-pub(crate) enum PatioDataPtrValue {
- DataElement { name: String },
- Null,
-}
-
-#[derive(Debug)]
-pub(crate) enum PatioIntValue {
- ByteOffsetOf {
- layout: PatioDataLayout,
- element_offsets: Vec<u32>,
- },
- ByteSizeOf {
- layout: PatioDataLayout,
- },
- Literal {
- value: i32,
- },
-}