Merge pull request #214 from paritytech/code-section

Rename Opcode to Instruction
This commit is contained in:
Nikolay Volf 2018-05-21 22:29:05 +03:00 committed by GitHub
commit f9dbb0cdb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 644 additions and 461 deletions

View File

@ -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;
}
}

View File

@ -224,7 +224,7 @@ impl<F> FuncBodyBuilder<F> {
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<F> FuncBodyBuilder<F> where F: Invoke<elements::FuncBody> {
}
/// 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();

View File

@ -29,9 +29,9 @@ impl<F> DataSegmentBuilder<F> {
}
}
/// 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
}

View File

@ -40,9 +40,9 @@ impl<F> GlobalBuilder<F> {
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
}

View File

@ -60,8 +60,8 @@ impl<F> MemoryBuilder<F> where F: Invoke<MemoryDefinition> {
pub fn with_data(mut self, index: u32, values: Vec<u8>) -> 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,
});

View File

@ -336,7 +336,7 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
/// # 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<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
/// # 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();

View File

@ -60,8 +60,8 @@ impl<F> TableBuilder<F> where F: Invoke<TableDefinition> {
pub fn with_element(mut self, index: u32, values: Vec<u32>) -> 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,
});

View File

@ -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<Local>,
opcodes: Opcodes,
instructions: Instructions,
}
impl FuncBody {
/// New function body with given `locals` and `opcodes`
pub fn new(locals: Vec<Local>, opcodes: Opcodes) -> Self {
FuncBody { locals: locals, opcodes: opcodes }
/// New function body with given `locals` and `instructions`
pub fn new(locals: Vec<Local>, 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<Local> { &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<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()?;
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()?;

View File

@ -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 }
}

View File

@ -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, opcodes};
pub use self::func::{Func, FuncBody, Local};
pub use self::segment::{ElementSegment, DataSegment};
pub use self::index_map::IndexMap;

View File

@ -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];

File diff suppressed because it is too large Load Diff

View File

@ -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,