Skip to content

Commit

Permalink
fix tilib enum format printing
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Jan 3, 2025
1 parent 651f579 commit 65917c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
15 changes: 14 additions & 1 deletion src/til/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use anyhow::{anyhow, ensure};

#[derive(Clone, Debug)]
pub struct Enum {
pub is_signed: bool,
pub is_unsigned: bool,
pub output_format: EnumFormat,
pub members: Vec<(Option<Vec<u8>>, u64)>,
pub groups: Option<Vec<u16>>,
Expand All @@ -26,6 +28,8 @@ impl Enum {
.map(|member| (fields.next().flatten(), member))
.collect();
Ok(Self {
is_signed: value.is_signed,
is_unsigned: value.is_unsigned,
output_format: value.output_format,
members,
groups: value.groups,
Expand All @@ -36,6 +40,8 @@ impl Enum {

#[derive(Clone, Debug)]
pub(crate) struct EnumRaw {
is_signed: bool,
is_unsigned: bool,
output_format: EnumFormat,
groups: Option<Vec<u16>>,
members: Vec<u64>,
Expand Down Expand Up @@ -72,7 +78,7 @@ impl EnumRaw {
"Enum BTE missing the Always sub-field"
);
let storage_size: Option<NonZeroU8> = match bte & BTE_SIZE_MASK {
0 => header.size_enum,
0 => None,
emsize @ 1..=4 => Some((1 << (emsize - 1)).try_into().unwrap()),
// Allowed at InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x4523c8 deserialize_enum
5..=7 => return Err(anyhow!("BTE emsize with reserved values")),
Expand All @@ -96,6 +102,11 @@ impl EnumRaw {
_ => unreachable!(),
};

// TODO ensure no bits from bte or taenum_bits are unparsed
let is_signed = taenum_bits.0 & TAENUM_SIGNED != 0;
let is_unsigned = taenum_bits.0 & TAENUM_UNSIGNED != 0;
// TODO ensure only signed/unsigned is allowed?
//
let is_64 = (taenum_bits.0 & TAENUM_64BIT) != 0;
let mut low_acc: u32 = 0;
let mut high_acc: u32 = 0;
Expand Down Expand Up @@ -123,6 +134,8 @@ impl EnumRaw {
.collect::<anyhow::Result<_>>()?;

Ok(TypeVariantRaw::Enum(EnumRaw {
is_signed,
is_unsigned,
output_format,
members,
groups,
Expand Down
1 change: 1 addition & 0 deletions src/til/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ impl TILSection {
title,
description,
compiler_id: header2.compiler_id,
// TODO panic if None?
size_enum: header2.size_enum.try_into().ok(),
size_int: header2.size_int.try_into()?,
size_bool: header2.size_bool.try_into()?,
Expand Down
27 changes: 9 additions & 18 deletions src/tools/tilib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,24 +848,15 @@ fn print_til_type_enum(
fmt.write_all(name)?;
write!(fmt, " ")?;
}
match (til_enum.storage_size, section.size_enum) {
(None, None) => {}
(Some(storage_size), Some(size_enum)) => {
if storage_size != size_enum {
let bits_required = til_enum
.members
.iter()
.map(|(_, value)| u64::BITS - value.leading_zeros())
.max()
.map(|x| x.max(1)) //can't have a value being represented in 0bits
.unwrap_or(8);
if bits_required / 8 < storage_size.get().into() {
write!(fmt, ": __int{} ", storage_size.get() as usize * 8)?;
}
}
}
(None, Some(_)) => {}
(Some(_), None) => {}
// InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x4443b0
if til_enum.storage_size.is_some() || til_enum.is_signed || til_enum.is_unsigned {
let bytes = til_enum.storage_size.or(section.size_enum).unwrap();
let signed = if til_enum.is_unsigned {
"unsigned "
} else {
""
};
write!(fmt, ": {signed}__int{} ", bytes.get() as usize * 8)?;
}
write!(fmt, "{{")?;
for (member_name, value) in &til_enum.members {
Expand Down

0 comments on commit 65917c7

Please sign in to comment.