From 710182ad26ce0155b71f20cbb8dc6ade45919802 Mon Sep 17 00:00:00 2001 From: Ryan Hunt Date: Mon, 6 Jan 2020 15:36:46 -0600 Subject: [PATCH] Wasm: Add support for nullref type Reference types now supports proper a nullref type. This commit changes the Wasm translator to support this. --- cranelift/wasm/src/func_translator.rs | 1 + cranelift/wasm/src/translation_utils.rs | 7 +++++-- cranelift/wasmtests/nullref.wat | 11 +++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 cranelift/wasmtests/nullref.wat diff --git a/cranelift/wasm/src/func_translator.rs b/cranelift/wasm/src/func_translator.rs index a7dbf0820a..cc61b28c9b 100644 --- a/cranelift/wasm/src/func_translator.rs +++ b/cranelift/wasm/src/func_translator.rs @@ -196,6 +196,7 @@ fn declare_locals( let constant_handle = builder.func.dfg.constants.insert([0; 16].to_vec().into()); builder.ins().vconst(ir::types::I8X16, constant_handle) } + NullRef => builder.ins().null(environ.reference_type()), AnyRef => builder.ins().null(environ.reference_type()), AnyFunc => builder.ins().null(environ.reference_type()), ty => return Err(wasm_unsupported!("unsupported local type {:?}", ty)), diff --git a/cranelift/wasm/src/translation_utils.rs b/cranelift/wasm/src/translation_utils.rs index 9d38d9dbb6..d431c13f29 100644 --- a/cranelift/wasm/src/translation_utils.rs +++ b/cranelift/wasm/src/translation_utils.rs @@ -131,7 +131,9 @@ pub fn type_to_type( wasmparser::Type::F32 => Ok(ir::types::F32), wasmparser::Type::F64 => Ok(ir::types::F64), wasmparser::Type::V128 => Ok(ir::types::I8X16), - wasmparser::Type::AnyRef | wasmparser::Type::AnyFunc => Ok(environ.reference_type()), + wasmparser::Type::AnyRef | wasmparser::Type::AnyFunc | wasmparser::Type::NullRef => { + Ok(environ.reference_type()) + } ty => Err(wasm_unsupported!("type_to_type: wasm type {:?}", ty)), } } @@ -171,6 +173,7 @@ pub fn blocktype_params_results( wasmparser::Type::V128 => (&[], &[wasmparser::Type::V128]), wasmparser::Type::AnyRef => (&[], &[wasmparser::Type::AnyRef]), wasmparser::Type::AnyFunc => (&[], &[wasmparser::Type::AnyFunc]), + wasmparser::Type::NullRef => (&[], &[wasmparser::Type::NullRef]), wasmparser::Type::EmptyBlockType => (&[], &[]), ty => return Err(wasm_unsupported!("blocktype_params_results: type {:?}", ty)), }, @@ -203,7 +206,7 @@ pub fn ebb_with_params( wasmparser::Type::F64 => { builder.append_ebb_param(ebb, ir::types::F64); } - wasmparser::Type::AnyRef | wasmparser::Type::AnyFunc => { + wasmparser::Type::AnyRef | wasmparser::Type::AnyFunc | wasmparser::Type::NullRef => { builder.append_ebb_param(ebb, environ.reference_type()); } wasmparser::Type::V128 => { diff --git a/cranelift/wasmtests/nullref.wat b/cranelift/wasmtests/nullref.wat new file mode 100644 index 0000000000..86714a2b6e --- /dev/null +++ b/cranelift/wasmtests/nullref.wat @@ -0,0 +1,11 @@ +(module + (func (result nullref) + ref.null + ) + + (func (result nullref) + (block (result nullref) + ref.null + ) + ) +)