From d765677fcc501da10b702f62dae02195fb100f44 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 10 Jan 2020 04:40:25 -0800 Subject: [PATCH] Add a `is_wasm_parameter` method to the wasm `FuncEnvironment`. (#1329) This provides a more flexible way to allow embedding to tell cranelift-wasm which function parameters are hidden, and which should be translated as wasm user variables. This replaces https://github.com/bytecodealliance/cranelift/pull/1086. --- cranelift/wasm/src/environ/dummy.rs | 4 ++++ cranelift/wasm/src/environ/spec.rs | 4 ++++ cranelift/wasm/src/func_translator.rs | 12 ++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cranelift/wasm/src/environ/dummy.rs b/cranelift/wasm/src/environ/dummy.rs index 6b6f210166..cb6d78e9f9 100644 --- a/cranelift/wasm/src/environ/dummy.rs +++ b/cranelift/wasm/src/environ/dummy.rs @@ -201,6 +201,10 @@ impl<'dummy_environment> TargetEnvironment for DummyFuncEnvironment<'dummy_envir } impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environment> { + fn is_wasm_parameter(&self, func: &ir::Function, index: usize) -> bool { + func.signature.params[index].purpose == ir::ArgumentPurpose::Normal + } + fn return_mode(&self) -> ReturnMode { self.return_mode } diff --git a/cranelift/wasm/src/environ/spec.rs b/cranelift/wasm/src/environ/spec.rs index 5a38d7da62..446a637020 100644 --- a/cranelift/wasm/src/environ/spec.rs +++ b/cranelift/wasm/src/environ/spec.rs @@ -138,6 +138,10 @@ pub trait TargetEnvironment { /// IR. The function environment provides information about the WebAssembly module as well as the /// runtime environment. pub trait FuncEnvironment: TargetEnvironment { + /// Is the given parameter of the given function a wasm-level parameter, as opposed to a hidden + /// parameter added for use by the implementation? + fn is_wasm_parameter(&self, func: &ir::Function, index: usize) -> bool; + /// Should the code be structured to use a single `fallthrough_return` instruction at the end /// of the function body, rather than `return` instructions as needed? This is used by VMs /// to append custom epilogues. diff --git a/cranelift/wasm/src/func_translator.rs b/cranelift/wasm/src/func_translator.rs index b6a46ecb01..2e7c1bc766 100644 --- a/cranelift/wasm/src/func_translator.rs +++ b/cranelift/wasm/src/func_translator.rs @@ -99,7 +99,7 @@ impl FuncTranslator { // `environ`. The callback functions may need to insert things in the entry block. builder.ensure_inserted_ebb(); - let num_params = declare_wasm_parameters(&mut builder, entry_block); + let num_params = declare_wasm_parameters(&mut builder, entry_block, environ); // Set up the translation state with a single pushed control block representing the whole // function and its return values. @@ -124,14 +124,18 @@ impl FuncTranslator { /// Declare local variables for the signature parameters that correspond to WebAssembly locals. /// /// Return the number of local variables declared. -fn declare_wasm_parameters(builder: &mut FunctionBuilder, entry_block: Ebb) -> usize { +fn declare_wasm_parameters( + builder: &mut FunctionBuilder, + entry_block: Ebb, + environ: &FE, +) -> usize { let sig_len = builder.func.signature.params.len(); let mut next_local = 0; for i in 0..sig_len { let param_type = builder.func.signature.params[i]; - // There may be additional special-purpose parameters following the normal WebAssembly + // There may be additional special-purpose parameters in addition to the normal WebAssembly // signature parameters. For example, a `vmctx` pointer. - if param_type.purpose == ir::ArgumentPurpose::Normal { + if environ.is_wasm_parameter(&builder.func, i) { // This is a normal WebAssembly signature parameter, so create a local for it. let local = Variable::new(next_local); builder.declare_var(local, param_type.value_type);