Attempt to add memory copy/fill (2)

This commit is contained in:
bainchild
2023-01-03 01:02:26 -06:00
parent 2f8eadad6c
commit 50b93641f2
5 changed files with 138 additions and 5 deletions
+30 -2
View File
@@ -4,8 +4,8 @@ use std::{
};
use wasm_ast::node::{
Block, Br, BrIf, BrTable, Call, CallIndirect, FuncData, If, LabelType, MemoryGrow, SetGlobal,
SetLocal, SetTemporary, Statement, StoreAt, Terminator,
Block, Br, BrIf, BrTable, Call, CallIndirect, FuncData, If, LabelType, MemoryGrow, MemoryCopy,
MemoryFill, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, Terminator,
};
use wasmparser::ValType;
@@ -282,6 +282,32 @@ impl DriverNoContext for MemoryGrow {
}
}
impl DriverNoContext for MemoryCopy {
fn write(&self, w: &mut dyn Write) -> Result<()> {
let dst = self.dst();
let src = self.src();
write!(w, "rt.allocator.copy(memory_at_0, {dst}, {src}, ")?;
self.size().write(w)?;
write!(w, ")")
}
}
impl DriverNoContext for MemoryFill {
fn write(&self, w: &mut dyn Write) -> Result<()> {
let mem = self.mem();
let value = self.value();
let n = self.n();
write!(w, "rt.allocator.fill(memory_at_0, {mem}, ")?;
value.write(w)?;
write!(w, ", ")?;
n.write(w)?;
write!(w, ")")
}
}
fn write_stat(stat: &dyn DriverNoContext, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
indentation!(mng, w)?;
stat.write(w)?;
@@ -301,6 +327,8 @@ impl Driver for Statement {
Self::SetGlobal(s) => write_stat(s, mng, w),
Self::StoreAt(s) => write_stat(s, mng, w),
Self::MemoryGrow(s) => write_stat(s, mng, w),
Self::MemoryCopy(s) => write_stat(s, mng, w),
Self::MemoryFill(s) => write_stat(s, mng, w)
}
}
}
+15
View File
@@ -944,6 +944,20 @@ do
return old
end
end
function allocator.copy(memory, dst, src, len)
for i = 1, len do
local v = load_byte(memory, src + i - 1)
store_byte(memory, dst + i - 1, v)
end
end
function allocator.fill(memory, dst, value, len)
for i = 1, len do
store_byte(memory, dst + i - 1, value)
end
end
module.load = load
module.store = store
@@ -951,3 +965,4 @@ do
end
return module