2022-12-30 22:16:07 -08:00
|
|
|
require 'ruby_vm/mjit/assembler'
|
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'
|
|
|
|
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
|
|
|
|
Qnil = Fiddle::Qnil
|
|
|
|
Qundef = Fiddle::Qundef
|
2022-12-11 21:42:25 -08:00
|
|
|
|
2022-12-23 16:23:21 -08:00
|
|
|
# Fixed registers
|
|
|
|
EC = :rdi # TODO: change this
|
|
|
|
CFP = :rsi # TODO: change this
|
|
|
|
SP = :rbx
|
|
|
|
|
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
|
2022-12-30 22:16:07 -08:00
|
|
|
@insn_compiler = InsnCompiler.new(@ocb)
|
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-30 22:05:53 -08:00
|
|
|
def compile(iseq)
|
2022-12-23 17:39:34 -08:00
|
|
|
# TODO: Support has_opt
|
|
|
|
return if iseq.body.param.flags.has_opt
|
|
|
|
|
2022-12-30 22:16:07 -08:00
|
|
|
asm = Assembler.new
|
2022-12-26 22:46:40 -08:00
|
|
|
asm.comment("Block: #{iseq.body.location.label}@#{pathobj_path(iseq.body.location.pathobj)}:#{iseq.body.location.first_lineno}")
|
2022-12-20 21:54:54 -08:00
|
|
|
compile_prologue(asm)
|
|
|
|
compile_block(asm, iseq)
|
2022-12-30 22:05:53 -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
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
private
|
2022-12-15 22:20:43 -08:00
|
|
|
|
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-30 22:16:07 -08:00
|
|
|
# @param asm [RubyVM::MJIT::Assembler]
|
2022-12-20 21:54:54 -08:00
|
|
|
def compile_prologue(asm)
|
2022-12-26 22:06:43 -08:00
|
|
|
asm.comment("MJIT entry")
|
|
|
|
|
2022-12-23 14:46:39 -08:00
|
|
|
# Save callee-saved registers used by JITed code
|
2022-12-23 16:23:21 -08:00
|
|
|
asm.push(SP)
|
2022-12-23 14:46:39 -08:00
|
|
|
|
|
|
|
# Load sp to a register
|
2022-12-23 16:23:21 -08:00
|
|
|
asm.mov(SP, [CFP, C.rb_control_frame_t.offsetof(:sp)]) # rbx = cfp->sp
|
2022-12-18 23:45:17 -08:00
|
|
|
end
|
|
|
|
|
2022-12-30 22:16:07 -08:00
|
|
|
# @param asm [RubyVM::MJIT::Assembler]
|
2022-12-20 21:54:54 -08:00
|
|
|
def compile_block(asm, iseq)
|
2022-12-26 14:09:45 -08:00
|
|
|
jit = JITState.new
|
2022-12-23 14:17:32 -08:00
|
|
|
ctx = Context.new
|
2022-12-26 14:09:45 -08:00
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
index = 0
|
|
|
|
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
|
|
|
|
|
|
|
|
case compile_insn(jit, ctx, asm, insn)
|
2022-12-23 10:43:18 -08:00
|
|
|
when EndBlock
|
|
|
|
break
|
|
|
|
when CantCompile
|
2022-12-30 21:27:12 -08:00
|
|
|
@exit_compiler.compile_exit(jit, ctx, asm)
|
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-26 14:09:45 -08:00
|
|
|
# @param jit [RubyVM::MJIT::JITState]
|
2022-12-23 14:17:32 -08:00
|
|
|
# @param ctx [RubyVM::MJIT::Context]
|
2022-12-30 22:16:07 -08:00
|
|
|
# @param asm [RubyVM::MJIT::Assembler]
|
2022-12-26 14:09:45 -08:00
|
|
|
def compile_insn(jit, ctx, asm, insn)
|
2022-12-26 22:46:40 -08:00
|
|
|
asm.incr_counter(:mjit_insns_count)
|
2022-12-26 22:06:43 -08:00
|
|
|
asm.comment("Insn: #{insn.name}")
|
2022-12-26 22:46:40 -08:00
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
case insn.name
|
2022-12-28 14:43:04 -08:00
|
|
|
# nop
|
|
|
|
# getlocal
|
|
|
|
# setlocal
|
|
|
|
# getblockparam
|
|
|
|
# setblockparam
|
|
|
|
# getblockparamproxy
|
|
|
|
# getspecial
|
|
|
|
# setspecial
|
|
|
|
# getinstancevariable
|
|
|
|
# setinstancevariable
|
|
|
|
# getclassvariable
|
|
|
|
# setclassvariable
|
|
|
|
# opt_getconstant_path
|
|
|
|
# getconstant
|
|
|
|
# setconstant
|
|
|
|
# getglobal
|
|
|
|
# setglobal
|
2022-12-26 14:09:45 -08:00
|
|
|
when :putnil then @insn_compiler.putnil(jit, ctx, asm)
|
2022-12-28 14:43:04 -08:00
|
|
|
# putself
|
|
|
|
when :putobject then @insn_compiler.putobject(jit, ctx, asm)
|
|
|
|
# putspecialobject
|
|
|
|
# putstring
|
|
|
|
# concatstrings
|
|
|
|
# anytostring
|
|
|
|
# toregexp
|
|
|
|
# intern
|
|
|
|
# newarray
|
|
|
|
# newarraykwsplat
|
|
|
|
# duparray
|
|
|
|
# duphash
|
|
|
|
# expandarray
|
|
|
|
# concatarray
|
|
|
|
# splatarray
|
|
|
|
# newhash
|
|
|
|
# newrange
|
|
|
|
# pop
|
|
|
|
# dup
|
|
|
|
# dupn
|
|
|
|
# swap
|
|
|
|
# opt_reverse
|
|
|
|
# topn
|
|
|
|
# setn
|
|
|
|
# adjuststack
|
|
|
|
# defined
|
|
|
|
# checkmatch
|
|
|
|
# checkkeyword
|
|
|
|
# checktype
|
|
|
|
# defineclass
|
|
|
|
# definemethod
|
|
|
|
# definesmethod
|
|
|
|
# send
|
|
|
|
# opt_send_without_block
|
|
|
|
# objtostring
|
|
|
|
# opt_str_freeze
|
|
|
|
# opt_nil_p
|
|
|
|
# opt_str_uminus
|
|
|
|
# opt_newarray_max
|
|
|
|
# opt_newarray_min
|
|
|
|
# invokesuper
|
|
|
|
# invokeblock
|
2022-12-30 21:27:12 -08:00
|
|
|
when :leave then @insn_compiler.leave(jit, ctx, asm)
|
2022-12-28 13:50:24 -08:00
|
|
|
# throw
|
|
|
|
# jump
|
|
|
|
# branchif
|
|
|
|
# branchunless
|
|
|
|
# branchnil
|
|
|
|
# once
|
|
|
|
# opt_case_dispatch
|
|
|
|
# opt_plus
|
|
|
|
# opt_minus
|
|
|
|
# opt_mult
|
|
|
|
# opt_div
|
|
|
|
# opt_mod
|
|
|
|
# opt_eq
|
|
|
|
# opt_neq
|
|
|
|
# opt_lt
|
|
|
|
# opt_le
|
|
|
|
# opt_gt
|
|
|
|
# opt_ge
|
|
|
|
# opt_ltlt
|
|
|
|
# opt_and
|
|
|
|
# opt_or
|
|
|
|
# opt_aref
|
|
|
|
# opt_aset
|
|
|
|
# opt_aset_with
|
|
|
|
# opt_aref_with
|
|
|
|
# opt_length
|
|
|
|
# opt_size
|
|
|
|
# opt_empty_p
|
|
|
|
# opt_succ
|
|
|
|
# opt_not
|
|
|
|
# opt_regexpmatch2
|
|
|
|
# invokebuiltin
|
|
|
|
# opt_invokebuiltin_delegate
|
|
|
|
# opt_invokebuiltin_delegate_leave
|
|
|
|
when :getlocal_WC_0 then @insn_compiler.getlocal_WC_0(jit, 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-26 22:46:40 -08:00
|
|
|
# vm_core.h: pathobj_path
|
|
|
|
def pathobj_path(pathobj)
|
|
|
|
if pathobj.is_a?(String)
|
|
|
|
pathobj
|
|
|
|
else
|
|
|
|
pathobj.first
|
|
|
|
end
|
|
|
|
end
|
2022-12-15 22:20:43 -08:00
|
|
|
end
|
2022-09-04 21:53:46 -07:00
|
|
|
end
|