Commit graph

182 commits

Author SHA1 Message Date
Fabian 4e9fa5e783 refactor 2020-12-31 19:14:29 -06:00
Fabian c47b4b252c Optimise cr0 access in task_switch_test (only low byte needed) 2020-12-31 19:14:29 -06:00
Fabian c207400922 Fix Rust warnings 2020-12-31 19:14:29 -06:00
Fabian c97600f5cb Also prepare gen_safe_read{64,128} for later optimisations 2020-12-31 19:14:29 -06:00
Fabian b23c4a9db2 Optimise pop reg 2020-12-31 19:14:29 -06:00
Fabian c5cd93678e Pass local to gen_safe_read, add single function for modrm_resolve and gen_safe_read (preparation for later optimisations) 2020-12-31 19:14:29 -06:00
Fabian f620aceb5d Use LocalOrImmediate for other arithmetic instructions 2020-12-31 19:14:28 -06:00
Fabian 0e8c8a1dda More opstats 2020-12-31 19:14:28 -06:00
Fabian 970739f60b Generate custom code for getzf/getcf/test_be 2020-12-31 19:14:28 -06:00
Fabian 701d59e0dd Custom codegen for absolute cli (FA) 2020-12-31 19:14:28 -06:00
Fabian b5a72061fb Custom codegen for absolute jmp/call (FF_2/FF_4) 2020-12-31 19:14:28 -06:00
Fabian 091b2324d9 Custom codegen for 8C 2020-12-31 19:14:28 -06:00
Fabian fdd1dc377d Custom codegen for xadd (0FC1) 2020-08-30 19:37:15 -05:00
Fabian 874818866a Codegen for mul32 + custom mul/imul (F7_[45]) 2020-08-30 19:37:15 -05:00
Fabian 2a3e4bfa86 Minor: Remove unused 2020-08-30 19:37:15 -05:00
Fabian b774db3f81 Fix: Clear prefixes when leaving instruction early 2020-08-30 19:37:15 -05:00
Fabian ac9ef7d447 gen_pop/gen_push: Avoid some unnecessary locals after registers-in-locals 2020-08-30 19:37:15 -05:00
Fabian 724090b319 Reduce code size by removing register restoring around safe_{read,write}*_jit 2020-08-30 19:37:15 -05:00
Fabian 723f78c14f Reduce code size by creating a block to jump to that handles the page fault case 2020-08-30 19:37:15 -05:00
Fabian a8308b988d Store registers in locals
This changes registers to be temporarily stored in wasm locals, across
each complete wasm module. Registers are moved from memory to locals
upon entering the wasm module and moved from locals to memory upon
leaving. Additionally, calls to functions that modify registers are
wrapped between moving registers to memory before and moving back to
locals after. This affects:

1. All non-custom instructions
2. safe_{read,write}_slow, since it may page fault (the slow path of all memory accesses)
3. task_switch_test* and trigger_ud
4. All block boundaries
5. The fallback functions of gen_safe_read_write (read-modify-write memory accesses)

The performance benefits are currently mostly eaten up by 1. and 4. (if
one calculates the total number of read/writes to registers in memory,
they are higher after this patch, as each instructions of typ 1. or 4.
requires moving all 8 register twice). This can be improved later by the
relatively mechanical work of making instructions custom (not
necessarily full code generation, only the part of the instruction where
registers are accessed). Multi-page wasm module generation will
significantly reduce the number of type 4. instructions.

Due to 2., the overall code size has significantly increased. This case
(the slow path of memory access) is often generated but rarely executed.
These moves can be removed in a later patch by a different scheme for
safe_{read,write}_slow, which has been left out of this patch for
simplicity of reviewing.

This also simplifies our code generation for storing registers, as

    instructions_body.const_i32(register_offset);
    // some computations ...
    instruction_body.store_i32();

turns into:

    // some computations ...
    write_register(register_index);

I.e., a prefix is not necessary anymore as locals are indexed directly.

