Skip to content

Commit c6daaee

Browse files
committed
skip derived
1 parent c0df912 commit c6daaee

File tree

4 files changed

+32
-39
lines changed

4 files changed

+32
-39
lines changed

src/patch/device.rs

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,19 @@ use yaml_rust::{yaml::Hash, Yaml};
44

55
use std::{fs::File, io::Read, path::Path};
66

7+
use super::iterators::{MatchIter, Matched};
78
use super::modify_register_properties;
89
use super::peripheral::PeripheralExt;
910
use super::yaml_ext::{AsType, GetVal};
1011
use super::{abspath, matchname, PatchResult, VAL_LVL};
1112
use super::{make_address_block, make_address_blocks, make_cpu, make_interrupt, make_peripheral};
1213

13-
pub struct PerIter<'a, 'b> {
14-
it: std::slice::IterMut<'a, Peripheral>,
15-
spec: &'b str,
16-
check_derived: bool,
17-
}
18-
19-
impl<'a, 'b> Iterator for PerIter<'a, 'b> {
20-
type Item = &'a mut Peripheral;
21-
fn next(&mut self) -> Option<Self::Item> {
22-
self.it.by_ref().find(|next| {
23-
matchname(&next.name, self.spec) && !(self.check_derived && next.derived_from.is_some())
24-
})
25-
}
26-
}
14+
pub type PerMatchIterMut<'a, 'b> = MatchIter<'b, std::slice::IterMut<'a, Peripheral>>;
2715

