Skip to content

Commit

Permalink
add DeleteRegisters, FixRegisterBitSizes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Aug 11, 2024
1 parent b9f6475 commit a269e20
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/transform/delete_registers.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
57 changes: 57 additions & 0 deletions src/transform/fix_register_bit_sizes.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
6 changes: 6 additions & 0 deletions src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand All @@ -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),
}
Expand All @@ -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),
Expand All @@ -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),
}
Expand Down

0 comments on commit a269e20

Please sign in to comment.