Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ __pycache__
# IDEs
.vscode


# Rust / Cargo
**/rust/Cargo.lock
**/rust/target/
28 changes: 24 additions & 4 deletions compiler/back_end/experimental/rust/emboss_codegen_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ def _generate_expression(
expr, ir, module, generated_fields, templates, self_ref="self"
):
if ir_util.is_constant(expr):
return str(ir_util.constant_value(expr))
val = ir_util.constant_value(expr)
if isinstance(val, bool):
return "true" if val else "false"
return str(val)

if expr.has_field("boolean_constant"):
return "true" if expr.boolean_constant.value else "false"
Expand Down Expand Up @@ -315,9 +318,12 @@ def _generate_expression(
templates.expr_multiplication, left=args[0], right=args[1]
)
if func == ir_data.FunctionMapping.MAXIMUM.value:
return code_template.format_template(
templates.expr_maximum, left=args[0], right=args[1]
)
res = args[0]
for a in args[1:]:
res = code_template.format_template(
templates.expr_maximum, left=res, right=a
)
return res
if func == ir_data.FunctionMapping.CHOICE.value:
return code_template.format_template(
templates.expr_choice,
Expand Down Expand Up @@ -907,11 +913,25 @@ def _generate_struct(type_ir, ir, module, templates, diagnostics, struct_name) -

generated_fields.add(field_name)

# Extract the frontend-synthesized $size_in_bytes expression tree to compute the
# dynamic layout completeness of the structure in CheckComplete::check_complete().
size_in_bytes = "0"
for field in type_ir.structure.field:
if field.name.name.text == "$size_in_bytes":
size_in_bytes = (
_generate_expression(
field.read_transform, ir, module, generated_fields, templates
)
or "0"
)
break

main_struct_def = code_template.format_template(
templates.struct_view,
struct_name=struct_name,
field_accessors="".join(field_accessors),
mut_field_accessors="".join(mut_field_accessors),
size_in_bytes=size_in_bytes,
)
return "".join(generated_nested_types) + main_struct_def

Expand Down
106 changes: 81 additions & 25 deletions compiler/back_end/experimental/rust/generated_code_templates
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,93 @@
// Generated by the Emboss compiler. DO NOT EDIT!
#![allow(dead_code, unused_imports, unused_variables, non_camel_case_types, unused_parens)]

use emboss_runtime::prelude::*;
pub use emboss_runtime::{Error, Storage, MutStorage, TryRead, TryWrite, UInt, Int, VirtualField};
pub use emboss_runtime::{Error, Int, MutStorage, Storage, TryRead, TryWrite, UInt, VirtualField};

${imports_list}

${struct_definitions}

// ** struct_view ** //
pub struct ${struct_name}<S: Storage> {
pub struct ${struct_name}<S: emboss_runtime::Storage, ST: emboss_runtime::State = emboss_runtime::UncheckedState> {
storage: S,
_marker: core::marker::PhantomData<ST>,
}

impl<S: Storage> ${struct_name}<S> {
impl<S: emboss_runtime::Storage> ${struct_name}<S, emboss_runtime::UncheckedState> {
pub fn new(storage: S) -> Self {
Self { storage }
Self {
storage,
_marker: core::marker::PhantomData,
}
}
}

impl<S: emboss_runtime::Storage, ST: emboss_runtime::State> ${struct_name}<S, ST> {
/// Constructs a view in a specific typestate.
///
/// # Safety
///
/// The caller must ensure that `storage` satisfies the layout invariants for `ST`.
pub unsafe fn new_in_state(storage: S) -> Self {
Self {
storage,
_marker: core::marker::PhantomData,
}
}
${field_accessors}
}

pub struct ${struct_name}Mut<S: MutStorage> {
pub struct ${struct_name}Mut<S: emboss_runtime::MutStorage, ST: emboss_runtime::State = emboss_runtime::UncheckedState> {
storage: S,
_marker: core::marker::PhantomData<ST>,
}

impl<S: MutStorage> ${struct_name}Mut<S> {
impl<S: emboss_runtime::MutStorage> ${struct_name}Mut<S, emboss_runtime::UncheckedState> {
pub fn new(storage: S) -> Self {
Self { storage }
Self {
storage,
_marker: core::marker::PhantomData,
}
}
}

impl<S: emboss_runtime::MutStorage, ST: emboss_runtime::State> ${struct_name}Mut<S, ST> {
/// Constructs a mutable view in a specific typestate.
///
/// # Safety
///
/// The caller must ensure that `storage` satisfies the layout invariants for `ST`.
pub unsafe fn new_in_state(storage: S) -> Self {
Self {
storage,
_marker: core::marker::PhantomData,
}
}
${mut_field_accessors}
}

impl<S: emboss_runtime::Storage> emboss_runtime::CheckComplete for ${struct_name}<S, emboss_runtime::UncheckedState> {
type Completed = ${struct_name}<S, emboss_runtime::CompleteState>;
fn check_complete(self) -> core::result::Result<Self::Completed, emboss_runtime::Error> {
let size = (${size_in_bytes}) as usize;
let _ = self.storage.slice(0, size)?;
// SAFETY: self.storage has been validated to contain the complete byte length required by the struct layout.
core::result::Result::Ok(unsafe { ${struct_name}::new_in_state(self.storage) })
}
}
Comment thread
yi-json marked this conversation as resolved.

impl<S: emboss_runtime::MutStorage> emboss_runtime::CheckComplete for ${struct_name}Mut<S, emboss_runtime::UncheckedState> {
type Completed = ${struct_name}Mut<S, emboss_runtime::CompleteState>;
fn check_complete(mut self) -> core::result::Result<Self::Completed, emboss_runtime::Error> {
let size = (${size_in_bytes}) as usize;
let _ = self.storage.slice_mut(0, size)?;
// SAFETY: self.storage has been validated to contain the complete byte length required by the struct layout.
core::result::Result::Ok(unsafe { ${struct_name}Mut::new_in_state(self.storage) })
}
}

// ** bit_external_field_accessor ** //
pub fn ${field_name}(&self) -> emboss_runtime::Bit${type_name}<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>> {
Comment thread
yi-json marked this conversation as resolved.
pub fn ${field_name}(&self) -> emboss_runtime::Bit${type_name}<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>, ST> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = 0;
let len = (${byte_length}) as usize; // Total struct size in bytes
Expand All @@ -57,11 +112,11 @@ ${mut_field_accessors}
core::result::Result::Ok((offset, len)) => self.storage.slice(offset, len),
core::result::Result::Err(e) => core::result::Result::Err(e),
};
emboss_runtime::Bit${type_name}::<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>>::new(storage)
emboss_runtime::Bit${type_name}::<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>, ST>::new(storage)
}

// ** bit_external_mut_field_accessor ** //
pub fn ${field_name}(&mut self) -> emboss_runtime::Bit${type_name}<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>> {
pub fn ${field_name}(&mut self) -> emboss_runtime::Bit${type_name}<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>, ST> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = 0;
let len = (${byte_length}) as usize;
Expand All @@ -71,11 +126,11 @@ ${mut_field_accessors}
core::result::Result::Ok((offset, len)) => self.storage.slice_mut(offset, len),
core::result::Result::Err(e) => core::result::Result::Err(e),
};
emboss_runtime::Bit${type_name}::<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>>::new(storage)
emboss_runtime::Bit${type_name}::<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>, ST>::new(storage)
}

// ** external_field_accessor ** //
pub fn ${field_name}(&self) -> ${type_name}<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>> {
pub fn ${field_name}(&self) -> emboss_runtime::${type_name}<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>, ST> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = (${byte_offset}) as usize;
let len = (${byte_length}) as usize;
Expand All @@ -85,11 +140,11 @@ ${mut_field_accessors}
core::result::Result::Ok((offset, len)) => self.storage.slice(offset, len),
core::result::Result::Err(e) => core::result::Result::Err(e),
};
${type_name}::<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>>::new(storage)
emboss_runtime::${type_name}::<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>, ST>::new(storage)
}

// ** external_mut_field_accessor ** //
pub fn ${field_name}(&mut self) -> ${type_name}<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>> {
pub fn ${field_name}(&mut self) -> emboss_runtime::${type_name}<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>, ST> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = (${byte_offset}) as usize;
let len = (${byte_length}) as usize;
Expand All @@ -99,11 +154,11 @@ ${mut_field_accessors}
core::result::Result::Ok((offset, len)) => self.storage.slice_mut(offset, len),
core::result::Result::Err(e) => core::result::Result::Err(e),
};
${type_name}::<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>>::new(storage)
emboss_runtime::${type_name}::<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>, ST>::new(storage)
}

// ** struct_field_accessor ** //
pub fn ${field_name}(&self) -> ${type_name}<core::result::Result<S::Sliced<'_>, emboss_runtime::Error>> {
pub fn ${field_name}(&self) -> ${type_name}<core::result::Result<S::Sliced<'_>, emboss_runtime::Error>, emboss_runtime::UncheckedState> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = (${byte_offset}) as usize;
let len = (${byte_length}) as usize;
Expand All @@ -113,11 +168,12 @@ ${mut_field_accessors}
core::result::Result::Ok((offset, len)) => self.storage.slice(offset, len),
core::result::Result::Err(e) => core::result::Result::Err(e),
};
${type_name}::new(storage)
// SAFETY: The inner struct view is created in UncheckedState, which requires explicit verification before access.
unsafe { ${type_name}::<core::result::Result<S::Sliced<'_>, emboss_runtime::Error>, emboss_runtime::UncheckedState>::new_in_state(storage) }
}

