diff --git a/src/transform/delete_registers.rs b/src/transform/delete_registers.rs new file mode 100644 index 0000000..9f00105 --- /dev/null +++ b/src/transform/delete_registers.rs @@ -0,0 +1,22 @@ +use serde::{Deserialize, Serialize}; + +use super::common::*; +use crate::ir::*; + +#[derive(Debug, Serialize, Deserialize)] +pub struct DeleteRegisters { + pub block: String, + pub from: String, +} + +impl DeleteRegisters { + pub fn run(&self, ir: &mut IR) -> anyhow::Result<()> { + let path_re = make_regex(&self.block)?; + let re = make_regex(&self.from)?; + for id in match_all(ir.blocks.keys().cloned(), &path_re) { + let b = ir.blocks.get_mut(&id).unwrap(); + b.items.retain(|i| !re.is_match(&i.name)); + } + Ok(()) + } +} diff --git a/src/transform/fix_register_bit_sizes.rs b/src/transform/fix_register_bit_sizes.rs new file mode 100644 index 0000000..cd72997 --- /dev/null +++ b/src/transform/fix_register_bit_sizes.rs @@ -0,0 +1,57 @@ +use serde::{Deserialize, Serialize}; + +use crate::ir::*; + +#[derive(Debug, Serialize, Deserialize)] +pub struct FixRegisterBitSizes {} + +impl FixRegisterBitSizes { + pub fn run(&self, ir: &mut IR) -> anyhow::Result<()> { + for (_, b) in &mut ir.blocks { + for i in &mut b.items { + if let BlockItemInner::Register(r) = &mut i.inner { + let orig_bit_size = r.bit_size; + let good_bit_size = match r.bit_size { + ..=8 => 8, + ..=16 => 16, + ..=32 => 32, + ..=64 => 64, + _ => panic!("Invalid register bit size {}", r.bit_size), + }; + if r.bit_size != good_bit_size { + r.bit_size = good_bit_size; + match &r.fieldset { + None => { + // create a new fieldset, with a single field with the original bit size. + r.fieldset = Some(i.name.clone()); + let fs = FieldSet { + bit_size: good_bit_size, + fields: vec![Field { + name: "val".to_string(), + bit_offset: BitOffset::Regular(0), + bit_size: orig_bit_size, + description: None, + enumm: None, + array: None, + }], + description: None, + extends: None, + }; + if ir.fieldsets.insert(i.name.clone(), fs).is_some() { + panic!("dup fieldset {}", i.name); + } + } + Some(fs) => { + // expand the size of the existing fieldset. + let fs = ir.fieldsets.get_mut(fs).unwrap(); + fs.bit_size = good_bit_size; + } + } + } + } + } + } + + Ok(()) + } +} diff --git a/src/transform/mod.rs b/src/transform/mod.rs index 4b4eca7..a24f746 100644 --- a/src/transform/mod.rs +++ b/src/transform/mod.rs @@ -256,7 +256,9 @@ pub mod delete_enums_used_in; pub mod delete_fieldsets; //pub mod find_duplicate_enums; //pub mod find_duplicate_fieldsets; +pub mod delete_registers; pub mod expand_extends; +pub mod fix_register_bit_sizes; pub mod make_block; pub mod make_field_array; pub mod make_register_array; @@ -278,6 +280,7 @@ pub enum Transform { DeleteEnums(delete_enums::DeleteEnums), DeleteEnumsUsedIn(delete_enums_used_in::DeleteEnumsUsedIn), DeleteFieldsets(delete_fieldsets::DeleteFieldsets), + DeleteRegisters(delete_registers::DeleteRegisters), MergeBlocks(merge_blocks::MergeBlocks), MergeEnums(merge_enums::MergeEnums), MergeFieldsets(merge_fieldsets::MergeFieldsets), @@ -289,6 +292,7 @@ pub enum Transform { MakeFieldArray(make_field_array::MakeFieldArray), MakeBlock(make_block::MakeBlock), ModifyByteOffset(modify_byte_offset::ModifyByteOffset), + FixRegisterBitSizes(fix_register_bit_sizes::FixRegisterBitSizes), //FindDuplicateEnums(find_duplicate_enums::FindDuplicateEnums), //FindDuplicateFieldsets(find_duplicate_fieldsets::FindDuplicateFieldsets), } @@ -302,6 +306,7 @@ impl Transform { Self::DeleteEnums(t) => t.run(ir), Self::DeleteEnumsUsedIn(t) => t.run(ir), Self::DeleteFieldsets(t) => t.run(ir), + Self::DeleteRegisters(t) => t.run(ir), Self::MergeBlocks(t) => t.run(ir), Self::MergeEnums(t) => t.run(ir), Self::MergeFieldsets(t) => t.run(ir), @@ -313,6 +318,7 @@ impl Transform { Self::MakeFieldArray(t) => t.run(ir), Self::MakeBlock(t) => t.run(ir), Self::ModifyByteOffset(t) => t.run(ir), + Self::FixRegisterBitSizes(t) => t.run(ir), //Self::FindDuplicateEnums(t) => t.run(ir), //Self::FindDuplicateFieldsets(t) => t.run(ir), }