From a932920dc339359336c1177b80042a813c06ddf5 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 5 Jan 2024 20:26:47 +0000 Subject: [PATCH] Add memory.copy and memory.fill support --- src/backend/mod.rs | 9 +++++++++ src/op_traits.rs | 10 ++++++++++ src/ops.rs | 14 ++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 5046953..8f3dbdf 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -514,6 +514,15 @@ impl<'a> WasmFuncBackend<'a> { Operator::MemoryGrow { mem } => { Some(wasm_encoder::Instruction::MemoryGrow(mem.index() as u32)) } + Operator::MemoryCopy { dst_mem, src_mem } => { + Some(wasm_encoder::Instruction::MemoryCopy { + src_mem: src_mem.index() as u32, + dst_mem: dst_mem.index() as u32, + }) + } + Operator::MemoryFill { mem } => { + Some(wasm_encoder::Instruction::MemoryFill(mem.index() as u32)) + } }; if let Some(inst) = inst { diff --git a/src/op_traits.rs b/src/op_traits.rs index 8e6a887..2cb11e3 100644 --- a/src/op_traits.rs +++ b/src/op_traits.rs @@ -221,6 +221,8 @@ pub fn op_inputs( Operator::TableSize { .. } => Ok(Cow::Borrowed(&[])), Operator::MemorySize { .. } => Ok(Cow::Borrowed(&[])), Operator::MemoryGrow { .. } => Ok(Cow::Borrowed(&[Type::I32])), + Operator::MemoryCopy { .. } => Ok(Cow::Borrowed(&[Type::I32, Type::I32, Type::I32])), + Operator::MemoryFill { .. } => Ok(Cow::Borrowed(&[Type::I32, Type::I32, Type::I32])), } } @@ -425,6 +427,8 @@ pub fn op_outputs( Operator::TableSize { .. } => Ok(Cow::Borrowed(&[Type::I32])), Operator::MemorySize { .. } => Ok(Cow::Borrowed(&[Type::I32])), Operator::MemoryGrow { .. } => Ok(Cow::Borrowed(&[Type::I32])), + Operator::MemoryCopy { .. } => Ok(Cow::Borrowed(&[])), + Operator::MemoryFill { .. } => Ok(Cow::Borrowed(&[])), } } @@ -635,6 +639,8 @@ impl Operator { Operator::TableSize { .. } => &[ReadTable], Operator::MemorySize { .. } => &[ReadMem], Operator::MemoryGrow { .. } => &[WriteMem, Trap], + Operator::MemoryCopy { .. } => &[Trap, ReadMem, WriteMem], + Operator::MemoryFill { .. } => &[Trap, WriteMem], } } @@ -853,6 +859,10 @@ impl std::fmt::Display for Operator { Operator::TableSize { table_index, .. } => write!(f, "table_size<{}>", table_index)?, Operator::MemorySize { mem } => write!(f, "memory_size<{}>", mem)?, Operator::MemoryGrow { mem } => write!(f, "memory_grow<{}>", mem)?, + Operator::MemoryCopy { dst_mem, src_mem } => { + write!(f, "memory_copy<{}, {}>", dst_mem, src_mem)? + } + Operator::MemoryFill { mem } => write!(f, "memory_fill<{}>", mem)?, } Ok(()) diff --git a/src/ops.rs b/src/ops.rs index ea7851d..f63a0bd 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -295,6 +295,13 @@ pub enum Operator { MemoryGrow { mem: Memory, }, + MemoryCopy { + dst_mem: Memory, + src_mem: Memory, + }, + MemoryFill { + mem: Memory, + }, } #[test] @@ -568,6 +575,13 @@ impl<'a, 'b> std::convert::TryFrom<&'b wasmparser::Operator<'a>> for Operator { &wasmparser::Operator::MemoryGrow { mem, .. } => Ok(Operator::MemoryGrow { mem: Memory::from(mem), }), + &wasmparser::Operator::MemoryCopy { dst_mem, src_mem } => Ok(Operator::MemoryCopy { + dst_mem: Memory::from(dst_mem), + src_mem: Memory::from(src_mem), + }), + &wasmparser::Operator::MemoryFill { mem } => { + Ok(Operator::MemoryFill { mem: Memory::from(mem) }) + } _ => Err(()), } }