2022-12-11 21:42:25 -08:00
|
|
|
require 'mjit/x86_64/assembler'
|
|
|
|
|
2022-12-11 21:16:33 -08:00
|
|
|
class RubyVM::MJIT::Compiler
|
2022-12-11 21:42:25 -08:00
|
|
|
# MJIT internals
|
|
|
|
Assembler = RubyVM::MJIT::Assembler
|
|
|
|
C = RubyVM::MJIT::C
|
|
|
|
|
|
|
|
# Ruby constants
|
|
|
|
Qundef = Fiddle::Qundef
|
|
|
|
|
|
|
|
attr_accessor :write_pos
|
2022-09-04 21:53:46 -07:00
|
|
|
|
2022-12-11 21:32:24 -08:00
|
|
|
# @param mem_block [Integer] JIT buffer address
|
|
|
|
def initialize(mem_block)
|
|
|
|
@mem_block = mem_block
|
|
|
|
@write_pos = 0
|
|
|
|
end
|
2022-11-28 21:33:55 -08:00
|
|
|
|
|
|
|
# @param iseq [RubyVM::MJIT::CPointer::Struct]
|
2022-12-11 21:16:33 -08:00
|
|
|
def compile(iseq)
|
2022-12-11 21:42:25 -08:00
|
|
|
return if iseq.body.location.label == '<main>'
|
2022-12-15 22:20:43 -08:00
|
|
|
iseq.body.jit_func = compile_iseq(iseq)
|
2022-12-11 21:42:25 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def write_addr
|
|
|
|
@mem_block + @write_pos
|
2022-09-04 21:53:46 -07:00
|
|
|
end
|
2022-12-15 22:20:43 -08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# ec -> RDI, cfp -> RSI
|
|
|
|
def compile_iseq(iseq)
|
|
|
|
addr = write_addr
|
|
|
|
asm = Assembler.new
|
|
|
|
|
|
|
|
# pop the current frame (ec->cfp++)
|
|
|
|
asm.add(:rsi, C.rb_control_frame_t.size)
|
|
|
|
asm.mov([:rdi, C.rb_execution_context_t.offsetof(:cfp)], :rsi)
|
|
|
|
|
|
|
|
# return a value
|
|
|
|
asm.mov(:rax, 7)
|
|
|
|
asm.ret
|
|
|
|
|
|
|
|
asm.compile(self)
|
|
|
|
addr
|
|
|
|
end
|
2022-09-04 21:53:46 -07:00
|
|
|
end
|