Skip to content

Commit b45bdce

Browse files
bors[bot]newAM
andauthored
Merge #554
554: Generate safe bits writers when possible r=burrbull a=newAM The 'bits' method of register writers can be safe if: * there is a single field that covers the entire register * that field can represent all values Co-authored-by: Alex Martens <alexmgit@protonmail.com>
2 parents 877196f + 4d6bcd4 commit b45bdce

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3131
- Bumped `xtensa-lx` and add `xtensa_lx::interrupt::InterruptNumber` implementation.
3232
- Don't use a mask when the width of the mask is the same as the width of the parent register.
3333
- Improved error handling
34+
- Registers with single fields that span the entire register now generate safe `bits` writers.
3435

3536
## [v0.19.0] - 2021-05-26
3637

src/generate/register.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,40 @@ pub fn render(
168168

169169
mod_items.extend(w_impl_items);
170170

171-
mod_items.extend(quote! {
172-
#[doc = "Writes raw bits to the register."]
173-
#[inline(always)]
174-
pub unsafe fn bits(&mut self, bits: #rty) -> &mut Self {
175-
self.0.bits(bits);
176-
self
171+
// the writer can be safe if:
172+
// * there is a single field that covers the entire register
173+
// * that field can represent all values
174+
let can_write_safe = match register
175+
.fields
176+
.as_ref()
177+
.and_then(|fields| fields.iter().next())
178+
.and_then(|field| field.write_constraint)
179+
{
180+
Some(WriteConstraint::Range(range)) => {
181+
range.min == 0 && range.max == u64::MAX >> (64 - rsize)
177182
}
178-
});
183+
_ => false,
184+
};
185+
186+
if can_write_safe {
187+
mod_items.extend(quote! {
188+
#[doc = "Writes raw bits to the register."]
189+
#[inline(always)]
190+
pub fn bits(&mut self, bits: #rty) -> &mut Self {
191+
unsafe { self.0.bits(bits) };
192+
self
193+
}
194+
});
195+
} else {
196+
mod_items.extend(quote! {
197+
#[doc = "Writes raw bits to the register."]
198+
#[inline(always)]
199+
pub unsafe fn bits(&mut self, bits: #rty) -> &mut Self {
200+
self.0.bits(bits);
201+
self
202+
}
203+
});
204+
}
179205

180206
close.to_tokens(&mut mod_items);
181207
}

0 commit comments

Comments
 (0)