From adc4d59caae83f0c619713f030d777a22782858f Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Thu, 9 Apr 2026 20:09:35 +0200 Subject: [PATCH] Fix formatting (cargo fmt) --- crates/core/src/codegen.rs | 83 +++++++++++++++++++++++---------- crates/core/src/outer.rs | 6 +-- crates/core/tests/comparison.rs | 25 ++++++---- 3 files changed, 75 insertions(+), 39 deletions(-) diff --git a/crates/core/src/codegen.rs b/crates/core/src/codegen.rs index b325728..fb7b55f 100644 --- a/crates/core/src/codegen.rs +++ b/crates/core/src/codegen.rs @@ -1118,7 +1118,10 @@ fn is_promotable_body(ops: &[IrOp]) -> bool { | IrOp::FtoS => return false, // IF with ELSE: promotable if both branches are promotable // and have the same net stack effect - IrOp::If { then_body, else_body } => { + IrOp::If { + then_body, + else_body, + } => { let Some(eb) = else_body else { return false; }; @@ -1239,16 +1242,36 @@ fn compute_stack_needs_rec(ops: &[IrOp], depth: &mut i32, min_accessed: &mut i32 IrOp::Over | IrOp::TwoDup => *depth - 2, IrOp::Swap | IrOp::Nip | IrOp::Tuck => *depth - 2, IrOp::Rot => *depth - 3, - IrOp::Add | IrOp::Sub | IrOp::Mul | IrOp::And | IrOp::Or | IrOp::Xor - | IrOp::Lshift | IrOp::Rshift | IrOp::ArithRshift - | IrOp::Eq | IrOp::NotEq | IrOp::Lt | IrOp::Gt | IrOp::LtUnsigned - | IrOp::DivMod | IrOp::Store | IrOp::CStore | IrOp::PlusStore => *depth - 2, - IrOp::Drop | IrOp::Negate | IrOp::Abs | IrOp::Invert - | IrOp::ZeroEq | IrOp::ZeroLt | IrOp::Fetch | IrOp::CFetch => *depth - 1, + IrOp::Add + | IrOp::Sub + | IrOp::Mul + | IrOp::And + | IrOp::Or + | IrOp::Xor + | IrOp::Lshift + | IrOp::Rshift + | IrOp::ArithRshift + | IrOp::Eq + | IrOp::NotEq + | IrOp::Lt + | IrOp::Gt + | IrOp::LtUnsigned + | IrOp::DivMod + | IrOp::Store + | IrOp::CStore + | IrOp::PlusStore => *depth - 2, + IrOp::Drop + | IrOp::Negate + | IrOp::Abs + | IrOp::Invert + | IrOp::ZeroEq + | IrOp::ZeroLt + | IrOp::Fetch + | IrOp::CFetch => *depth - 1, IrOp::TwoDrop => *depth - 2, IrOp::FetchFloat | IrOp::StoreFloat | IrOp::StoF => *depth - 1, // Control flow reads are handled by recursion below - IrOp::If { .. } => *depth - 1, // consumes condition + IrOp::If { .. } => *depth - 1, // consumes condition IrOp::DoLoop { .. } => *depth - 2, // consumes limit + index _ => *depth, }; @@ -1256,7 +1279,10 @@ fn compute_stack_needs_rec(ops: &[IrOp], depth: &mut i32, min_accessed: &mut i32 // Then: update depth. For control flow, recurse instead of using stack_delta. match op { - IrOp::If { then_body, else_body } => { + IrOp::If { + then_body, + else_body, + } => { *depth -= 1; // consume condition let saved = *depth; compute_stack_needs_rec(then_body, depth, min_accessed); @@ -1301,7 +1327,11 @@ fn compute_stack_needs_rec(ops: &[IrOp], depth: &mut i32, min_accessed: &mut i32 *depth = saved; } IrOp::BeginDoubleWhileRepeat { - outer_test, inner_test, body, after_repeat, else_body, + outer_test, + inner_test, + body, + after_repeat, + else_body, } => { let saved = *depth; compute_stack_needs_rec(outer_test, depth, min_accessed); @@ -1934,8 +1964,7 @@ fn emit_promoted_op(f: &mut Function, op: &IrOp, sim: &mut StackSim) { IrOp::LoopJ => { if sim.loop_index_stack.len() >= 2 { - let (outer_index, _) = - sim.loop_index_stack[sim.loop_index_stack.len() - 2]; + let (outer_index, _) = sim.loop_index_stack[sim.loop_index_stack.len() - 2]; let result = sim.alloc(); f.instruction(&Instruction::LocalGet(outer_index)); f.instruction(&Instruction::LocalSet(result)); @@ -2130,9 +2159,7 @@ fn body_needs_return_stack(ops: &[IrOp]) -> bool { } } } - IrOp::DoLoop { body, .. } - | IrOp::BeginUntil { body } - | IrOp::BeginAgain { body } => { + IrOp::DoLoop { body, .. } | IrOp::BeginUntil { body } | IrOp::BeginAgain { body } => { if body_needs_return_stack(body) { return true; } @@ -2186,8 +2213,7 @@ fn count_loop_depth(ops: &[IrOp]) -> u32 { max = max.max(count_loop_depth(eb)); } } - IrOp::BeginUntil { body } - | IrOp::BeginAgain { body } => { + IrOp::BeginUntil { body } | IrOp::BeginAgain { body } => { max = max.max(count_loop_depth(body)); } IrOp::BeginWhileRepeat { test, body } => { @@ -3671,15 +3697,22 @@ mod tests { else_body: None, }])); // IF with ELSE is promotable - assert!(is_promotable(&[IrOp::PushI32(1), IrOp::If { - then_body: vec![IrOp::PushI32(1)], - else_body: Some(vec![IrOp::PushI32(0)]), - }])); + assert!(is_promotable(&[ + IrOp::PushI32(1), + IrOp::If { + then_body: vec![IrOp::PushI32(1)], + else_body: Some(vec![IrOp::PushI32(0)]), + } + ])); // DO/LOOP with stack-neutral body is promotable - assert!(is_promotable(&[IrOp::PushI32(10), IrOp::PushI32(0), IrOp::DoLoop { - body: vec![IrOp::RFetch, IrOp::Drop], - is_plus_loop: false, - }])); + assert!(is_promotable(&[ + IrOp::PushI32(10), + IrOp::PushI32(0), + IrOp::DoLoop { + body: vec![IrOp::RFetch, IrOp::Drop], + is_plus_loop: false, + } + ])); assert!(!is_promotable(&[])); } diff --git a/crates/core/src/outer.rs b/crates/core/src/outer.rs index 22f8349..e1ef782 100644 --- a/crates/core/src/outer.rs +++ b/crates/core/src/outer.rs @@ -5148,10 +5148,8 @@ impl ForthVM { let sp = dsp.get(&mut caller).unwrap_i32() as u32; let new_sp = sp - 2 * CELL_SIZE; let data = memory.data_mut(&mut caller); - data[new_sp as usize..new_sp as usize + 4] - .copy_from_slice(&hi.to_le_bytes()); - data[new_sp as usize + 4..new_sp as usize + 8] - .copy_from_slice(&lo.to_le_bytes()); + data[new_sp as usize..new_sp as usize + 4].copy_from_slice(&hi.to_le_bytes()); + data[new_sp as usize + 4..new_sp as usize + 8].copy_from_slice(&lo.to_le_bytes()); dsp.set(&mut caller, Val::I32(new_sp as i32))?; Ok(()) }, diff --git a/crates/core/tests/comparison.rs b/crates/core/tests/comparison.rs index 257eb33..b20cbf4 100644 --- a/crates/core/tests/comparison.rs +++ b/crates/core/tests/comparison.rs @@ -32,7 +32,11 @@ fn probe_gforth(candidate: &str) -> bool { fn find_gforth() -> Option<&'static str> { GFORTH_PATH .get_or_init(|| { - for candidate in &["/opt/homebrew/bin/gforth", "/usr/local/bin/gforth", "gforth"] { + for candidate in &[ + "/opt/homebrew/bin/gforth", + "/usr/local/bin/gforth", + "gforth", + ] { if probe_gforth(candidate) { return Some(candidate.to_string()); } @@ -83,7 +87,7 @@ fn run_wafer(code: &str) -> EngineResult { return EngineResult { output, success: false, - } + }; } } } @@ -108,7 +112,7 @@ fn run_wafer_optimized(code: &str) -> EngineResult { return EngineResult { output, success: false, - } + }; } } } @@ -132,7 +136,11 @@ fn run_gforth_engine(gforth: &str, code: &str) -> Option { } else { format!("{flat} bye") }; - let output = Command::new(gforth).arg("-e").arg(&with_bye).output().ok()?; + let output = Command::new(gforth) + .arg("-e") + .arg(&with_bye) + .output() + .ok()?; Some(EngineResult { output: String::from_utf8_lossy(&output.stdout).into_owned(), success: output.status.success(), @@ -642,9 +650,7 @@ fn perf_benchmarks() -> Vec { fn build_wafer_release() -> Option { // Find workspace root (two levels up from crates/core) let manifest_dir = env!("CARGO_MANIFEST_DIR"); - let workspace_root = std::path::Path::new(manifest_dir) - .parent()? - .parent()?; + let workspace_root = std::path::Path::new(manifest_dir).parent()?.parent()?; let output = Command::new("cargo") .args(["build", "--release", "-p", "wafer"]) .current_dir(workspace_root) @@ -657,9 +663,8 @@ fn build_wafer_release() -> Option { ); return None; } - let target_dir = workspace_root.join( - std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string()), - ); + let target_dir = workspace_root + .join(std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string())); let binary = target_dir.join("release/wafer"); if binary.exists() { Some(binary.to_string_lossy().into_owned())