From 8de5ac71e7725953c03b6c138c6f88514d4db020 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 21 May 2018 14:15:09 +0300 Subject: [PATCH 1/3] rename Opcode to Instruction --- examples/inject.rs | 12 +-- src/builder/code.rs | 8 +- src/builder/data.rs | 6 +- src/builder/global.rs | 6 +- src/builder/memory.rs | 4 +- src/builder/module.rs | 8 +- src/builder/table.rs | 4 +- src/elements/func.rs | 30 ++++---- src/elements/global_entry.rs | 4 +- src/elements/mod.rs | 2 +- src/elements/module.rs | 4 +- src/elements/ops.rs | 139 +++++++++++++++++------------------ src/elements/section.rs | 6 +- 13 files changed, 116 insertions(+), 117 deletions(-) diff --git a/examples/inject.rs b/examples/inject.rs index b008f84..0e58e23 100644 --- a/examples/inject.rs +++ b/examples/inject.rs @@ -5,21 +5,21 @@ use std::env; use parity_wasm::elements; use parity_wasm::builder; -pub fn inject_nop(opcodes: &mut elements::Opcodes) { - use parity_wasm::elements::Opcode::*; - let opcodes = opcodes.elements_mut(); +pub fn inject_nop(instructions: &mut elements::Instructions) { + use parity_wasm::elements::Instruction::*; + let instructions = instructions.elements_mut(); let mut position = 0; loop { - let need_inject = match &opcodes[position] { + let need_inject = match &instructions[position] { &Block(_) | &If(_) => true, _ => false, }; if need_inject { - opcodes.insert(position + 1, Nop); + instructions.insert(position + 1, Nop); } position += 1; - if position >= opcodes.len() { + if position >= instructions.len() { break; } } diff --git a/src/builder/code.rs b/src/builder/code.rs index 0c736f6..cc64562 100644 --- a/src/builder/code.rs +++ b/src/builder/code.rs @@ -224,7 +224,7 @@ impl FuncBodyBuilder { pub fn with_callback(callback: F) -> Self { FuncBodyBuilder { callback: callback, - body: elements::FuncBody::new(Vec::new(), elements::Opcodes::empty()), + body: elements::FuncBody::new(Vec::new(), elements::Instructions::empty()), } } } @@ -243,8 +243,8 @@ impl FuncBodyBuilder where F: Invoke { } /// Set code of the function - pub fn with_opcodes(mut self, opcodes: elements::Opcodes) -> Self { - *self.body.code_mut() = opcodes; + pub fn with_instructions(mut self, instructions: elements::Instructions) -> Self { + *self.body.code_mut() = instructions; self } @@ -402,7 +402,7 @@ mod tests { .return_type().i32() .build() .body() - .with_opcodes(elements::Opcodes::empty()) + .with_instructions(elements::Instructions::empty()) .build() .build(); diff --git a/src/builder/data.rs b/src/builder/data.rs index 31eddb7..b3dd60c 100644 --- a/src/builder/data.rs +++ b/src/builder/data.rs @@ -29,9 +29,9 @@ impl DataSegmentBuilder { } } - /// Set offset initialization opcode. `End` opcode will be added automatically. - pub fn offset(mut self, opcode: elements::Opcode) -> Self { - self.offset = elements::InitExpr::new(vec![opcode, elements::Opcode::End]); + /// Set offset initialization instruction. `End` instruction will be added automatically. + pub fn offset(mut self, instruction: elements::Instruction) -> Self { + self.offset = elements::InitExpr::new(vec![instruction, elements::Instruction::End]); self } diff --git a/src/builder/global.rs b/src/builder/global.rs index 5685d08..71f9313 100644 --- a/src/builder/global.rs +++ b/src/builder/global.rs @@ -40,9 +40,9 @@ impl GlobalBuilder { self } - /// Set initialization expression opcode for this global (`end` opcode will be added automatically) - pub fn init_expr(mut self, opcode: elements::Opcode) -> Self { - self.init_expr = elements::InitExpr::new(vec![opcode, elements::Opcode::End]); + /// Set initialization expression instruction for this global (`end` instruction will be added automatically) + pub fn init_expr(mut self, instruction: elements::Instruction) -> Self { + self.init_expr = elements::InitExpr::new(vec![instruction, elements::Instruction::End]); self } diff --git a/src/builder/memory.rs b/src/builder/memory.rs index eeb2e20..822ba14 100644 --- a/src/builder/memory.rs +++ b/src/builder/memory.rs @@ -60,8 +60,8 @@ impl MemoryBuilder where F: Invoke { pub fn with_data(mut self, index: u32, values: Vec) -> Self { self.memory.data.push(MemoryDataDefinition { offset: elements::InitExpr::new(vec![ - elements::Opcode::I32Const(index as i32), - elements::Opcode::End, + elements::Instruction::I32Const(index as i32), + elements::Instruction::End, ]), values: values, }); diff --git a/src/builder/module.rs b/src/builder/module.rs index cd4efa8..1f10ac8 100644 --- a/src/builder/module.rs +++ b/src/builder/module.rs @@ -336,7 +336,7 @@ impl ModuleBuilder where F: Invoke { /// # Examples /// ``` /// use parity_wasm::builder::module; - /// use parity_wasm::elements::Opcode::*; + /// use parity_wasm::elements::Instruction::*; /// /// let module = module() /// .global() @@ -359,7 +359,7 @@ impl ModuleBuilder where F: Invoke { /// # Examples /// ``` /// use parity_wasm::builder::module; - /// use parity_wasm::elements::Opcode::*; + /// use parity_wasm::elements::Instruction::*; /// /// let module = module() /// .global() @@ -551,7 +551,7 @@ mod tests { #[test] fn global() { let module = module() - .global().value_type().i64().mutable().init_expr(::elements::Opcode::I64Const(5)).build() + .global().value_type().i64().mutable().init_expr(::elements::Instruction::I64Const(5)).build() .build(); assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1); @@ -561,7 +561,7 @@ mod tests { fn data() { let module = module() .data() - .offset(::elements::Opcode::I32Const(16)) + .offset(::elements::Instruction::I32Const(16)) .value(vec![0u8, 15, 10, 5, 25]) .build() .build(); diff --git a/src/builder/table.rs b/src/builder/table.rs index e612dad..b30c808 100644 --- a/src/builder/table.rs +++ b/src/builder/table.rs @@ -60,8 +60,8 @@ impl TableBuilder where F: Invoke { pub fn with_element(mut self, index: u32, values: Vec) -> Self { self.table.elements.push(TableEntryDefinition { offset: elements::InitExpr::new(vec![ - elements::Opcode::I32Const(index as i32), - elements::Opcode::End, + elements::Instruction::I32Const(index as i32), + elements::Instruction::End, ]), values: values, }); diff --git a/src/elements/func.rs b/src/elements/func.rs index d7077fd..8c9c217 100644 --- a/src/elements/func.rs +++ b/src/elements/func.rs @@ -1,7 +1,7 @@ use io; use std::vec::Vec; use super::{ - Deserialize, Error, ValueType, VarUint32, CountedList, Opcodes, + Deserialize, Error, ValueType, VarUint32, CountedList, Instructions, Serialize, CountedWriter, CountedListWriter, }; use elements::section::SectionReader; @@ -85,32 +85,32 @@ impl Serialize for Local { #[derive(Debug, Clone, PartialEq)] pub struct FuncBody { locals: Vec, - opcodes: Opcodes, + instructions: Instructions, } impl FuncBody { - /// New function body with given `locals` and `opcodes` - pub fn new(locals: Vec, opcodes: Opcodes) -> Self { - FuncBody { locals: locals, opcodes: opcodes } + /// New function body with given `locals` and `instructions` + pub fn new(locals: Vec, instructions: Instructions) -> Self { + FuncBody { locals: locals, instructions: instructions } } - /// List of individual opcodes + /// List of individual instructions pub fn empty() -> Self { - FuncBody { locals: Vec::new(), opcodes: Opcodes::empty() } + FuncBody { locals: Vec::new(), instructions: Instructions::empty() } } /// Locals declared in function body. pub fn locals(&self) -> &[Local] { &self.locals } - /// Opcode sequence of the function body. Minimal opcode sequence - /// is just `&[Opcode::End]` - pub fn code(&self) -> &Opcodes { &self.opcodes } + /// Instruction list of the function body. Minimal instruction list + /// is just `&[Instruction::End]` + pub fn code(&self) -> &Instructions { &self.instructions } /// Locals declared in function body (mutable). pub fn locals_mut(&mut self) -> &mut Vec { &mut self.locals } - /// Opcode sequence of the function body (mutable). - pub fn code_mut(&mut self) -> &mut Opcodes { &mut self.opcodes } + /// Instruction list of the function body (mutable). + pub fn code_mut(&mut self) -> &mut Instructions { &mut self.instructions } } impl Deserialize for FuncBody { @@ -120,9 +120,9 @@ impl Deserialize for FuncBody { // todo: maybe use reader.take(section_length) let mut body_reader = SectionReader::new(reader)?; let locals: Vec = CountedList::::deserialize(&mut body_reader)?.into_inner(); - let opcodes = Opcodes::deserialize(&mut body_reader)?; + let instructions = Instructions::deserialize(&mut body_reader)?; body_reader.close()?; - Ok(FuncBody { locals: locals, opcodes: opcodes }) + Ok(FuncBody { locals: locals, instructions: instructions }) } } @@ -139,7 +139,7 @@ impl Serialize for FuncBody { ); counted_list.serialize(&mut counted_writer)?; - let code = self.opcodes; + let code = self.instructions; code.serialize(&mut counted_writer)?; counted_writer.done()?; diff --git a/src/elements/global_entry.rs b/src/elements/global_entry.rs index e4e3755..1678328 100644 --- a/src/elements/global_entry.rs +++ b/src/elements/global_entry.rs @@ -18,11 +18,11 @@ impl GlobalEntry { } /// Global type. pub fn global_type(&self) -> &GlobalType { &self.global_type } - /// Initialization expression (opcodes) for global. + /// Initialization expression (instructions) for global. pub fn init_expr(&self) -> &InitExpr { &self.init_expr } /// Global type (mutable) pub fn global_type_mut(&mut self) -> &mut GlobalType { &mut self.global_type } - /// Initialization expression (opcodes) for global (mutable) + /// Initialization expression (instructions) for global (mutable) pub fn init_expr_mut(&mut self) -> &mut InitExpr { &mut self.init_expr } } diff --git a/src/elements/mod.rs b/src/elements/mod.rs index 7e1bd9f..25aafce 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -50,7 +50,7 @@ pub use self::primitives::{ Uint64, VarUint64, CountedList, CountedWriter, CountedListWriter, }; pub use self::types::{Type, ValueType, BlockType, FunctionType, TableElementType}; -pub use self::ops::{Opcode, Opcodes, InitExpr}; +pub use self::ops::{Instruction, Instructions, InitExpr}; pub use self::func::{Func, FuncBody, Local}; pub use self::segment::{ElementSegment, DataSegment}; pub use self::index_map::IndexMap; diff --git a/src/elements/module.rs b/src/elements/module.rs index 78a6934..f17c202 100644 --- a/src/elements/module.rs +++ b/src/elements/module.rs @@ -584,7 +584,7 @@ mod integration_tests { #[test] fn const_() { - use super::super::Opcode::*; + use super::super::Instruction::*; let module = deserialize_file("./res/cases/v1/const.wasm").expect("Should be deserialized"); let func = &module.code_section().expect("Code section to exist").bodies()[0]; @@ -612,7 +612,7 @@ mod integration_tests { #[test] fn store() { - use super::super::Opcode::*; + use super::super::Instruction::*; let module = deserialize_file("./res/cases/v1/offset.wasm").expect("Should be deserialized"); let func = &module.code_section().expect("Code section to exist").bodies()[0]; diff --git a/src/elements/ops.rs b/src/elements/ops.rs index b293060..450fdbd 100644 --- a/src/elements/ops.rs +++ b/src/elements/ops.rs @@ -9,104 +9,103 @@ use super::{ VarInt32, VarInt64, }; -/// Collection of opcodes (usually inside a block section). +/// List of instructions (usually inside a block section). #[derive(Debug, Clone, PartialEq)] -pub struct Opcodes(Vec); +pub struct Instructions(Vec); -impl Opcodes { - /// New list of opcodes from vector of opcodes. - pub fn new(elements: Vec) -> Self { - Opcodes(elements) +impl Instructions { + /// New list of instructions from vector of instructions. + pub fn new(elements: Vec) -> Self { + Instructions(elements) } - /// Empty expression with only `Opcode::End` opcode. + /// Empty expression with only `Instruction::End` instruction. pub fn empty() -> Self { - Opcodes(vec![Opcode::End]) + Instructions(vec![Instruction::End]) } - /// List of individual opcodes. - pub fn elements(&self) -> &[Opcode] { &self.0 } + /// List of individual instructions. + pub fn elements(&self) -> &[Instruction] { &self.0 } - /// Individual opcodes, mutable. - pub fn elements_mut(&mut self) -> &mut Vec { &mut self.0 } + /// Individual instructions, mutable. + pub fn elements_mut(&mut self) -> &mut Vec { &mut self.0 } } -impl Deserialize for Opcodes { +impl Deserialize for Instructions { type Error = Error; fn deserialize(reader: &mut R) -> Result { - let mut opcodes = Vec::new(); + let mut instructions = Vec::new(); let mut block_count = 1usize; loop { - let opcode = Opcode::deserialize(reader)?; - if opcode.is_terminal() { + let instruction = Instruction::deserialize(reader)?; + if instruction.is_terminal() { block_count -= 1; - } else if opcode.is_block() { - block_count = block_count.checked_add(1).ok_or(Error::Other("too many opcodes"))?; + } else if instruction.is_block() { + block_count = block_count.checked_add(1).ok_or(Error::Other("too many instructions"))?; } - opcodes.push(opcode); + instructions.push(instruction); if block_count == 0 { break; } } - Ok(Opcodes(opcodes)) + Ok(Instructions(instructions)) } } /// Initialization expression. #[derive(Debug, Clone, PartialEq)] -pub struct InitExpr(Vec); +pub struct InitExpr(Vec); impl InitExpr { - /// New initialization expression from list of opcodes. - /// `code` must end with the `Opcode::End` opcode! - pub fn new(code: Vec) -> Self { + /// New initialization expression from instruction list. + /// `code` must end with the `Instruction::End` instruction! + pub fn new(code: Vec) -> Self { InitExpr(code) } - /// Empty expression with only `Opcode::End` opcode + /// Empty expression with only `Instruction::End` instruction pub fn empty() -> Self { - InitExpr(vec![Opcode::End]) + InitExpr(vec![Instruction::End]) } - /// List of opcodes used in the expression. - pub fn code(&self) -> &[Opcode] { + /// List of instructions used in the expression. + pub fn code(&self) -> &[Instruction] { &self.0 } - /// List of opcodes used in the expression. - pub fn code_mut(&mut self) -> &mut Vec { + /// List of instructions used in the expression. + pub fn code_mut(&mut self) -> &mut Vec { &mut self.0 } } -// todo: check if kind of opcode sequence is valid as an expression impl Deserialize for InitExpr { type Error = Error; fn deserialize(reader: &mut R) -> Result { - let mut opcodes = Vec::new(); + let mut instructions = Vec::new(); loop { - let opcode = Opcode::deserialize(reader)?; - let is_terminal = opcode.is_terminal(); - opcodes.push(opcode); + let instruction = Instruction::deserialize(reader)?; + let is_terminal = instruction.is_terminal(); + instructions.push(instruction); if is_terminal { break; } } - Ok(InitExpr(opcodes)) + Ok(InitExpr(instructions)) } } -/// Opcode +/// Instruction #[derive(Clone, Debug, PartialEq)] #[allow(missing_docs)] -pub enum Opcode { +pub enum Instruction { Unreachable, Nop, Block(BlockType), @@ -131,7 +130,7 @@ pub enum Opcode { GetGlobal(u32), SetGlobal(u32), - // All store/load opcodes operate with 'memory immediates' + // All store/load instructions operate with 'memory immediates' // which represented here as (flag, offset) tuple I32Load(u32, u32), I64Load(u32, u32), @@ -297,30 +296,30 @@ pub enum Opcode { F64ReinterpretI64, } -impl Opcode { - /// Is this opcode starts the new block (which should end with terminal opcode). +impl Instruction { + /// Is this instruction starts the new block (which should end with terminal instruction). pub fn is_block(&self) -> bool { match self { - &Opcode::Block(_) | &Opcode::Loop(_) | &Opcode::If(_) => true, + &Instruction::Block(_) | &Instruction::Loop(_) | &Instruction::If(_) => true, _ => false, } } - /// Is this opcode determines the termination of opcode sequence - /// `true` for `Opcode::End` + /// Is this instruction determines the termination of instruction sequence + /// `true` for `Instruction::End` pub fn is_terminal(&self) -> bool { match self { - &Opcode::End => true, + &Instruction::End => true, _ => false, } } } -impl Deserialize for Opcode { +impl Deserialize for Instruction { type Error = Error; fn deserialize(reader: &mut R) -> Result { - use self::Opcode::*; + use self::Instruction::*; let val: u8 = Uint8::deserialize(reader)?.into(); @@ -622,11 +621,11 @@ macro_rules! op { }); } -impl Serialize for Opcode { +impl Serialize for Instruction { type Error = Error; fn serialize(self, writer: &mut W) -> Result<(), Self::Error> { - use self::Opcode::*; + use self::Instruction::*; match self { Unreachable => op!(writer, 0x00), @@ -939,9 +938,9 @@ macro_rules! fmt_op { }); } -impl fmt::Display for Opcode { +impl fmt::Display for Instruction { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Opcode::*; + use self::Instruction::*; use super::BlockType; match *self { @@ -1186,7 +1185,7 @@ impl fmt::Display for Opcode { } } -impl Serialize for Opcodes { +impl Serialize for Instructions { type Error = Error; fn serialize(self, writer: &mut W) -> Result<(), Self::Error> { @@ -1213,36 +1212,36 @@ impl Serialize for InitExpr { #[test] fn ifelse() { // see if-else.wast/if-else.wasm - let opcode = super::deserialize_buffer::(&[0x04, 0x7F, 0x41, 0x05, 0x05, 0x41, 0x07, 0x0B, 0x0B]) + let instruction_list = super::deserialize_buffer::(&[0x04, 0x7F, 0x41, 0x05, 0x05, 0x41, 0x07, 0x0B, 0x0B]) .expect("valid hex of if instruction"); - let opcodes = opcode.elements(); - match &opcodes[0] { - &Opcode::If(_) => (), - _ => panic!("Should be deserialized as if opcode"), + let instructions = instruction_list.elements(); + match &instructions[0] { + &Instruction::If(_) => (), + _ => panic!("Should be deserialized as if instruction"), } - let before_else = opcodes.iter().skip(1) - .take_while(|op| match **op { Opcode::Else => false, _ => true }).count(); - let after_else = opcodes.iter().skip(1) - .skip_while(|op| match **op { Opcode::Else => false, _ => true }) - .take_while(|op| match **op { Opcode::End => false, _ => true }) + let before_else = instructions.iter().skip(1) + .take_while(|op| match **op { Instruction::Else => false, _ => true }).count(); + let after_else = instructions.iter().skip(1) + .skip_while(|op| match **op { Instruction::Else => false, _ => true }) + .take_while(|op| match **op { Instruction::End => false, _ => true }) .count() - - 1; // minus Opcode::Else itself + - 1; // minus Instruction::Else itself assert_eq!(before_else, after_else); } #[test] fn display() { - let opcode = Opcode::GetLocal(0); - assert_eq!("get_local 0", format!("{}", opcode)); + let instruction = Instruction::GetLocal(0); + assert_eq!("get_local 0", format!("{}", instruction)); - let opcode = Opcode::F64Store(0, 24); - assert_eq!("f64.store offset=24", format!("{}", opcode)); + let instruction = Instruction::F64Store(0, 24); + assert_eq!("f64.store offset=24", format!("{}", instruction)); - let opcode = Opcode::I64Store(0, 0); - assert_eq!("i64.store", format!("{}", opcode)); + let instruction = Instruction::I64Store(0, 0); + assert_eq!("i64.store", format!("{}", instruction)); } #[test] fn size_off() { - assert!(::std::mem::size_of::() <= 24); + assert!(::std::mem::size_of::() <= 24); } diff --git a/src/elements/section.rs b/src/elements/section.rs index 4b50124..4300890 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -805,7 +805,7 @@ mod tests { use super::super::{ deserialize_buffer, deserialize_file, ValueType, InitExpr, DataSegment, - serialize, ElementSegment, Opcodes, BlockType, Local, FuncBody, + serialize, ElementSegment, Instructions, BlockType, Local, FuncBody, }; use super::{Section, TypeSection, Type, DataSection, ElementSection, CodeSection}; @@ -1089,13 +1089,13 @@ mod tests { #[test] fn code_section_ser() { - use super::super::Opcode::*; + use super::super::Instruction::*; let code_section = CodeSection::with_bodies( vec![ FuncBody::new( vec![Local::new(1, ValueType::I32)], - Opcodes::new(vec![ + Instructions::new(vec![ Block(BlockType::Value(ValueType::I32)), GetGlobal(0), End, From 102a1c4f5c66a9eb652e9bfdd442078063f4c0a6 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 21 May 2018 17:02:55 +0300 Subject: [PATCH 2/3] consts and to consts --- src/elements/mod.rs | 2 +- src/elements/ops.rs | 527 +++++++++++++++++++++++++++++--------------- 2 files changed, 356 insertions(+), 173 deletions(-) diff --git a/src/elements/mod.rs b/src/elements/mod.rs index 25aafce..9e9c785 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -50,7 +50,7 @@ pub use self::primitives::{ Uint64, VarUint64, CountedList, CountedWriter, CountedListWriter, }; pub use self::types::{Type, ValueType, BlockType, FunctionType, TableElementType}; -pub use self::ops::{Instruction, Instructions, InitExpr}; +pub use self::ops::{Instruction, Instructions, InitExpr, opcodes}; pub use self::func::{Func, FuncBody, Local}; pub use self::segment::{ElementSegment, DataSegment}; pub use self::index_map::IndexMap; diff --git a/src/elements/ops.rs b/src/elements/ops.rs index 450fdbd..eaf82a7 100644 --- a/src/elements/ops.rs +++ b/src/elements/ops.rs @@ -315,27 +315,210 @@ impl Instruction { } } +#[allow(missing_docs)] +pub mod opcodes { + pub const UNREACHABLE: u8 = 0x00; + pub const NOP: u8 = 0x01; + pub const BLOCK: u8 = 0x02; + pub const LOOP: u8 = 0x03; + pub const IF: u8 = 0x04; + pub const ELSE: u8 = 0x05; + pub const END: u8 = 0x0b; + pub const BR: u8 = 0x0c; + pub const BRIF: u8 = 0x0d; + pub const BRTABLE: u8 = 0x0e; + pub const RETURN: u8 = 0x0f; + pub const CALL: u8 = 0x10; + pub const CALLINDIRECT: u8 = 0x11; + pub const DROP: u8 = 0x1a; + pub const SELECT: u8 = 0x1b; + pub const GETLOCAL: u8 = 0x20; + pub const SETLOCAL: u8 = 0x21; + pub const TEELOCAL: u8 = 0x22; + pub const GETGLOBAL: u8 = 0x23; + pub const SETGLOBAL: u8 = 0x24; + pub const I32LOAD: u8 = 0x28; + pub const I64LOAD: u8 = 0x29; + pub const F32LOAD: u8 = 0x2a; + pub const F64LOAD: u8 = 0x2b; + pub const I32LOAD8S: u8 = 0x2c; + pub const I32LOAD8U: u8 = 0x2d; + pub const I32LOAD16S: u8 = 0x2e; + pub const I32LOAD16U: u8 = 0x2f; + pub const I64LOAD8S: u8 = 0x30; + pub const I64LOAD8U: u8 = 0x31; + pub const I64LOAD16S: u8 = 0x32; + pub const I64LOAD16U: u8 = 0x33; + pub const I64LOAD32S: u8 = 0x34; + pub const I64LOAD32U: u8 = 0x35; + pub const I32STORE: u8 = 0x36; + pub const I64STORE: u8 = 0x37; + pub const F32STORE: u8 = 0x38; + pub const F64STORE: u8 = 0x39; + pub const I32STORE8: u8 = 0x3a; + pub const I32STORE16: u8 = 0x3b; + pub const I64STORE8: u8 = 0x3c; + pub const I64STORE16: u8 = 0x3d; + pub const I64STORE32: u8 = 0x3e; + pub const CURRENTMEMORY: u8 = 0x3f; + pub const GROWMEMORY: u8 = 0x40; + pub const I32CONST: u8 = 0x41; + pub const I64CONST: u8 = 0x42; + pub const F32CONST: u8 = 0x43; + pub const F64CONST: u8 = 0x44; + pub const I32EQZ: u8 = 0x45; + pub const I32EQ: u8 = 0x46; + pub const I32NE: u8 = 0x47; + pub const I32LTS: u8 = 0x48; + pub const I32LTU: u8 = 0x49; + pub const I32GTS: u8 = 0x4a; + pub const I32GTU: u8 = 0x4b; + pub const I32LES: u8 = 0x4c; + pub const I32LEU: u8 = 0x4d; + pub const I32GES: u8 = 0x4e; + pub const I32GEU: u8 = 0x4f; + pub const I64EQZ: u8 = 0x50; + pub const I64EQ: u8 = 0x51; + pub const I64NE: u8 = 0x52; + pub const I64LTS: u8 = 0x53; + pub const I64LTU: u8 = 0x54; + pub const I64GTS: u8 = 0x55; + pub const I64GTU: u8 = 0x56; + pub const I64LES: u8 = 0x57; + pub const I64LEU: u8 = 0x58; + pub const I64GES: u8 = 0x59; + pub const I64GEU: u8 = 0x5a; + + pub const F32EQ: u8 = 0x5b; + pub const F32NE: u8 = 0x5c; + pub const F32LT: u8 = 0x5d; + pub const F32GT: u8 = 0x5e; + pub const F32LE: u8 = 0x5f; + pub const F32GE: u8 = 0x60; + + pub const F64EQ: u8 = 0x61; + pub const F64NE: u8 = 0x62; + pub const F64LT: u8 = 0x63; + pub const F64GT: u8 = 0x64; + pub const F64LE: u8 = 0x65; + pub const F64GE: u8 = 0x66; + + pub const I32CLZ: u8 = 0x67; + pub const I32CTZ: u8 = 0x68; + pub const I32POPCNT: u8 = 0x69; + pub const I32ADD: u8 = 0x6a; + pub const I32SUB: u8 = 0x6b; + pub const I32MUL: u8 = 0x6c; + pub const I32DIVS: u8 = 0x6d; + pub const I32DIVU: u8 = 0x6e; + pub const I32REMS: u8 = 0x6f; + pub const I32REMU: u8 = 0x70; + pub const I32AND: u8 = 0x71; + pub const I32OR: u8 = 0x72; + pub const I32XOR: u8 = 0x73; + pub const I32SHL: u8 = 0x74; + pub const I32SHRS: u8 = 0x75; + pub const I32SHRU: u8 = 0x76; + pub const I32ROTL: u8 = 0x77; + pub const I32ROTR: u8 = 0x78; + + pub const I64CLZ: u8 = 0x79; + pub const I64CTZ: u8 = 0x7a; + pub const I64POPCNT: u8 = 0x7b; + pub const I64ADD: u8 = 0x7c; + pub const I64SUB: u8 = 0x7d; + pub const I64MUL: u8 = 0x7e; + pub const I64DIVS: u8 = 0x7f; + pub const I64DIVU: u8 = 0x80; + pub const I64REMS: u8 = 0x81; + pub const I64REMU: u8 = 0x82; + pub const I64AND: u8 = 0x83; + pub const I64OR: u8 = 0x84; + pub const I64XOR: u8 = 0x85; + pub const I64SHL: u8 = 0x86; + pub const I64SHRS: u8 = 0x87; + pub const I64SHRU: u8 = 0x88; + pub const I64ROTL: u8 = 0x89; + pub const I64ROTR: u8 = 0x8a; + pub const F32ABS: u8 = 0x8b; + pub const F32NEG: u8 = 0x8c; + pub const F32CEIL: u8 = 0x8d; + pub const F32FLOOR: u8 = 0x8e; + pub const F32TRUNC: u8 = 0x8f; + pub const F32NEAREST: u8 = 0x90; + pub const F32SQRT: u8 = 0x91; + pub const F32ADD: u8 = 0x92; + pub const F32SUB: u8 = 0x93; + pub const F32MUL: u8 = 0x94; + pub const F32DIV: u8 = 0x95; + pub const F32MIN: u8 = 0x96; + pub const F32MAX: u8 = 0x97; + pub const F32COPYSIGN: u8 = 0x98; + pub const F64ABS: u8 = 0x99; + pub const F64NEG: u8 = 0x9a; + pub const F64CEIL: u8 = 0x9b; + pub const F64FLOOR: u8 = 0x9c; + pub const F64TRUNC: u8 = 0x9d; + pub const F64NEAREST: u8 = 0x9e; + pub const F64SQRT: u8 = 0x9f; + pub const F64ADD: u8 = 0xa0; + pub const F64SUB: u8 = 0xa1; + pub const F64MUL: u8 = 0xa2; + pub const F64DIV: u8 = 0xa3; + pub const F64MIN: u8 = 0xa4; + pub const F64MAX: u8 = 0xa5; + pub const F64COPYSIGN: u8 = 0xa6; + + pub const I32WRAPI64: u8 = 0xa7; + pub const I32TRUNCSF32: u8 = 0xa8; + pub const I32TRUNCUF32: u8 = 0xa9; + pub const I32TRUNCSF64: u8 = 0xaa; + pub const I32TRUNCUF64: u8 = 0xab; + pub const I64EXTENDSI32: u8 = 0xac; + pub const I64EXTENDUI32: u8 = 0xad; + pub const I64TRUNCSF32: u8 = 0xae; + pub const I64TRUNCUF32: u8 = 0xaf; + pub const I64TRUNCSF64: u8 = 0xb0; + pub const I64TRUNCUF64: u8 = 0xb1; + pub const F32CONVERTSI32: u8 = 0xb2; + pub const F32CONVERTUI32: u8 = 0xb3; + pub const F32CONVERTSI64: u8 = 0xb4; + pub const F32CONVERTUI64: u8 = 0xb5; + pub const F32DEMOTEF64: u8 = 0xb6; + pub const F64CONVERTSI32: u8 = 0xb7; + pub const F64CONVERTUI32: u8 = 0xb8; + pub const F64CONVERTSI64: u8 = 0xb9; + pub const F64CONVERTUI64: u8 = 0xba; + pub const F64PROMOTEF32: u8 = 0xbb; + + pub const I32REINTERPRETF32: u8 = 0xbc; + pub const I64REINTERPRETF64: u8 = 0xbd; + pub const F32REINTERPRETI32: u8 = 0xbe; + pub const F64REINTERPRETI64: u8 = 0xbf; +} + impl Deserialize for Instruction { type Error = Error; fn deserialize(reader: &mut R) -> Result { use self::Instruction::*; + use self::opcodes::*; let val: u8 = Uint8::deserialize(reader)?.into(); Ok( match val { - 0x00 => Unreachable, - 0x01 => Nop, - 0x02 => Block(BlockType::deserialize(reader)?), - 0x03 => Loop(BlockType::deserialize(reader)?), - 0x04 => If(BlockType::deserialize(reader)?), - 0x05 => Else, - 0x0b => End, + UNREACHABLE => Unreachable, + NOP => Nop, + BLOCK => Block(BlockType::deserialize(reader)?), + LOOP => Loop(BlockType::deserialize(reader)?), + IF => If(BlockType::deserialize(reader)?), + ELSE => Else, + END => End, - 0x0c => Br(VarUint32::deserialize(reader)?.into()), - 0x0d => BrIf(VarUint32::deserialize(reader)?.into()), - 0x0e => { + BR => Br(VarUint32::deserialize(reader)?.into()), + BRIF => BrIf(VarUint32::deserialize(reader)?.into()), + BRTABLE => { let t1: Vec = CountedList::::deserialize(reader)? .into_inner() .into_iter() @@ -344,9 +527,9 @@ impl Deserialize for Instruction { BrTable(t1.into_boxed_slice(), VarUint32::deserialize(reader)?.into()) }, - 0x0f => Return, - 0x10 => Call(VarUint32::deserialize(reader)?.into()), - 0x11 => { + RETURN => Return, + CALL => Call(VarUint32::deserialize(reader)?.into()), + CALLINDIRECT => { let signature: u32 = VarUint32::deserialize(reader)?.into(); let table_ref: u8 = Uint8::deserialize(reader)?.into(); if table_ref != 0 { return Err(Error::InvalidTableReference(table_ref)); } @@ -356,253 +539,253 @@ impl Deserialize for Instruction { table_ref, ) }, - 0x1a => Drop, - 0x1b => Select, + DROP => Drop, + SELECT => Select, - 0x20 => GetLocal(VarUint32::deserialize(reader)?.into()), - 0x21 => SetLocal(VarUint32::deserialize(reader)?.into()), - 0x22 => TeeLocal(VarUint32::deserialize(reader)?.into()), - 0x23 => GetGlobal(VarUint32::deserialize(reader)?.into()), - 0x24 => SetGlobal(VarUint32::deserialize(reader)?.into()), + GETLOCAL => GetLocal(VarUint32::deserialize(reader)?.into()), + SETLOCAL => SetLocal(VarUint32::deserialize(reader)?.into()), + TEELOCAL => TeeLocal(VarUint32::deserialize(reader)?.into()), + GETGLOBAL => GetGlobal(VarUint32::deserialize(reader)?.into()), + SETGLOBAL => SetGlobal(VarUint32::deserialize(reader)?.into()), - 0x28 => I32Load( + I32LOAD => I32Load( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x29 => I64Load( + I64LOAD => I64Load( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x2a => F32Load( + F32LOAD => F32Load( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x2b => F64Load( + F64LOAD => F64Load( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x2c => I32Load8S( + I32LOAD8S => I32Load8S( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x2d => I32Load8U( + I32LOAD8U => I32Load8U( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x2e => I32Load16S( + I32LOAD16S => I32Load16S( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x2f => I32Load16U( + I32LOAD16U => I32Load16U( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x30 => I64Load8S( + I64LOAD8S => I64Load8S( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x31 => I64Load8U( + I64LOAD8U => I64Load8U( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x32 => I64Load16S( + I64LOAD16S => I64Load16S( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x33 => I64Load16U( + I64LOAD16U => I64Load16U( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x34 => I64Load32S( + I64LOAD32S => I64Load32S( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x35 => I64Load32U( + I64LOAD32U => I64Load32U( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x36 => I32Store( + I32STORE => I32Store( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x37 => I64Store( + I64STORE => I64Store( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x38 => F32Store( + F32STORE => F32Store( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x39 => F64Store( + F64STORE => F64Store( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x3a => I32Store8( + I32STORE8 => I32Store8( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x3b => I32Store16( + I32STORE16 => I32Store16( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x3c => I64Store8( + I64STORE8 => I64Store8( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x3d => I64Store16( + I64STORE16 => I64Store16( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x3e => I64Store32( + I64STORE32 => I64Store32( VarUint32::deserialize(reader)?.into(), VarUint32::deserialize(reader)?.into()), - 0x3f => { + CURRENTMEMORY => { let mem_ref: u8 = Uint8::deserialize(reader)?.into(); if mem_ref != 0 { return Err(Error::InvalidMemoryReference(mem_ref)); } CurrentMemory(mem_ref) }, - 0x40 => { + GROWMEMORY => { let mem_ref: u8 = Uint8::deserialize(reader)?.into(); if mem_ref != 0 { return Err(Error::InvalidMemoryReference(mem_ref)); } GrowMemory(mem_ref) } - 0x41 => I32Const(VarInt32::deserialize(reader)?.into()), - 0x42 => I64Const(VarInt64::deserialize(reader)?.into()), - 0x43 => F32Const(Uint32::deserialize(reader)?.into()), - 0x44 => F64Const(Uint64::deserialize(reader)?.into()), - 0x45 => I32Eqz, - 0x46 => I32Eq, - 0x47 => I32Ne, - 0x48 => I32LtS, - 0x49 => I32LtU, - 0x4a => I32GtS, - 0x4b => I32GtU, - 0x4c => I32LeS, - 0x4d => I32LeU, - 0x4e => I32GeS, - 0x4f => I32GeU, + I32CONST => I32Const(VarInt32::deserialize(reader)?.into()), + I64CONST => I64Const(VarInt64::deserialize(reader)?.into()), + F32CONST => F32Const(Uint32::deserialize(reader)?.into()), + F64CONST => F64Const(Uint64::deserialize(reader)?.into()), + I32EQZ => I32Eqz, + I32EQ => I32Eq, + I32NE => I32Ne, + I32LTS => I32LtS, + I32LTU => I32LtU, + I32GTS => I32GtS, + I32GTU => I32GtU, + I32LES => I32LeS, + I32LEU => I32LeU, + I32GES => I32GeS, + I32GEU => I32GeU, - 0x50 => I64Eqz, - 0x51 => I64Eq, - 0x52 => I64Ne, - 0x53 => I64LtS, - 0x54 => I64LtU, - 0x55 => I64GtS, - 0x56 => I64GtU, - 0x57 => I64LeS, - 0x58 => I64LeU, - 0x59 => I64GeS, - 0x5a => I64GeU, + I64EQZ => I64Eqz, + I64EQ => I64Eq, + I64NE => I64Ne, + I64LTS => I64LtS, + I64LTU => I64LtU, + I64GTS => I64GtS, + I64GTU => I64GtU, + I64LES => I64LeS, + I64LEU => I64LeU, + I64GES => I64GeS, + I64GEU => I64GeU, - 0x5b => F32Eq, - 0x5c => F32Ne, - 0x5d => F32Lt, - 0x5e => F32Gt, - 0x5f => F32Le, - 0x60 => F32Ge, + F32EQ => F32Eq, + F32NE => F32Ne, + F32LT => F32Lt, + F32GT => F32Gt, + F32LE => F32Le, + F32GE => F32Ge, - 0x61 => F64Eq, - 0x62 => F64Ne, - 0x63 => F64Lt, - 0x64 => F64Gt, - 0x65 => F64Le, - 0x66 => F64Ge, + F64EQ => F64Eq, + F64NE => F64Ne, + F64LT => F64Lt, + F64GT => F64Gt, + F64LE => F64Le, + F64GE => F64Ge, - 0x67 => I32Clz, - 0x68 => I32Ctz, - 0x69 => I32Popcnt, - 0x6a => I32Add, - 0x6b => I32Sub, - 0x6c => I32Mul, - 0x6d => I32DivS, - 0x6e => I32DivU, - 0x6f => I32RemS, - 0x70 => I32RemU, - 0x71 => I32And, - 0x72 => I32Or, - 0x73 => I32Xor, - 0x74 => I32Shl, - 0x75 => I32ShrS, - 0x76 => I32ShrU, - 0x77 => I32Rotl, - 0x78 => I32Rotr, + I32CLZ => I32Clz, + I32CTZ => I32Ctz, + I32POPCNT => I32Popcnt, + I32ADD => I32Add, + I32SUB => I32Sub, + I32MUL => I32Mul, + I32DIVS => I32DivS, + I32DIVU => I32DivU, + I32REMS => I32RemS, + I32REMU => I32RemU, + I32AND => I32And, + I32OR => I32Or, + I32XOR => I32Xor, + I32SHL => I32Shl, + I32SHRS => I32ShrS, + I32SHRU => I32ShrU, + I32ROTL => I32Rotl, + I32ROTR => I32Rotr, - 0x79 => I64Clz, - 0x7a => I64Ctz, - 0x7b => I64Popcnt, - 0x7c => I64Add, - 0x7d => I64Sub, - 0x7e => I64Mul, - 0x7f => I64DivS, - 0x80 => I64DivU, - 0x81 => I64RemS, - 0x82 => I64RemU, - 0x83 => I64And, - 0x84 => I64Or, - 0x85 => I64Xor, - 0x86 => I64Shl, - 0x87 => I64ShrS, - 0x88 => I64ShrU, - 0x89 => I64Rotl, - 0x8a => I64Rotr, - 0x8b => F32Abs, - 0x8c => F32Neg, - 0x8d => F32Ceil, - 0x8e => F32Floor, - 0x8f => F32Trunc, - 0x90 => F32Nearest, - 0x91 => F32Sqrt, - 0x92 => F32Add, - 0x93 => F32Sub, - 0x94 => F32Mul, - 0x95 => F32Div, - 0x96 => F32Min, - 0x97 => F32Max, - 0x98 => F32Copysign, - 0x99 => F64Abs, - 0x9a => F64Neg, - 0x9b => F64Ceil, - 0x9c => F64Floor, - 0x9d => F64Trunc, - 0x9e => F64Nearest, - 0x9f => F64Sqrt, - 0xa0 => F64Add, - 0xa1 => F64Sub, - 0xa2 => F64Mul, - 0xa3 => F64Div, - 0xa4 => F64Min, - 0xa5 => F64Max, - 0xa6 => F64Copysign, + I64CLZ => I64Clz, + I64CTZ => I64Ctz, + I64POPCNT => I64Popcnt, + I64ADD => I64Add, + I64SUB => I64Sub, + I64MUL => I64Mul, + I64DIVS => I64DivS, + I64DIVU => I64DivU, + I64REMS => I64RemS, + I64REMU => I64RemU, + I64AND => I64And, + I64OR => I64Or, + I64XOR => I64Xor, + I64SHL => I64Shl, + I64SHRS => I64ShrS, + I64SHRU => I64ShrU, + I64ROTL => I64Rotl, + I64ROTR => I64Rotr, + F32ABS => F32Abs, + F32NEG => F32Neg, + F32CEIL => F32Ceil, + F32FLOOR => F32Floor, + F32TRUNC => F32Trunc, + F32NEAREST => F32Nearest, + F32SQRT => F32Sqrt, + F32ADD => F32Add, + F32SUB => F32Sub, + F32MUL => F32Mul, + F32DIV => F32Div, + F32MIN => F32Min, + F32MAX => F32Max, + F32COPYSIGN => F32Copysign, + F64ABS => F64Abs, + F64NEG => F64Neg, + F64CEIL => F64Ceil, + F64FLOOR => F64Floor, + F64TRUNC => F64Trunc, + F64NEAREST => F64Nearest, + F64SQRT => F64Sqrt, + F64ADD => F64Add, + F64SUB => F64Sub, + F64MUL => F64Mul, + F64DIV => F64Div, + F64MIN => F64Min, + F64MAX => F64Max, + F64COPYSIGN => F64Copysign, - 0xa7 => I32WrapI64, - 0xa8 => I32TruncSF32, - 0xa9 => I32TruncUF32, - 0xaa => I32TruncSF64, - 0xab => I32TruncUF64, - 0xac => I64ExtendSI32, - 0xad => I64ExtendUI32, - 0xae => I64TruncSF32, - 0xaf => I64TruncUF32, - 0xb0 => I64TruncSF64, - 0xb1 => I64TruncUF64, - 0xb2 => F32ConvertSI32, - 0xb3 => F32ConvertUI32, - 0xb4 => F32ConvertSI64, - 0xb5 => F32ConvertUI64, - 0xb6 => F32DemoteF64, - 0xb7 => F64ConvertSI32, - 0xb8 => F64ConvertUI32, - 0xb9 => F64ConvertSI64, - 0xba => F64ConvertUI64, - 0xbb => F64PromoteF32, + I32WRAPI64 => I32WrapI64, + I32TRUNCSF32 => I32TruncSF32, + I32TRUNCUF32 => I32TruncUF32, + I32TRUNCSF64 => I32TruncSF64, + I32TRUNCUF64 => I32TruncUF64, + I64EXTENDSI32 => I64ExtendSI32, + I64EXTENDUI32 => I64ExtendUI32, + I64TRUNCSF32 => I64TruncSF32, + I64TRUNCUF32 => I64TruncUF32, + I64TRUNCSF64 => I64TruncSF64, + I64TRUNCUF64 => I64TruncUF64, + F32CONVERTSI32 => F32ConvertSI32, + F32CONVERTUI32 => F32ConvertUI32, + F32CONVERTSI64 => F32ConvertSI64, + F32CONVERTUI64 => F32ConvertUI64, + F32DEMOTEF64 => F32DemoteF64, + F64CONVERTSI32 => F64ConvertSI32, + F64CONVERTUI32 => F64ConvertUI32, + F64CONVERTSI64 => F64ConvertSI64, + F64CONVERTUI64 => F64ConvertUI64, + F64PROMOTEF32 => F64PromoteF32, - 0xbc => I32ReinterpretF32, - 0xbd => I64ReinterpretF64, - 0xbe => F32ReinterpretI32, - 0xbf => F64ReinterpretI64, + I32REINTERPRETF32 => I32ReinterpretF32, + I64REINTERPRETF64 => I64ReinterpretF64, + F32REINTERPRETI32 => F32ReinterpretI32, + F64REINTERPRETI64 => F64ReinterpretI64, _ => { return Err(Error::UnknownOpcode(val)); } } From a415aa91eafa3a83ed3a6db3c6f2a2007ef04107 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 21 May 2018 17:16:40 +0300 Subject: [PATCH 3/3] also for storage --- src/elements/ops.rs | 345 ++++++++++++++++++++++---------------------- 1 file changed, 173 insertions(+), 172 deletions(-) diff --git a/src/elements/ops.rs b/src/elements/ops.rs index eaf82a7..05a24f6 100644 --- a/src/elements/ops.rs +++ b/src/elements/ops.rs @@ -809,28 +809,29 @@ impl Serialize for Instruction { fn serialize(self, writer: &mut W) -> Result<(), Self::Error> { use self::Instruction::*; + use self::opcodes::*; match self { - Unreachable => op!(writer, 0x00), - Nop => op!(writer, 0x01), - Block(block_type) => op!(writer, 0x02, { + Unreachable => op!(writer, UNREACHABLE), + Nop => op!(writer, NOP), + Block(block_type) => op!(writer, BLOCK, { block_type.serialize(writer)?; }), - Loop(block_type) => op!(writer, 0x03, { + Loop(block_type) => op!(writer, LOOP, { block_type.serialize(writer)?; }), - If(block_type) => op!(writer, 0x04, { + If(block_type) => op!(writer, IF, { block_type.serialize(writer)?; }), - Else => op!(writer, 0x05), - End => op!(writer, 0x0b), - Br(idx) => op!(writer, 0x0c, { + Else => op!(writer, ELSE), + End => op!(writer, END), + Br(idx) => op!(writer, BR, { VarUint32::from(idx).serialize(writer)?; }), - BrIf(idx) => op!(writer, 0x0d, { + BrIf(idx) => op!(writer, BRIF, { VarUint32::from(idx).serialize(writer)?; }), - BrTable(table, default) => op!(writer, 0x0e, { + BrTable(table, default) => op!(writer, BRTABLE, { let list_writer = CountedListWriter::( table.len(), table.into_iter().map(|x| VarUint32::from(*x)), @@ -838,271 +839,271 @@ impl Serialize for Instruction { list_writer.serialize(writer)?; VarUint32::from(default).serialize(writer)?; }), - Return => op!(writer, 0x0f), - Call(index) => op!(writer, 0x10, { + Return => op!(writer, RETURN), + Call(index) => op!(writer, CALL, { VarUint32::from(index).serialize(writer)?; }), - CallIndirect(index, reserved) => op!(writer, 0x11, { + CallIndirect(index, reserved) => op!(writer, CALLINDIRECT, { VarUint32::from(index).serialize(writer)?; Uint8::from(reserved).serialize(writer)?; }), - Drop => op!(writer, 0x1a), - Select => op!(writer, 0x1b), - GetLocal(index) => op!(writer, 0x20, { + Drop => op!(writer, DROP), + Select => op!(writer, SELECT), + GetLocal(index) => op!(writer, GETLOCAL, { VarUint32::from(index).serialize(writer)?; }), - SetLocal(index) => op!(writer, 0x21, { + SetLocal(index) => op!(writer, SETLOCAL, { VarUint32::from(index).serialize(writer)?; }), - TeeLocal(index) => op!(writer, 0x22, { + TeeLocal(index) => op!(writer, TEELOCAL, { VarUint32::from(index).serialize(writer)?; }), - GetGlobal(index) => op!(writer, 0x23, { + GetGlobal(index) => op!(writer, GETGLOBAL, { VarUint32::from(index).serialize(writer)?; }), - SetGlobal(index) => op!(writer, 0x24, { + SetGlobal(index) => op!(writer, SETGLOBAL, { VarUint32::from(index).serialize(writer)?; }), - I32Load(flags, offset) => op!(writer, 0x28, { + I32Load(flags, offset) => op!(writer, I32LOAD, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Load(flags, offset) => op!(writer, 0x29, { + I64Load(flags, offset) => op!(writer, I64LOAD, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - F32Load(flags, offset) => op!(writer, 0x2a, { + F32Load(flags, offset) => op!(writer, F32LOAD, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - F64Load(flags, offset) => op!(writer, 0x2b, { + F64Load(flags, offset) => op!(writer, F64LOAD, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I32Load8S(flags, offset) => op!(writer, 0x2c, { + I32Load8S(flags, offset) => op!(writer, I32LOAD8S, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I32Load8U(flags, offset) => op!(writer, 0x2d, { + I32Load8U(flags, offset) => op!(writer, I32LOAD8U, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I32Load16S(flags, offset) => op!(writer, 0x2e, { + I32Load16S(flags, offset) => op!(writer, I32LOAD16S, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I32Load16U(flags, offset) => op!(writer, 0x2f, { + I32Load16U(flags, offset) => op!(writer, I32LOAD16U, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Load8S(flags, offset) => op!(writer, 0x30, { + I64Load8S(flags, offset) => op!(writer, I64LOAD8S, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Load8U(flags, offset) => op!(writer, 0x31, { + I64Load8U(flags, offset) => op!(writer, I64LOAD8U, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Load16S(flags, offset) => op!(writer, 0x32, { + I64Load16S(flags, offset) => op!(writer, I64LOAD16S, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Load16U(flags, offset) => op!(writer, 0x33, { + I64Load16U(flags, offset) => op!(writer, I64LOAD16U, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Load32S(flags, offset) => op!(writer, 0x34, { + I64Load32S(flags, offset) => op!(writer, I64LOAD32S, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Load32U(flags, offset) => op!(writer, 0x35, { + I64Load32U(flags, offset) => op!(writer, I64LOAD32U, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I32Store(flags, offset) => op!(writer, 0x36, { + I32Store(flags, offset) => op!(writer, I32STORE, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Store(flags, offset) => op!(writer, 0x37, { + I64Store(flags, offset) => op!(writer, I64STORE, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - F32Store(flags, offset) => op!(writer, 0x38, { + F32Store(flags, offset) => op!(writer, F32STORE, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - F64Store(flags, offset) => op!(writer, 0x39, { + F64Store(flags, offset) => op!(writer, F64STORE, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I32Store8(flags, offset) => op!(writer, 0x3a, { + I32Store8(flags, offset) => op!(writer, I32STORE8, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I32Store16(flags, offset) => op!(writer, 0x3b, { + I32Store16(flags, offset) => op!(writer, I32STORE16, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Store8(flags, offset) => op!(writer, 0x3c, { + I64Store8(flags, offset) => op!(writer, I64STORE8, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Store16(flags, offset) => op!(writer, 0x3d, { + I64Store16(flags, offset) => op!(writer, I64STORE16, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - I64Store32(flags, offset) => op!(writer, 0x3e, { + I64Store32(flags, offset) => op!(writer, I64STORE32, { VarUint32::from(flags).serialize(writer)?; VarUint32::from(offset).serialize(writer)?; }), - CurrentMemory(flag) => op!(writer, 0x3f, { + CurrentMemory(flag) => op!(writer, CURRENTMEMORY, { Uint8::from(flag).serialize(writer)?; }), - GrowMemory(flag) => op!(writer, 0x40, { + GrowMemory(flag) => op!(writer, GROWMEMORY, { Uint8::from(flag).serialize(writer)?; }), - I32Const(def) => op!(writer, 0x41, { + I32Const(def) => op!(writer, I32CONST, { VarInt32::from(def).serialize(writer)?; }), - I64Const(def) => op!(writer, 0x42, { + I64Const(def) => op!(writer, I64CONST, { VarInt64::from(def).serialize(writer)?; }), - F32Const(def) => op!(writer, 0x43, { + F32Const(def) => op!(writer, F32CONST, { Uint32::from(def).serialize(writer)?; }), - F64Const(def) => op!(writer, 0x44, { + F64Const(def) => op!(writer, F64CONST, { Uint64::from(def).serialize(writer)?; }), - I32Eqz => op!(writer, 0x45), - I32Eq => op!(writer, 0x46), - I32Ne => op!(writer, 0x47), - I32LtS => op!(writer, 0x48), - I32LtU => op!(writer, 0x49), - I32GtS => op!(writer, 0x4a), - I32GtU => op!(writer, 0x4b), - I32LeS => op!(writer, 0x4c), - I32LeU => op!(writer, 0x4d), - I32GeS => op!(writer, 0x4e), - I32GeU => op!(writer, 0x4f), + I32Eqz => op!(writer, I32EQZ), + I32Eq => op!(writer, I32EQ), + I32Ne => op!(writer, I32NE), + I32LtS => op!(writer, I32LTS), + I32LtU => op!(writer, I32LTU), + I32GtS => op!(writer, I32GTS), + I32GtU => op!(writer, I32GTU), + I32LeS => op!(writer, I32LES), + I32LeU => op!(writer, I32LEU), + I32GeS => op!(writer, I32GES), + I32GeU => op!(writer, I32GEU), - I64Eqz => op!(writer, 0x50), - I64Eq => op!(writer, 0x51), - I64Ne => op!(writer, 0x52), - I64LtS => op!(writer, 0x53), - I64LtU => op!(writer, 0x54), - I64GtS => op!(writer, 0x55), - I64GtU => op!(writer, 0x56), - I64LeS => op!(writer, 0x57), - I64LeU => op!(writer, 0x58), - I64GeS => op!(writer, 0x59), - I64GeU => op!(writer, 0x5a), + I64Eqz => op!(writer, I64EQZ), + I64Eq => op!(writer, I64EQ), + I64Ne => op!(writer, I64NE), + I64LtS => op!(writer, I64LTS), + I64LtU => op!(writer, I64LTU), + I64GtS => op!(writer, I64GTS), + I64GtU => op!(writer, I64GTU), + I64LeS => op!(writer, I64LES), + I64LeU => op!(writer, I64LEU), + I64GeS => op!(writer, I64GES), + I64GeU => op!(writer, I64GEU), - F32Eq => op!(writer, 0x5b), - F32Ne => op!(writer, 0x5c), - F32Lt => op!(writer, 0x5d), - F32Gt => op!(writer, 0x5e), - F32Le => op!(writer, 0x5f), - F32Ge => op!(writer, 0x60), + F32Eq => op!(writer, F32EQ), + F32Ne => op!(writer, F32NE), + F32Lt => op!(writer, F32LT), + F32Gt => op!(writer, F32GT), + F32Le => op!(writer, F32LE), + F32Ge => op!(writer, F32GE), - F64Eq => op!(writer, 0x61), - F64Ne => op!(writer, 0x62), - F64Lt => op!(writer, 0x63), - F64Gt => op!(writer, 0x64), - F64Le => op!(writer, 0x65), - F64Ge => op!(writer, 0x66), + F64Eq => op!(writer, F64EQ), + F64Ne => op!(writer, F64NE), + F64Lt => op!(writer, F64LT), + F64Gt => op!(writer, F64GT), + F64Le => op!(writer, F64LE), + F64Ge => op!(writer, F64GE), - I32Clz => op!(writer, 0x67), - I32Ctz => op!(writer, 0x68), - I32Popcnt => op!(writer, 0x69), - I32Add => op!(writer, 0x6a), - I32Sub => op!(writer, 0x6b), - I32Mul => op!(writer, 0x6c), - I32DivS => op!(writer, 0x6d), - I32DivU => op!(writer, 0x6e), - I32RemS => op!(writer, 0x6f), - I32RemU => op!(writer, 0x70), - I32And => op!(writer, 0x71), - I32Or => op!(writer, 0x72), - I32Xor => op!(writer, 0x73), - I32Shl => op!(writer, 0x74), - I32ShrS => op!(writer, 0x75), - I32ShrU => op!(writer, 0x76), - I32Rotl => op!(writer, 0x77), - I32Rotr => op!(writer, 0x78), + I32Clz => op!(writer, I32CLZ), + I32Ctz => op!(writer, I32CTZ), + I32Popcnt => op!(writer, I32POPCNT), + I32Add => op!(writer, I32ADD), + I32Sub => op!(writer, I32SUB), + I32Mul => op!(writer, I32MUL), + I32DivS => op!(writer, I32DIVS), + I32DivU => op!(writer, I32DIVU), + I32RemS => op!(writer, I32REMS), + I32RemU => op!(writer, I32REMU), + I32And => op!(writer, I32AND), + I32Or => op!(writer, I32OR), + I32Xor => op!(writer, I32XOR), + I32Shl => op!(writer, I32SHL), + I32ShrS => op!(writer, I32SHRS), + I32ShrU => op!(writer, I32SHRU), + I32Rotl => op!(writer, I32ROTL), + I32Rotr => op!(writer, I32ROTR), - I64Clz => op!(writer, 0x79), - I64Ctz => op!(writer, 0x7a), - I64Popcnt => op!(writer, 0x7b), - I64Add => op!(writer, 0x7c), - I64Sub => op!(writer, 0x7d), - I64Mul => op!(writer, 0x7e), - I64DivS => op!(writer, 0x7f), - I64DivU => op!(writer, 0x80), - I64RemS => op!(writer, 0x81), - I64RemU => op!(writer, 0x82), - I64And => op!(writer, 0x83), - I64Or => op!(writer, 0x84), - I64Xor => op!(writer, 0x85), - I64Shl => op!(writer, 0x86), - I64ShrS => op!(writer, 0x87), - I64ShrU => op!(writer, 0x88), - I64Rotl => op!(writer, 0x89), - I64Rotr => op!(writer, 0x8a), - F32Abs => op!(writer, 0x8b), - F32Neg => op!(writer, 0x8c), - F32Ceil => op!(writer, 0x8d), - F32Floor => op!(writer, 0x8e), - F32Trunc => op!(writer, 0x8f), - F32Nearest => op!(writer, 0x90), - F32Sqrt => op!(writer, 0x91), - F32Add => op!(writer, 0x92), - F32Sub => op!(writer, 0x93), - F32Mul => op!(writer, 0x94), - F32Div => op!(writer, 0x95), - F32Min => op!(writer, 0x96), - F32Max => op!(writer, 0x97), - F32Copysign => op!(writer, 0x98), - F64Abs => op!(writer, 0x99), - F64Neg => op!(writer, 0x9a), - F64Ceil => op!(writer, 0x9b), - F64Floor => op!(writer, 0x9c), - F64Trunc => op!(writer, 0x9d), - F64Nearest => op!(writer, 0x9e), - F64Sqrt => op!(writer, 0x9f), - F64Add => op!(writer, 0xa0), - F64Sub => op!(writer, 0xa1), - F64Mul => op!(writer, 0xa2), - F64Div => op!(writer, 0xa3), - F64Min => op!(writer, 0xa4), - F64Max => op!(writer, 0xa5), - F64Copysign => op!(writer, 0xa6), + I64Clz => op!(writer, I64CLZ), + I64Ctz => op!(writer, I64CTZ), + I64Popcnt => op!(writer, I64POPCNT), + I64Add => op!(writer, I64ADD), + I64Sub => op!(writer, I64SUB), + I64Mul => op!(writer, I64MUL), + I64DivS => op!(writer, I64DIVS), + I64DivU => op!(writer, I64DIVU), + I64RemS => op!(writer, I64REMS), + I64RemU => op!(writer, I64REMU), + I64And => op!(writer, I64AND), + I64Or => op!(writer, I64OR), + I64Xor => op!(writer, I64XOR), + I64Shl => op!(writer, I64SHL), + I64ShrS => op!(writer, I64SHRS), + I64ShrU => op!(writer, I64SHRU), + I64Rotl => op!(writer, I64ROTL), + I64Rotr => op!(writer, I64ROTR), + F32Abs => op!(writer, F32ABS), + F32Neg => op!(writer, F32NEG), + F32Ceil => op!(writer, F32CEIL), + F32Floor => op!(writer, F32FLOOR), + F32Trunc => op!(writer, F32TRUNC), + F32Nearest => op!(writer, F32NEAREST), + F32Sqrt => op!(writer, F32SQRT), + F32Add => op!(writer, F32ADD), + F32Sub => op!(writer, F32SUB), + F32Mul => op!(writer, F32MUL), + F32Div => op!(writer, F32DIV), + F32Min => op!(writer, F32MIN), + F32Max => op!(writer, F32MAX), + F32Copysign => op!(writer, F32COPYSIGN), + F64Abs => op!(writer, F64ABS), + F64Neg => op!(writer, F64NEG), + F64Ceil => op!(writer, F64CEIL), + F64Floor => op!(writer, F64FLOOR), + F64Trunc => op!(writer, F64TRUNC), + F64Nearest => op!(writer, F64NEAREST), + F64Sqrt => op!(writer, F64SQRT), + F64Add => op!(writer, F64ADD), + F64Sub => op!(writer, F64SUB), + F64Mul => op!(writer, F64MUL), + F64Div => op!(writer, F64DIV), + F64Min => op!(writer, F64MIN), + F64Max => op!(writer, F64MAX), + F64Copysign => op!(writer, F64COPYSIGN), - I32WrapI64 => op!(writer, 0xa7), - I32TruncSF32 => op!(writer, 0xa8), - I32TruncUF32 => op!(writer, 0xa9), - I32TruncSF64 => op!(writer, 0xaa), - I32TruncUF64 => op!(writer, 0xab), - I64ExtendSI32 => op!(writer, 0xac), - I64ExtendUI32 => op!(writer, 0xad), - I64TruncSF32 => op!(writer, 0xae), - I64TruncUF32 => op!(writer, 0xaf), - I64TruncSF64 => op!(writer, 0xb0), - I64TruncUF64 => op!(writer, 0xb1), - F32ConvertSI32 => op!(writer, 0xb2), - F32ConvertUI32 => op!(writer, 0xb3), - F32ConvertSI64 => op!(writer, 0xb4), - F32ConvertUI64 => op!(writer, 0xb5), - F32DemoteF64 => op!(writer, 0xb6), - F64ConvertSI32 => op!(writer, 0xb7), - F64ConvertUI32 => op!(writer, 0xb8), - F64ConvertSI64 => op!(writer, 0xb9), - F64ConvertUI64 => op!(writer, 0xba), - F64PromoteF32 => op!(writer, 0xbb), + I32WrapI64 => op!(writer, I32WRAPI64), + I32TruncSF32 => op!(writer, I32TRUNCSF32), + I32TruncUF32 => op!(writer, I32TRUNCUF32), + I32TruncSF64 => op!(writer, I32TRUNCSF64), + I32TruncUF64 => op!(writer, I32TRUNCUF64), + I64ExtendSI32 => op!(writer, I64EXTENDSI32), + I64ExtendUI32 => op!(writer, I64EXTENDUI32), + I64TruncSF32 => op!(writer, I64TRUNCSF32), + I64TruncUF32 => op!(writer, I64TRUNCUF32), + I64TruncSF64 => op!(writer, I64TRUNCSF64), + I64TruncUF64 => op!(writer, I64TRUNCUF64), + F32ConvertSI32 => op!(writer, F32CONVERTSI32), + F32ConvertUI32 => op!(writer, F32CONVERTUI32), + F32ConvertSI64 => op!(writer, F32CONVERTSI64), + F32ConvertUI64 => op!(writer, F32CONVERTUI64), + F32DemoteF64 => op!(writer, F32DEMOTEF64), + F64ConvertSI32 => op!(writer, F64CONVERTSI32), + F64ConvertUI32 => op!(writer, F64CONVERTUI32), + F64ConvertSI64 => op!(writer, F64CONVERTSI64), + F64ConvertUI64 => op!(writer, F64CONVERTUI64), + F64PromoteF32 => op!(writer, F64PROMOTEF32), - I32ReinterpretF32 => op!(writer, 0xbc), - I64ReinterpretF64 => op!(writer, 0xbd), - F32ReinterpretI32 => op!(writer, 0xbe), - F64ReinterpretI64 => op!(writer, 0xbf), + I32ReinterpretF32 => op!(writer, I32REINTERPRETF32), + I64ReinterpretF64 => op!(writer, I64REINTERPRETF64), + F32ReinterpretI32 => op!(writer, F32REINTERPRETI32), + F64ReinterpretI64 => op!(writer, F64REINTERPRETI64), } Ok(())