From 18307bb79c1ad1a50893b2b1bca1e626dd62f23e Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Mon, 22 Jul 2019 12:15:56 -0700 Subject: [PATCH] Improve panic/unreachable/unimplemented usage. Refactor a little. --- lib/clif-backend/src/module.rs | 2 +- lib/llvm-backend/src/code.rs | 31 ++++++++----------------------- lib/runtime-c-api/src/export.rs | 2 +- lib/runtime-c-api/src/instance.rs | 2 +- lib/runtime-c-api/src/value.rs | 10 +++++----- lib/runtime-core/src/instance.rs | 2 +- lib/runtime-core/src/loader.rs | 7 ++++--- 7 files changed, 21 insertions(+), 35 deletions(-) diff --git a/lib/clif-backend/src/module.rs b/lib/clif-backend/src/module.rs index 91e14bf7c..e7f73b9d0 100644 --- a/lib/clif-backend/src/module.rs +++ b/lib/clif-backend/src/module.rs @@ -108,7 +108,7 @@ impl From> for Type { ir::types::F32 => Type::F32, ir::types::F64 => Type::F64, ir::types::I32X4 => Type::V128, - _ => panic!("unsupported wasm type"), + _ => unimplemented!("unsupported wasm type"), } } } diff --git a/lib/llvm-backend/src/code.rs b/lib/llvm-backend/src/code.rs index dd42ae39a..2d2fddcb7 100644 --- a/lib/llvm-backend/src/code.rs +++ b/lib/llvm-backend/src/code.rs @@ -111,12 +111,11 @@ fn trunc_sat( // int_min or int_max. let is_signed = int_min_value != 0; + let ivec_element_ty = ivec_ty.get_element_type().into_int_type(); let int_min_value = splat_vector( builder, intrinsics, - ivec_ty - .get_element_type() - .into_int_type() + ivec_element_ty .const_int(int_min_value, is_signed) .as_basic_value_enum(), ivec_ty, @@ -125,9 +124,7 @@ fn trunc_sat( let int_max_value = splat_vector( builder, intrinsics, - ivec_ty - .get_element_type() - .into_int_type() + ivec_element_ty .const_int(int_max_value, is_signed) .as_basic_value_enum(), ivec_ty, @@ -135,38 +132,26 @@ fn trunc_sat( ); let lower_bound = if is_signed { builder.build_signed_int_to_float( - ivec_ty - .get_element_type() - .into_int_type() - .const_int(lower_bound, is_signed), + ivec_element_ty.const_int(lower_bound, is_signed), fvec_ty.get_element_type().into_float_type(), "", ) } else { builder.build_unsigned_int_to_float( - ivec_ty - .get_element_type() - .into_int_type() - .const_int(lower_bound, is_signed), + ivec_element_ty.const_int(lower_bound, is_signed), fvec_ty.get_element_type().into_float_type(), "", ) }; let upper_bound = if is_signed { builder.build_signed_int_to_float( - ivec_ty - .get_element_type() - .into_int_type() - .const_int(upper_bound, is_signed), + ivec_element_ty.const_int(upper_bound, is_signed), fvec_ty.get_element_type().into_float_type(), "", ) } else { builder.build_unsigned_int_to_float( - ivec_ty - .get_element_type() - .into_int_type() - .const_int(upper_bound, is_signed), + ivec_element_ty.const_int(upper_bound, is_signed), fvec_ty.get_element_type().into_float_type(), "", ) @@ -3948,7 +3933,7 @@ impl FunctionCodeGenerator for LLVMFunctionCodeGenerator { Operator::I16x8AllTrue => intrinsics.i16x8_ty, Operator::I32x4AllTrue => intrinsics.i32x4_ty, Operator::I64x2AllTrue => intrinsics.i64x2_ty, - _ => panic!(), + _ => unreachable!(), }; let v = state.pop1()?.into_int_value(); let lane_int_ty = context.custom_width_int_type(vec_ty.get_size()); diff --git a/lib/runtime-c-api/src/export.rs b/lib/runtime-c-api/src/export.rs index d93f987a4..2facfe6c3 100644 --- a/lib/runtime-c-api/src/export.rs +++ b/lib/runtime-c-api/src/export.rs @@ -402,7 +402,7 @@ pub unsafe extern "C" fn wasmer_export_func_call( tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64: x }, }, - _ => panic!("not implemented"), + Value::V128(_) => unimplemented!("returning V128 type"), }; results[0] = ret; } diff --git a/lib/runtime-c-api/src/instance.rs b/lib/runtime-c-api/src/instance.rs index 4fc76f766..5a1f8719a 100644 --- a/lib/runtime-c-api/src/instance.rs +++ b/lib/runtime-c-api/src/instance.rs @@ -173,7 +173,7 @@ pub unsafe extern "C" fn wasmer_instance_call( tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64: x }, }, - _ => panic!("not implemented"), + Value::V128(_) => unimplemented!("calling function with V128 parameter"), }; results[0] = ret; } diff --git a/lib/runtime-c-api/src/value.rs b/lib/runtime-c-api/src/value.rs index 62f62c3ac..e8e4019ad 100644 --- a/lib/runtime-c-api/src/value.rs +++ b/lib/runtime-c-api/src/value.rs @@ -51,7 +51,7 @@ impl From for Value { tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64 }, } => Value::F64(F64), - _ => panic!("not implemented"), + _ => unreachable!("unknown WASM type"), } } } @@ -76,7 +76,7 @@ impl From for wasmer_value_t { tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64: x }, }, - _ => panic!("not implemented"), + Value::V128(_) => unimplemented!("V128 not supported in C API"), } } } @@ -89,7 +89,7 @@ impl From for wasmer_value_tag { Type::I64 => wasmer_value_tag::WASM_I64, Type::F32 => wasmer_value_tag::WASM_F32, Type::F64 => wasmer_value_tag::WASM_F64, - _ => panic!("not implemented"), + Type::V128 => unreachable!("V128 not supported in C API"), } } } @@ -102,7 +102,7 @@ impl From for Type { wasmer_value_tag::WASM_I64 => Type::I64, wasmer_value_tag::WASM_F32 => Type::F32, wasmer_value_tag::WASM_F64 => Type::F64, - _ => panic!("not implemented"), + _ => unreachable!("unknown WASM type"), } } } @@ -114,7 +114,7 @@ impl From<&wasmer_runtime::wasm::Type> for wasmer_value_tag { Type::I64 => wasmer_value_tag::WASM_I64, Type::F32 => wasmer_value_tag::WASM_F32, Type::F64 => wasmer_value_tag::WASM_F64, - _ => panic!("not implemented"), + Type::V128 => unimplemented!("V128 not supported in C API"), } } } diff --git a/lib/runtime-core/src/instance.rs b/lib/runtime-core/src/instance.rs index d724e9bd4..bc9e27636 100644 --- a/lib/runtime-core/src/instance.rs +++ b/lib/runtime-core/src/instance.rs @@ -618,7 +618,7 @@ fn call_func_with_index( Type::I64 => Value::I64(raw as i64), Type::F32 => Value::F32(f32::from_bits(raw as u32)), Type::F64 => Value::F64(f64::from_bits(raw)), - _ => unreachable!(), + Type::V128 => unreachable!("V128 does not map to any single value"), }; match signature.returns() { diff --git a/lib/runtime-core/src/loader.rs b/lib/runtime-core/src/loader.rs index 44be58ce9..9a790c6a4 100644 --- a/lib/runtime-core/src/loader.rs +++ b/lib/runtime-core/src/loader.rs @@ -83,9 +83,10 @@ impl Instance for LocalInstance { match args_u64.len() { 0 => (transmute::<_, extern "C" fn() -> u128>(addr))(), 1 => (transmute::<_, extern "C" fn(u64) -> u128>(addr))(args_u64[0]), - 2 => { - (transmute::<_, extern "C" fn(u64, u64) -> u128>(addr))(args_u64[0], args_u64[1]) - } + 2 => (transmute::<_, extern "C" fn(u64, u64) -> u128>(addr))( + args_u64[0], + args_u64[1], + ), 3 => (transmute::<_, extern "C" fn(u64, u64, u64) -> u128>(addr))( args_u64[0], args_u64[1],