diff --git a/cranelift/wasm/src/code_translator.rs b/cranelift/wasm/src/code_translator.rs index 664d839023..1801c6539b 100644 --- a/cranelift/wasm/src/code_translator.rs +++ b/cranelift/wasm/src/code_translator.rs @@ -133,7 +133,7 @@ pub fn translate_operator( ***********************************************************************************/ Operator::Block { ty } => { let next = builder.create_ebb(); - if let Ok(ty_cre) = blocktype_to_type(*ty) { + if let Some(ty_cre) = blocktype_to_type(*ty)? { builder.append_ebb_param(next, ty_cre); } state.push_block(next, num_return_values(*ty)?); @@ -141,7 +141,7 @@ pub fn translate_operator( Operator::Loop { ty } => { let loop_body = builder.create_ebb(); let next = builder.create_ebb(); - if let Ok(ty_cre) = blocktype_to_type(*ty) { + if let Some(ty_cre) = blocktype_to_type(*ty)? { builder.append_ebb_param(next, ty_cre); } builder.ins().jump(loop_body, &[]); @@ -168,7 +168,7 @@ pub fn translate_operator( // and we add nothing; // - either the If have an Else clause, in that case the destination of this jump // instruction will be changed later when we translate the Else operator. - if let Ok(ty_cre) = blocktype_to_type(*ty) { + if let Some(ty_cre) = blocktype_to_type(*ty)? { builder.append_ebb_param(if_not, ty_cre); } state.push_if(jump_inst, if_not, num_return_values(*ty)?); diff --git a/cranelift/wasm/src/translation_utils.rs b/cranelift/wasm/src/translation_utils.rs index 90967d4102..f033b8660c 100644 --- a/cranelift/wasm/src/translation_utils.rs +++ b/cranelift/wasm/src/translation_utils.rs @@ -115,35 +115,43 @@ pub struct Memory { /// Helper function translating wasmparser types to Cranelift types when possible. pub fn type_to_type(ty: wasmparser::Type) -> WasmResult { - Ok(match ty { - wasmparser::Type::I32 => ir::types::I32, - wasmparser::Type::I64 => ir::types::I64, - wasmparser::Type::F32 => ir::types::F32, - wasmparser::Type::F64 => ir::types::F64, - ty => wasm_unsupported!("unsupported wasm type {:?}", ty), - }) + match ty { + wasmparser::Type::I32 => Ok(ir::types::I32), + wasmparser::Type::I64 => Ok(ir::types::I64), + wasmparser::Type::F32 => Ok(ir::types::F32), + wasmparser::Type::F64 => Ok(ir::types::F64), + ty => wasm_unsupported!("type_to_type: wasm type {:?}", ty), + } } /// Helper function translating wasmparser possible table types to Cranelift types when possible, /// or None for Func tables. pub fn tabletype_to_type(ty: wasmparser::Type) -> WasmResult> { - Ok(match ty { - wasmparser::Type::I32 => Some(ir::types::I32), - wasmparser::Type::I64 => Some(ir::types::I64), - wasmparser::Type::F32 => Some(ir::types::F32), - wasmparser::Type::F64 => Some(ir::types::F64), - wasmparser::Type::AnyFunc => None, - ty => wasm_unsupported!("unsupported table wasm type {:?}", ty), - }) + match ty { + wasmparser::Type::I32 => Ok(Some(ir::types::I32)), + wasmparser::Type::I64 => Ok(Some(ir::types::I64)), + wasmparser::Type::F32 => Ok(Some(ir::types::F32)), + wasmparser::Type::F64 => Ok(Some(ir::types::F64)), + wasmparser::Type::AnyFunc => Ok(None), + ty => wasm_unsupported!("tabletype_to_type: table wasm type {:?}", ty), + } } /// Helper function translating wasmparser block signatures to Cranelift types when possible. -pub fn blocktype_to_type(ty: wasmparser::TypeOrFuncType) -> WasmResult { - match ty { - wasmparser::TypeOrFuncType::Type(ty) => type_to_type(ty), - wasmparser::TypeOrFuncType::FuncType(_) => { - wasm_unsupported!("multi-value block signature {:?}", ty); - } +pub fn blocktype_to_type(ty_or_ft: wasmparser::TypeOrFuncType) -> WasmResult> { + match ty_or_ft { + wasmparser::TypeOrFuncType::Type(ty) => match ty { + wasmparser::Type::I32 => Ok(Some(ir::types::I32)), + wasmparser::Type::I64 => Ok(Some(ir::types::I64)), + wasmparser::Type::F32 => Ok(Some(ir::types::F32)), + wasmparser::Type::F64 => Ok(Some(ir::types::F64)), + wasmparser::Type::EmptyBlockType => Ok(None), + ty => wasm_unsupported!("blocktype_to_type: type {:?}", ty), + }, + wasmparser::TypeOrFuncType::FuncType(_) => wasm_unsupported!( + "blocktype_to_type: multi-value block signature {:?}", + ty_or_ft + ), } }