// ** struct_mut_field_accessor ** //
pub fn ${field_name}(&mut self) -> ${type_name}Mut<core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>> {
pub fn ${field_name}(&mut self) -> ${type_name}Mut<core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>, emboss_runtime::UncheckedState> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = (${byte_offset}) as usize;
let len = (${byte_length}) as usize;
Expand All @@ -127,7 +183,8 @@ ${mut_field_accessors}
core::result::Result::Ok((offset, len)) => self.storage.slice_mut(offset, len),
core::result::Result::Err(e) => core::result::Result::Err(e),
};
${type_name}Mut::new(storage)
// SAFETY: The inner struct view is created in UncheckedState, which requires explicit verification before access.
unsafe { ${type_name}Mut::<core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>, emboss_runtime::UncheckedState>::new_in_state(storage) }
}

// ** enum_definition ** //
Expand Down Expand Up @@ -195,7 +252,7 @@ core::cmp::min((${left}) as i64, (${right}) as i64)


// ** enum_field_accessor ** //
pub fn ${field_name}(&self) -> emboss_runtime::EnumView<${enum_name}, emboss_runtime::UInt<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>>> {
pub fn ${field_name}(&self) -> emboss_runtime::EnumView<${enum_name}, emboss_runtime::UInt<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>, ST>> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = (${byte_offset}) as usize;
let len = (${byte_length}) as usize;
Expand All @@ -209,7 +266,7 @@ core::cmp::min((${left}) as i64, (${right}) as i64)
}

