Merge pull request #26 from bainchild/trunk
Attempt to add memory copy/fill (allocator edition)
This commit is contained in:
@@ -795,6 +795,19 @@ do
|
||||
return old
|
||||
end
|
||||
end
|
||||
|
||||
function allocator.copy(memory, destination, source, length)
|
||||
local destination_addr = by_offset(memory.data, destination)
|
||||
local source_addr = by_offset(memory.data, source)
|
||||
|
||||
ffi.copy(destination_addr, source_addr, length)
|
||||
end
|
||||
|
||||
function allocator.fill(memory, address, value, length)
|
||||
local start = by_offset(memory.data, address)
|
||||
|
||||
ffi.fill(start, length, value)
|
||||
end
|
||||
|
||||
module.load = load
|
||||
module.store = store
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -944,6 +944,20 @@ do
|
||||
return old
|
||||
end
|
||||
end
|
||||
|
||||
function allocator.copy(memory, destination, source, length)
|
||||
for i = 1, length do
|
||||
local v = load_byte(memory, source + i - 1)
|
||||
|
||||
store_byte(memory, destination + i - 1, v)
|
||||
end
|
||||
end
|
||||
|
||||
function allocator.fill(memory, destination, value, length)
|
||||
for i = 1, length do
|
||||
store_byte(memory, destination + i - 1, value)
|
||||
end
|
||||
end
|
||||
|
||||
module.load = load
|
||||
module.store = store
|
||||
@@ -951,3 +965,4 @@ do
|
||||
end
|
||||
|
||||
return module
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -301,6 +301,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();
|
||||
let size = self.size();
|
||||
|
||||
write!(w, "rt.store.copy(memory_at_0, {dst}, {src}, ")?;
|
||||
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.store.copy(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)?;
|
||||
@@ -320,6 +346,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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user