Skip to content

Commit

Permalink
Revive emit_debug_print (bytecodealliance#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-emrich committed Jan 29, 2024
1 parent 1ef4c1a commit bd81f03
Showing 1 changed file with 70 additions and 68 deletions.
138 changes: 70 additions & 68 deletions crates/cranelift/src/func_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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(),
Expand All @@ -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,
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -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<ir::Value> {
// 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();

Expand All @@ -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();

Expand Down

0 comments on commit bd81f03

Please sign in to comment.