From e6e340fa0ae363f563a89bf07c3a77ba08785d35 Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Tue, 29 May 2018 22:46:11 -0400 Subject: [PATCH] Update matches with single arm to be if-let statements --- src/ext.rs | 31 +++++++------------ src/gas.rs | 23 +++++--------- src/optimizer.rs | 64 ++++++++++++--------------------------- src/pack.rs | 61 ++++++++++++++++--------------------- src/stack_height/mod.rs | 24 ++++++--------- src/stack_height/thunk.rs | 5 ++- src/symbols.rs | 41 ++++++++++--------------- 7 files changed, 91 insertions(+), 158 deletions(-) diff --git a/src/ext.rs b/src/ext.rs index 5983785..8a232f8 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -11,26 +11,20 @@ type Insertion = (usize, u32, u32, String); pub fn update_call_index(opcodes: &mut elements::Opcodes, original_imports: usize, inserts: &[Insertion]) { use parity_wasm::elements::Opcode::*; for opcode in opcodes.elements_mut().iter_mut() { - match opcode { - &mut Call(ref mut call_index) => { - if let Some(pos) = inserts.iter().position(|x| x.1 == *call_index) { - *call_index = (original_imports + pos) as u32; - } else if *call_index as usize > original_imports { - *call_index += inserts.len() as u32; - } - }, - _ => { } + if let &mut Call(ref mut call_index) = opcode { + if let Some(pos) = inserts.iter().position(|x| x.1 == *call_index) { + *call_index = (original_imports + pos) as u32; + } else if *call_index as usize > original_imports { + *call_index += inserts.len() as u32; + } } } } pub fn memory_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::MemorySection> { - for section in module.sections_mut() { - match section { - &mut elements::Section::Memory(ref mut sect) => { - return Some(sect); - }, - _ => { } + for section in module.sections_mut() { + if let &mut elements::Section::Memory(ref mut sect) = section { + return Some(sect); } } None @@ -182,11 +176,8 @@ pub fn externalize( }, &mut elements::Section::Export(ref mut export_section) => { for ref mut export in export_section.entries_mut() { - match 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 let &mut elements::Internal::Function(ref mut func_index) = export.internal_mut() { + if *func_index >= import_funcs_total as u32 { *func_index += replaces.len() as u32; } } } }, diff --git a/src/gas.rs b/src/gas.rs index 730e703..18d1197 100644 --- a/src/gas.rs +++ b/src/gas.rs @@ -6,11 +6,8 @@ use rules; pub fn update_call_index(opcodes: &mut elements::Opcodes, inserted_index: u32) { use parity_wasm::elements::Opcode::*; for opcode in opcodes.elements_mut().iter_mut() { - match opcode { - &mut Call(ref mut call_index) => { - if *call_index >= inserted_index { *call_index += 1} - }, - _ => { }, + if let &mut Call(ref mut call_index) = opcode { + 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::*; let mut counter = 0; for opcode in opcodes.elements_mut() { - match *opcode { - GrowMemory(_) => { - *opcode = Call(grow_counter_func); - counter += 1; - }, - _ => {} + if let GrowMemory(_) = *opcode { + *opcode = Call(grow_counter_func); + counter += 1; } } counter @@ -244,11 +238,8 @@ pub fn inject_gas_counter(module: elements::Module, rules: &rules::Set) }, &mut elements::Section::Export(ref mut export_section) => { for ref mut export in export_section.entries_mut() { - match export.internal_mut() { - &mut elements::Internal::Function(ref mut func_index) => { - if *func_index >= gas_func { *func_index += 1} - }, - _ => {} + if let &mut elements::Internal::Function(ref mut func_index) = export.internal_mut() { + if *func_index >= gas_func { *func_index += 1} } } }, diff --git a/src/optimizer.rs b/src/optimizer.rs index 890aab0..2d86ddc 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -273,13 +273,10 @@ pub fn optimize( pub fn update_call_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) { use parity_wasm::elements::Opcode::*; for opcode in opcodes.elements_mut().iter_mut() { - match opcode { - &mut Call(ref mut call_index) => { - 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); - *call_index -= totalle as u32; - }, - _ => { }, + if let &mut Call(ref mut call_index) = opcode { + 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); + *call_index -= totalle as u32; } } } @@ -303,24 +300,18 @@ pub fn update_global_index(opcodes: &mut Vec, eliminated_indic pub fn update_type_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[usize]) { use parity_wasm::elements::Opcode::*; for opcode in opcodes.elements_mut().iter_mut() { - match opcode { - &mut CallIndirect(ref mut call_index, _) => { - 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); - *call_index -= totalle as u32; - }, - _ => { }, + if let &mut CallIndirect(ref mut call_index, _) = opcode { + 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); + *call_index -= totalle as u32; } } } pub fn import_section<'a>(module: &'a mut elements::Module) -> Option<&'a mut elements::ImportSection> { for section in module.sections_mut() { - match section { - &mut elements::Section::Import(ref mut sect) => { - return Some(sect); - }, - _ => { } + if let &mut elements::Section::Import(ref mut sect) = section { + return Some(sect); } } 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> { for section in module.sections_mut() { - match section { - &mut elements::Section::Global(ref mut sect) => { - return Some(sect); - }, - _ => { } + if let &mut elements::Section::Global(ref mut sect) = section { + return Some(sect); } } 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> { for section in module.sections_mut() { - match section { - &mut elements::Section::Function(ref mut sect) => { - return Some(sect); - }, - _ => { } + if let &mut elements::Section::Function(ref mut sect) = section { + return Some(sect); } } 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> { for section in module.sections_mut() { - match section { - &mut elements::Section::Code(ref mut sect) => { - return Some(sect); - }, - _ => { } + if let &mut elements::Section::Code(ref mut sect) = section { + return Some(sect); } } 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> { for section in module.sections_mut() { - match section { - &mut elements::Section::Export(ref mut sect) => { - return Some(sect); - }, - _ => { } + if let &mut elements::Section::Export(ref mut sect) = section { + return Some(sect); } } 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> { for section in module.sections_mut() { - match section { - &mut elements::Section::Type(ref mut sect) => { - return Some(sect); - }, - _ => { } + if let &mut elements::Section::Type(ref mut sect) = section { + return Some(sect); } } None diff --git a/src/pack.rs b/src/pack.rs index 85d4c2b..206d879 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -120,11 +120,8 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> }, elements::Section::Export(ref mut export_section) => { for ref mut export in export_section.entries_mut() { - match export.internal_mut() { - &mut elements::Internal::Function(ref mut func_index) => { - if *func_index >= ret_func { *func_index += 1} - }, - _ => {} + if let &mut elements::Internal::Function(ref mut func_index) = export.internal_mut() { + if *func_index >= ret_func { *func_index += 1} } } }, @@ -163,28 +160,25 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> let mut code_data_address = 0i32; for section in ctor_module.sections_mut() { - match section { - &mut Section::Data(ref mut data_section) => { - let (index, offset) = if let Some(ref entry) = data_section.entries().iter().last() { - if let Opcode::I32Const(offst) = entry.offset().code()[0] { - let len = entry.value().len() as i32; - let offst = offst as i32; - (entry.index(), offst + (len + 4) - len % 4) - } else { - (0, 0) - } + if let &mut Section::Data(ref mut data_section) = section { + let (index, offset) = if let Some(ref entry) = data_section.entries().iter().last() { + if let Opcode::I32Const(offst) = entry.offset().code()[0] { + let len = entry.value().len() as i32; + let offst = offst as i32; + (entry.index(), offst + (len + 4) - len % 4) } else { (0, 0) - }; - let code_data = DataSegment::new( - index, - InitExpr::new(vec![Opcode::I32Const(offset), Opcode::End]), - raw_module.clone() - ); - data_section.entries_mut().push(code_data); - code_data_address = offset; - }, - _ => {;} + } + } else { + (0, 0) + }; + let code_data = DataSegment::new( + index, + InitExpr::new(vec![Opcode::I32Const(offset), Opcode::End]), + 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, mut ctor_module: elements::Module) -> .build(); for section in new_module.sections_mut() { - match section { - &mut Section::Export(ref mut export_section) => { - for entry in export_section.entries_mut().iter_mut() { - if CREATE_SYMBOL == entry.field() { - // change "CREATE_SYMBOL" export name into default "CALL_SYMBOL" - *entry.field_mut() = CALL_SYMBOL.to_owned(); - *entry.internal_mut() = elements::Internal::Function(last_function_index as u32); - } + if let &mut Section::Export(ref mut export_section) = section { + for entry in export_section.entries_mut().iter_mut() { + if CREATE_SYMBOL == entry.field() { + // change "CREATE_SYMBOL" export name into default "CALL_SYMBOL" + *entry.field_mut() = CALL_SYMBOL.to_owned(); + *entry.internal_mut() = elements::Internal::Function(last_function_index as u32); } - }, - _ => { }, + } } }; diff --git a/src/stack_height/mod.rs b/src/stack_height/mod.rs index 119dedc..95d2863 100644 --- a/src/stack_height/mod.rs +++ b/src/stack_height/mod.rs @@ -165,15 +165,12 @@ fn generate_stack_height_global(ctx: &mut Context, module: &mut elements::Module // Try to find an existing global section. for section in module.sections_mut() { - match *section { - elements::Section::Global(ref mut gs) => { - gs.entries_mut().push(global_entry); + if let elements::Section::Global(ref mut gs) = *section { + gs.entries_mut().push(global_entry); - let stack_height_global_idx = (gs.entries().len() as u32) - 1; - ctx.stack_height_global_idx = Some(stack_height_global_idx); - return; - } - _ => {} + let stack_height_global_idx = (gs.entries().len() as u32) - 1; + ctx.stack_height_global_idx = Some(stack_height_global_idx); + return; } } @@ -233,14 +230,11 @@ fn compute_stack_cost(func_idx: u32, module: &elements::Module) -> Result Result<(), Error> { for section in module.sections_mut() { - match *section { - elements::Section::Code(ref mut code_section) => { - for func_body in code_section.bodies_mut() { - let mut opcodes = func_body.code_mut(); - instrument_function(ctx, opcodes)?; - } + if let elements::Section::Code(ref mut code_section) = *section { + for func_body in code_section.bodies_mut() { + let mut opcodes = func_body.code_mut(); + instrument_function(ctx, opcodes)?; } - _ => {} } } Ok(()) diff --git a/src/stack_height/thunk.rs b/src/stack_height/thunk.rs index ea1354b..666084d 100644 --- a/src/stack_height/thunk.rs +++ b/src/stack_height/thunk.rs @@ -142,9 +142,8 @@ pub(crate) fn generate_thunks( match *section { elements::Section::Export(ref mut export_section) => { for entry in export_section.entries_mut() { - match *entry.internal_mut() { - Internal::Function(ref mut function_idx) => fixup(function_idx), - _ => {} + if let Internal::Function(ref mut function_idx) = *entry.internal_mut() { + fixup(function_idx) } } } diff --git a/src/symbols.rs b/src/symbols.rs index 4e778a8..ce2803a 100644 --- a/src/symbols.rs +++ b/src/symbols.rs @@ -19,14 +19,11 @@ pub fn resolve_function(module: &elements::Module, index: u32) -> Symbol { let mut functions = 0; if let Some(import_section) = module.import_section() { for (item_index, item) in import_section.entries().iter().enumerate() { - match item.external() { - &elements::External::Function(_) => { - if functions == index { - return Symbol::Import(item_index as usize); - } - functions += 1; - }, - _ => {} + if let &elements::External::Function(_) = item.external() { + if functions == index { + return Symbol::Import(item_index as usize); + } + functions += 1; } } } @@ -38,14 +35,11 @@ pub fn resolve_global(module: &elements::Module, index: u32) -> Symbol { let mut globals = 0; if let Some(import_section) = module.import_section() { for (item_index, item) in import_section.entries().iter().enumerate() { - match item.external() { - &elements::External::Global(_) => { - if globals == index { - return Symbol::Import(item_index as usize); - } - globals += 1; - }, - _ => {} + if let &elements::External::Global(_) = item.external() { + if globals == index { + return Symbol::Import(item_index as usize); + } + globals += 1; } } } @@ -109,15 +103,12 @@ pub fn expand_symbols(module: &elements::Module, set: &mut Set) { }, Import(idx) => { let entry = &module.import_section().expect("Import section to exist").entries()[idx]; - match entry.external() { - &elements::External::Function(type_idx) => { - let type_symbol = Symbol::Type(type_idx as usize); - if !stop.contains(&type_symbol) { - fringe.push(type_symbol); - } - set.insert(type_symbol); - }, - _ => {} + if let &elements::External::Function(type_idx) = entry.external() { + let type_symbol = Symbol::Type(type_idx as usize); + if !stop.contains(&type_symbol) { + fringe.push(type_symbol); + } + set.insert(type_symbol); } }, Function(idx) => {