Compile a real return value
This commit is contained in:
parent
3fa4d41460
commit
6fc336fedc
@ -5,8 +5,7 @@ class RubyVM::MJIT::Assembler
|
|||||||
@bytes = []
|
@bytes = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile(compiler)
|
def compile(compiler) = with_dump_disasm(compiler) do
|
||||||
with_dump_disasm(compiler) do
|
|
||||||
RubyVM::MJIT::C.mjit_mark_writable
|
RubyVM::MJIT::C.mjit_mark_writable
|
||||||
write_bytes(compiler.write_addr, @bytes)
|
write_bytes(compiler.write_addr, @bytes)
|
||||||
RubyVM::MJIT::C.mjit_mark_executable
|
RubyVM::MJIT::C.mjit_mark_executable
|
||||||
@ -14,13 +13,26 @@ class RubyVM::MJIT::Assembler
|
|||||||
compiler.write_pos += @bytes.size
|
compiler.write_pos += @bytes.size
|
||||||
@bytes.clear
|
@bytes.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add(_reg, imm)
|
||||||
|
# REX.W [83] RSI ib
|
||||||
|
@bytes.push(0x48, 0x83, 0xc6, imm)
|
||||||
end
|
end
|
||||||
|
|
||||||
def mov(_reg, val)
|
def mov(reg, val)
|
||||||
@bytes.push(0xb8, val, 0x00, 0x00, 0x00)
|
case reg
|
||||||
|
when :rax
|
||||||
|
# REX.W [C7] RAX imm32
|
||||||
|
@bytes.push(0x48, 0xc7, 0xc0, val, 0x00, 0x00, 0x00)
|
||||||
|
else
|
||||||
|
# REX.W [89] [rdi+val],rsi
|
||||||
|
@bytes.push(0x48, 0x89, 0x77, reg.last)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def ret
|
def ret
|
||||||
|
# Near return
|
||||||
|
# [C3]
|
||||||
@bytes.push(0xc3)
|
@bytes.push(0xc3)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,12 +47,15 @@ module RubyVM::MJIT # :nodoc: all
|
|||||||
type[@addr + offset / 8] = value
|
type[@addr + offset / 8] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param sizeof [Integer]
|
# @param size [Integer]
|
||||||
# @param members [Hash{ Symbol => [Integer, RubyVM::MJIT::CType::*] }]
|
# @param members [Hash{ Symbol => [Integer, RubyVM::MJIT::CType::*] }]
|
||||||
def self.define(sizeof, members)
|
def self.define(size, members)
|
||||||
Class.new(self) do
|
Class.new(self) do
|
||||||
# Return the size of this type
|
# Return the size of this type
|
||||||
define_singleton_method(:sizeof) { sizeof }
|
define_singleton_method(:size) { size }
|
||||||
|
|
||||||
|
# Return the offset to a field
|
||||||
|
define_singleton_method(:offsetof) { |field| members.fetch(field).last / 8 }
|
||||||
|
|
||||||
# Get the offset of a member named +name+
|
# Get the offset of a member named +name+
|
||||||
define_singleton_method(:offsetof) { |name|
|
define_singleton_method(:offsetof) { |name|
|
||||||
@ -62,9 +65,9 @@ module RubyVM::MJIT # :nodoc: all
|
|||||||
|
|
||||||
define_method(:initialize) do |addr = nil|
|
define_method(:initialize) do |addr = nil|
|
||||||
if addr.nil? # TODO: get rid of this feature later
|
if addr.nil? # TODO: get rid of this feature later
|
||||||
addr = Fiddle.malloc(sizeof)
|
addr = Fiddle.malloc(size)
|
||||||
end
|
end
|
||||||
super(addr, sizeof, members)
|
super(addr, size, members)
|
||||||
end
|
end
|
||||||
|
|
||||||
members.each do |member, (type, offset, to_ruby)|
|
members.each do |member, (type, offset, to_ruby)|
|
||||||
|
@ -19,15 +19,29 @@ class RubyVM::MJIT::Compiler
|
|||||||
# @param iseq [RubyVM::MJIT::CPointer::Struct]
|
# @param iseq [RubyVM::MJIT::CPointer::Struct]
|
||||||
def compile(iseq)
|
def compile(iseq)
|
||||||
return if iseq.body.location.label == '<main>'
|
return if iseq.body.location.label == '<main>'
|
||||||
|
iseq.body.jit_func = compile_iseq(iseq)
|
||||||
iseq.body.jit_func = write_addr
|
|
||||||
asm = Assembler.new
|
|
||||||
asm.mov(:eax, Qundef)
|
|
||||||
asm.ret
|
|
||||||
asm.compile(self)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_addr
|
def write_addr
|
||||||
@mem_block + @write_pos
|
@mem_block + @write_pos
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user