YJIT: Support concattoarray and pushtoarray (#9708)

This commit is contained in:
Takashi Kokubun 2024-01-25 13:45:58 -08:00 коммит произвёл GitHub
Родитель f0224adf2f
Коммит 2034e6ad5a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
6 изменённых файлов: 73 добавлений и 0 удалений

Просмотреть файл

@ -4459,3 +4459,17 @@ assert_equal '[[{:a=>1}], {}]', %q{
body body
} }
# concatarray
assert_equal '[1, 2]', %q{
def foo(a, b) = [a, b]
arr = [2]
foo(*[1], *arr)
}
# pushtoarray
assert_equal '[1, 2]', %q{
def foo(a, b) = [a, b]
arr = [1]
foo(*arr, 2)
}

Просмотреть файл

@ -5237,6 +5237,12 @@ rb_vm_concat_array(VALUE ary1, VALUE ary2st)
return vm_concat_array(ary1, ary2st); return vm_concat_array(ary1, ary2st);
} }
VALUE
rb_vm_concat_to_array(VALUE ary1, VALUE ary2st)
{
return vm_concat_to_array(ary1, ary2st);
}
static VALUE static VALUE
vm_splat_array(VALUE flag, VALUE ary) vm_splat_array(VALUE flag, VALUE ary)
{ {

Просмотреть файл

@ -133,6 +133,7 @@ fn main() {
.allowlist_function("rb_ary_new_capa") .allowlist_function("rb_ary_new_capa")
.allowlist_function("rb_ary_store") .allowlist_function("rb_ary_store")
.allowlist_function("rb_ary_resurrect") .allowlist_function("rb_ary_resurrect")
.allowlist_function("rb_ary_cat")
.allowlist_function("rb_ary_clear") .allowlist_function("rb_ary_clear")
.allowlist_function("rb_ary_dup") .allowlist_function("rb_ary_dup")
.allowlist_function("rb_ary_push") .allowlist_function("rb_ary_push")

Просмотреть файл

@ -1459,6 +1459,54 @@ fn gen_concatarray(
Some(KeepCompiling) Some(KeepCompiling)
} }
// concat second array to first array.
// first argument must already be an array.
// attempts to convert second object to array using to_a.
fn gen_concattoarray(
jit: &mut JITState,
asm: &mut Assembler,
_ocb: &mut OutlinedCb,
) -> Option<CodegenStatus> {
// Save the PC and SP because the callee may allocate
jit_prepare_routine_call(jit, asm);
// Get the operands from the stack
let ary2_opnd = asm.stack_opnd(0);
let ary1_opnd = asm.stack_opnd(1);
let ary = asm.ccall(rb_vm_concat_to_array as *const u8, vec![ary1_opnd, ary2_opnd]);
asm.stack_pop(2); // Keep them on stack during ccall for GC
let stack_ret = asm.stack_push(Type::TArray);
asm.mov(stack_ret, ary);
Some(KeepCompiling)
}
// push given number of objects to array directly before.
fn gen_pushtoarray(
jit: &mut JITState,
asm: &mut Assembler,
_ocb: &mut OutlinedCb,
) -> Option<CodegenStatus> {
let num = jit.get_arg(0).as_u64();
// Save the PC and SP because the callee may allocate
jit_prepare_routine_call(jit, asm);
// Get the operands from the stack
let ary_opnd = asm.stack_opnd(num as i32);
let objp_opnd = asm.lea(asm.ctx.sp_opnd(-(num as isize) * SIZEOF_VALUE as isize));
let ary = asm.ccall(rb_ary_cat as *const u8, vec![ary_opnd, objp_opnd, num.into()]);
asm.stack_pop(num as usize + 1); // Keep it on stack during ccall for GC
let stack_ret = asm.stack_push(Type::TArray);
asm.mov(stack_ret, ary);
Some(KeepCompiling)
}
// new range initialized from top 2 values // new range initialized from top 2 values
fn gen_newrange( fn gen_newrange(
jit: &mut JITState, jit: &mut JITState,
@ -8904,6 +8952,8 @@ fn get_gen_fn(opcode: VALUE) -> Option<InsnGenFn> {
YARVINSN_opt_newarray_send => Some(gen_opt_newarray_send), YARVINSN_opt_newarray_send => Some(gen_opt_newarray_send),
YARVINSN_splatarray => Some(gen_splatarray), YARVINSN_splatarray => Some(gen_splatarray),
YARVINSN_concatarray => Some(gen_concatarray), YARVINSN_concatarray => Some(gen_concatarray),
YARVINSN_concattoarray => Some(gen_concattoarray),
YARVINSN_pushtoarray => Some(gen_pushtoarray),
YARVINSN_newrange => Some(gen_newrange), YARVINSN_newrange => Some(gen_newrange),
YARVINSN_putstring => Some(gen_putstring), YARVINSN_putstring => Some(gen_putstring),
YARVINSN_expandarray => Some(gen_expandarray), YARVINSN_expandarray => Some(gen_expandarray),

Просмотреть файл

@ -114,6 +114,7 @@ pub use autogened::*;
extern "C" { extern "C" {
pub fn rb_vm_splat_array(flag: VALUE, ary: VALUE) -> VALUE; pub fn rb_vm_splat_array(flag: VALUE, ary: VALUE) -> VALUE;
pub fn rb_vm_concat_array(ary1: VALUE, ary2st: VALUE) -> VALUE; pub fn rb_vm_concat_array(ary1: VALUE, ary2st: VALUE) -> VALUE;
pub fn rb_vm_concat_to_array(ary1: VALUE, ary2st: VALUE) -> VALUE;
pub fn rb_vm_defined( pub fn rb_vm_defined(
ec: EcPtr, ec: EcPtr,
reg_cfp: CfpPtr, reg_cfp: CfpPtr,

Просмотреть файл

@ -936,6 +936,7 @@ extern "C" {
pub fn rb_ary_store(ary: VALUE, key: ::std::os::raw::c_long, val: VALUE); pub fn rb_ary_store(ary: VALUE, key: ::std::os::raw::c_long, val: VALUE);
pub fn rb_ary_dup(ary: VALUE) -> VALUE; pub fn rb_ary_dup(ary: VALUE) -> VALUE;
pub fn rb_ary_resurrect(ary: VALUE) -> VALUE; pub fn rb_ary_resurrect(ary: VALUE) -> VALUE;
pub fn rb_ary_cat(ary: VALUE, train: *const VALUE, len: ::std::os::raw::c_long) -> VALUE;
pub fn rb_ary_push(ary: VALUE, elem: VALUE) -> VALUE; pub fn rb_ary_push(ary: VALUE, elem: VALUE) -> VALUE;
pub fn rb_ary_clear(ary: VALUE) -> VALUE; pub fn rb_ary_clear(ary: VALUE) -> VALUE;
pub fn rb_hash_new() -> VALUE; pub fn rb_hash_new() -> VALUE;