2022-12-23 14:02:23 -08:00
|
|
|
require 'mjit/codegen'
|
2022-12-23 14:17:32 -08:00
|
|
|
require 'mjit/context'
|
2022-12-17 13:39:35 -08:00
|
|
|
require 'mjit/instruction'
|
|
|
|
require 'mjit/x86_assembler'
|
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
|
|
|
|
Qnil = Fiddle::Qnil
|
|
|
|
Qundef = Fiddle::Qundef
|
2022-12-11 21:42:25 -08:00
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
class Compiler
|
|
|
|
attr_accessor :write_pos
|
2022-09-04 21:53:46 -07:00
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
# @param mem_block [Integer] JIT buffer address
|
|
|
|
def initialize(mem_block)
|
|
|
|
@mem_block = mem_block
|
|
|
|
@write_pos = 0
|
2022-12-23 14:02:23 -08:00
|
|
|
@codegen = Codegen.new
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
2022-11-28 21:33:55 -08:00
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
# @param iseq [RubyVM::MJIT::CPointer::Struct]
|
2022-12-17 22:00:16 -08:00
|
|
|
def call(iseq)
|
2022-12-17 13:39:35 -08:00
|
|
|
return if iseq.body.location.label == '<main>'
|
2022-12-20 21:54:54 -08:00
|
|
|
|
|
|
|
asm = X86Assembler.new
|
|
|
|
compile_prologue(asm)
|
|
|
|
compile_block(asm, iseq)
|
|
|
|
iseq.body.jit_func = compile(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
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
def write_addr
|
|
|
|
@mem_block + @write_pos
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2022-12-15 22:20:43 -08:00
|
|
|
|
2022-12-23 14:17:32 -08:00
|
|
|
# @param asm [RubyVM::MJIT::X86Assembler]
|
2022-12-23 10:43:18 -08:00
|
|
|
def compile(asm)
|
|
|
|
start_addr = write_addr
|
|
|
|
|
|
|
|
C.mjit_mark_writable
|
|
|
|
@write_pos += asm.compile(start_addr)
|
|
|
|
C.mjit_mark_executable
|
|
|
|
|
|
|
|
end_addr = write_addr
|
|
|
|
if C.mjit_opts.dump_disasm && start_addr < end_addr
|
|
|
|
dump_disasm(start_addr, end_addr)
|
|
|
|
end
|
|
|
|
start_addr
|
|
|
|
end
|
|
|
|
|
2022-12-20 21:54:54 -08:00
|
|
|
# ec: rdi
|
|
|
|
# cfp: rsi
|
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-23 14:17:32 -08:00
|
|
|
# @param asm [RubyVM::MJIT::X86Assembler]
|
2022-12-20 21:54:54 -08:00
|
|
|
def compile_prologue(asm)
|
2022-12-23 14:46:39 -08:00
|
|
|
# Save callee-saved registers used by JITed code
|
|
|
|
asm.push(:rbx)
|
|
|
|
|
|
|
|
# Load sp to a register
|
2022-12-20 21:54:54 -08:00
|
|
|
asm.mov(:rbx, [:rsi, C.rb_control_frame_t.offsetof(:sp)]) # rbx = cfp->sp
|
2022-12-18 23:45:17 -08:00
|
|
|
end
|
|
|
|
|
2022-12-23 14:17:32 -08:00
|
|
|
# @param asm [RubyVM::MJIT::X86Assembler]
|
2022-12-20 21:54:54 -08:00
|
|
|
def compile_block(asm, iseq)
|
2022-12-23 14:17:32 -08:00
|
|
|
ctx = Context.new
|
2022-12-17 13:39:35 -08:00
|
|
|
index = 0
|
|
|
|
while index < iseq.body.iseq_size
|
|
|
|
insn = decode_insn(iseq.body.iseq_encoded[index])
|
2022-12-23 14:17:32 -08:00
|
|
|
case compile_insn(ctx, asm, insn)
|
2022-12-23 10:43:18 -08:00
|
|
|
when EndBlock
|
|
|
|
break
|
|
|
|
when CantCompile
|
2022-12-23 14:17:32 -08:00
|
|
|
compile_exit(ctx, asm, (iseq.body.iseq_encoded + index).to_i)
|
2022-12-17 13:39:35 -08:00
|
|
|
break
|
|
|
|
end
|
|
|
|
index += insn.len
|
|
|
|
end
|
|
|
|
end
|
2022-12-15 22:20:43 -08:00
|
|
|
|
2022-12-23 14:17:32 -08:00
|
|
|
# @param ctx [RubyVM::MJIT::Context]
|
|
|
|
# @param asm [RubyVM::MJIT::X86Assembler]
|
|
|
|
def compile_insn(ctx, asm, insn)
|
2022-12-17 13:39:35 -08:00
|
|
|
case insn.name
|
2022-12-23 14:17:32 -08:00
|
|
|
when :putnil then @codegen.putnil(ctx, asm)
|
|
|
|
when :leave then @codegen.leave(ctx, asm)
|
2022-12-23 10:43:18 -08:00
|
|
|
else CantCompile
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
|
|
|
end
|
2022-12-15 22:20:43 -08:00
|
|
|
|
2022-12-23 14:17:32 -08:00
|
|
|
# @param ctx [RubyVM::MJIT::Context]
|
|
|
|
# @param asm [RubyVM::MJIT::X86Assembler]
|
|
|
|
def compile_exit(ctx, asm, exit_pc)
|
2022-12-23 10:43:18 -08:00
|
|
|
# update pc
|
|
|
|
asm.mov(:rax, exit_pc) # rax = exit_pc
|
|
|
|
asm.mov([:rsi, C.rb_control_frame_t.offsetof(:pc)], :rax) # cfp->pc = rax
|
2022-12-20 21:54:54 -08:00
|
|
|
|
2022-12-23 14:17:32 -08:00
|
|
|
# update sp
|
|
|
|
if ctx.stack_size > 0
|
|
|
|
asm.add(:rbx, C.VALUE.size * ctx.stack_size) # rbx += stack_size
|
|
|
|
asm.mov([:rsi, C.rb_control_frame_t.offsetof(:sp)], :rbx) # cfp->sp = rbx
|
|
|
|
end
|
2022-12-20 21:54:54 -08:00
|
|
|
|
2022-12-23 14:46:39 -08:00
|
|
|
# Restore callee-saved registers
|
|
|
|
asm.pop(:rbx)
|
|
|
|
|
2022-12-23 10:43:18 -08:00
|
|
|
asm.mov(:rax, Qundef)
|
|
|
|
asm.ret
|
2022-12-20 21:54:54 -08:00
|
|
|
end
|
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
def decode_insn(encoded)
|
|
|
|
INSNS.fetch(C.rb_vm_insn_decode(encoded))
|
|
|
|
end
|
2022-12-17 22:00:16 -08:00
|
|
|
|
|
|
|
def dump_disasm(from, to)
|
|
|
|
C.dump_disasm(from, to).each do |address, mnemonic, op_str|
|
|
|
|
puts " 0x#{"%p" % address}: #{mnemonic} #{op_str}"
|
|
|
|
end
|
2022-12-23 14:46:39 -08:00
|
|
|
puts
|
2022-12-17 22:00:16 -08:00
|
|
|
end
|
2022-12-15 22:20:43 -08:00
|
|
|
end
|
2022-09-04 21:53:46 -07:00
|
|
|
end
|