Skip to content

Commit

Permalink
clean accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Oct 19, 2024
1 parent cb66cd4 commit dbd8734
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Skip generating `.add(0)` and `1 *` in accessors
- Bump MSRV of generated code to 1.76
- move `must_use` from methods to generic type

Expand Down
22 changes: 10 additions & 12 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
doc,
name,
ty,
offset: unsuffixed(info.address_offset),
offset: info.address_offset,
})
.raw_if(false);
cluster_expanded.push(RegisterBlockField {
Expand Down Expand Up @@ -1057,9 +1057,9 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
doc,
name: accessor_name.clone(),
ty: ty.clone(),
offset: unsuffixed(info.address_offset),
dim: unsuffixed(array_info.dim),
increment: unsuffixed(array_info.dim_increment),
offset: info.address_offset,
dim: array_info.dim,
increment: array_info.dim_increment,
})
.raw_if(!array_convertible),
);
Expand All @@ -1071,7 +1071,6 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
ci.address_offset,
ci.description.as_deref().unwrap_or(&ci.name),
);
let i = unsuffixed(i as u64);
accessors.push(
Accessor::ArrayElem(ArrayElemAccessor {
doc,
Expand Down Expand Up @@ -1114,7 +1113,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
doc,
name,
ty: ty.clone(),
offset: unsuffixed(info.address_offset),
offset: info.address_offset,
})
.raw_if(false);
cluster_expanded.push(RegisterBlockField {
Expand Down Expand Up @@ -1166,7 +1165,7 @@ fn expand_register(
doc,
name,
ty,
offset: unsuffixed(info.address_offset),
offset: info.address_offset,
})
.raw_if(false);
register_expanded.push(RegisterBlockField {
Expand Down Expand Up @@ -1240,9 +1239,9 @@ fn expand_register(
doc,
name: accessor_name.clone(),
ty: ty.clone(),
offset: unsuffixed(info.address_offset),
dim: unsuffixed(array_info.dim),
increment: unsuffixed(array_info.dim_increment),
offset: info.address_offset,
dim: array_info.dim,
increment: array_info.dim_increment,
})
.raw_if(!array_convertible),
);
Expand All @@ -1259,7 +1258,6 @@ fn expand_register(
ri.address_offset,
ri.description.as_deref().unwrap_or(&ri.name),
);
let i = unsuffixed(i as u64);
accessors.push(
Accessor::ArrayElem(ArrayElemAccessor {
doc,
Expand Down Expand Up @@ -1302,7 +1300,7 @@ fn expand_register(
doc,
name,
ty: ty.clone(),
offset: unsuffixed(info.address_offset),
offset: info.address_offset,
})
.raw_if(false);
register_expanded.push(RegisterBlockField {
Expand Down
21 changes: 14 additions & 7 deletions src/generate/peripheral/accessor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};

use crate::util::unsuffixed;

#[derive(Clone, Debug)]
pub enum Accessor {
Reg(RegAccessor),
Expand Down Expand Up @@ -51,11 +53,12 @@ impl ToTokens for AccessType {
ty,
offset,
})) => {
let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o)));
quote! {
#[doc = #doc]
#[inline(always)]
pub const fn #name(&self) -> &#ty {
unsafe { &*core::ptr::from_ref(self).cast::<u8>().add(#offset).cast() }
unsafe { &*core::ptr::from_ref(self).cast::<u8>() #offset .cast() }
}
}
}
Expand Down Expand Up @@ -84,7 +87,10 @@ impl ToTokens for AccessType {
increment,
})) => {
let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site());
let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::<u8>().add(#offset).add(#increment * n).cast() } };
let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o)));
let dim = unsuffixed(*dim);
let increment = (*increment != 1).then(|| unsuffixed(*increment)).map(|i| quote!(#i *));
let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::<u8>() #offset .add(#increment n).cast() } };
quote! {
#[doc = #doc]
#[inline(always)]
Expand All @@ -109,6 +115,7 @@ impl ToTokens for AccessType {
basename,
i,
} = elem;
let i = unsuffixed(*i as u64);
quote! {
#[doc = #doc]
#[inline(always)]
Expand All @@ -127,17 +134,17 @@ pub struct RegAccessor {
pub doc: String,
pub name: Ident,
pub ty: syn::Type,
pub offset: syn::LitInt,
pub offset: u32,
}

#[derive(Clone, Debug)]
pub struct ArrayAccessor {
pub doc: String,
pub name: Ident,
pub ty: syn::Type,
pub offset: syn::LitInt,
pub dim: syn::LitInt,
pub increment: syn::LitInt,
pub offset: u32,
pub dim: u32,
pub increment: u32,
}

#[derive(Clone, Debug)]
Expand All @@ -146,5 +153,5 @@ pub struct ArrayElemAccessor {
pub name: Ident,
pub ty: syn::Type,
pub basename: Ident,
pub i: syn::LitInt,
pub i: usize,
}

0 comments on commit dbd8734

Please sign in to comment.