fix skipping immediates for memory instructions

This commit is contained in:
Folkert 2023-07-08 23:07:15 +02:00
parent c7ccc2092a
commit f813591042
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
2 changed files with 74 additions and 36 deletions

View file

@ -192,6 +192,29 @@ impl From<u8> for OpCode {
}
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MemoryInstruction {
MemoryInit = 8,
DataDrop = 9,
MemoryCopy = 10,
MemoryFill = 11,
}
impl TryFrom<u8> for MemoryInstruction {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
8 => Ok(Self::MemoryInit),
9 => Ok(Self::DataDrop),
10 => Ok(Self::MemoryCopy),
11 => Ok(Self::MemoryFill),
_ => Err(value),
}
}
}
/// The format of the *immediate* operands of an operator
/// Immediates appear directly in the byte stream after the opcode,
/// rather than being popped off the value stack. These are the possible forms.
@ -205,6 +228,7 @@ enum OpImmediates {
Leb64x1,
Leb32x2,
BrTable,
Memory,
}
fn immediates_for(op: OpCode) -> Result<OpImmediates, String> {
@ -233,7 +257,7 @@ fn immediates_for(op: OpCode) -> Result<OpImmediates, String> {
| I64STORE32 => Leb32x2,
CURRENTMEMORY | GROWMEMORY => Byte1,
MEMORY => Leb32x2,
MEMORY => Memory,
I32CONST => Leb32x1,
I64CONST => Leb64x1,
@ -314,6 +338,29 @@ impl SkipBytes for OpCode {
u32::skip_bytes(bytes, cursor)?;
}
}
Memory => {
match MemoryInstruction::try_from(bytes[*cursor + 1]) {
Ok(op) => match op {
MemoryInstruction::MemoryInit => {
// memory.init
todo!("WASM instruction: memory.init")
}
MemoryInstruction::DataDrop => {
// data.drop x
todo!("WASM instruction: data.drop")
}
MemoryInstruction::MemoryCopy => {
// memory.copy
*cursor += 1 + 1 + 2;
}
MemoryInstruction::MemoryFill => {
// memory.fill
*cursor += 1 + 1 + 1;
}
},
Err(other) => unreachable!("invalid memory instruction {other:?}"),
}
}
}
Ok(())
}