Further patches will allow getting rid of some temporary locals, as
registers now can be used directly.
2020-08-30 19:37:15 -05:00
Fabian 56dc1af7cc Split SAFE_WRITE stat into WRITE and READ_WRITE 2020-08-30 19:37:15 -05:00
Fabian 32f988a08d Pass JitContext to gen_get_reg* (preparation for registers-in-locals) 2020-08-30 19:37:15 -05:00
Fabian 0c42ea0d1f Custom code generation for leave (C9) 2020-08-30 19:37:15 -05:00
Fabian 837e6ff362 Custom code generation for ret imm (C2) 2020-08-30 19:37:15 -05:00
Fabian aef22e38ad Add missing tracking of jit exit points 2020-08-30 19:37:15 -05:00
Fabian 69b834c8de Move condition function generation into codegen module and use it for cmovcc/setcc 2020-08-30 19:37:15 -05:00
Fabian 2837ccd06b Support for gen_safe_read128 and code generation for MOVDQU (F30F6F) 2020-08-30 19:37:15 -05:00
Fabian 440b67eda5 Support for gen_safe_write128 and code generation for MOVAPS/MOVDQA (0F29/660F7F) 2020-08-30 19:37:15 -05:00
Fabian e2ab5eabdd Code generation for missing memory operations (8-bit shifts, shrd, shld, xadd) 2020-08-30 19:37:15 -05:00
Fabian fdce557820 Codegen for fpu instructions (memory stores: fst/fstp/fist/fistp) (D9_[23], DB_[23], DD_[23], DF_[237]) 2020-08-30 19:37:15 -05:00
Fabian b11b2725bb Use i64 locals for gen_safe_write64 2020-08-30 19:37:15 -05:00
Fabian c452c357dd Codegen for fpu instructions (DE group) 2020-08-30 19:37:15 -05:00
Fabian 21caefbffd Codegen for fpu instructions (DC group) 2020-08-30 19:37:15 -05:00
Fabian f797ce10c9 64-bit operations for gen_safe_{read,write,read_write} 2020-08-30 19:37:15 -05:00
Fabian ec059a9f27 Codegen for fpu instructions (D8 group) 2020-08-30 19:37:15 -05:00
Fabian 48e6843a87 Remove S_ prefix from profiler 2020-08-30 19:29:54 -05:00
Fabian 7e1d398e05 Track last executed jump instruction, check for missed entry points while looking for compiled code 2020-08-30 19:29:54 -05:00
Fabian 7e3f1ad401 gen_fn: Accept wasm builder, not jit context 2020-08-30 19:29:54 -05:00
Fabian 2635ed71b4 jit memory access for 8-bit read-modify-write operations 2020-08-30 19:29:54 -05:00
Fabian 83febd1376 Remove duplicated constants (TLB_*) 2020-08-30 19:29:54 -05:00
Fabian bf895ff1b2 profiler: Keep track of fast/slow path for jitted memory access 2020-08-30 19:29:54 -05:00
Fabian b70a5c081a Allow fast path for memory reads from pages that contain code 2020-08-30 19:29:54 -05:00
Awal Garg 54151e2306 jit 0x0FBF 2020-08-30 19:29:54 -05:00
Fabian 295985e8e0 Remove code section: Only a single buffer is used for generating code 2020-08-30 19:29:54 -05:00
Fabian 41b60d278c Accept builder in gen_jmp_rel16, simplifying 2020-08-30 19:29:54 -05:00
Fabian fa958d95c3 Fix warnings: Remove unused stuff 2020-08-30 19:29:54 -05:00
Fabian fa50294b47 Generate code for read-modify-write instructions (C1/D1/D3: Shifts and rotates) 2020-08-30 19:29:54 -05:00
Fabian 3706bcac12 Use jit for read-modify-write arithmetic instructions 2020-08-30 19:29:54 -05:00
Fabian cfb9cd8abe Partial custom implementation for arithmethic instructions with read-memory 2020-08-30 19:29:54 -05:00
Fabian 1d24c5952d Cleanup of codegen api 2020-08-30 19:29:54 -05:00
Fabian ca36680d52 PAGE_FAULT is u8 2020-08-30 19:29:54 -05:00
Fabian b0eff6b951 Implement 8-bit memory accesses 2020-08-30 19:29:54 -05:00
Fabian d4d7d236d5 Make all instructions non-faulting; handle faulting case in gen_safe_{read,write} (#44) 2020-08-30 19:29:54 -05:00
Fabian 8051db8ac6 Access from allocated memory 2020-08-30 19:29:53 -05:00
Fabian 1a96072840 Use u8 for page_fault 2020-08-30 19:29:53 -05:00
Fabian a5cbf53da5 Fix jit in presence of new page fault handling
Makes the following a block boundary:

- push
- Any non-custom instruction that uses modrm encoding
- Any sse/fpu instruction

This commit affects performance negatively. In order to fix this, the
above instructions need to be implemented using custom code generators
for the memory access.
2020-08-30 19:29:53 -05:00
Fabian 1faf8ccc86 Allocate memory via Rust instead of ahead-of-time 2020-08-30 19:29:53 -05:00
Awal Garg 26fc44a61f simplify push16,32 2020-08-30 19:29:53 -05:00
Awal Garg bfc04b80fb jit E8 2020-08-30 19:29:53 -05:00
Awal Garg 4d7f87c515 avoid local in gen_safe_read_write 2020-08-30 19:29:53 -05:00
Awal Garg b6aa8efc37 merge push16_ss* and push32_ss* functions 2020-08-30 19:29:53 -05:00
Awal Garg 2f5b1311c6 re-add gen_safe_read_write 2020-08-30 19:29:53 -05:00
Awal Garg fb9a6e714a remove unused codegen functions (register related) 2020-08-30 19:29:53 -05:00
Awal Garg be7d21e79f add gen_push32_ss{16,32} 2020-08-30 19:29:53 -05:00
Awal Garg 5c2ab56b3b add gen_push16_ss{16,32}
This adds the ImmVal enum type too.
2020-08-30 19:29:53 -05:00
Awal Garg bad796b9eb deduplicate safe_{read,write}* functions 2020-08-30 19:29:53 -05:00
Awal Garg 22e570e1eb remove commented out code 2020-08-30 19:29:53 -05:00
Awal Garg 470246651d move {set,tee}_new_local to builder 2020-08-30 19:29:53 -05:00
Awal Garg 046e399b55 extend Vec<u8> with wasm_util functions 2020-08-30 19:29:53 -05:00
Awal Garg 9b0fdc5019 Improve locals handling
Issuing of locals should only happen with set_new_local and
tee_new_local now
2020-08-30 19:29:53 -05:00
Awal Garg 57d79c256d remove unused local 2020-08-30 19:29:53 -05:00
Awal Garg 07630b3947 fix comment 2020-08-30 19:29:53 -05:00
Awal Garg 5f22e393ff jit safe_read16 2020-08-30 19:29:53 -05:00
Awal Garg 6f45d5f6ed add gen_safe_write16, jit instr16_C7_0_mem 2020-08-30 19:29:53 -05:00
Awal Garg 7a14a010a8 Improve handling of locals and add tests
- adds alloc_local and free_local
- slightly better wasmgen tests
- caller of gen_safe_write32 is responsible for allocating
  and freeing locals for address and value
- updates expect-tests
2020-08-30 19:29:53 -05:00
Fabian bdef74eced Generate code for task_switch_test{,_mmx}, use non-raising exceptions 2020-08-30 19:29:53 -05:00
Awal Garg 56714600eb has_flat_segmentation optimization 2020-08-30 19:29:53 -05:00
Awal Garg 8303948954 use local for storing {e}sp 2020-08-30 19:29:53 -05:00
Awal Garg b3e415cf9f jit inline 0xC3 2020-08-30 19:29:53 -05:00
Awal Garg 9224fc3878 jit inline 0xEB 2020-08-30 19:29:53 -05:00
Fabian 8585af1f89 Use dbg_assert over assert 2020-08-30 19:29:13 -05:00
Fabian 3a8d644d75 Port jit to Rust
The following files and functions were ported:
- jit.c
- codegen.c
- _jit functions in instructions*.c and misc_instr.c
- generate_{analyzer,jit}.js (produces Rust code)
- jit_* from cpu.c

And the following data structures:
- hot_code_addresses
- wasm_table_index_free_list
- entry_points
- jit_cache_array
- page_first_jit_cache_entry

Other miscellaneous changes:
- Page is an abstract type
- Addresses, locals and bitflags are unsigned
- Make the number of entry points a growable type
- Avoid use of global state wherever possible
- Delete string packing
- Make CachedStateFlags abstract
- Make AnalysisType product type
- Make BasicBlockType product type
- Restore opcode assertion
- Set opt-level=2 in debug mode (for test performance)
- Delete JIT_ALWAYS instrumentation (now possible via api)
- Refactor generate_analyzer.js
- Refactor generate_jit.js
2020-08-30 19:29:13 -05:00