diff --git a/src/rust/codegen.rs b/src/rust/codegen.rs index 33d913b1..5a26edc1 100644 --- a/src/rust/codegen.rs +++ b/src/rust/codegen.rs @@ -1560,7 +1560,7 @@ pub enum ConditionNegate { } pub fn gen_getzf(ctx: &mut JitContext, negate: ConditionNegate) { - match &ctx.last_instruction { + match &ctx.previous_instruction { Instruction::Cmp { .. } | Instruction::Sub { .. } => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); // TODO: Could use local for cmp x, 0; sub x, y @@ -1624,7 +1624,7 @@ pub fn gen_getzf(ctx: &mut JitContext, negate: ConditionNegate) { } pub fn gen_getcf(ctx: &mut JitContext, negate: ConditionNegate) { - match &ctx.last_instruction { + match &ctx.previous_instruction { Instruction::Cmp { dest, source, @@ -1708,7 +1708,7 @@ pub fn gen_getcf_unoptimised(builder: &mut WasmBuilder) { } pub fn gen_getsf(ctx: &mut JitContext) { - match &ctx.last_instruction { + match &ctx.previous_instruction { &Instruction::Cmp { opsize, .. } | &Instruction::Sub { opsize, .. } | &Instruction::Arithmetic { opsize } => { @@ -1798,7 +1798,7 @@ pub fn gen_getof(builder: &mut WasmBuilder) { } pub fn gen_test_be(ctx: &mut JitContext, negate: ConditionNegate) { - match &ctx.last_instruction { + match &ctx.previous_instruction { Instruction::Cmp { dest, source, @@ -1886,7 +1886,7 @@ pub fn gen_test_be(ctx: &mut JitContext, negate: ConditionNegate) { } pub fn gen_test_l(ctx: &mut JitContext, negate: ConditionNegate) { - match &ctx.last_instruction { + match &ctx.previous_instruction { Instruction::Cmp { dest, source, @@ -1962,7 +1962,7 @@ pub fn gen_test_l(ctx: &mut JitContext, negate: ConditionNegate) { } pub fn gen_test_le(ctx: &mut JitContext, negate: ConditionNegate) { - match &ctx.last_instruction { + match &ctx.previous_instruction { Instruction::Cmp { dest, source, diff --git a/src/rust/jit.rs b/src/rust/jit.rs index bf534666..8668bad7 100644 --- a/src/rust/jit.rs +++ b/src/rust/jit.rs @@ -232,7 +232,8 @@ pub struct JitContext<'a> { pub start_of_current_instruction: u32, pub exit_with_fault_label: Label, pub exit_label: Label, - pub last_instruction: Instruction, + pub current_instruction: Instruction, + pub previous_instruction: Instruction, pub instruction_counter: WasmLocal, } impl<'a> JitContext<'a> { @@ -1029,7 +1030,8 @@ fn jit_generate_module( start_of_current_instruction: 0, exit_with_fault_label, exit_label, - last_instruction: Instruction::Other, + current_instruction: Instruction::Other, + previous_instruction: Instruction::Other, instruction_counter, }; @@ -1853,7 +1855,8 @@ fn jit_generate_basic_block(ctx: &mut JitContext, block: &BasicBlock) { ctx.builder.set_local(&ctx.instruction_counter); ctx.cpu.eip = start_addr; - ctx.last_instruction = Instruction::Other; + ctx.current_instruction = Instruction::Other; + ctx.previous_instruction = Instruction::Other; loop { let mut instruction = 0; @@ -1875,9 +1878,6 @@ fn jit_generate_basic_block(ctx: &mut JitContext, block: &BasicBlock) { codegen::gen_set_eip_low_bits(ctx.builder, stop_addr as i32 & 0xFFF); } } - else { - ctx.last_instruction = Instruction::Other; - } let wasm_length_before = ctx.builder.instruction_body_length(); @@ -1916,6 +1916,8 @@ fn jit_generate_basic_block(ctx: &mut JitContext, block: &BasicBlock) { dbg_assert!(false); break; } + + ctx.previous_instruction = mem::replace(&mut ctx.current_instruction, Instruction::Other); } } diff --git a/src/rust/jit_instructions.rs b/src/rust/jit_instructions.rs index 2455aedc..f732f015 100644 --- a/src/rust/jit_instructions.rs +++ b/src/rust/jit_instructions.rs @@ -911,7 +911,7 @@ macro_rules! define_instruction_read_write_mem32( ); fn gen_add8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; + ctx.current_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; ctx.builder.const_i32(global_pointers::last_op1 as i32); ctx.builder.get_local(dest_operand); @@ -934,7 +934,7 @@ fn gen_add8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc .load_fixed_u8(global_pointers::last_result as u32); } fn gen_add32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Arithmetic { opsize: OPSIZE_32 }; + ctx.current_instruction = Instruction::Arithmetic { opsize: OPSIZE_32 }; codegen::gen_set_last_op1(ctx.builder, &dest_operand); @@ -949,7 +949,7 @@ fn gen_add32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Lo } fn gen_sub8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; + ctx.current_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; ctx.builder.const_i32(global_pointers::last_op1 as i32); ctx.builder.get_local(dest_operand); @@ -972,7 +972,7 @@ fn gen_sub8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc .load_fixed_u8(global_pointers::last_result as u32); } fn gen_sub32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Sub { opsize: OPSIZE_32 }; + ctx.current_instruction = Instruction::Sub { opsize: OPSIZE_32 }; codegen::gen_set_last_op1(ctx.builder, &dest_operand); @@ -992,7 +992,7 @@ fn gen_cmp( source_operand: &LocalOrImmediate, size: i32, ) { - ctx.last_instruction = Instruction::Cmp { + ctx.current_instruction = Instruction::Cmp { dest: if ctx.register_locals.iter().any(|l| l == dest_operand) { InstructionOperand::WasmLocal(dest_operand.unsafe_clone()) } @@ -1205,7 +1205,7 @@ fn gen_sbb32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Lo } fn gen_and8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; + ctx.current_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; ctx.builder.const_i32(global_pointers::last_result as i32); ctx.builder.get_local(dest_operand); @@ -1224,7 +1224,7 @@ fn gen_and8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc .load_fixed_u8(global_pointers::last_result as u32); } fn gen_and32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Arithmetic { opsize: OPSIZE_32 }; + ctx.current_instruction = Instruction::Arithmetic { opsize: OPSIZE_32 }; ctx.builder.get_local(&dest_operand); source_operand.gen_get(ctx.builder); @@ -1246,7 +1246,7 @@ fn gen_test( source_operand: &LocalOrImmediate, size: i32, ) { - ctx.last_instruction = Instruction::Arithmetic { opsize: size }; + ctx.current_instruction = Instruction::Arithmetic { opsize: size }; ctx.builder.const_i32(global_pointers::last_result as i32); if source_operand.eq_local(dest_operand) { @@ -1277,7 +1277,7 @@ fn gen_test32(ctx: &mut JitContext, dest: &WasmLocal, source: &LocalOrImmediate) } fn gen_or8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; + ctx.current_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; ctx.builder.const_i32(global_pointers::last_result as i32); ctx.builder.get_local(dest_operand); @@ -1296,7 +1296,7 @@ fn gen_or8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loca .load_fixed_u8(global_pointers::last_result as u32); } fn gen_or32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Arithmetic { opsize: OPSIZE_32 }; + ctx.current_instruction = Instruction::Arithmetic { opsize: OPSIZE_32 }; ctx.builder.get_local(&dest_operand); source_operand.gen_get(ctx.builder); @@ -1313,7 +1313,7 @@ fn gen_or32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc } fn gen_xor8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; + ctx.current_instruction = Instruction::Arithmetic { opsize: OPSIZE_8 }; ctx.builder.const_i32(global_pointers::last_result as i32); ctx.builder.get_local(dest_operand); @@ -1332,7 +1332,7 @@ fn gen_xor8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc .load_fixed_u8(global_pointers::last_result as u32); } fn gen_xor32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.last_instruction = Instruction::Arithmetic { opsize: OPSIZE_32 }; + ctx.current_instruction = Instruction::Arithmetic { opsize: OPSIZE_32 }; if source_operand.eq_local(dest_operand) { ctx.builder.const_i32(0); @@ -2192,7 +2192,7 @@ pub fn instr32_3D_jit(ctx: &mut JitContext, imm32: u32) { } fn gen_inc(ctx: &mut JitContext, dest_operand: &WasmLocal, size: i32) { - ctx.last_instruction = Instruction::Arithmetic { opsize: size }; + ctx.current_instruction = Instruction::Arithmetic { opsize: size }; let builder = &mut ctx.builder; builder.const_i32(global_pointers::flags as i32); @@ -2239,7 +2239,7 @@ fn gen_inc32(ctx: &mut JitContext, dest_operand: &WasmLocal) { } fn gen_dec(ctx: &mut JitContext, dest_operand: &WasmLocal, size: i32) { - ctx.last_instruction = Instruction::Arithmetic { opsize: size }; + ctx.current_instruction = Instruction::Arithmetic { opsize: size }; let builder = &mut ctx.builder; builder.const_i32(global_pointers::flags as i32);