2022-12-30 22:16:07 -08:00
|
|
|
require 'ruby_vm/mjit/assembler'
|
2023-01-02 14:11:06 -08:00
|
|
|
require 'ruby_vm/mjit/block'
|
2023-01-07 13:21:14 -08:00
|
|
|
require 'ruby_vm/mjit/branch_stub'
|
2022-12-30 21:34:36 -08:00
|
|
|
require 'ruby_vm/mjit/code_block'
|
2022-12-27 23:04:53 -08:00
|
|
|
require 'ruby_vm/mjit/context'
|
2022-12-30 21:27:12 -08:00
|
|
|
require 'ruby_vm/mjit/exit_compiler'
|
2022-12-27 23:04:53 -08:00
|
|
|
require 'ruby_vm/mjit/insn_compiler'
|
|
|
|
require 'ruby_vm/mjit/instruction'
|
2023-01-02 14:11:06 -08:00
|
|
|
require 'ruby_vm/mjit/invariants'
|
2022-12-27 23:04:53 -08:00
|
|
|
require 'ruby_vm/mjit/jit_state'
|
2022-12-11 21:42:25 -08:00
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
module RubyVM::MJIT
|
|
|
|
# Compilation status
|
2022-12-23 10:43:18 -08:00
|
|
|
KeepCompiling = :KeepCompiling
|
|
|
|
CantCompile = :CantCompile
|
|
|
|
EndBlock = :EndBlock
|
|
|
|
|
|
|
|
# Ruby constants
|
2023-01-02 22:53:14 -08:00
|
|
|
Qtrue = Fiddle::Qtrue
|
|
|
|
Qfalse = Fiddle::Qfalse
|
2022-12-23 10:43:18 -08:00
|
|
|
Qnil = Fiddle::Qnil
|
|
|
|
Qundef = Fiddle::Qundef
|
2022-12-11 21:42:25 -08:00
|
|
|
|
2022-12-31 14:14:53 -08:00
|
|
|
# Callee-saved registers
|
|
|
|
# TODO: support using r12/r13 here
|
|
|
|
EC = :r14
|
|
|
|
CFP = :r15
|
2022-12-23 16:23:21 -08:00
|
|
|
SP = :rbx
|
|
|
|
|
2023-01-02 22:53:14 -08:00
|
|
|
# Scratch registers: rax, rcx
|
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
class Compiler
|
|
|
|
attr_accessor :write_pos
|
2022-09-04 21:53:46 -07:00
|
|
|
|
2022-12-26 22:46:40 -08:00
|
|
|
def self.decode_insn(encoded)
|
|
|
|
INSNS.fetch(C.rb_vm_insn_decode(encoded))
|
|
|
|
end
|
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
# @param mem_block [Integer] JIT buffer address
|
2022-12-30 21:34:36 -08:00
|
|
|
# @param mem_size [Integer] JIT buffer size
|
|
|
|
def initialize(mem_block, mem_size)
|
2022-12-30 22:16:07 -08:00
|
|
|
@cb = CodeBlock.new(mem_block: mem_block, mem_size: mem_size / 2)
|
2022-12-30 23:42:00 -08:00
|
|
|
@ocb = CodeBlock.new(mem_block: mem_block + mem_size / 2, mem_size: mem_size / 2, outlined: true)
|
2022-12-30 21:27:12 -08:00
|
|
|
@exit_compiler = ExitCompiler.new
|
2023-02-03 22:42:13 -08:00
|
|
|
@insn_compiler = InsnCompiler.new(@cb, @ocb, @exit_compiler)
|
2023-01-07 21:24:30 -08:00
|
|
|
|
|
|
|
@leave_exit = Assembler.new.then do |asm|
|
|
|
|
@exit_compiler.compile_leave_exit(asm)
|
|
|
|
@ocb.write(asm)
|
|
|
|
end
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
2022-11-28 21:33:55 -08:00
|
|
|
|
2022-12-31 13:41:32 -08:00
|
|
|
# Compile an ISEQ from its entry point.
|
|
|
|
# @param iseq `RubyVM::MJIT::CPointer::Struct_rb_iseq_t`
|
|
|
|
# @param cfp `RubyVM::MJIT::CPointer::Struct_rb_control_frame_t`
|
|
|
|
def compile(iseq, cfp)
|
2022-12-23 17:39:34 -08:00
|
|
|
# TODO: Support has_opt
|
|
|
|
return if iseq.body.param.flags.has_opt
|
|
|
|
|
2023-02-03 22:42:13 -08:00
|
|
|
jit = JITState.new(iseq:, cfp:)
|
2022-12-30 22:16:07 -08:00
|
|
|
asm = Assembler.new
|
2022-12-31 13:41:32 -08:00
|
|
|
asm.comment("Block: #{iseq.body.location.label}@#{C.rb_iseq_path(iseq)}:#{iseq.body.location.first_lineno}")
|
2022-12-20 21:54:54 -08:00
|
|
|
compile_prologue(asm)
|
2023-02-03 22:42:13 -08:00
|
|
|
compile_block(asm, jit:)
|
2023-02-07 00:17:13 -08:00
|
|
|
iseq.body.jit_func = @cb.write(asm)
|
2022-12-17 13:39:35 -08:00
|
|
|
rescue Exception => e
|
2022-12-18 23:45:17 -08:00
|
|
|
$stderr.puts e.full_message # TODO: check verbose
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
2022-12-11 21:42:25 -08:00
|
|
|
|
2023-01-07 13:21:14 -08:00
|
|
|
# Compile a branch stub.
|
|
|
|
# @param branch_stub [RubyVM::MJIT::BranchStub]
|
|
|
|
# @param cfp `RubyVM::MJIT::CPointer::Struct_rb_control_frame_t`
|
2023-02-07 00:17:13 -08:00
|
|
|
# @param target0_p [TrueClass,FalseClass]
|
2023-01-07 13:21:14 -08:00
|
|
|
# @return [Integer] The starting address of the compiled branch stub
|
2023-02-07 00:17:13 -08:00
|
|
|
def branch_stub_hit(branch_stub, cfp, target0_p)
|
2023-01-07 13:21:14 -08:00
|
|
|
# Update cfp->pc for `jit.at_current_insn?`
|
2023-02-07 00:17:13 -08:00
|
|
|
target = target0_p ? branch_stub.target0 : branch_stub.target1
|
|
|
|
cfp.pc = target.pc
|
2023-01-07 13:21:14 -08:00
|
|
|
|
|
|
|
# Prepare the jump target
|
|
|
|
new_asm = Assembler.new.tap do |asm|
|
|
|
|
jit = JITState.new(iseq: branch_stub.iseq, cfp:)
|
2023-02-07 00:17:13 -08:00
|
|
|
compile_block(asm, jit:, pc: target.pc, ctx: target.ctx.dup)
|
2023-01-07 13:21:14 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Rewrite the branch stub
|
|
|
|
if @cb.write_addr == branch_stub.end_addr
|
2023-02-07 00:17:13 -08:00
|
|
|
# If the branch stub's jump is the last code, allow overwriting part of
|
|
|
|
# the old branch code with the new block code.
|
2023-01-07 13:21:14 -08:00
|
|
|
@cb.set_write_addr(branch_stub.start_addr)
|
2023-02-07 00:17:13 -08:00
|
|
|
branch_stub.shape = target0_p ? Next0 : Next1
|
2023-01-07 13:21:14 -08:00
|
|
|
Assembler.new.tap do |branch_asm|
|
2023-02-07 00:17:13 -08:00
|
|
|
branch_stub.compile.call(branch_asm)
|
2023-01-07 13:21:14 -08:00
|
|
|
@cb.write(branch_asm)
|
|
|
|
end
|
|
|
|
|
2023-02-07 00:17:13 -08:00
|
|
|
# Compile a fallthrough right after the new branch code
|
|
|
|
if target0_p
|
|
|
|
branch_stub.target0.address = @cb.write(new_asm)
|
2023-01-07 13:21:14 -08:00
|
|
|
else
|
2023-02-07 00:17:13 -08:00
|
|
|
branch_stub.target1.address = @cb.write(new_asm)
|
2023-01-07 13:21:14 -08:00
|
|
|
end
|
|
|
|
else
|
2023-02-07 00:17:13 -08:00
|
|
|
# Otherwise, just prepare the new block somewhere
|
|
|
|
if target0_p
|
|
|
|
branch_stub.target0.address = @cb.write(new_asm)
|
2023-01-07 13:21:14 -08:00
|
|
|
else
|
2023-02-07 00:17:13 -08:00
|
|
|
branch_stub.target1.address = @cb.write(new_asm)
|
2023-01-07 13:21:14 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Update jump destinations
|
|
|
|
@cb.with_write_addr(branch_stub.start_addr) do
|
2023-02-07 00:17:13 -08:00
|
|
|
branch_asm = Assembler.new
|
|
|
|
branch_stub.compile.call(branch_asm)
|
2023-01-07 13:21:14 -08:00
|
|
|
@cb.write(branch_asm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-07 00:17:13 -08:00
|
|
|
if target0_p
|
|
|
|
branch_stub.target0.address
|
2023-01-07 13:21:14 -08:00
|
|
|
else
|
2023-02-07 00:17:13 -08:00
|
|
|
branch_stub.target1.address
|
2023-01-07 13:21:14 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
private
|
2022-12-15 22:20:43 -08:00
|
|
|
|
2022-12-23 14:46:39 -08:00
|
|
|
# Callee-saved: rbx, rsp, rbp, r12, r13, r14, r15
|
|
|
|
# Caller-saved: rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11
|
|
|
|
#
|
2022-12-30 22:16:07 -08:00
|
|
|
# @param asm [RubyVM::MJIT::Assembler]
|
2022-12-20 21:54:54 -08:00
|
|
|
def compile_prologue(asm)
|
2023-01-07 14:26:38 -08:00
|
|
|
asm.comment('MJIT entry point')
|
2022-12-26 22:06:43 -08:00
|
|
|
|
2022-12-23 14:46:39 -08:00
|
|
|
# Save callee-saved registers used by JITed code
|
2022-12-31 14:14:53 -08:00
|
|
|
asm.push(CFP)
|
|
|
|
asm.push(EC)
|
2022-12-23 16:23:21 -08:00
|
|
|
asm.push(SP)
|
2022-12-23 14:46:39 -08:00
|
|
|
|
2022-12-31 14:14:53 -08:00
|
|
|
# Move arguments EC and CFP to dedicated registers
|
|
|
|
asm.mov(EC, :rdi)
|
|
|
|
asm.mov(CFP, :rsi)
|
|
|
|
|
|
|
|
# Load sp to a dedicated register
|
2022-12-23 16:23:21 -08:00
|
|
|
asm.mov(SP, [CFP, C.rb_control_frame_t.offsetof(:sp)]) # rbx = cfp->sp
|
2023-01-07 21:24:30 -08:00
|
|
|
|
|
|
|
# Setup cfp->jit_return
|
|
|
|
asm.mov(:rax, @leave_exit)
|
|
|
|
asm.mov([CFP, C.rb_control_frame_t.offsetof(:jit_return)], :rax)
|
2022-12-18 23:45:17 -08:00
|
|
|
end
|
|
|
|
|
2022-12-30 22:16:07 -08:00
|
|
|
# @param asm [RubyVM::MJIT::Assembler]
|
2023-01-02 14:11:06 -08:00
|
|
|
def compile_block(asm, jit:, pc: jit.iseq.body.iseq_encoded.to_i, ctx: Context.new)
|
|
|
|
# Mark the block start address and prepare an exit code storage
|
|
|
|
jit.block = Block.new(pc:)
|
|
|
|
asm.block(jit.block)
|
|
|
|
|
|
|
|
# Compile each insn
|
2022-12-31 13:41:32 -08:00
|
|
|
iseq = jit.iseq
|
2023-01-02 14:11:06 -08:00
|
|
|
index = (pc - iseq.body.iseq_encoded.to_i) / C.VALUE.size
|
2022-12-17 13:39:35 -08:00
|
|
|
while index < iseq.body.iseq_size
|
2022-12-26 22:46:40 -08:00
|
|
|
insn = self.class.decode_insn(iseq.body.iseq_encoded[index])
|
2022-12-26 14:09:45 -08:00
|
|
|
jit.pc = (iseq.body.iseq_encoded + index).to_i
|
|
|
|
|
2023-01-07 13:21:14 -08:00
|
|
|
case status = @insn_compiler.compile(jit, ctx, asm, insn)
|
|
|
|
when KeepCompiling
|
|
|
|
index += insn.len
|
2022-12-23 10:43:18 -08:00
|
|
|
when EndBlock
|
2023-01-02 14:11:06 -08:00
|
|
|
# TODO: pad nops if entry exit exists
|
2022-12-23 10:43:18 -08:00
|
|
|
break
|
|
|
|
when CantCompile
|
2023-01-02 14:11:06 -08:00
|
|
|
@exit_compiler.compile_side_exit(jit, ctx, asm)
|
2022-12-17 13:39:35 -08:00
|
|
|
break
|
2023-01-07 13:21:14 -08:00
|
|
|
else
|
|
|
|
raise "compiling #{insn.name} returned unexpected status: #{status.inspect}"
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
|
|
|
end
|
2023-02-07 00:00:09 -08:00
|
|
|
|
|
|
|
incr_counter(:compiled_block_count)
|
|
|
|
end
|
|
|
|
|
|
|
|
def incr_counter(name)
|
|
|
|
if C.mjit_opts.stats
|
|
|
|
C.rb_mjit_counters[name][0] += 1
|
|
|
|
end
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
2022-12-15 22:20:43 -08:00
|
|
|
end
|
2022-09-04 21:53:46 -07:00
|
|
|
end
|