Skip to content

Commit

Permalink
Allow special case of expandarray with nil
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn authored and XrXr committed Oct 20, 2021
1 parent d098c55 commit fd34c83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions test/ruby/test_yjit.rb
Expand Up @@ -136,6 +136,19 @@ def foo(str)
RUBY
end

def test_expandarray
assert_compiles(<<~'RUBY', insns: %i[expandarray], result: [1, 2])
a, b = [1, 2]
RUBY
end

def test_expandarray_nil
assert_compiles(<<~'RUBY', insns: %i[expandarray], result: [nil, nil])
a, b = nil
[a, b]
RUBY
end

def test_compile_opt_getinlinecache
assert_compiles(<<~RUBY, insns: %i[opt_getinlinecache], result: 123, min_calls: 2)
def get_foo
Expand Down
11 changes: 11 additions & 0 deletions yjit_codegen.c
Expand Up @@ -987,8 +987,19 @@ gen_expandarray(jitstate_t* jit, ctx_t* ctx)
// num is the number of requested values. If there aren't enough in the
// array then we're going to push on nils.
rb_num_t num = (rb_num_t) jit_get_arg(jit, 0);
val_type_t array_type = ctx_get_opnd_type(ctx, OPND_STACK(0));
x86opnd_t array_opnd = ctx_stack_pop(ctx, 1);

if (array_type.type == ETYPE_NIL) {
// special case for a, b = nil pattern
// push N nils onto the stack
for (int i = 0; i < num; i++) {
x86opnd_t push = ctx_stack_push(ctx, TYPE_NIL);
mov(cb, push, imm_opnd(Qnil));
}
return YJIT_KEEP_COMPILING;
}

// Move the array from the stack into REG0 and check that it's an array.
mov(cb, REG0, array_opnd);
guard_object_is_heap(cb, REG0, ctx, COUNTED_EXIT(side_exit, expandarray_not_array));
Expand Down

0 comments on commit fd34c83

Please sign in to comment.