Skip to content

Commit

Permalink
👷 Fix clippy, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Philogy committed Sep 19, 2024
1 parent 876077a commit 41015bd
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,21 @@ impl MarkMap {
}
}

fn max_ref_extra_bytes(known_size: usize, total_refs: usize) -> u8 {
let mut ref_extra_bytes: u8 = 1;
while 1 << (8 * ref_extra_bytes) < known_size + total_refs * (ref_extra_bytes as usize) {
ref_extra_bytes += 1;
}
ref_extra_bytes
}

pub fn assemble_maximized(asm: &[Asm]) -> Vec<u8> {
let total_refs = asm
.iter()
.filter(|chunk| matches!(chunk, Asm::Ref(_)))
.count();
let known_size: usize = asm.iter().map(|chunk| chunk.size()).sum();
let ref_extra_bytes: u8 = {
let mut ref_extra_bytes: u8 = 1;
while 1 << (8 * ref_extra_bytes) < known_size + total_refs * (ref_extra_bytes as usize) {
ref_extra_bytes += 1;
}
ref_extra_bytes
};
let ref_extra_bytes: u8 = max_ref_extra_bytes(known_size, total_refs);

let (mark_map, total_size) = MarkMap::build(asm, ref_extra_bytes);
let mut final_code = Vec::with_capacity(total_size);
Expand Down Expand Up @@ -114,23 +116,14 @@ pub fn assemble_maximized(asm: &[Asm]) -> Vec<u8> {
const MAX_CHANGES: usize = 100;

pub fn assemble_minimized(asm: &[Asm], allow_push0: bool) -> Vec<u8> {
let total_refs = asm
.iter()
.filter(|chunk| matches!(chunk, Asm::Ref(_)))
.count();
let known_size: usize = asm.iter().map(|chunk| chunk.size()).sum();

let (mark_map, total_size) =
{
let ref_extra_bytes: u8 = {
let mut ref_extra_bytes: u8 = 1;
while 1 << (8 * ref_extra_bytes)
< known_size + total_refs * (ref_extra_bytes as usize)
{
ref_extra_bytes += 1;
}
ref_extra_bytes
};
let total_refs = asm
.iter()
.filter(|chunk| matches!(chunk, Asm::Ref(_)))
.count();
let known_size: usize = asm.iter().map(|chunk| chunk.size()).sum();
let ref_extra_bytes: u8 = max_ref_extra_bytes(known_size, total_refs);
let (mut mark_map, mut total_size) = MarkMap::build(asm, ref_extra_bytes);

let mut made_a_change = true;
Expand All @@ -149,6 +142,7 @@ pub fn assemble_minimized(asm: &[Asm], allow_push0: bool) -> Vec<u8> {
) as usize,
made_a_change,
),
#[allow(clippy::identity_op)]
Asm::Mark(id) => (
offset + 0,
made_a_change || mark_map.set_mark_offset(*id, offset),
Expand Down

0 comments on commit 41015bd

Please sign in to comment.