Skip to content
Merged
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
23 changes: 12 additions & 11 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,14 @@ pub fn fields(
r_impl_items: &mut TokenStream,
w_impl_items: &mut TokenStream,
) -> Result<()> {
let span = Span::call_site();
let can_read = [Access::ReadOnly, Access::ReadWriteOnce, Access::ReadWrite].contains(&access);
let can_write = access != Access::ReadOnly;

// TODO enumeratedValues
for f in fields.into_iter() {
for f in fields.iter() {
// TODO(AJM) - do we need to do anything with this range type?
let BitRange { offset, width, .. } = f.bit_range;
let span = Span::call_site();
let sc = Ident::new(&f.name.to_sanitized_snake_case(), span);
let pc = f.name.to_sanitized_upper_case();
let bits = Ident::new(if width == 1 { "bit" } else { "bits" }, span);
Expand All @@ -234,18 +237,19 @@ pub fn fields(
description_with_bits.push_str(&description);
}

let can_read = [Access::ReadOnly, Access::ReadWriteOnce, Access::ReadWrite]
.contains(&access)
let can_read = can_read
&& (f.access != Some(Access::WriteOnly))
&& (f.access != Some(Access::WriteOnce));
let can_write = (access != Access::ReadOnly) && (f.access != Some(Access::ReadOnly));
let can_write = can_write && (f.access != Some(Access::ReadOnly));

let mask = 1u64.wrapping_neg() >> (64 - width);
let hexmask = &util::hex(mask);
let offset = u64::from(offset);
let rv = reset_value.map(|rv| (rv >> offset) & mask);
let fty = width.to_ty()?;
let evs = &f.enumerated_values;
let quotedfield = String::from("`") + &f.name + "`";
let readerdoc = String::from("Reader of field ") + &quotedfield;
Comment on lines +251 to +252
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to have just one line for this. Is the performance impact significant compared to format!()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core::fmt::write makes up 17.5% of all CPU cycles used in svd2rust so I'm looking for ways to reduce its usage in addition to trying to have canned code available instead of regenerating everything with every invocation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code is not faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's completely in the noise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about merging into one line? It looks more readable and should be (insignificantly) faster:

let readerdoc = String::from("Reader of field `") + &f.name + "`";

If you prefer to leave things as they are, just bors r= me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have other plans with the quoted field name. ;) Moving out the readerdoc was just to get rid of the three separate definitions.

Still working on figuring out how to make proc_macro2 play ball with respect to being able to pre-generate the TokenTrees/TokenStreams and simply change the field names


let lookup_results = lookup(
evs,
Expand Down Expand Up @@ -298,9 +302,8 @@ pub fn fields(
let base_pc_r = Ident::new(&(pc.clone() + "_A"), span);
derive_from_base(mod_items, &base, &pc_r, &base_pc_r, &description);

let doc = format!("Reader of field `{}`", f.name);
mod_items.extend(quote! {
#[doc = #doc]
#[doc = #readerdoc]
pub type #_pc_r = crate::R<#fty, #pc_r>;
});
} else {
Expand Down Expand Up @@ -381,19 +384,17 @@ pub fn fields(
});
}

let doc = format!("Reader of field `{}`", f.name);
mod_items.extend(quote! {
#[doc = #doc]
#[doc = #readerdoc]
pub type #_pc_r = crate::R<#fty, #pc_r>;
impl #_pc_r {
#enum_items
}
});
}
} else {
let doc = format!("Reader of field `{}`", f.name);
mod_items.extend(quote! {
#[doc = #doc]
#[doc = #readerdoc]
pub type #_pc_r = crate::R<#fty, #fty>;
})
}
Expand Down