diff --git a/rust2/benches/loopremove.bf b/rust2/benches/loopremove.bf deleted file mode 100644 index 8c81277..0000000 --- a/rust2/benches/loopremove.bf +++ /dev/null @@ -1,4 +0,0 @@ ->+++++[-]++++++++++[[-->+++++++++>+++++++++>+++++++++<<<<->]]>.>.>. -<[-]<[-]<[-] ->>>>+ >>>>> ++++++++++ <<<< <<<<< -+[[>>>>]]+[[++--[+++>>]]>.][-] \ No newline at end of file diff --git a/rust2/benches/opts.rs b/rust2/benches/opts.rs index 8308592..29fcca1 100644 --- a/rust2/benches/opts.rs +++ b/rust2/benches/opts.rs @@ -32,14 +32,12 @@ fn run_bf(bf: &str) { fn optimized(c: &mut Criterion) { let fizzbuzz = include_str!("fizzbuzz.bf"); let bench = include_str!("bench.bf"); - let loopremove = include_str!("loopremove.bf"); let twinkle = include_str!("twinkle.bf"); let bottles = include_str!("bottles.bf"); let hanoi = include_str!("hanoi.bf"); c.bench_function("fizzbuzz", |b| b.iter(|| run_bf(black_box(fizzbuzz)))); c.bench_function("bench", |b| b.iter(|| run_bf(black_box(bench)))); - c.bench_function("loopremove", |b| b.iter(|| run_bf(black_box(loopremove)))); c.bench_function("twinkle", |b| b.iter(|| run_bf(black_box(twinkle)))); c.bench_function("bottles", |b| b.iter(|| run_bf(black_box(bottles)))); c.bench_function("hanoi", |b| b.iter(|| run_bf(black_box(hanoi)))); diff --git a/rust2/src/opts.rs b/rust2/src/opts.rs index 1aad54c..adbe207 100644 --- a/rust2/src/opts.rs +++ b/rust2/src/opts.rs @@ -154,8 +154,16 @@ fn pass_find_set_null(ir: &mut Ir<'_>) { /// pass that replaces `SetNull Add(5)` with `SetN(5)` fn pass_set_n(ir: &mut Ir<'_>) { let stmts = &mut ir.stmts; - let mut i = 0; - while i < stmts.len() - 1 { + for i in 0..stmts.len() { + let a = &mut stmts[i]; + if let StmtKind::Loop(body) = &mut a.kind { + pass_set_n(body); + } + + if i >= stmts.len() - 1 { + break; // we are the last element + } + let a = &stmts[i]; if let StmtKind::SetNull = a.kind() { let b = &stmts[i + 1]; @@ -163,7 +171,6 @@ fn pass_set_n(ir: &mut Ir<'_>) { StmtKind::Add(n) => StmtKind::SetN(*n), StmtKind::Sub(n) => StmtKind::SetN(0u8.wrapping_sub(*n)), _ => { - i += 1; continue; } }; @@ -171,6 +178,5 @@ fn pass_set_n(ir: &mut Ir<'_>) { stmts.remove(i + 1); stmts[i] = Stmt::new(new, span); } - i += 1; } } diff --git a/rust2/test.bf b/rust2/test.bf index 8fa0f72..b58debc 100644 --- a/rust2/test.bf +++ b/rust2/test.bf @@ -1 +1 @@ -++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++. +[>[-]+++++++++++++++++++++++++[>[-]++++++[-]<-]<-] \ No newline at end of file