From 65afdbbfa578357c1061ca4dac26ae26bdbafb83 Mon Sep 17 00:00:00 2001 From: Doehyun Baek Date: Mon, 26 Aug 2024 14:18:45 +0900 Subject: [PATCH] fix: increment counter first --- crates/replay_gen/src/wasmgen.rs | 16 +++++++++------- tests/core/circular-call/reference.r3 | 11 +++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 tests/core/circular-call/reference.r3 diff --git a/crates/replay_gen/src/wasmgen.rs b/crates/replay_gen/src/wasmgen.rs index edb624c..8072f50 100644 --- a/crates/replay_gen/src/wasmgen.rs +++ b/crates/replay_gen/src/wasmgen.rs @@ -173,6 +173,12 @@ pub fn generate_replay_wasm( stream, &format!("(func ${name} (@name \"r3_{name}\") (export \"{name}\") {tystr}\n",), )?; + write( + stream, + &format!( + "(global.get {global_idx}) (i64.const 1) (i64.add) (global.set {global_idx})\n" + ), + )?; for (i, body) in func.bodys.iter().enumerate() { if let Some(body) = body { let mut bodystr = String::new(); @@ -211,22 +217,18 @@ pub fn generate_replay_wasm( if memory_writes.len() > 0 { merge_memory_writes(&mut bodystr, memory_writes, &mut data_segments); } + // We add 1 to i because on ith iteration, counter is i + 1 + let iter_count = i + 1; write( stream, &format!( "(if - (i64.eq (global.get {global_idx}) (i64.const {i})) + (i64.eq (global.get {global_idx}) (i64.const {iter_count})) (then {bodystr}))\n" ), )?; } } - write( - stream, - &format!( - "(global.get {global_idx}) (i64.const 1) (i64.add) (global.set {global_idx})\n" - ), - )?; let mut current = 0; for r in func.results.iter() { let ty = &code.funcs.get(&funcidx).unwrap().ty; diff --git a/tests/core/circular-call/reference.r3 b/tests/core/circular-call/reference.r3 new file mode 100644 index 0000000..f3a910f --- /dev/null +++ b/tests/core/circular-call/reference.r3 @@ -0,0 +1,11 @@ +EC;2;func_724; +IC;1 +IR;1; +IC;1 +EC;2;func_724; +IC;1 +IR;1; +IC;1 +IR;1; +IR;1; +ER \ No newline at end of file