From 38a84342a2d9bdd89f85b9db77c9d7fb56b7398f Mon Sep 17 00:00:00 2001 From: Fabian Date: Sun, 26 Aug 2018 19:47:02 -0500 Subject: [PATCH] Give Rust more indirect function slots and clean up the code --- src/browser/starter.js | 2 +- src/const.js | 3 +++ src/cpu.js | 7 +++---- src/rust/cpu2/cpu.rs | 6 +++++- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/browser/starter.js b/src/browser/starter.js index 55ef3784..89b2c0d7 100644 --- a/src/browser/starter.js +++ b/src/browser/starter.js @@ -105,7 +105,7 @@ function V86Starter(options) // }, this); //} - const wasm_table = new WebAssembly.Table({ element: "anyfunc", "initial": 0x10000 + 0x100 }); + const wasm_table = new WebAssembly.Table({ element: "anyfunc", "initial": WASM_TABLE_SIZE + WASM_TABLE_OFFSET }); var wasm_shared_funcs = { "__assert_fail": (condition, file, line, fun) => { diff --git a/src/const.js b/src/const.js index d9865c51..4c8c9e6a 100644 --- a/src/const.js +++ b/src/const.js @@ -358,6 +358,9 @@ const P_RUN_INTERPRETED = 4; /** @const */ var WASM_TABLE_SIZE = 0x10000; +/** @const */ +var WASM_TABLE_OFFSET = 1024; + /** @const */ var JIT_CACHE_ARRAY_SIZE = 0x40000; diff --git a/src/cpu.js b/src/cpu.js index 249f2610..fca9b98b 100644 --- a/src/cpu.js +++ b/src/cpu.js @@ -369,7 +369,7 @@ CPU.prototype.jit_force_generate = function(addr) CPU.prototype.jit_clear_func = function(index) { dbg_assert(index >= 0 && index < WASM_TABLE_SIZE); - this.wm.imports.env[WASM_EXPORT_TABLE_NAME + 0x100].set(index, null); + this.wm.imports.env[WASM_EXPORT_TABLE_NAME].set(index + WASM_TABLE_OFFSET, null); }; CPU.prototype.get_state = function() @@ -1318,7 +1318,7 @@ CPU.prototype.codegen_finalize = function(wasm_table_index, start, end, first_op first_opcode, state_flags); // The following will throw if f isn't an exported function - this.wm.imports["env"][WASM_EXPORT_TABLE_NAME].set(wasm_table_index + 0x100, f); + this.wm.imports["env"][WASM_EXPORT_TABLE_NAME].set(wasm_table_index + WASM_TABLE_OFFSET, f); if(this.test_hook_did_finalize_wasm) { @@ -1498,11 +1498,10 @@ CPU.prototype.jit_clear_cache = function() this.jit_empty_cache(); const table = this.wm.exports[WASM_EXPORT_TABLE_NAME] || this.wm.imports["env"][WASM_EXPORT_TABLE_NAME]; - const offset = 0x100; for(let i = 0; i < WASM_TABLE_SIZE; i++) { - table.set(offset + i, null); + table.set(WASM_TABLE_OFFSET + i, null); } }; diff --git a/src/rust/cpu2/cpu.rs b/src/rust/cpu2/cpu.rs index 78016b67..b490445b 100644 --- a/src/rust/cpu2/cpu.rs +++ b/src/rust/cpu2/cpu.rs @@ -33,6 +33,10 @@ use cpu2::modrm::{resolve_modrm16, resolve_modrm32}; use cpu2::profiler::*; use cpu2::profiler::{profiler_stat_increment, profiler_stat_increment_by}; +/// The offset for our generated functions in the wasm table. Every index less than this is +/// reserved for rustc's indirect functions +pub const WASM_TABLE_OFFSET: u32 = 1024; + #[derive(Copy, Clone)] #[repr(C)] pub union reg64 { @@ -777,7 +781,7 @@ pub unsafe fn cycle_internal() -> () { wasm_table_index = (entry & 65535i32 as u32) as u16; initial_state = (entry >> 16i32) as u16; call_indirect1( - (wasm_table_index as u32).wrapping_add(256i32 as u32) as i32, + (wasm_table_index as u32).wrapping_add(WASM_TABLE_OFFSET as u32) as i32, initial_state, ); profiler_stat_increment_by(