mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-05-23 20:51:33 +00:00
Merge pull request #214 from paritytech/code-section
Rename Opcode to Instruction
This commit is contained in:
commit
f9dbb0cdb1
@ -5,21 +5,21 @@ use std::env;
|
|||||||
use parity_wasm::elements;
|
use parity_wasm::elements;
|
||||||
use parity_wasm::builder;
|
use parity_wasm::builder;
|
||||||
|
|
||||||
pub fn inject_nop(opcodes: &mut elements::Opcodes) {
|
pub fn inject_nop(instructions: &mut elements::Instructions) {
|
||||||
use parity_wasm::elements::Opcode::*;
|
use parity_wasm::elements::Instruction::*;
|
||||||
let opcodes = opcodes.elements_mut();
|
let instructions = instructions.elements_mut();
|
||||||
let mut position = 0;
|
let mut position = 0;
|
||||||
loop {
|
loop {
|
||||||
let need_inject = match &opcodes[position] {
|
let need_inject = match &instructions[position] {
|
||||||
&Block(_) | &If(_) => true,
|
&Block(_) | &If(_) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
if need_inject {
|
if need_inject {
|
||||||
opcodes.insert(position + 1, Nop);
|
instructions.insert(position + 1, Nop);
|
||||||
}
|
}
|
||||||
|
|
||||||
position += 1;
|
position += 1;
|
||||||
if position >= opcodes.len() {
|
if position >= instructions.len() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -224,7 +224,7 @@ impl<F> FuncBodyBuilder<F> {
|
|||||||
pub fn with_callback(callback: F) -> Self {
|
pub fn with_callback(callback: F) -> Self {
|
||||||
FuncBodyBuilder {
|
FuncBodyBuilder {
|
||||||
callback: callback,
|
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<F> FuncBodyBuilder<F> where F: Invoke<elements::FuncBody> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set code of the function
|
/// Set code of the function
|
||||||
pub fn with_opcodes(mut self, opcodes: elements::Opcodes) -> Self {
|
pub fn with_instructions(mut self, instructions: elements::Instructions) -> Self {
|
||||||
*self.body.code_mut() = opcodes;
|
*self.body.code_mut() = instructions;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -402,7 +402,7 @@ mod tests {
|
|||||||
.return_type().i32()
|
.return_type().i32()
|
||||||
.build()
|
.build()
|
||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::empty())
|
.with_instructions(elements::Instructions::empty())
|
||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
@ -29,9 +29,9 @@ impl<F> DataSegmentBuilder<F> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set offset initialization opcode. `End` opcode will be added automatically.
|
/// Set offset initialization instruction. `End` instruction will be added automatically.
|
||||||
pub fn offset(mut self, opcode: elements::Opcode) -> Self {
|
pub fn offset(mut self, instruction: elements::Instruction) -> Self {
|
||||||
self.offset = elements::InitExpr::new(vec![opcode, elements::Opcode::End]);
|
self.offset = elements::InitExpr::new(vec![instruction, elements::Instruction::End]);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,9 +40,9 @@ impl<F> GlobalBuilder<F> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set initialization expression opcode for this global (`end` opcode will be added automatically)
|
/// Set initialization expression instruction for this global (`end` instruction will be added automatically)
|
||||||
pub fn init_expr(mut self, opcode: elements::Opcode) -> Self {
|
pub fn init_expr(mut self, instruction: elements::Instruction) -> Self {
|
||||||
self.init_expr = elements::InitExpr::new(vec![opcode, elements::Opcode::End]);
|
self.init_expr = elements::InitExpr::new(vec![instruction, elements::Instruction::End]);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,8 +60,8 @@ impl<F> MemoryBuilder<F> where F: Invoke<MemoryDefinition> {
|
|||||||
pub fn with_data(mut self, index: u32, values: Vec<u8>) -> Self {
|
pub fn with_data(mut self, index: u32, values: Vec<u8>) -> Self {
|
||||||
self.memory.data.push(MemoryDataDefinition {
|
self.memory.data.push(MemoryDataDefinition {
|
||||||
offset: elements::InitExpr::new(vec![
|
offset: elements::InitExpr::new(vec![
|
||||||
elements::Opcode::I32Const(index as i32),
|
elements::Instruction::I32Const(index as i32),
|
||||||
elements::Opcode::End,
|
elements::Instruction::End,
|
||||||
]),
|
]),
|
||||||
values: values,
|
values: values,
|
||||||
});
|
});
|
||||||
|
@ -336,7 +336,7 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use parity_wasm::builder::module;
|
/// use parity_wasm::builder::module;
|
||||||
/// use parity_wasm::elements::Opcode::*;
|
/// use parity_wasm::elements::Instruction::*;
|
||||||
///
|
///
|
||||||
/// let module = module()
|
/// let module = module()
|
||||||
/// .global()
|
/// .global()
|
||||||
@ -359,7 +359,7 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use parity_wasm::builder::module;
|
/// use parity_wasm::builder::module;
|
||||||
/// use parity_wasm::elements::Opcode::*;
|
/// use parity_wasm::elements::Instruction::*;
|
||||||
///
|
///
|
||||||
/// let module = module()
|
/// let module = module()
|
||||||
/// .global()
|
/// .global()
|
||||||
@ -551,7 +551,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn global() {
|
fn global() {
|
||||||
let module = module()
|
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();
|
.build();
|
||||||
|
|
||||||
assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1);
|
assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1);
|
||||||
@ -561,7 +561,7 @@ mod tests {
|
|||||||
fn data() {
|
fn data() {
|
||||||
let module = module()
|
let module = module()
|
||||||
.data()
|
.data()
|
||||||
.offset(::elements::Opcode::I32Const(16))
|
.offset(::elements::Instruction::I32Const(16))
|
||||||
.value(vec![0u8, 15, 10, 5, 25])
|
.value(vec![0u8, 15, 10, 5, 25])
|
||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
@ -60,8 +60,8 @@ impl<F> TableBuilder<F> where F: Invoke<TableDefinition> {
|
|||||||
pub fn with_element(mut self, index: u32, values: Vec<u32>) -> Self {
|
pub fn with_element(mut self, index: u32, values: Vec<u32>) -> Self {
|
||||||
self.table.elements.push(TableEntryDefinition {
|
self.table.elements.push(TableEntryDefinition {
|
||||||
offset: elements::InitExpr::new(vec![
|
offset: elements::InitExpr::new(vec![
|
||||||
elements::Opcode::I32Const(index as i32),
|
elements::Instruction::I32Const(index as i32),
|
||||||
elements::Opcode::End,
|
elements::Instruction::End,
|
||||||
]),
|
]),
|
||||||
values: values,
|
values: values,
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use io;
|
use io;
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
use super::{
|
use super::{
|
||||||
Deserialize, Error, ValueType, VarUint32, CountedList, Opcodes,
|
Deserialize, Error, ValueType, VarUint32, CountedList, Instructions,
|
||||||
Serialize, CountedWriter, CountedListWriter,
|
Serialize, CountedWriter, CountedListWriter,
|
||||||
};
|
};
|
||||||
use elements::section::SectionReader;
|
use elements::section::SectionReader;
|
||||||
@ -85,32 +85,32 @@ impl Serialize for Local {
|
|||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct FuncBody {
|
pub struct FuncBody {
|
||||||
locals: Vec<Local>,
|
locals: Vec<Local>,
|
||||||
opcodes: Opcodes,
|
instructions: Instructions,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FuncBody {
|
impl FuncBody {
|
||||||
/// New function body with given `locals` and `opcodes`
|
/// New function body with given `locals` and `instructions`
|
||||||
pub fn new(locals: Vec<Local>, opcodes: Opcodes) -> Self {
|
pub fn new(locals: Vec<Local>, instructions: Instructions) -> Self {
|
||||||
FuncBody { locals: locals, opcodes: opcodes }
|
FuncBody { locals: locals, instructions: instructions }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List of individual opcodes
|
/// List of individual instructions
|
||||||
pub fn empty() -> Self {
|
pub fn empty() -> Self {
|
||||||
FuncBody { locals: Vec::new(), opcodes: Opcodes::empty() }
|
FuncBody { locals: Vec::new(), instructions: Instructions::empty() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Locals declared in function body.
|
/// Locals declared in function body.
|
||||||
pub fn locals(&self) -> &[Local] { &self.locals }
|
pub fn locals(&self) -> &[Local] { &self.locals }
|
||||||
|
|
||||||
/// Opcode sequence of the function body. Minimal opcode sequence
|
/// Instruction list of the function body. Minimal instruction list
|
||||||
/// is just `&[Opcode::End]`
|
/// is just `&[Instruction::End]`
|
||||||
pub fn code(&self) -> &Opcodes { &self.opcodes }
|
pub fn code(&self) -> &Instructions { &self.instructions }
|
||||||
|
|
||||||
/// Locals declared in function body (mutable).
|
/// Locals declared in function body (mutable).
|
||||||
pub fn locals_mut(&mut self) -> &mut Vec<Local> { &mut self.locals }
|
pub fn locals_mut(&mut self) -> &mut Vec<Local> { &mut self.locals }
|
||||||
|
|
||||||
/// Opcode sequence of the function body (mutable).
|
/// Instruction list of the function body (mutable).
|
||||||
pub fn code_mut(&mut self) -> &mut Opcodes { &mut self.opcodes }
|
pub fn code_mut(&mut self) -> &mut Instructions { &mut self.instructions }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deserialize for FuncBody {
|
impl Deserialize for FuncBody {
|
||||||
@ -120,9 +120,9 @@ impl Deserialize for FuncBody {
|
|||||||
// todo: maybe use reader.take(section_length)
|
// todo: maybe use reader.take(section_length)
|
||||||
let mut body_reader = SectionReader::new(reader)?;
|
let mut body_reader = SectionReader::new(reader)?;
|
||||||
let locals: Vec<Local> = CountedList::<Local>::deserialize(&mut body_reader)?.into_inner();
|
let locals: Vec<Local> = CountedList::<Local>::deserialize(&mut body_reader)?.into_inner();
|
||||||
let opcodes = Opcodes::deserialize(&mut body_reader)?;
|
let instructions = Instructions::deserialize(&mut body_reader)?;
|
||||||
body_reader.close()?;
|
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)?;
|
counted_list.serialize(&mut counted_writer)?;
|
||||||
|
|
||||||
let code = self.opcodes;
|
let code = self.instructions;
|
||||||
code.serialize(&mut counted_writer)?;
|
code.serialize(&mut counted_writer)?;
|
||||||
|
|
||||||
counted_writer.done()?;
|
counted_writer.done()?;
|
||||||
|
@ -18,11 +18,11 @@ impl GlobalEntry {
|
|||||||
}
|
}
|
||||||
/// Global type.
|
/// Global type.
|
||||||
pub fn global_type(&self) -> &GlobalType { &self.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 }
|
pub fn init_expr(&self) -> &InitExpr { &self.init_expr }
|
||||||
/// Global type (mutable)
|
/// Global type (mutable)
|
||||||
pub fn global_type_mut(&mut self) -> &mut GlobalType { &mut self.global_type }
|
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 }
|
pub fn init_expr_mut(&mut self) -> &mut InitExpr { &mut self.init_expr }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ pub use self::primitives::{
|
|||||||
Uint64, VarUint64, CountedList, CountedWriter, CountedListWriter,
|
Uint64, VarUint64, CountedList, CountedWriter, CountedListWriter,
|
||||||
};
|
};
|
||||||
pub use self::types::{Type, ValueType, BlockType, FunctionType, TableElementType};
|
pub use self::types::{Type, ValueType, BlockType, FunctionType, TableElementType};
|
||||||
pub use self::ops::{Opcode, Opcodes, InitExpr};
|
pub use self::ops::{Instruction, Instructions, InitExpr, opcodes};
|
||||||
pub use self::func::{Func, FuncBody, Local};
|
pub use self::func::{Func, FuncBody, Local};
|
||||||
pub use self::segment::{ElementSegment, DataSegment};
|
pub use self::segment::{ElementSegment, DataSegment};
|
||||||
pub use self::index_map::IndexMap;
|
pub use self::index_map::IndexMap;
|
||||||
|
@ -584,7 +584,7 @@ mod integration_tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn const_() {
|
fn const_() {
|
||||||
use super::super::Opcode::*;
|
use super::super::Instruction::*;
|
||||||
|
|
||||||
let module = deserialize_file("./res/cases/v1/const.wasm").expect("Should be deserialized");
|
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];
|
let func = &module.code_section().expect("Code section to exist").bodies()[0];
|
||||||
@ -612,7 +612,7 @@ mod integration_tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn store() {
|
fn store() {
|
||||||
use super::super::Opcode::*;
|
use super::super::Instruction::*;
|
||||||
|
|
||||||
let module = deserialize_file("./res/cases/v1/offset.wasm").expect("Should be deserialized");
|
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];
|
let func = &module.code_section().expect("Code section to exist").bodies()[0];
|
||||||
|
1011
src/elements/ops.rs
1011
src/elements/ops.rs
File diff suppressed because it is too large
Load Diff
@ -805,7 +805,7 @@ mod tests {
|
|||||||
|
|
||||||
use super::super::{
|
use super::super::{
|
||||||
deserialize_buffer, deserialize_file, ValueType, InitExpr, DataSegment,
|
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};
|
use super::{Section, TypeSection, Type, DataSection, ElementSection, CodeSection};
|
||||||
|
|
||||||
@ -1089,13 +1089,13 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn code_section_ser() {
|
fn code_section_ser() {
|
||||||
use super::super::Opcode::*;
|
use super::super::Instruction::*;
|
||||||
|
|
||||||
let code_section = CodeSection::with_bodies(
|
let code_section = CodeSection::with_bodies(
|
||||||
vec![
|
vec![
|
||||||
FuncBody::new(
|
FuncBody::new(
|
||||||
vec![Local::new(1, ValueType::I32)],
|
vec![Local::new(1, ValueType::I32)],
|
||||||
Opcodes::new(vec![
|
Instructions::new(vec![
|
||||||
Block(BlockType::Value(ValueType::I32)),
|
Block(BlockType::Value(ValueType::I32)),
|
||||||
GetGlobal(0),
|
GetGlobal(0),
|
||||||
End,
|
End,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user