2022-09-05 00:53:46 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# TODO: Merge this to mjit.rb
|
|
|
|
if RubyVM::MJIT.enabled?
|
|
|
|
begin
|
|
|
|
require 'etc'
|
2022-09-05 02:25:26 -04:00
|
|
|
require 'fiddle'
|
2022-09-05 00:53:46 -04:00
|
|
|
rescue LoadError
|
|
|
|
return # skip miniruby
|
|
|
|
end
|
|
|
|
|
2022-09-05 02:25:26 -04:00
|
|
|
if Fiddle::SIZEOF_VOIDP == 8
|
2022-09-05 00:53:46 -04:00
|
|
|
require 'mjit/c_64'
|
|
|
|
else
|
|
|
|
require 'mjit/c_32'
|
|
|
|
end
|
|
|
|
|
|
|
|
class << RubyVM::MJIT::C
|
|
|
|
def ROBJECT_EMBED_LEN_MAX
|
|
|
|
Primitive.cexpr! 'INT2NUM(RBIMPL_EMBED_LEN_MAX_OF(VALUE))'
|
|
|
|
end
|
|
|
|
|
|
|
|
def cdhash_to_hash(cdhash_addr)
|
|
|
|
Primitive.cdhash_to_hash(cdhash_addr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def builtin_compiler(f, bf, index, stack_size, builtin_inline_p)
|
2022-09-05 11:23:53 -04:00
|
|
|
Primitive.builtin_compile(f, bf.to_i, index, stack_size, builtin_inline_p)
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_cache_for_send(cc, insn)
|
2022-09-05 11:23:53 -04:00
|
|
|
Primitive.has_cache_for_send(cc.to_i, insn)
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rb_iseq_check(iseq)
|
2022-09-05 11:23:53 -04:00
|
|
|
_iseq_addr = iseq.to_i
|
|
|
|
iseq_addr = Primitive.cexpr! 'PTR2NUM((VALUE)rb_iseq_check((rb_iseq_t *)NUM2PTR(_iseq_addr)))'
|
2022-09-05 00:53:46 -04:00
|
|
|
rb_iseq_t.new(iseq_addr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def rb_iseq_path(iseq)
|
2022-09-05 11:23:53 -04:00
|
|
|
_iseq_addr = iseq.to_i
|
|
|
|
Primitive.cexpr! 'rb_iseq_path((rb_iseq_t *)NUM2PTR(_iseq_addr))'
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def vm_ci_argc(ci)
|
2022-09-05 11:23:53 -04:00
|
|
|
_ci_addr = ci.to_i
|
|
|
|
Primitive.cexpr! 'UINT2NUM(vm_ci_argc((CALL_INFO)NUM2PTR(_ci_addr)))'
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def vm_ci_flag(ci)
|
2022-09-05 11:23:53 -04:00
|
|
|
_ci_addr = ci.to_i
|
|
|
|
Primitive.cexpr! 'UINT2NUM(vm_ci_flag((CALL_INFO)NUM2PTR(_ci_addr)))'
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rb_splat_or_kwargs_p(ci)
|
2022-09-05 11:23:53 -04:00
|
|
|
_ci_addr = ci.to_i
|
|
|
|
Primitive.cexpr! 'RBOOL(rb_splat_or_kwargs_p((CALL_INFO)NUM2PTR(_ci_addr)))'
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fastpath_applied_iseq_p(ci, cc, iseq)
|
2022-09-05 11:23:53 -04:00
|
|
|
_ci_addr = ci.to_i
|
|
|
|
_cc_addr = cc.to_i
|
|
|
|
_iseq_addr = iseq.to_i
|
|
|
|
Primitive.cexpr! 'RBOOL(fastpath_applied_iseq_p((CALL_INFO)NUM2PTR(_ci_addr), (CALL_CACHE)NUM2PTR(_cc_addr), (rb_iseq_t *)NUM2PTR(_iseq_addr)))'
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def mjit_opts
|
|
|
|
addr = Primitive.cexpr! 'PTR2NUM((VALUE)&mjit_opts)'
|
|
|
|
mjit_options.new(addr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mjit_call_attribute_sp_inc(insn, operands)
|
2022-09-05 11:23:53 -04:00
|
|
|
_operands_addr = operands.to_i
|
|
|
|
Primitive.cexpr! 'LONG2NUM(mjit_call_attribute_sp_inc(NUM2INT(insn), (VALUE *)NUM2PTR(_operands_addr)))'
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def mjit_capture_cc_entries(compiled_body, captured_body)
|
2022-09-05 11:23:53 -04:00
|
|
|
_compiled_body_addr = compiled_body.to_i
|
|
|
|
_captured_body_addr = captured_body.to_i
|
|
|
|
Primitive.cexpr! 'INT2NUM(mjit_capture_cc_entries((struct rb_iseq_constant_body *)NUM2PTR(_compiled_body_addr), (struct rb_iseq_constant_body *)NUM2PTR(_captured_body_addr)))'
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
#const struct rb_iseq_constant_body *body, union iseq_inline_storage_entry *is_entries
|
|
|
|
def mjit_capture_is_entries(body, is_entries)
|
2022-09-05 11:23:53 -04:00
|
|
|
_body_addr = body.to_i
|
|
|
|
_is_entries_addr = is_entries.to_i
|
2022-09-05 00:53:46 -04:00
|
|
|
Primitive.cstmt! %{
|
2022-09-05 11:23:53 -04:00
|
|
|
mjit_capture_is_entries((struct rb_iseq_constant_body *)NUM2PTR(_body_addr), (union iseq_inline_storage_entry *)NUM2PTR(_is_entries_addr));
|
2022-09-05 00:53:46 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-09-06 17:21:00 -04:00
|
|
|
# Convert encoded VM pointers to insn BINs.
|
2022-09-05 00:53:46 -04:00
|
|
|
def rb_vm_insn_decode(encoded)
|
|
|
|
Primitive.cexpr! 'INT2NUM(rb_vm_insn_decode(NUM2PTR(encoded)))'
|
|
|
|
end
|
|
|
|
|
2022-09-06 17:21:00 -04:00
|
|
|
# Convert insn BINs to encoded VM pointers. This one is not used by the compiler, but useful for debugging.
|
|
|
|
def rb_vm_insn_encode(bin)
|
|
|
|
Primitive.cexpr! 'PTR2NUM((VALUE)rb_vm_get_insns_address_table()[NUM2INT(bin)])'
|
|
|
|
end
|
|
|
|
|
2022-09-05 00:53:46 -04:00
|
|
|
def insn_may_depend_on_sp_or_pc(insn, opes)
|
2022-09-05 11:23:53 -04:00
|
|
|
_opes_addr = opes.to_i
|
|
|
|
Primitive.cexpr! 'RBOOL(insn_may_depend_on_sp_or_pc(NUM2INT(insn), (VALUE *)NUM2PTR(_opes_addr)))'
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Convert Integer VALUE to an actual Ruby object
|
|
|
|
def to_ruby(value)
|
|
|
|
Primitive.cexpr! '(VALUE)NUM2PTR(value)'
|
|
|
|
end
|
|
|
|
|
2022-09-06 16:54:37 -04:00
|
|
|
# Convert RubyVM::InstructionSequence to C.rb_iseq_t. Not used by the compiler, but useful for debugging.
|
|
|
|
def rb_iseqw_to_iseq(iseqw)
|
|
|
|
iseq_addr = Primitive.cexpr! 'PTR2NUM((VALUE)rb_iseqw_to_iseq(iseqw))'
|
|
|
|
rb_iseq_t.new(iseq_addr)
|
2022-09-05 00:53:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: remove this after migration
|
|
|
|
def fprintf(f, str)
|
|
|
|
Primitive.cstmt! %{
|
|
|
|
fprintf((FILE *)NUM2PTR(f), "%s", RSTRING_PTR(str));
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def rb_cFalseClass; Primitive.cexpr! 'PTR2NUM(rb_cFalseClass)' end
|
|
|
|
def rb_cNilClass; Primitive.cexpr! 'PTR2NUM(rb_cNilClass)' end
|
|
|
|
def rb_cTrueClass; Primitive.cexpr! 'PTR2NUM(rb_cTrueClass)' end
|
|
|
|
def rb_cInteger; Primitive.cexpr! 'PTR2NUM(rb_cInteger)' end
|
|
|
|
def rb_cSymbol; Primitive.cexpr! 'PTR2NUM(rb_cSymbol)' end
|
|
|
|
def rb_cFloat; Primitive.cexpr! 'PTR2NUM(rb_cFloat)' end
|
|
|
|
end
|
|
|
|
|
|
|
|
require "mjit/compiler"
|
|
|
|
end
|