From bd81f0389605d1f967dc74d8a8f290fc76a57b75 Mon Sep 17 00:00:00 2001 From: Frank Emrich Date: Mon, 29 Jan 2024 11:07:52 +0000 Subject: [PATCH] Revive emit_debug_print (#84) --- crates/cranelift/src/func_environ.rs | 138 ++++++++++++++------------- 1 file changed, 70 insertions(+), 68 deletions(-) diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs index 9683860084ed..c50c833ff692 100644 --- a/crates/cranelift/src/func_environ.rs +++ b/crates/cranelift/src/func_environ.rs @@ -227,9 +227,9 @@ mod typed_continuation_helpers { env.generate_builtin_call_no_return_val(builder, index, sig, vec![ptr, len]); } }; - let _print_int = |env: &mut crate::func_environ::FuncEnvironment<'a>, - builder: &mut FunctionBuilder, - val: ir::Value| { + let print_int = |env: &mut crate::func_environ::FuncEnvironment<'a>, + builder: &mut FunctionBuilder, + val: ir::Value| { let index = BuiltinFunctionIndex::tc_print_int(); let sig = env.builtin_function_signatures.tc_print_int(builder.func); let ty = builder.func.dfg.value_type(val); @@ -240,9 +240,9 @@ mod typed_continuation_helpers { }; env.generate_builtin_call_no_return_val(builder, index, sig, vec![val]); }; - let _print_pointer = |env: &mut crate::func_environ::FuncEnvironment<'a>, - builder: &mut FunctionBuilder, - ptr: ir::Value| { + let print_pointer = |env: &mut crate::func_environ::FuncEnvironment<'a>, + builder: &mut FunctionBuilder, + ptr: ir::Value| { let index = BuiltinFunctionIndex::tc_print_pointer(); let sig = env .builtin_function_signatures @@ -251,30 +251,32 @@ mod typed_continuation_helpers { }; if wasmtime_continuations::ENABLE_DEBUG_PRINTING { - // TODO(dhil): This needs to be rewritten to drop the regex dependency. - // Regex matching { followed by something without { or } followed by }. - //let placeholder_re = regex::Regex::new(r"\{[^{}]*\}").unwrap(); - - let prev_end = 0; - let i = 0; - // for ph_match in placeholder_re.find_iter(s) { - // let start = ph_match.start(); - // let end = ph_match.end(); - - // assert!( - // i < vals.len(), - // "Must supply as many entries in vals as there are placeholders in the string" - // ); - - // print_s_infix(env, builder, prev_end, start); - // match ph_match.as_str() { - // "{}" => print_int(env, builder, vals[i]), - // "{:p}" => print_pointer(env, builder, vals[i]), - // u => panic!("Unsupported placeholder in debug_print input string: {}", u), - // } - // prev_end = end; - // i += 1; - // } + let mut prev_end = 0; + let mut i = 0; + + let mut ph_matches: Vec<(usize, &'static str)> = s + .match_indices("{}") + .chain(s.match_indices("{:p}")) + .collect(); + ph_matches.sort_by_key(|(index, _)| *index); + + for (start, matched_ph) in ph_matches { + let end = start + matched_ph.len(); + + assert!( + i < vals.len(), + "Must supply as many entries in vals as there are placeholders in the string" + ); + + print_s_infix(env, builder, prev_end, start); + match matched_ph { + "{}" => print_int(env, builder, vals[i]), + "{:p}" => print_pointer(env, builder, vals[i]), + u => panic!("Unsupported placeholder in debug_print input string: {}", u), + } + prev_end = end; + i += 1; + } assert_eq!( i, vals.len(), @@ -296,7 +298,7 @@ mod typed_continuation_helpers { /// * `msg` : String literal, containing placeholders like those supported by println! /// * remaining arguments: ir::Values filled into the placeholders in `msg` #[allow(unused_macros)] - macro_rules! _emit_debug_println { + macro_rules! emit_debug_println { ($env : expr, $builder : expr, $msg : literal, $( $arg:expr ),*) => { let msg_newline : &'static str= std::concat!( $msg, @@ -420,7 +422,7 @@ mod typed_continuation_helpers { }; } - macro_rules! _emit_debug_assert_eq { + macro_rules! emit_debug_assert_eq { ($env: expr, $builder: expr, $v1 : expr, $v2: expr) => { emit_debug_assert_icmp!($env, $builder, IntCC::Equal, "==", $v1, $v2); }; @@ -700,16 +702,16 @@ mod typed_continuation_helpers { let zero = builder.ins().iconst(ir::types::I64, 0); emit_debug_assert_ne!(env, builder, required_capacity, zero); - // if cfg!(debug_assertions) { - // let data = self.get_data(builder); - // emit_debug_println!( - // env, - // builder, - // "[ensure_capacity] contobj/base {:p}, buffer is {:p}", - // self.base, - // data - // ); - // } + if cfg!(debug_assertions) { + let data = self.get_data(builder); + emit_debug_println!( + env, + builder, + "[ensure_capacity] contobj/base {:p}, buffer is {:p}", + self.base, + data + ); + } let capacity = self.get_capacity(builder); @@ -733,19 +735,19 @@ mod typed_continuation_helpers { builder.switch_to_block(insufficient_capacity_block); builder.seal_block(insufficient_capacity_block); - // emit_debug_println!( - // env, - // builder, - // "[ensure_capacity] need to increase capacity from {} to {}", - // capacity, - // required_capacity - // ); + emit_debug_println!( + env, + builder, + "[ensure_capacity] need to increase capacity from {} to {}", + capacity, + required_capacity + ); - // if cfg!(debug_assertions) { - // // We must only re-allocate while there is no data in the buffer. - // let length = self.get_length(builder); - // emit_debug_assert_eq!(env, builder, length, zero); - // } + if cfg!(debug_assertions) { + // We must only re-allocate while there is no data in the buffer. + let length = self.get_length(builder); + emit_debug_assert_eq!(env, builder, length, zero); + } let align = builder.ins().iconst( I64, @@ -783,15 +785,15 @@ mod typed_continuation_helpers { /// Loading starts at index 0 of the Payloads object. pub fn load_data_entries<'a>( &self, - _env: &mut crate::func_environ::FuncEnvironment<'a>, + env: &mut crate::func_environ::FuncEnvironment<'a>, builder: &mut FunctionBuilder, load_types: &[ir::Type], ) -> Vec { - // if cfg!(debug_assertions) { - // let length = self.get_length(builder); - // let load_count = builder.ins().iconst(I64, load_types.len() as i64); - // emit_debug_assert_ule!(env, builder, load_count, length); - // } + if cfg!(debug_assertions) { + let length = self.get_length(builder); + let load_count = builder.ins().iconst(I64, load_types.len() as i64); + emit_debug_assert_ule!(env, builder, load_count, length); + } let memflags = ir::MemFlags::trusted(); @@ -816,19 +818,19 @@ mod typed_continuation_helpers { /// entries. pub fn store_data_entries<'a>( &self, - _env: &mut crate::func_environ::FuncEnvironment<'a>, + env: &mut crate::func_environ::FuncEnvironment<'a>, builder: &mut FunctionBuilder, values: &[ir::Value], ) { let store_count = builder.ins().iconst(I64, values.len() as i64); - // if cfg!(debug_assertions) { - // let capacity = self.get_capacity(builder); - // let length = self.get_length(builder); - // let zero = builder.ins().iconst(I64, 0); - // emit_debug_assert_ule!(env, builder, store_count, capacity); - // emit_debug_assert_eq!(env, builder, length, zero); - // } + if cfg!(debug_assertions) { + let capacity = self.get_capacity(builder); + let length = self.get_length(builder); + let zero = builder.ins().iconst(I64, 0); + emit_debug_assert_ule!(env, builder, store_count, capacity); + emit_debug_assert_eq!(env, builder, length, zero); + } let memflags = ir::MemFlags::trusted();