Merge pull request #87 from HCastano/use-if-let-statements

Update matches with single arm to be if-let statements
This commit is contained in:
Nikolay Volf
2018-05-31 16:37:47 +03:00
committed by GitHub
7 changed files with 91 additions and 158 deletions

View File

@ -11,26 +11,20 @@ type Insertion = (usize, u32, u32, String);
pub fn update_call_index(opcodes: &mut elements::Opcodes, original_imports: usize, inserts: &[Insertion]) { pub fn update_call_index(opcodes: &mut elements::Opcodes, original_imports: usize, inserts: &[Insertion]) {
use parity_wasm::elements::Opcode::*; use parity_wasm::elements::Opcode::*;
for opcode in opcodes.elements_mut().iter_mut() { for opcode in opcodes.elements_mut().iter_mut() {
match opcode { if let &mut Call(ref mut call_index) = opcode {
&mut Call(ref mut call_index) => { if let Some(pos) = inserts.iter().position(|x| x.1 == *call_index) {
if let Some(pos) = inserts.iter().position(|x| x.1 == *call_index) { *call_index = (original_imports + pos) as u32;
*call_index = (original_imports + pos) as u32; } else if *call_index as usize > original_imports {
} else if *call_index as usize > original_imports { *call_index += inserts.len() as u32;
*call_index += inserts.len() as u32; }
}
},
_ => { }
} }
} }
} }
pub fn memory_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::MemorySection> { pub fn memory_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::MemorySection> {
for section in module.sections_mut() { for section in module.sections_mut() {
match section { if let &mut elements::Section::Memory(ref mut sect) = section {
&mut elements::Section::Memory(ref mut sect) => { return Some(sect);
return Some(sect);
},
_ => { }
} }
} }
None None
@ -182,11 +176,8 @@ pub fn externalize(
}, },
&mut elements::Section::Export(ref mut export_section) => { &mut elements::Section::Export(ref mut export_section) => {
for ref mut export in export_section.entries_mut() { for ref mut export in export_section.entries_mut() {
match export.internal_mut() { if let &mut elements::Internal::Function(ref mut func_index) = export.internal_mut() {
&mut elements::Internal::Function(ref mut func_index) => { if *func_index >= import_funcs_total as u32 { *func_index += replaces.len() as u32; }
if *func_index >= import_funcs_total as u32 { *func_index += replaces.len() as u32; }
},
_ => {}
} }
} }
}, },

View File

@ -6,11 +6,8 @@ use rules;
pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) { pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) {
use parity_wasm::elements::Opcode::*; use parity_wasm::elements::Opcode::*;
for opcode in opcodes.elements_mut().iter_mut() { for opcode in opcodes.elements_mut().iter_mut() {
match opcode { if let &mut Call(ref mut call_index) = opcode {
&mut Call(ref mut call_index) => { if *call_index >= inserted_index { *call_index += 1}
if *call_index >= inserted_index { *call_index += 1}
},
_ => { },
} }
} }
} }
@ -90,12 +87,9 @@ fn inject_grow_counter(opcodes: &mut elements::Opcodes, grow_counter_func: u32)
use parity_wasm::elements::Opcode::*; use parity_wasm::elements::Opcode::*;
let mut counter = 0; let mut counter = 0;
for opcode in opcodes.elements_mut() { for opcode in opcodes.elements_mut() {
match *opcode { if let GrowMemory(_) = *opcode {
GrowMemory(_) => { *opcode = Call(grow_counter_func);
*opcode = Call(grow_counter_func); counter += 1;
counter += 1;
},
_ => {}
} }
} }
counter counter
@ -244,11 +238,8 @@ pub fn inject_gas_counter(module: elements::Module, rules: &rules::Set)
}, },
&mut elements::Section::Export(ref mut export_section) => { &mut elements::Section::Export(ref mut export_section) => {
for ref mut export in export_section.entries_mut() { for ref mut export in export_section.entries_mut() {
match export.internal_mut() { if let &mut elements::Internal::Function(ref mut func_index) = export.internal_mut() {
&mut elements::Internal::Function(ref mut func_index) => { if *func_index >= gas_func { *func_index += 1}
if *func_index >= gas_func { *func_index += 1}
},
_ => {}
} }
} }
}, },

