svd2utra: replace use of usize by u32/u64

The rationale is that those types don't depend on host pointer size
values.  This could've caused trouble when parsing SVDs for 64-bit
devices on a 32-bit host.

Signed-off-by: Jean-Pierre De Jesus DIAZ <me@jeandudey.tech>
This commit is contained in:
Jean-Pierre De Jesus DIAZ 2023-05-19 13:40:58 +02:00
parent 4dd50032e7
commit edd25611ff
No known key found for this signature in database
GPG key ID: 6279AEC20A9524EC

View file

@ -18,14 +18,14 @@ pub enum ParseError {
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]
pub struct Field { pub struct Field {
name: String, name: String,
lsb: usize, lsb: u32,
msb: usize, msb: u32,
} }
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]
pub struct Register { pub struct Register {
name: String, name: String,
offset: usize, offset: u64,
description: Option<String>, description: Option<String>,
fields: Vec<Field>, fields: Vec<Field>,
} }
@ -33,14 +33,14 @@ pub struct Register {
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]
pub struct Interrupt { pub struct Interrupt {
name: String, name: String,
value: usize, value: u64,
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct Peripheral { pub struct Peripheral {
name: String, name: String,
pub base: usize, pub base: u64,
_size: usize, _size: u64,
interrupt: Vec<Interrupt>, interrupt: Vec<Interrupt>,
registers: Vec<Register>, registers: Vec<Register>,
} }
@ -48,8 +48,8 @@ pub struct Peripheral {
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct MemoryRegion { pub struct MemoryRegion {
pub name: String, pub name: String,
pub base: usize, pub base: u64,
pub size: usize, pub size: u64,
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -98,10 +98,16 @@ pub fn get_base(value: &str) -> (&str, u32) {
} }
} }
fn parse_usize(value: &[u8]) -> Result<usize, ParseError> { fn parse_u64(value: &[u8]) -> Result<u64, ParseError> {
let value_as_str = String::from_utf8(value.to_vec()).or(Err(ParseError::NonUTF8))?; let value_as_str = String::from_utf8(value.to_vec()).or(Err(ParseError::NonUTF8))?;
let (value, base) = get_base(&value_as_str); let (value, base) = get_base(&value_as_str);
usize::from_str_radix(value, base).or(Err(ParseError::ParseIntError)) u64::from_str_radix(value, base).or(Err(ParseError::ParseIntError))
}
fn parse_u32(value: &[u8]) -> Result<u32, ParseError> {
let value_as_str = String::from_utf8(value.to_vec()).or(Err(ParseError::NonUTF8))?;
let (value, base) = get_base(&value_as_str);
u32::from_str_radix(value, base).or(Err(ParseError::ParseIntError))
} }
fn extract_contents<T: BufRead>(reader: &mut Reader<T>) -> Result<String, ParseError> { fn extract_contents<T: BufRead>(reader: &mut Reader<T>) -> Result<String, ParseError> {
@ -133,8 +139,8 @@ fn generate_field<T: BufRead>(reader: &mut Reader<T>) -> Result<Field, ParseErro
let tag_name = std::str::from_utf8(&tag_binding).unwrap(); let tag_name = std::str::from_utf8(&tag_binding).unwrap();
match tag_name { match tag_name {
"name" if name.is_none() => name = Some(extract_contents(reader)?), "name" if name.is_none() => name = Some(extract_contents(reader)?),
"lsb" => lsb = Some(parse_usize(extract_contents(reader)?.as_bytes())?), "lsb" => lsb = Some(parse_u32(extract_contents(reader)?.as_bytes())?),
"msb" => msb = Some(parse_usize(extract_contents(reader)?.as_bytes())?), "msb" => msb = Some(parse_u32(extract_contents(reader)?.as_bytes())?),
"bitRange" => { "bitRange" => {
let range = extract_contents(reader)?; let range = extract_contents(reader)?;
if !range.starts_with('[') || !range.ends_with(']') { if !range.starts_with('[') || !range.ends_with(']') {
@ -146,22 +152,22 @@ fn generate_field<T: BufRead>(reader: &mut Reader<T>) -> Result<Field, ParseErro
parts parts
.next() .next()
.ok_or(ParseError::UnexpectedValue)? .ok_or(ParseError::UnexpectedValue)?
.parse::<usize>() .parse::<u32>()
.map_err(|_| ParseError::ParseIntError)?, .map_err(|_| ParseError::ParseIntError)?,
); );
lsb = Some( lsb = Some(
parts parts
.next() .next()
.ok_or(ParseError::UnexpectedValue)? .ok_or(ParseError::UnexpectedValue)?
.parse::<usize>() .parse::<u32>()
.map_err(|_| ParseError::ParseIntError)?, .map_err(|_| ParseError::ParseIntError)?,
); );
} }
"bitWidth" => { "bitWidth" => {
bit_width = Some(parse_usize(extract_contents(reader)?.as_bytes())?) bit_width = Some(parse_u32(extract_contents(reader)?.as_bytes())?)
} }
"bitOffset" => { "bitOffset" => {
bit_offset = Some(parse_usize(extract_contents(reader)?.as_bytes())?) bit_offset = Some(parse_u32(extract_contents(reader)?.as_bytes())?)
} }
_ => (), _ => (),
} }
@ -233,7 +239,7 @@ fn generate_register<T: BufRead>(reader: &mut Reader<T>) -> Result<Register, Par
match tag_name { match tag_name {
"name" => name = Some(extract_contents(reader)?), "name" => name = Some(extract_contents(reader)?),
"addressOffset" => { "addressOffset" => {
offset = Some(parse_usize(extract_contents(reader)?.as_bytes())?) offset = Some(parse_u64(extract_contents(reader)?.as_bytes())?)
} }
"fields" => generate_fields(reader, &mut fields)?, "fields" => generate_fields(reader, &mut fields)?,
_ => (), _ => (),
@ -272,7 +278,7 @@ fn generate_interrupts<T: BufRead>(
.map_err(|_| ParseError::NonUTF8)?; .map_err(|_| ParseError::NonUTF8)?;
match tag_name { match tag_name {
"name" => name = Some(extract_contents(reader)?), "name" => name = Some(extract_contents(reader)?),
"value" => value = Some(parse_usize(extract_contents(reader)?.as_bytes())?), "value" => value = Some(parse_u64(extract_contents(reader)?.as_bytes())?),
_ => (), _ => (),
} }
} }
@ -318,7 +324,7 @@ fn generate_registers<T: BufRead>(
Ok(()) Ok(())
} }
fn derive_peripheral(base: &Peripheral, child_name: &str, child_base: usize) -> Peripheral { fn derive_peripheral(base: &Peripheral, child_name: &str, child_base: u64) -> Peripheral {
Peripheral { Peripheral {
name: child_name.to_owned(), name: child_name.to_owned(),
base: child_base, base: child_base,
@ -348,9 +354,9 @@ fn generate_peripheral<T: BufRead>(
match tag_name { match tag_name {
"name" => name = Some(extract_contents(reader)?), "name" => name = Some(extract_contents(reader)?),
"baseAddress" => { "baseAddress" => {
base = Some(parse_usize(extract_contents(reader)?.as_bytes())?) base = Some(parse_u64(extract_contents(reader)?.as_bytes())?)
} }
"size" => size = Some(parse_usize(extract_contents(reader)?.as_bytes())?), "size" => size = Some(parse_u64(extract_contents(reader)?.as_bytes())?),
"registers" => generate_registers(reader, &mut registers)?, "registers" => generate_registers(reader, &mut registers)?,
"interrupt" => generate_interrupts(reader, &mut interrupts)?, "interrupt" => generate_interrupts(reader, &mut interrupts)?,
_ => (), _ => (),
@ -438,9 +444,9 @@ fn generate_memory_region<T: BufRead>(reader: &mut Reader<T>) -> Result<MemoryRe
match tag_name { match tag_name {
"name" => name = Some(extract_contents(reader)?), "name" => name = Some(extract_contents(reader)?),
"baseAddress" => { "baseAddress" => {
base = Some(parse_usize(extract_contents(reader)?.as_bytes())?) base = Some(parse_u64(extract_contents(reader)?.as_bytes())?)
} }
"size" => size = Some(parse_usize(extract_contents(reader)?.as_bytes())?), "size" => size = Some(parse_u64(extract_contents(reader)?.as_bytes())?),
_ => (), _ => (),
} }
} }