// ** enum_mut_field_accessor ** //
pub fn ${field_name}(&mut self) -> emboss_runtime::EnumViewMut<${enum_name}, emboss_runtime::UInt<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>>> {
pub fn ${field_name}(&mut self) -> emboss_runtime::EnumViewMut<${enum_name}, emboss_runtime::UInt<${bits}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>, ST>> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = (${byte_offset}) as usize;
let len = (${byte_length}) as usize;
Expand Down Expand Up @@ -371,7 +428,7 @@ emboss_runtime::EnumViewMut::new(emboss_runtime::UInt::new(storage))
}

// ** bit_enum_field_accessor ** //
pub fn ${field_name}(&self) -> emboss_runtime::EnumView<${enum_name}, emboss_runtime::BitUInt<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>>> {
pub fn ${field_name}(&self) -> emboss_runtime::EnumView<${enum_name}, emboss_runtime::BitUInt<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::Sliced<'_>, emboss_runtime::Error>, ST>> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = 0;
let len = (${byte_length}) as usize;
Expand All @@ -385,7 +442,7 @@ emboss_runtime::EnumViewMut::new(emboss_runtime::UInt::new(storage))
}

// ** bit_enum_mut_field_accessor ** //
pub fn ${field_name}(&mut self) -> emboss_runtime::EnumViewMut<${enum_name}, emboss_runtime::BitUInt<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>>> {
pub fn ${field_name}(&mut self) -> emboss_runtime::EnumViewMut<${enum_name}, emboss_runtime::BitUInt<${bits}, ${bit_offset}, emboss_runtime::${byte_order}, core::result::Result<S::SlicedMut<'_>, emboss_runtime::Error>, ST>> {
let offset_res = (|| -> core::result::Result<(usize, usize), emboss_runtime::Error> {
let offset = 0;
let len = (${byte_length}) as usize;
Expand All @@ -397,4 +454,3 @@ emboss_runtime::EnumViewMut::new(emboss_runtime::UInt::new(storage))
};
emboss_runtime::EnumViewMut::new(emboss_runtime::BitUInt::new(storage))
}

1 change: 1 addition & 0 deletions runtime/experimental/rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ rust_library(
srcs = [
"src/lib.rs",
"src/prelude.rs",
"src/typestate.rs",
],
)
Loading