summaryrefslogtreecommitdiff
path: root/lib/ruby_vm/mjit
diff options
context:
space:
mode:
authorTakashi Kokubun <[email protected]>2022-12-26 14:09:45 -0800
committerTakashi Kokubun <[email protected]>2023-03-05 22:11:20 -0800
commit3b398513bea65bb0237bb8b7a383ba2874bb0b84 (patch)
tree87954f8ae73692ebd9709c6a38a1def8bad02e64 /lib/ruby_vm/mjit
parent114f8d3e330735fda394c58d7a373913f9ccb93b (diff)
Check interrupts on leave
Diffstat (limited to 'lib/ruby_vm/mjit')
-rw-r--r--lib/ruby_vm/mjit/compiler.rb63
1 files changed, 35 insertions, 28 deletions
diff --git a/lib/ruby_vm/mjit/compiler.rb b/lib/ruby_vm/mjit/compiler.rb
index c744c533a0..6641c749b6 100644
--- a/lib/ruby_vm/mjit/compiler.rb
+++ b/lib/ruby_vm/mjit/compiler.rb
@@ -1,6 +1,7 @@
-require 'mjit/codegen'
require 'mjit/context'
+require 'mjit/insn_compiler'
require 'mjit/instruction'
+require 'mjit/jit_state'
require 'mjit/x86_assembler'
module RubyVM::MJIT
@@ -21,11 +22,32 @@ module RubyVM::MJIT
class Compiler
attr_accessor :write_pos
+ # @param jit [RubyVM::MJIT::JITState]
+ # @param ctx [RubyVM::MJIT::Context]
+ # @param asm [RubyVM::MJIT::X86Assembler]
+ def self.compile_exit(jit, ctx, asm)
+ # update pc
+ asm.mov(:rax, jit.pc) # rax = jit.pc
+ asm.mov([CFP, C.rb_control_frame_t.offsetof(:pc)], :rax) # cfp->pc = rax
+
+ # update sp
+ if ctx.stack_size > 0
+ asm.add(SP, C.VALUE.size * ctx.stack_size) # rbx += stack_size
+ asm.mov([CFP, C.rb_control_frame_t.offsetof(:sp)], SP) # cfp->sp = rbx
+ end
+
+ # Restore callee-saved registers
+ asm.pop(SP)
+
+ asm.mov(:rax, Qundef)
+ asm.ret
+ end
+
# @param mem_block [Integer] JIT buffer address
def initialize(mem_block)
@mem_block = mem_block
@write_pos = 0
- @codegen = Codegen.new
+ @insn_compiler = InsnCompiler.new
end
# @param iseq [RubyVM::MJIT::CPointer::Struct]
@@ -79,58 +101,43 @@ module RubyVM::MJIT
# @param asm [RubyVM::MJIT::X86Assembler]
def compile_block(asm, iseq)
+ jit = JITState.new
ctx = Context.new
+
index = 0
while index < iseq.body.iseq_size
insn = decode_insn(iseq.body.iseq_encoded[index])
- case compile_insn(ctx, asm, insn)
+ jit.pc = (iseq.body.iseq_encoded + index).to_i
+
+ case compile_insn(jit, ctx, asm, insn)
when EndBlock
break
when CantCompile
- compile_exit(ctx, asm, (iseq.body.iseq_encoded + index).to_i)
+ self.class.compile_exit(jit, ctx, asm)
break
end
index += insn.len
end
end
+ # @param jit [RubyVM::MJIT::JITState]
# @param ctx [RubyVM::MJIT::Context]
# @param asm [RubyVM::MJIT::X86Assembler]
- def compile_insn(ctx, asm, insn)
+ def compile_insn(jit, ctx, asm, insn)
case insn.name
- when :putnil then @codegen.putnil(ctx, asm)
- when :leave then @codegen.leave(ctx, asm)
+ when :putnil then @insn_compiler.putnil(jit, ctx, asm)
+ when :leave then @insn_compiler.leave(jit, ctx, asm)
else CantCompile
end
end
- # @param ctx [RubyVM::MJIT::Context]
- # @param asm [RubyVM::MJIT::X86Assembler]
- def compile_exit(ctx, asm, exit_pc)
- # update pc
- asm.mov(:rax, exit_pc) # rax = exit_pc
- asm.mov([CFP, C.rb_control_frame_t.offsetof(:pc)], :rax) # cfp->pc = rax
-
- # update sp
- if ctx.stack_size > 0
- asm.add(SP, C.VALUE.size * ctx.stack_size) # rbx += stack_size
- asm.mov([CFP, C.rb_control_frame_t.offsetof(:sp)], SP) # cfp->sp = rbx
- end
-
- # Restore callee-saved registers
- asm.pop(SP)
-
- asm.mov(:rax, Qundef)
- asm.ret
- end
-
def decode_insn(encoded)
INSNS.fetch(C.rb_vm_insn_decode(encoded))
end
def dump_disasm(from, to)
C.dump_disasm(from, to).each do |address, mnemonic, op_str|
- puts " 0x#{"%p" % address}: #{mnemonic} #{op_str}"
+ puts " 0x#{"%x" % address}: #{mnemonic} #{op_str}"
end
puts
end