View File

@ -273,13 +273,10 @@ pub fn optimize(
pub fn update_call_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) { pub fn update_call_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) {
use parity_wasm::elements::Opcode::*; use parity_wasm::elements::Opcode::*;
for opcode in opcodes.elements_mut().iter_mut() { for opcode in opcodes.elements_mut().iter_mut() {
match opcode { if let &mut Call(ref mut call_index) = opcode {
&mut Call(ref mut call_index) => { let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *call_index).count();
let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *call_index).count(); trace!("rewired call {} -> call {}", *call_index, *call_index - totalle as u32);
trace!("rewired call {} -> call {}", *call_index, *call_index - totalle as u32); *call_index -= totalle as u32;
*call_index -= totalle as u32;
},
_ => { },
} }
} }
} }
@ -303,24 +300,18 @@ pub fn update_global_index(opcodes: &mut Vec<elements::Opcode>, eliminated_indic
pub fn update_type_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) { pub fn update_type_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) {
use parity_wasm::elements::Opcode::*; use parity_wasm::elements::Opcode::*;
for opcode in opcodes.elements_mut().iter_mut() { for opcode in opcodes.elements_mut().iter_mut() {
match opcode { if let &mut CallIndirect(ref mut call_index, _) = opcode {
&mut CallIndirect(ref mut call_index, _) => { let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *call_index).count();
let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *call_index).count(); trace!("rewired call_indrect {} -> call_indirect {}", *call_index, *call_index - totalle as u32);
trace!("rewired call_indrect {} -> call_indirect {}", *call_index, *call_index - totalle as u32); *call_index -= totalle as u32;
*call_index -= totalle as u32;
},
_ => { },
} }
} }
} }
pub fn import_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::ImportSection> { pub fn import_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::ImportSection> {
for section in module.sections_mut() { for section in module.sections_mut() {
match section { if let &mut elements::Section::Import(ref mut sect) = section {
&mut elements::Section::Import(ref mut sect) => { return Some(sect);
return Some(sect);
},
_ => { }
} }
} }
None None
@ -328,11 +319,8 @@ pub fn import_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut el
pub fn global_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::GlobalSection> { pub fn global_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::GlobalSection> {
for section in module.sections_mut() { for section in module.sections_mut() {
match section { if let &mut elements::Section::Global(ref mut sect) = section {
&mut elements::Section::Global(ref mut sect) => { return Some(sect);
return Some(sect);
},
_ => { }
} }
} }
None None
@ -340,11 +328,8 @@ pub fn global_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut el
pub fn function_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::FunctionSection> { pub fn function_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::FunctionSection> {
for section in module.sections_mut() { for section in module.sections_mut() {
match section { if let &mut elements::Section::Function(ref mut sect) = section {
&mut elements::Section::Function(ref mut sect) => { return Some(sect);
return Some(sect);
},
_ => { }
} }
} }
None None
@ -352,11 +337,8 @@ pub fn function_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut
pub fn code_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::CodeSection> { pub fn code_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::CodeSection> {
for section in module.sections_mut() { for section in module.sections_mut() {
match section { if let &mut elements::Section::Code(ref mut sect) = section {
&mut elements::Section::Code(ref mut sect) => { return Some(sect);
return Some(sect);
},
_ => { }
} }
} }
None None
@ -364,11 +346,8 @@ pub fn code_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elem
pub fn export_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::ExportSection> { pub fn export_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::ExportSection> {
for section in module.sections_mut() { for section in module.sections_mut() {
match section { if let &mut elements::Section::Export(ref mut sect) = section {
&mut elements::Section::Export(ref mut sect) => { return Some(sect);
return Some(sect);
},
_ => { }
} }
} }
None None
@ -376,11 +355,8 @@ pub fn export_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut el
pub fn type_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::TypeSection> { pub fn type_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::TypeSection> {
for section in module.sections_mut() { for section in module.sections_mut() {
match section { if let &mut elements::Section::Type(ref mut sect) = section {
&mut elements::Section::Type(ref mut sect) => { return Some(sect);
return Some(sect);
},
_ => { }
} }
} }
None None

View File

@ -120,11 +120,8 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module) ->
}, },
elements::Section::Export(ref mut export_section) => { elements::Section::Export(ref mut export_section) => {
for ref mut export in export_section.entries_mut() { for ref mut export in export_section.entries_mut() {
match export.internal_mut() { if let &mut elements::Internal::Function(ref mut func_index) = export.internal_mut() {
&mut elements::Internal::Function(ref mut func_index) => { if *func_index >= ret_func { *func_index += 1}
if *func_index >= ret_func { *func_index += 1}
},
_ => {}
} }
} }
}, },
@ -163,28 +160,25 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module) ->
let mut code_data_address = 0i32; let mut code_data_address = 0i32;
for section in ctor_module.sections_mut() { for section in ctor_module.sections_mut() {
match section { if let &mut Section::Data(ref mut data_section) = section {
&mut Section::Data(ref mut data_section) => { let (index, offset) = if let Some(ref entry) = data_section.entries().iter().last() {
let (index, offset) = if let Some(ref entry) = data_section.entries().iter().last() { if let Opcode::I32Const(offst) = entry.offset().code()[0] {
if let Opcode::I32Const(offst) = entry.offset().code()[0] { let len = entry.value().len() as i32;
let len = entry.value().len() as i32; let offst = offst as i32;
let offst = offst as i32; (entry.index(), offst + (len + 4) - len % 4)
(entry.index(), offst + (len + 4) - len % 4)
} else {
(0, 0)
}
} else { } else {
(0, 0) (0, 0)
}; }
let code_data = DataSegment::new( } else {
index, (0, 0)
InitExpr::new(vec![Opcode::I32Const(offset), Opcode::End]), };
raw_module.clone() let code_data = DataSegment::new(
); index,
data_section.entries_mut().push(code_data); InitExpr::new(vec![Opcode::I32Const(offset), Opcode::End]),
code_data_address = offset; raw_module.clone()
}, );
_ => {;} data_section.entries_mut().push(code_data);
code_data_address = offset;
} }
} }
@ -203,17 +197,14 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module) ->
.build(); .build();
for section in new_module.sections_mut() { for section in new_module.sections_mut() {
match section { if let &mut Section::Export(ref mut export_section) = section {
&mut Section::Export(ref mut export_section) => { for entry in export_section.entries_mut().iter_mut() {
for entry in export_section.entries_mut().iter_mut() { if CREATE_SYMBOL == entry.field() {
if CREATE_SYMBOL == entry.field() { // change "CREATE_SYMBOL" export name into default "CALL_SYMBOL"
// change "CREATE_SYMBOL" export name into default "CALL_SYMBOL" *entry.field_mut() = CALL_SYMBOL.to_owned();
*entry.field_mut() = CALL_SYMBOL.to_owned(); *entry.internal_mut() = elements::Internal::Function(last_function_index as u32);
*entry.internal_mut() = elements::Internal::Function(last_function_index as u32);
}
} }
}, }
_ => { },
} }
}; };

View File

@ -165,15 +165,12 @@ fn generate_stack_height_global(ctx: &mut Context, module: &mut elements::Module
// Try to find an existing global section. // Try to find an existing global section.
for section in module.sections_mut() { for section in module.sections_mut() {
match *section { if let elements::Section::Global(ref mut gs) = *section {
elements::Section::Global(ref mut gs) => { gs.entries_mut().push(global_entry);
gs.entries_mut().push(global_entry);
let stack_height_global_idx = (gs.entries().len() as u32) - 1; let stack_height_global_idx = (gs.entries().len() as u32) - 1;
ctx.stack_height_global_idx = Some(stack_height_global_idx); ctx.stack_height_global_idx = Some(stack_height_global_idx);
return; return;
}
_ => {}
} }
} }
@ -233,14 +230,11 @@ fn compute_stack_cost(func_idx: u32, module: &elements::Module) -> Result<u32, E
fn instrument_functions(ctx: &mut Context, module: &mut elements::Module) -> Result<(), Error> { fn instrument_functions(ctx: &mut Context, module: &mut elements::Module) -> Result<(), Error> {
for section in module.sections_mut() { for section in module.sections_mut() {
match *section { if let elements::Section::Code(ref mut code_section) = *section {
elements::Section::Code(ref mut code_section) => { for func_body in code_section.bodies_mut() {
for func_body in code_section.bodies_mut() { let mut opcodes = func_body.code_mut();
let mut opcodes = func_body.code_mut(); instrument_function(ctx, opcodes)?;
instrument_function(ctx, opcodes)?;
}
} }
_ => {}
} }
} }
Ok(()) Ok(())

View File

@ -142,9 +142,8 @@ pub(crate) fn generate_thunks(
match *section { match *section {
elements::Section::Export(ref mut export_section) => { elements::Section::Export(ref mut export_section) => {
for entry in export_section.entries_mut() { for entry in export_section.entries_mut() {
match *entry.internal_mut() { if let Internal::Function(ref mut function_idx) = *entry.internal_mut() {
Internal::Function(ref mut function_idx) => fixup(function_idx), fixup(function_idx)
_ => {}
} }
} }
} }

View File

@ -19,14 +19,11 @@ pub fn resolve_function(module: &elements::Module, index: u32) -> Symbol {
let mut functions = 0; let mut functions = 0;
if let Some(import_section) = module.import_section() { if let Some(import_section) = module.import_section() {
for (item_index, item) in import_section.entries().iter().enumerate() { for (item_index, item) in import_section.entries().iter().enumerate() {
match item.external() { if let &elements::External::Function(_) = item.external() {
&elements::External::Function(_) => { if functions == index {
if functions == index { return Symbol::Import(item_index as usize);
return Symbol::Import(item_index as usize); }
} functions += 1;
functions += 1;
},
_ => {}
} }
} }
} }
@ -38,14 +35,11 @@ pub fn resolve_global(module: &elements::Module, index: u32) -> Symbol {
let mut globals = 0; let mut globals = 0;
if let Some(import_section) = module.import_section() { if let Some(import_section) = module.import_section() {
for (item_index, item) in import_section.entries().iter().enumerate() { for (item_index, item) in import_section.entries().iter().enumerate() {
match item.external() { if let &elements::External::Global(_) = item.external() {
&elements::External::Global(_) => { if globals == index {
if globals == index { return Symbol::Import(item_index as usize);
return Symbol::Import(item_index as usize); }
} globals += 1;
globals += 1;
},
_ => {}
} }
} }
} }
@ -109,15 +103,12 @@ pub fn expand_symbols(module: &elements::Module, set: &mut Set<Symbol>) {
}, },
Import(idx) => { Import(idx) => {
let entry = &module.import_section().expect("Import section to exist").entries()[idx]; let entry = &module.import_section().expect("Import section to exist").entries()[idx];
match entry.external() { if let &elements::External::Function(type_idx) = entry.external() {
&elements::External::Function(type_idx) => { let type_symbol = Symbol::Type(type_idx as usize);
let type_symbol = Symbol::Type(type_idx as usize); if !stop.contains(&type_symbol) {
if !stop.contains(&type_symbol) { fringe.push(type_symbol);
fringe.push(type_symbol); }
} set.insert(type_symbol);
set.insert(type_symbol);
},
_ => {}
} }
}, },
Function(idx) => { Function(idx) => {