Skip to content

Commit

Permalink
fix i32 overflow in periph addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Feb 16, 2024
1 parent 689341a commit 247ccbe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn render(_opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result

for p in sorted(&d.peripherals, |p| p.base_address) {
let name = Ident::new(&p.name, span);
let address = util::hex(p.base_address);
let address = util::hex_usize(p.base_address);
let doc = util::doc(&p.description);

if let Some(block_name) = &p.block {
Expand Down
42 changes: 24 additions & 18 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,30 +167,36 @@ pub fn replace_suffix(name: &str, suffix: &str) -> String {
}
}

/// Turns `n` into an unsuffixed separated hex token
pub fn hex(n: u64) -> TokenStream {
pub fn hex_str(n: u64) -> String {
let (h4, h3, h2, h1) = (
(n >> 48) & 0xffff,
(n >> 32) & 0xffff,
(n >> 16) & 0xffff,
n & 0xffff,
);
TokenStream::from_str(
&(if h4 != 0 {
format!("0x{:04x}_{:04x}_{:04x}_{:04x}", h4, h3, h2, h1)
} else if h3 != 0 {
format!("0x{:04x}_{:04x}_{:04x}", h3, h2, h1)
} else if h2 != 0 {
format!("0x{:04x}_{:04x}", h2, h1)
} else if h1 & 0xff00 != 0 {
format!("0x{:04x}", h1)
} else if h1 != 0 {
format!("0x{:02x}", h1 & 0xff)
} else {
"0".to_string()
}),
)
.unwrap()
if h4 != 0 {
format!("0x{:04x}_{:04x}_{:04x}_{:04x}", h4, h3, h2, h1)
} else if h3 != 0 {
format!("0x{:04x}_{:04x}_{:04x}", h3, h2, h1)
} else if h2 != 0 {
format!("0x{:04x}_{:04x}", h2, h1)
} else if h1 & 0xff00 != 0 {
format!("0x{:04x}", h1)
} else if h1 != 0 {
format!("0x{:02x}", h1 & 0xff)
} else {
"0".to_string()
}
}

/// Turns `n` into an unsuffixed separated hex token
pub fn hex(n: u64) -> TokenStream {
TokenStream::from_str(&hex_str(n)).unwrap()
}

/// Turns `n` into an unsuffixed separated hex token
pub fn hex_usize(n: u64) -> TokenStream {
TokenStream::from_str(&format!("{}usize", hex_str(n))).unwrap()
}

/// Turns `n` into an unsuffixed token
Expand Down

0 comments on commit 247ccbe

Please sign in to comment.