From d99b43e4b7aae1fde986c83055de99356981c363 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sun, 18 Mar 2018 13:47:17 -0700 Subject: [PATCH] Add a hook to the wasm FuncEnvironment for emitting loop headers. This will allow wasm implementations that wish to insert code into every loop, for example to insert an interrupt check or a safepoint. do so without relying on asynchronous signals. --- lib/wasm/src/code_translator.rs | 1 + lib/wasm/src/environ/spec.rs | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/wasm/src/code_translator.rs b/lib/wasm/src/code_translator.rs index aab015c536..bb675cb1c7 100644 --- a/lib/wasm/src/code_translator.rs +++ b/lib/wasm/src/code_translator.rs @@ -137,6 +137,7 @@ pub fn translate_operator( builder.ins().jump(loop_body, &[]); state.push_loop(loop_body, next, num_return_values(ty)); builder.switch_to_block(loop_body); + environ.translate_loop_header(builder.cursor()); } Operator::If { ty } => { let val = state.pop1(); diff --git a/lib/wasm/src/environ/spec.rs b/lib/wasm/src/environ/spec.rs index 732438a573..7f73c3fc16 100644 --- a/lib/wasm/src/environ/spec.rs +++ b/lib/wasm/src/environ/spec.rs @@ -144,6 +144,14 @@ pub trait FuncEnvironment { index: MemoryIndex, heap: ir::Heap, ) -> ir::Value; + + /// Emit code at the beginning of every wasm loop. + /// + /// This can be used to insert explicit interrupt or safepoint checking at + /// the beginnings of loops. + fn translate_loop_header(&mut self, _pos: FuncCursor) { + // By default, don't emit anything. + } } /// An object satisfying the `ModuleEnvironment` trait can be passed as argument to the