Skip to content

Commit 68c4384

Browse files
committed
add wide-arithmetic execution handlers
1 parent 2138a8a commit 68c4384

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

crates/wasmi/src/engine/executor/handler/dispatch.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@ pub fn op_code_to_handler(code: OpCode) -> Handler {
457457
OpCode::TableSet_Ii => exec::table_set_ii,
458458
OpCode::ElemDrop => exec::elem_drop,
459459
OpCode::RefFunc => exec::ref_func,
460+
// wide-arithmetic
461+
OpCode::I64Add128 => exec::i64_add128,
462+
OpCode::I64Sub128 => exec::i64_sub128,
463+
OpCode::I64MulWide => exec::i64_mul_wide,
464+
OpCode::U64MulWide => exec::u64_mul_wide,
460465
// unconditional branch
461466
OpCode::Branch => exec::branch,
462467
OpCode::BranchTable => exec::branch_table,

crates/wasmi/src/engine/executor/handler/exec.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,68 @@ pub fn ref_func(
923923
dispatch!(state, ip, sp, mem0, mem0_len, instance)
924924
}
925925

926+
macro_rules! impl_i64_binop128 {
927+
(
928+
$( fn $handler:ident($op:ident) = $eval:expr );* $(;)?
929+
) => {
930+
$(
931+
pub fn $handler(
932+
state: &mut VmState,
933+
ip: Ip,
934+
sp: Sp,
935+
mem0: Mem0Ptr,
936+
mem0_len: Mem0Len,
937+
instance: Inst,
938+
) -> Done {
939+
let (ip, crate::ir::decode::$op { results, lhs_lo, lhs_hi, rhs_lo, rhs_hi }) = unsafe { decode_op(ip) };
940+
let lhs_lo: i64 = get_value(lhs_lo, sp);
941+
let lhs_hi: i64 = get_value(lhs_hi, sp);
942+
let rhs_lo: i64 = get_value(rhs_lo, sp);
943+
let rhs_hi: i64 = get_value(rhs_hi, sp);
944+
let results = results.to_array();
945+
let (result_lo, result_hi) = $eval(lhs_lo, lhs_hi, rhs_lo, rhs_hi);
946+
set_value(sp, results[0], result_lo);
947+
set_value(sp, results[1], result_hi);
948+
dispatch!(state, ip, sp, mem0, mem0_len, instance)
949+
}
950+
)*
951+
};
952+
}
953+
impl_i64_binop128! {
954+
fn i64_add128(I64Add128) = wasm::i64_add128;
955+
fn i64_sub128(I64Sub128) = wasm::i64_sub128;
956+
}
957+
958+
macro_rules! impl_i64_mul_wide {
959+
(
960+
$( fn $handler:ident($op:ident) = $eval:expr );* $(;)?
961+
) => {
962+
$(
963+
pub fn $handler(
964+
state: &mut VmState,
965+
ip: Ip,
966+
sp: Sp,
967+
mem0: Mem0Ptr,
968+
mem0_len: Mem0Len,
969+
instance: Inst,
970+
) -> Done {
971+
let (ip, crate::ir::decode::$op { results, lhs, rhs }) = unsafe { decode_op(ip) };
972+
let lhs: i64 = get_value(lhs, sp);
973+
let rhs: i64 = get_value(rhs, sp);
974+
let (result_lo, result_hi) = $eval(lhs, rhs);
975+
let results = results.to_array();
976+
set_value(sp, results[0], result_lo);
977+
set_value(sp, results[1], result_hi);
978+
dispatch!(state, ip, sp, mem0, mem0_len, instance)
979+
}
980+
)*
981+
};
982+
}
983+
impl_i64_mul_wide! {
984+
fn i64_mul_wide(I64MulWide) = wasm::i64_mul_wide_s;
985+
fn u64_mul_wide(U64MulWide) = wasm::i64_mul_wide_u;
986+
}
987+
926988
/// Fetches the branch table index value and normalizes it to clamp between `0..len_targets`.
927989
fn fetch_branch_table_target(sp: Sp, index: Slot, len_targets: u32) -> usize {
928990
let index: u32 = get_value(index, sp);

crates/wast/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ macro_rules! foreach_test {
182182
fn spec_utf8_import_field("spec/utf8-import-field");
183183
fn spec_utf8_import_module("spec/utf8-import-module");
184184
fn spec_utf8_invalid_encoding("spec/utf8-invalid-encoding");
185-
// fn spec_wide_arithmetic("spec/proposals/wide-arithmetic/wide-arithmetic");
185+
fn spec_wide_arithmetic("spec/proposals/wide-arithmetic/wide-arithmetic");
186186

187187
// Wasmi specific test cases and regression tests.
188188
fn wasmi_fibonacci("wasmi/tests/fibonacci");

0 commit comments

Comments
 (0)