Skip to content

Commit

Permalink
Add option to keep holey arrays in transforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Oct 27, 2024
1 parent 92efe4e commit 6732b5b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
35 changes: 27 additions & 8 deletions src/transform/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,15 @@ pub(crate) fn replace_block_ids(ir: &mut IR, from: &HashSet<String>, to: String)
}
}

pub(crate) fn calc_array(mut offsets: Vec<u32>) -> (u32, Array) {
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum ArrayMode {
#[default]
Standard,
Cursed,
Holey,
}

pub(crate) fn calc_array(mut offsets: Vec<u32>, mode: ArrayMode) -> anyhow::Result<(u32, Array)> {
offsets.sort_unstable();

// Guess stride.
Expand All @@ -261,19 +269,30 @@ pub(crate) fn calc_array(mut offsets: Vec<u32>) -> (u32, Array) {
.all(|(n, &i)| i == start_offset + (n as u32) * stride)
{
// Array is regular,
(
return Ok((
start_offset,
Array::Regular(RegularArray {
len: offsets.len() as _,
stride,
}),
)
} else {
// Array is irregular,
for o in &mut offsets {
*o -= start_offset
));
}

// Array is irregular, If we wanted a regular array, fail.
match mode {
ArrayMode::Standard => {
bail!("arrayize: items are not evenly spaced. Set `mode: Cursed` to allow index->offset relation to be non-linear, or `mode: Holey` to keep it linear but fill the holes with indexes that won't be valid.")
}
ArrayMode::Cursed => {
for o in &mut offsets {
*o -= start_offset
}
Ok((start_offset, Array::Cursed(CursedArray { offsets })))
}
ArrayMode::Holey => {
let len = (offsets.last().unwrap() - offsets.first().unwrap()) / stride + 1;
Ok((start_offset, Array::Regular(RegularArray { len, stride })))
}
(start_offset, Array::Cursed(CursedArray { offsets }))
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/transform/make_field_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct MakeFieldArray {
pub from: String,
pub to: String,
#[serde(default)]
pub allow_cursed: bool,
pub mode: ArrayMode,
}

impl MakeFieldArray {
Expand Down Expand Up @@ -55,13 +55,10 @@ impl MakeFieldArray {
info!(" {}", i.name);
}

let (offset, array) =
calc_array(items.iter().map(|x| x.bit_offset.min_offset()).collect());
if let Array::Cursed(_) = &array {
if !self.allow_cursed {
bail!("arrayize: items {} are not evenly spaced. Set `allow_cursed: true` to allow this.", to)
}
}
let (offset, array) = calc_array(
items.iter().map(|x| x.bit_offset.min_offset()).collect(),
self.mode,
)?;

let mut item = items[0].clone();

Expand Down
11 changes: 3 additions & 8 deletions src/transform/make_register_array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::bail;
use log::*;
use serde::{Deserialize, Serialize};

Expand All @@ -11,7 +10,7 @@ pub struct MakeRegisterArray {
pub from: String,
pub to: String,
#[serde(default)]
pub allow_cursed: bool,
pub mode: ArrayMode,
}

impl MakeRegisterArray {
Expand Down Expand Up @@ -39,12 +38,8 @@ impl MakeRegisterArray {
info!(" {}", i.name);
}

let (offset, array) = calc_array(items.iter().map(|x| x.byte_offset).collect());
if let Array::Cursed(_) = &array {
if !self.allow_cursed {
bail!("arrayize: items {} are not evenly spaced. Set `allow_cursed: true` to allow this.", to);
}
}
let (offset, array) =
calc_array(items.iter().map(|x| x.byte_offset).collect(), self.mode)?;

let mut item = items[0].clone();

Expand Down

0 comments on commit 6732b5b

Please sign in to comment.