Wasm: Allow environment to translate some global.set/get operations

Spidermonkey will need to emit pre/post barriers for global.set/get to a
reference type. #1176 and #1299 plan to add a template concept that could
be used to implement this. Once that has been stabilized, we should be able
to remove this code in favor of templates easily.
This commit is contained in:
Ryan Hunt
2020-01-06 15:41:02 -06:00
parent f41bf5ecca
commit 41f225804b
3 changed files with 50 additions and 1 deletions

View File

@@ -28,7 +28,7 @@ use crate::state::{ControlStackFrame, ElseData, FuncTranslationState, ModuleTran
use crate::translation_utils::{ use crate::translation_utils::{
blocktype_params_results, ebb_with_params, f32_translation, f64_translation, blocktype_params_results, ebb_with_params, f32_translation, f64_translation,
}; };
use crate::translation_utils::{FuncIndex, MemoryIndex, SignatureIndex, TableIndex}; use crate::translation_utils::{FuncIndex, GlobalIndex, MemoryIndex, SignatureIndex, TableIndex};
use crate::wasm_unsupported; use crate::wasm_unsupported;
use core::{i32, u32}; use core::{i32, u32};
use cranelift_codegen::ir::condcodes::{FloatCC, IntCC}; use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
@@ -95,6 +95,10 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
let flags = ir::MemFlags::trusted(); let flags = ir::MemFlags::trusted();
builder.ins().load(ty, flags, addr, offset) builder.ins().load(ty, flags, addr, offset)
} }
GlobalVariable::Custom => environ.translate_custom_global_get(
builder.cursor(),
GlobalIndex::from_u32(*global_index),
)?,
}; };
state.push1(val); state.push1(val);
} }
@@ -108,6 +112,14 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
debug_assert_eq!(ty, builder.func.dfg.value_type(val)); debug_assert_eq!(ty, builder.func.dfg.value_type(val));
builder.ins().store(flags, val, addr, offset); builder.ins().store(flags, val, addr, offset);
} }
GlobalVariable::Custom => {
let val = state.pop1();
environ.translate_custom_global_set(
builder.cursor(),
GlobalIndex::from_u32(*global_index),
val,
)?;
}
} }
} }
/********************************* Stack misc *************************************** /********************************* Stack misc ***************************************

View File

@@ -504,6 +504,23 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ
) -> WasmResult<ir::Value> { ) -> WasmResult<ir::Value> {
Ok(pos.ins().null(self.reference_type())) Ok(pos.ins().null(self.reference_type()))
} }
fn translate_custom_global_get(
&mut self,
mut pos: FuncCursor,
_global_index: GlobalIndex,
) -> WasmResult<ir::Value> {
Ok(pos.ins().iconst(I32, -1))
}
fn translate_custom_global_set(
&mut self,
_pos: FuncCursor,
_global_index: GlobalIndex,
_val: ir::Value,
) -> WasmResult<()> {
Ok(())
}
} }
impl TargetEnvironment for DummyEnvironment { impl TargetEnvironment for DummyEnvironment {

View File

@@ -36,6 +36,9 @@ pub enum GlobalVariable {
/// The global variable's type. /// The global variable's type.
ty: ir::Type, ty: ir::Type,
}, },
/// This is a global variable that needs to be handled by the environment.
Custom,
} }
/// A WebAssembly translation error. /// A WebAssembly translation error.
@@ -406,6 +409,23 @@ pub trait FuncEnvironment: TargetEnvironment {
/// Translate a `ref.func` WebAssembly instruction. /// Translate a `ref.func` WebAssembly instruction.
fn translate_ref_func(&mut self, pos: FuncCursor, func_index: u32) -> WasmResult<ir::Value>; fn translate_ref_func(&mut self, pos: FuncCursor, func_index: u32) -> WasmResult<ir::Value>;
/// Translate a `global.get` WebAssembly instruction at `pos` for a global
/// that is custom.
fn translate_custom_global_get(
&mut self,
pos: FuncCursor,
global_index: GlobalIndex,
) -> WasmResult<ir::Value>;
/// Translate a `global.set` WebAssembly instruction at `pos` for a global
/// that is custom.
fn translate_custom_global_set(
&mut self,
pos: FuncCursor,
global_index: GlobalIndex,
val: ir::Value,
) -> WasmResult<()>;
/// Emit code at the beginning of every wasm loop. /// Emit code at the beginning of every wasm loop.
/// ///
/// This can be used to insert explicit interrupt or safepoint checking at /// This can be used to insert explicit interrupt or safepoint checking at