Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions crates/celox-backend-cranelift/src/translator/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,24 @@ impl SIRTranslator {
} else {
r
};
// Logical shifts by at least the physical width produce zero.
// `r` is zero-extended from a logical width no wider than
// `common_ty`, so this compares the real count without truncation.
let logical_shift_in_range = if matches!(op, BinaryOp::Shl | BinaryOp::Shr) {
Some(state.builder.ins().icmp_imm_u(
IntCC::UnsignedLessThan,
r,
i64::from(common_ty.bits()),
))
} else {
None
};
let saturate_logical_shift = |builder: &mut FunctionBuilder, shifted: Value| {
let in_range =
logical_shift_in_range.expect("logical shift count range is computed");
let zero = builder.ins().iconst(common_ty, 0);
builder.ins().select(in_range, shifted, zero)
};

let build_icmp = |builder: &mut FunctionBuilder, cc: IntCC| {
let b1_res = builder.ins().icmp(cc, l, r);
Expand Down Expand Up @@ -424,11 +442,13 @@ impl SIRTranslator {
BinaryOp::Or => state.builder.ins().bor(l, r),
BinaryOp::Xor => state.builder.ins().bxor(l, r),
BinaryOp::Shr => {
let shifted = state.builder.ins().ushr(l, r);
let raw = state.builder.ins().ushr(l, r);
let shifted = saturate_logical_shift(state.builder, raw);
apply_d_width_mask(state, shifted, common_ty, d_width)
}
BinaryOp::Shl => {
let shifted = state.builder.ins().ishl(l, r);
let raw = state.builder.ins().ishl(l, r);
let shifted = saturate_logical_shift(state.builder, raw);
apply_d_width_mask(state, shifted, common_ty, d_width)
}
BinaryOp::Sar => {
Expand Down Expand Up @@ -510,7 +530,8 @@ impl SIRTranslator {
BinaryOp::Shr => {
let zero = state.builder.ins().iconst(common_ty, 0);
let shift_amt_has_x = state.builder.ins().icmp(IntCC::NotEqual, r_m, zero);
let shifted_m = state.builder.ins().ushr(l_m, r);
let raw_m = state.builder.ins().ushr(l_m, r);
let shifted_m = saturate_logical_shift(state.builder, raw_m);
let all_ones = state.builder.ins().iconst(common_ty, -1);
let m = state
.builder
Expand All @@ -521,7 +542,8 @@ impl SIRTranslator {
BinaryOp::Shl => {
let zero = state.builder.ins().iconst(common_ty, 0);
let shift_amt_has_x = state.builder.ins().icmp(IntCC::NotEqual, r_m, zero);
let shifted_m = state.builder.ins().ishl(l_m, r);
let raw_m = state.builder.ins().ishl(l_m, r);
let shifted_m = saturate_logical_shift(state.builder, raw_m);
let all_ones = state.builder.ins().iconst(common_ty, -1);
let m = state
.builder
Expand Down
22 changes: 20 additions & 2 deletions crates/celox-backend-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,9 +1648,11 @@ fn compile_binary_narrow(
BinaryOp::Xor => instrs.push(Instruction::I64Xor),
BinaryOp::Shl => {
instrs.push(Instruction::I64Shl);
emit_logical_shift_saturation(instrs, r.value_idx);
}
BinaryOp::Shr => {
instrs.push(Instruction::I64ShrU);
emit_logical_shift_saturation(instrs, r.value_idx);
}
BinaryOp::Sar => {
// Arithmetic shift right — need sign extension first.
Expand Down Expand Up @@ -2629,8 +2631,14 @@ fn compile_binary_mask_narrow(
instrs.push(Instruction::LocalGet(rhs.value_idx));
}
match op {
BinaryOp::Shl => instrs.push(Instruction::I64Shl),
BinaryOp::Shr => instrs.push(Instruction::I64ShrU),
BinaryOp::Shl => {
instrs.push(Instruction::I64Shl);
emit_logical_shift_saturation(instrs, rhs.value_idx);
}
BinaryOp::Shr => {
instrs.push(Instruction::I64ShrU);
emit_logical_shift_saturation(instrs, rhs.value_idx);
}
BinaryOp::Sar => instrs.push(Instruction::I64ShrS),
_ => unreachable!(),
}
Expand Down Expand Up @@ -5947,6 +5955,16 @@ fn emit_sar_count(instrs: &mut Vec<Instruction<'static>>, count: u32) {
instrs.push(Instruction::Select);
}

/// Replaces the shifted value on the stack with zero when the logical shift
/// count is at least 64. Wasm shifts use the count modulo 64.
fn emit_logical_shift_saturation(instrs: &mut Vec<Instruction<'static>>, count: u32) {
instrs.push(Instruction::I64Const(0));
instrs.push(Instruction::LocalGet(count));
instrs.push(Instruction::I64Const(64));
instrs.push(Instruction::I64LtU);
instrs.push(Instruction::Select);
}

fn emit_sign_extend(instrs: &mut Vec<Instruction<'static>>, local_idx: u32, width: usize) {
instrs.push(Instruction::LocalGet(local_idx));
if width < 64 {
Expand Down
Loading
Loading