2816
/// Collecting methods for processing device contents
2917
pub trait DeviceExt {
3018
/// Iterates over all peripherals that match pspec
31-
fn iter_peripherals<'a, 'b>(
32-
&'a mut self,
33-
spec: &'b str,
34-
check_derived: bool,
35-
) -> PerIter<'a, 'b>;
19+
fn iter_peripherals<'a, 'b>(&'a mut self, spec: &'b str) -> PerMatchIterMut<'a, 'b>;
3620

3721
/// Work through a device, handling all peripherals
3822
fn process(&mut self, device: &Hash, update_fields: bool) -> PatchResult;
@@ -73,17 +57,8 @@ pub trait DeviceExt {
7357
}
7458

7559
impl DeviceExt for Device {
76-
fn iter_peripherals<'a, 'b>(
77-
&'a mut self,
78-
spec: &'b str,
79-
check_derived: bool,
80-
) -> PerIter<'a, 'b> {
81-
// check_derived=True
82-
PerIter {
83-
spec,
84-
check_derived,
85-
it: self.peripherals.iter_mut(),
86-
}
60+
fn iter_peripherals<'a, 'b>(&'a mut self, spec: &'b str) -> PerMatchIterMut<'a, 'b> {
61+
self.peripherals.iter_mut().matched(spec)
8762
}
8863

8964
fn process(&mut self, device: &Hash, update_fields: bool) -> PatchResult {
@@ -245,7 +220,7 @@ impl DeviceExt for Device {
245220
}
246221

247222
fn modify_peripheral(&mut self, pspec: &str, pmod: &Hash) -> PatchResult {
248-
for ptag in self.iter_peripherals(pspec, true) {
223+
for ptag in self.iter_peripherals(pspec) {
249224
ptag.modify_from(make_peripheral(pmod, true)?, VAL_LVL)?;
250225
if let Some(ints) = pmod.get_hash("interrupts")? {
251226
for (iname, val) in ints {
@@ -364,7 +339,10 @@ impl DeviceExt for Device {
364339
}
365340

366341
fn clear_fields(&mut self, pspec: &str) -> PatchResult {
367-
for ptag in self.iter_peripherals(pspec, false) {
342+
for ptag in self.iter_peripherals(pspec) {
343+
if ptag.derived_from.is_some() {
344+
continue;
345+
}
368346
ptag.clear_fields("*")?;
369347
}
370348
Ok(())
@@ -378,7 +356,7 @@ impl DeviceExt for Device {
378356
) -> PatchResult {
379357
// Find all peripherals that match the spec
380358
let mut pcount = 0;
381-
for ptag in self.iter_peripherals(pspec, false) {
359+
for ptag in self.iter_peripherals(pspec) {
382360
pcount += 1;
383361
ptag.process(peripheral, update_fields)
384362
.with_context(|| format!("Processing peripheral `{}`", ptag.name))?;

src/patch/peripheral.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,9 @@ impl PeripheralExt for Peripheral {
723723

724724
fn clear_fields(&mut self, rspec: &str) -> PatchResult {
725725
for rtag in self.iter_registers(rspec) {
726+
if rtag.derived_from.is_some() {
727+
continue;
728+
}
726729
rtag.clear_field("*")?;
727730
}
728731
Ok(())

src/patch/register.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ pub trait RegisterExt {
9090

9191
impl RegisterExt for Register {
9292
fn process(&mut self, rmod: &Hash, pname: &str, update_fields: bool) -> PatchResult {
93+
if self.derived_from.is_some() {
94+
return Ok(());
95+
}
9396
// Handle deletions
9497
for fspec in rmod.str_vec_iter("_delete") {
9598
self.delete_field(fspec)
@@ -272,6 +275,9 @@ impl RegisterExt for Register {
272275

273276
fn clear_field(&mut self, fspec: &str) -> PatchResult {
274277
for ftag in self.iter_fields(fspec) {
278+
if ftag.derived_from.is_some() {
279+
continue;
280+
}
275281
ftag.enumerated_values = Vec::new();
276282
ftag.write_constraint = None;
277283
}

svdtools/patch.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,11 @@ class Device:
405405
def __init__(self, device):
406406
self.device = device
407407

408-
def iter_peripherals(self, pspec, check_derived=True):
408+
def iter_peripherals(self, pspec):
409409
"""Iterates over all peripherals that match pspec."""
410410
for ptag in self.device.iter("peripheral"):
411411
name = ptag.find("name").text
412412
if matchname(name, pspec):
413-
if check_derived and "derivedFrom" in ptag.attrib:
414-
continue
415413
yield ptag
416414

417415
def modify_child(self, key, val):
@@ -491,7 +489,7 @@ def add_peripheral(self, pname, padd):
491489

492490
def delete_peripheral(self, pspec):
493491
"""Delete registers matched by rspec inside ptag."""
494-
for ptag in list(self.iter_peripherals(pspec, check_derived=False)):
492+
for ptag in list(self.iter_peripherals(pspec)):
495493
self.device.find("peripherals").remove(ptag)
496494

497495
def derive_peripheral(self, pname, pderive):
@@ -586,15 +584,17 @@ def rebase_peripheral(self, pnew, pold):
586584

587585
def clear_fields(self, pspec):
588586
"""Clear contents of all fields inside peripherals matched by pspec"""
589-
for ptag in self.iter_peripherals(pspec, check_derived=False):
587+
for ptag in self.iter_peripherals(pspec):
588+
if "derivedFrom" in ptag.attrib:
589+
continue
590590
p = Peripheral(ptag)
591591
p.clear_fields("*")
592592

593593
def process_peripheral(self, pspec, peripheral, update_fields=True):
594594
"""Work through a peripheral, handling all registers."""
595595
# Find all peripherals that match the spec
596596
pcount = 0
597-
for ptag in self.iter_peripherals(pspec, check_derived=False):
597+
for ptag in self.iter_peripherals(pspec):
598598
pcount += 1
599599
p = Peripheral(ptag)
600600

@@ -1024,6 +1024,8 @@ def collect_in_cluster(self, cname, cmod):
10241024
def clear_fields(self, rspec):
10251025
"""Clear contents of all fields inside registers matched by rspec"""
10261026
for rtag in list(self.iter_registers(rspec)):
1027+
if "derivedFrom" in rtag.attrib:
1028+
continue
10271029
r = Register(rtag)
10281030
r.clear_field("*")
10291031

@@ -1033,6 +1035,8 @@ def process_register(self, rspec, register, update_fields=True):
10331035
pname = self.ptag.find("name").text
10341036
rcount = 0
10351037
for rtag in self.iter_registers(rspec):
1038+
if "derivedFrom" in rtag.attrib:
1039+
continue
10361040
r = Register(rtag)
10371041
rcount += 1
10381042
# Handle deletions
@@ -1195,6 +1199,8 @@ def delete_field(self, fspec):
11951199
def clear_field(self, fspec):
11961200
"""Clear contents of fields matched by fspec inside rtag."""
11971201
for ftag in list(self.iter_fields(fspec)):
1202+
if "derivedFrom" in ftag.attrib:
1203+
continue
11981204
for tag in ftag.findall("enumeratedValues"):
11991205
ftag.remove(tag)
12001206
for tag in ftag.findall("writeConstraint"):

0 commit comments

Comments
 (0)