From 61fe6de7899c4055b67f458ab8115332bc67f07b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Str=C3=B6m?= Date: Mon, 21 Oct 2024 11:31:11 +0000 Subject: [PATCH 1/2] Add a methods to debug stack contents and locals --- clar2wasm/src/wasm_generator.rs | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/clar2wasm/src/wasm_generator.rs b/clar2wasm/src/wasm_generator.rs index d24e69c4..c65aaf35 100644 --- a/clar2wasm/src/wasm_generator.rs +++ b/clar2wasm/src/wasm_generator.rs @@ -1713,6 +1713,57 @@ impl WasmGenerator { builder.call(self.func_by_name("debug_msg")); } + /// Dump the top of the stack to debug messages + pub fn debug_dump_stack>( + &mut self, + builder: &mut InstrSeqBuilder, + message: M, + expected_types: &[ValType], + ) { + self.debug_msg(builder, message); + self.debug_msg(builder, ""); + let mut locals = vec![]; + + for t in expected_types { + let l = self.borrow_local(*t); + builder.local_tee(*l); + locals.push(l); + match t { + ValType::I32 => self.debug_log_i32(builder), + ValType::I64 => self.debug_log_i64(builder), + _ => unimplemented!("unsupported stack dump type"), + } + } + self.debug_msg(builder, ""); + + // restore the stack + while let Some(l) = locals.pop() { + builder.local_get(*l); + } + } + + pub fn debug_log_local_i32>( + &mut self, + builder: &mut InstrSeqBuilder, + message: M, + local_id: &LocalId, + ) { + self.debug_msg(builder, message); + builder.local_get(*local_id); + self.debug_log_i32(builder) + } + + pub fn debug_log_local_i64>( + &mut self, + builder: &mut InstrSeqBuilder, + message: M, + local_id: &LocalId, + ) { + self.debug_msg(builder, message); + builder.local_get(*local_id); + self.debug_log_i64(builder) + } + #[allow(dead_code)] /// Log an i64 that is on top of the stack. pub fn debug_log_i64(&self, builder: &mut InstrSeqBuilder) { From 0659bf2e3864f422854afadf6870004bf5472f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Str=C3=B6m?= Date: Mon, 21 Oct 2024 12:27:07 +0000 Subject: [PATCH 2/2] clippy allow unimplemented --- clar2wasm/src/wasm_generator.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clar2wasm/src/wasm_generator.rs b/clar2wasm/src/wasm_generator.rs index c65aaf35..f4233d3c 100644 --- a/clar2wasm/src/wasm_generator.rs +++ b/clar2wasm/src/wasm_generator.rs @@ -1731,7 +1731,13 @@ impl WasmGenerator { match t { ValType::I32 => self.debug_log_i32(builder), ValType::I64 => self.debug_log_i64(builder), - _ => unimplemented!("unsupported stack dump type"), + _ => { + // allow unimplemented in debug code + #[allow(clippy::unimplemented)] + { + unimplemented!("unsupported stack dump type") + } + } } } self.debug_msg(builder, "");