Skip to content

Commit

Permalink
feat(zink): use for-loop for jump target matchings
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Nov 27, 2024
1 parent 7bc9018 commit 6ecb29e
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions codegen/src/jump/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ impl JumpTable {

/// Shifts the program counter for functions.
pub fn shift_func_target(&mut self, ptr: u16, offset: u16) -> Result<()> {
if self.func.is_empty() {
tracing::trace!("No functions to shift.");
return Ok(());
}

self.func.iter_mut().try_for_each(|(index, target)| {
let next_target = *target + offset;

Expand All @@ -86,26 +81,23 @@ impl JumpTable {

/// Shifts the program counter for labels.
pub fn shift_label_target(&mut self, ptr: u16, offset: u16) -> Result<()> {
if self.jump.is_empty() {
tracing::trace!("No labels to shift.");
return Ok(());
}

self.jump.iter_mut().try_for_each(|(pc, jump)| {
if let Jump::Label(target) = jump {
let next_target = *target + offset;
for (pc, jump) in self.jump.iter_mut() {
let target: &mut u16 = match jump {
Jump::Label(target) => target,
// Jump::Offset { target, .. } => target,
_ => continue,
};

if *target > ptr {
tracing::trace!(
"shift Label(0x{pc:x}) target with offset={offset}: 0x{target:x}(0x{ptr:x}) -> 0x{:x}",
next_target,
);
let next_target = *target + offset;
if *target > ptr {
tracing::trace!(
"shift Label(0x{pc:x}) target with offset={offset}: 0x{target:x}(0x{ptr:x}) -> 0x{:x}",
next_target,
);

*target = next_target;
}
*target = next_target;
}

Ok(())
})
}
Ok(())
}
}

0 comments on commit 6ecb29e

Please sign in to comment.