Refactor jit_return with BranchStub

This commit is contained in:
Takashi Kokubun 2023-02-07 11:36:45 -08:00
parent 6c1b1fa1f5
commit d88b59be92
2 changed files with 19 additions and 13 deletions

View file

@ -71,12 +71,11 @@ module RubyVM::MJIT
asm.jmp(:rax) asm.jmp(:rax)
end end
# @param jit [RubyVM::MJIT::JITState]
# @param ctx [RubyVM::MJIT::Context] # @param ctx [RubyVM::MJIT::Context]
# @param asm [RubyVM::MJIT::Assembler] # @param asm [RubyVM::MJIT::Assembler]
# @param branch_stub [RubyVM::MJIT::BranchStub] # @param branch_stub [RubyVM::MJIT::BranchStub]
# @param target0_p [TrueClass,FalseClass] # @param target0_p [TrueClass,FalseClass]
def compile_branch_stub(jit, ctx, asm, branch_stub, target0_p) def compile_branch_stub(ctx, asm, branch_stub, target0_p)
# Call rb_mjit_branch_stub_hit # Call rb_mjit_branch_stub_hit
asm.comment("branch stub hit: #{branch_stub.iseq.body.location.label}@#{C.rb_iseq_path(branch_stub.iseq)}:#{iseq_lineno(branch_stub.iseq, target0_p ? branch_stub.target0.pc : branch_stub.target1.pc)}") asm.comment("branch stub hit: #{branch_stub.iseq.body.location.label}@#{C.rb_iseq_path(branch_stub.iseq)}:#{iseq_lineno(branch_stub.iseq, target0_p ? branch_stub.target0.pc : branch_stub.target1.pc)}")
asm.mov(:rdi, to_value(branch_stub)) asm.mov(:rdi, to_value(branch_stub))

View file

@ -286,11 +286,11 @@ module RubyVM::MJIT
target1: BranchTarget.new(ctx:, pc: jit.pc + C.VALUE.size * jit.insn.len), # fallthrough target1: BranchTarget.new(ctx:, pc: jit.pc + C.VALUE.size * jit.insn.len), # fallthrough
) )
branch_stub.target0.address = Assembler.new.then do |ocb_asm| branch_stub.target0.address = Assembler.new.then do |ocb_asm|
@exit_compiler.compile_branch_stub(jit, ctx, ocb_asm, branch_stub, true) @exit_compiler.compile_branch_stub(ctx, ocb_asm, branch_stub, true)
@ocb.write(ocb_asm) @ocb.write(ocb_asm)
end end
branch_stub.target1.address = Assembler.new.then do |ocb_asm| branch_stub.target1.address = Assembler.new.then do |ocb_asm|
@exit_compiler.compile_branch_stub(jit, ctx, ocb_asm, branch_stub, false) @exit_compiler.compile_branch_stub(ctx, ocb_asm, branch_stub, false)
@ocb.write(ocb_asm) @ocb.write(ocb_asm)
end end
@ -707,19 +707,26 @@ module RubyVM::MJIT
return_ctx = ctx.dup return_ctx = ctx.dup
return_ctx.stack_size -= argc + ((flags & C.VM_CALL_ARGS_BLOCKARG == 0) ? 0 : 1) # Pop args return_ctx.stack_size -= argc + ((flags & C.VM_CALL_ARGS_BLOCKARG == 0) ? 0 : 1) # Pop args
return_ctx.sp_offset = 1 # SP is in the position after popping a receiver and arguments return_ctx.sp_offset = 1 # SP is in the position after popping a receiver and arguments
jit_return_stub = BlockStub.new(iseq: jit.iseq, pc: next_pc, ctx: return_ctx) branch_stub = BranchStub.new(
jit_return = Assembler.new.then do |ocb_asm| iseq: jit.iseq,
@exit_compiler.compile_block_stub(return_ctx, ocb_asm, jit_return_stub) shape: Default,
target0: BranchTarget.new(ctx: return_ctx, pc: next_pc),
)
branch_stub.target0.address = Assembler.new.then do |ocb_asm|
@exit_compiler.compile_branch_stub(return_ctx, ocb_asm, branch_stub, true)
@ocb.write(ocb_asm) @ocb.write(ocb_asm)
end end
jit_return_stub.change_block = proc do |jump_asm, new_addr| branch_stub.compile = proc do |branch_asm|
jump_asm.comment('set jit_return to callee CFP') branch_asm.comment('set jit_return to callee CFP')
jump_asm.stub(jit_return_stub) do branch_asm.stub(branch_stub) do
jump_asm.mov(:rax, new_addr) case branch_stub.shape
jump_asm.mov([CFP, C.rb_control_frame_t.offsetof(:jit_return)], :rax) in Default
branch_asm.mov(:rax, branch_stub.target0.address)
branch_asm.mov([CFP, C.rb_control_frame_t.offsetof(:jit_return)], :rax)
end
end end
end end
jit_return_stub.change_block.call(asm, jit_return) branch_stub.compile.call(asm)
asm.comment('set callee CFP to ec->cfp') asm.comment('set callee CFP to ec->cfp')
asm.mov([EC, C.rb_execution_context_t.offsetof(:cfp)], CFP) asm.mov([EC, C.rb_execution_context_t.offsetof(:cfp)], CFP)