Skip to content

Commit 3b57eac

Browse files
committed
Generate safe bits writers when possible
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
1 parent 877196f commit 3b57eac

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
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: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,33 @@ pub fn render(
168168

169169
mod_items.extend(w_impl_items);
170170

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 unsafefn: Option<Ident> = register
175+
.fields
176+
.as_ref()
177+
.map(|fields| fields.iter().next())
178+
.flatten()
179+
.map(|field| field.write_constraint)
180+
.flatten()
181+
.map(|constraint| match constraint {
182+
WriteConstraint::Range(range) => {
183+
if range.min == 0 && range.max == 2_u64.pow(rsize) - 1 {
184+
None
185+
} else {
186+
Some(())
187+
}
188+
}
189+
_ => Some(()),
190+
})
191+
.flatten()
192+
.map(|_| Ident::new("unsafe", span));
193+
171194
mod_items.extend(quote! {
172195
#[doc = "Writes raw bits to the register."]
173196
#[inline(always)]
174-
pub unsafe fn bits(&mut self, bits: #rty) -> &mut Self {
197+
pub #unsafefn fn bits(&mut self, bits: #rty) -> &mut Self {
175198
self.0.bits(bits);
176199
self
177200
}

0 commit comments

Comments
 (0)