From e306adfccffb9d883810f9c388a5c41bec3e6e6c Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Fri, 4 Aug 2017 01:30:20 +0200 Subject: [PATCH 1/2] Implemented new functions to format literals as seperated hex tokens The two main reasons behind this change are ensuring that the numbers (mostly addresses, masks and default values) line up with the datasheet for easier searching and verification since those are pretty much always expressed in hexadecimal. The other reason being clippy now complains about long numeric literals (without seperators) being hard to read -- I agree. Signed-off-by: Daniel Egger --- src/generate.rs | 16 ++++++++-------- src/util.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/generate.rs b/src/generate.rs index 97b3af5c..f7a26b29 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -5,7 +5,7 @@ use cast::u64; use quote::{Tokens, ToTokens}; use svd::{Access, BitRange, Defaults, Device, EnumeratedValues, Field, Peripheral, Register, Usage, WriteConstraint}; -use syn::{self, Ident, Lit}; +use syn::{self, Ident}; use errors::*; use util::{self, ToSanitizedSnakeCase, ToSanitizedUpperCase, U32Ext, BITS_PER_BYTE}; @@ -424,7 +424,7 @@ pub fn peripheral( ) -> Result<()> { let name = Ident::new(&*p.name.to_uppercase()); let name_pc = Ident::new(&*p.name.to_sanitized_upper_case()); - let address = util::unsuffixed(u64(p.base_address)); + let address = util::hex(p.base_address); let description = util::respace(p.description.as_ref().unwrap_or(&p.name)); items.push(quote! { @@ -756,7 +756,7 @@ pub fn register( let rv = register .reset_value .or(defs.reset_value) - .map(|rv| util::unsuffixed(u64(rv))) + .map(|rv| util::hex(rv)) .ok_or_else(|| { format!("Register {} has no reset value", register.name) @@ -868,9 +868,9 @@ pub fn fields( access: Option, description: String, evs: &'a [EnumeratedValues], - mask: Lit, + mask: Tokens, name: &'a str, - offset: Lit, + offset: Tokens, pc_r: Ident, pc_w: Ident, sc: Ident, @@ -914,9 +914,9 @@ pub fn fields( access: f.access, evs: &f.enumerated_values, sc: Ident::new(&*sc), - mask: util::unsuffixed_or_bool((1 << width) - 1, width), + mask: util::hex_or_bool((((1 as u64) << width) - 1) as u32, width), name: &f.name, - offset: util::unsuffixed(u64(f.bit_range.offset)), + offset: util::hex(f.bit_range.offset), ty: width.to_ty()?, write_constraint: f.write_constraint.as_ref(), }) @@ -1077,7 +1077,7 @@ pub fn fields( .iter() .map(|v| { let value = - util::unsuffixed_or_bool(v.value, f.width); + util::hex_or_bool(v.value as u32, f.width); let pc = &v.pc; quote! { diff --git a/src/util.rs b/src/util.rs index c90583a0..a62aea3d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -4,6 +4,7 @@ use inflections::Inflect; use svd::{self, Access, EnumeratedValues, Field, Peripheral, Register, Usage}; use syn::{self, Ident, IntTy, Lit}; +use quote::Tokens; use errors::*; @@ -279,6 +280,32 @@ pub fn access_of(register: &Register) -> Access { ) } +/// Turns `n` into an unsuffixed separated hex tokens +pub fn hex(n: u32) -> Tokens { + let mut t = Tokens::new(); + let (h2, h1) = ((n >> 16) & 0xffff, n & 0xffff); + t.append(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 { + String::from("0") + }); + t +} + +pub fn hex_or_bool(n: u32, width: u32) -> Tokens { + if width == 1 { + let mut t = Tokens::new(); + t.append(if n == 0 { "false" } else { "true" }); + t + } else { + hex(n) + } +} + /// Turns `n` into an unsuffixed literal pub fn unsuffixed(n: u64) -> Lit { Lit::Int(n, IntTy::Unsuffixed) From b5756e8dc787fae2d497e06d488cefb8c5f1c8d6 Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Sat, 23 Sep 2017 16:30:14 +0200 Subject: [PATCH 2/2] Addressed request of expressing the OFFSET in decimal rather than hex This changes the util::unsuffixed() and util::unsuffixed_or_bool() functions to return a quote::Tokens instead of a syn::Lit. Signed-off-by: Daniel Egger --- src/generate.rs | 2 +- src/util.rs | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/generate.rs b/src/generate.rs index f7a26b29..233d3e57 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -916,7 +916,7 @@ pub fn fields( sc: Ident::new(&*sc), mask: util::hex_or_bool((((1 as u64) << width) - 1) as u32, width), name: &f.name, - offset: util::hex(f.bit_range.offset), + offset: util::unsuffixed(u64::from(f.bit_range.offset)), ty: width.to_ty()?, write_constraint: f.write_constraint.as_ref(), }) diff --git a/src/util.rs b/src/util.rs index a62aea3d..5173deb8 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,7 +3,7 @@ use std::borrow::Cow; use inflections::Inflect; use svd::{self, Access, EnumeratedValues, Field, Peripheral, Register, Usage}; -use syn::{self, Ident, IntTy, Lit}; +use syn::{self, Ident}; use quote::Tokens; use errors::*; @@ -280,7 +280,7 @@ pub fn access_of(register: &Register) -> Access { ) } -/// Turns `n` into an unsuffixed separated hex tokens +/// Turns `n` into an unsuffixed separated hex token pub fn hex(n: u32) -> Tokens { let mut t = Tokens::new(); let (h2, h1) = ((n >> 16) & 0xffff, n & 0xffff); @@ -306,18 +306,18 @@ pub fn hex_or_bool(n: u32, width: u32) -> Tokens { } } -/// Turns `n` into an unsuffixed literal -pub fn unsuffixed(n: u64) -> Lit { - Lit::Int(n, IntTy::Unsuffixed) +/// Turns `n` into an unsuffixed token +pub fn unsuffixed(n: u64) -> Tokens { + let mut t = Tokens::new(); + t.append(format!("{}", n)); + t } -pub fn unsuffixed_or_bool(n: u64, width: u32) -> Lit { +pub fn unsuffixed_or_bool(n: u64, width: u32) -> Tokens { if width == 1 { - if n == 0 { - Lit::Bool(false) - } else { - Lit::Bool(true) - } + let mut t = Tokens::new(); + t.append(if n == 0 { "false" } else { "true" }); + t } else { unsuffixed(n) }