diff --git a/src/generate.rs b/src/generate.rs index 97b3af5c..233d3e57 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::unsuffixed(u64::from(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..5173deb8 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,7 +3,8 @@ 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::*; @@ -279,18 +280,44 @@ pub fn access_of(register: &Register) -> Access { ) } -/// Turns `n` into an unsuffixed literal -pub fn unsuffixed(n: u64) -> Lit { - Lit::Int(n, IntTy::Unsuffixed) +/// 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); + 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 unsuffixed_or_bool(n: u64, width: u32) -> Lit { +pub fn hex_or_bool(n: u32, 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 { + hex(n) + } +} + +/// 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) -> Tokens { + if width == 1 { + let mut t = Tokens::new(); + t.append(if n == 0 { "false" } else { "true" }); + t } else { unsuffixed(n) }