Skip to content

Commit

Permalink
Port invokesuper to the new backend IR (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Aug 25, 2022
1 parent 3411d7c commit 813fdfc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
28 changes: 28 additions & 0 deletions bootstraptest/test_yjit_new_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,34 @@ def bar = foo(1) { 2 }
bar
}

# invokesuper
assert_equal '[1, 2, 3]', %q{
class Foo
def foo(a = 1) = a
end
class Bar < Foo
def foo = [super, super(2), 3]
end
Bar.new.foo
}
assert_equal '[1, 2, 3]', %q{
class Foo
def zsuper(a, b) = [a, b]
end
class Bar < Foo
def zsuper(a, b) = super + [3]
end
Bar.new.zsuper(1, 2)
}
assert_equal 'A!', %q{
class Foo < String
def initialize(a) = super
def upcase = super + "!"
end
Foo.new("a").upcase
}

# getglobal
assert_equal '333', %q{
$bar = 333
Expand Down
48 changes: 21 additions & 27 deletions yjit/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4998,19 +4998,18 @@ fn gen_send(
return gen_send_general(jit, ctx, asm, ocb, cd, block);
}

/*
fn gen_invokesuper(
jit: &mut JITState,
ctx: &mut Context,
cb: &mut CodeBlock,
asm: &mut Assembler,
ocb: &mut OutlinedCb,
) -> CodegenStatus {
let cd: *const rb_call_data = jit_get_arg(jit, 0).as_ptr();
let block: Option<IseqPtr> = jit_get_arg(jit, 1).as_optional_ptr();

// Defer compilation so we can specialize on class of receiver
if !jit_at_current_insn(jit) {
defer_compilation(jit, ctx, cb, ocb);
defer_compilation(jit, ctx, asm, ocb);
return EndBlock;
}

Expand Down Expand Up @@ -5048,19 +5047,19 @@ fn gen_invokesuper(
// Don't JIT calls that aren't simple
// Note, not using VM_CALL_ARGS_SIMPLE because sometimes we pass a block.
if ci_flags & VM_CALL_ARGS_SPLAT != 0 {
gen_counter_incr!(cb, send_args_splat);
gen_counter_incr!(asm, send_args_splat);
return CantCompile;
}
if ci_flags & VM_CALL_KWARG != 0 {
gen_counter_incr!(cb, send_keywords);
gen_counter_incr!(asm, send_keywords);
return CantCompile;
}
if ci_flags & VM_CALL_KW_SPLAT != 0 {
gen_counter_incr!(cb, send_kw_splat);
gen_counter_incr!(asm, send_kw_splat);
return CantCompile;
}
if ci_flags & VM_CALL_ARGS_BLOCKARG != 0 {
gen_counter_incr!(cb, send_block_arg);
gen_counter_incr!(asm, send_block_arg);
return CantCompile;
}

Expand Down Expand Up @@ -5100,16 +5099,15 @@ fn gen_invokesuper(
return CantCompile;
}

add_comment(cb, "guard known me");
mov(cb, REG0, mem_opnd(64, REG_CFP, RUBY_OFFSET_CFP_EP));
let ep_me_opnd = mem_opnd(
asm.comment("guard known me");
let ep_opnd = asm.load(Opnd::mem(64, CFP, RUBY_OFFSET_CFP_EP));
let ep_me_opnd = Opnd::mem(
64,
REG0,
ep_opnd,
(SIZEOF_VALUE as i32) * (VM_ENV_DATA_INDEX_ME_CREF as i32),
);
jit_mov_gc_ptr(jit, cb, REG1, me_as_value);
cmp(cb, ep_me_opnd, REG1);
jne_ptr(cb, counted_exit!(ocb, side_exit, invokesuper_me_changed));
asm.cmp(ep_me_opnd, me_as_value.into());
asm.jne(counted_exit!(ocb, side_exit, invokesuper_me_changed).into());

if block.is_none() {
// Guard no block passed
Expand All @@ -5118,21 +5116,18 @@ fn gen_invokesuper(
//
// TODO: this could properly forward the current block handler, but
// would require changes to gen_send_*
add_comment(cb, "guard no block given");
asm.comment("guard no block given");
// EP is in REG0 from above
let ep_specval_opnd = mem_opnd(
let ep_opnd = asm.load(Opnd::mem(64, CFP, RUBY_OFFSET_CFP_EP));
let ep_specval_opnd = Opnd::mem(
64,
REG0,
ep_opnd,
(SIZEOF_VALUE as i32) * (VM_ENV_DATA_INDEX_SPECVAL as i32),
);
cmp(cb, ep_specval_opnd, uimm_opnd(VM_BLOCK_HANDLER_NONE.into()));
jne_ptr(cb, counted_exit!(ocb, side_exit, invokesuper_block));
asm.cmp(ep_specval_opnd, VM_BLOCK_HANDLER_NONE.into());
asm.jne(counted_exit!(ocb, side_exit, invokesuper_block).into());
}

// Points to the receiver operand on the stack
let recv = ctx.stack_opnd(argc);
mov(cb, REG0, recv);
// We need to assume that both our current method entry and the super
// method entry we invoke remain stable
assume_method_lookup_stable(jit, ocb, current_defined_class, me);
Expand All @@ -5142,14 +5137,13 @@ fn gen_invokesuper(
ctx.clear_local_types();

match cme_def_type {
VM_METHOD_TYPE_ISEQ => gen_send_iseq(jit, ctx, cb, ocb, ci, cme, block, argc),
VM_METHOD_TYPE_ISEQ => gen_send_iseq(jit, ctx, asm, ocb, ci, cme, block, argc),
VM_METHOD_TYPE_CFUNC => {
gen_send_cfunc(jit, ctx, cb, ocb, ci, cme, block, argc, ptr::null())
gen_send_cfunc(jit, ctx, asm, ocb, ci, cme, block, argc, ptr::null())
}
_ => unreachable!(),
}
}
*/

fn gen_leave(
jit: &mut JITState,
Expand Down Expand Up @@ -5929,7 +5923,7 @@ fn get_gen_fn(opcode: VALUE) -> Option<InsnGenFn> {
//YARVINSN_getblockparam => Some(gen_getblockparam),
YARVINSN_opt_send_without_block => Some(gen_opt_send_without_block),
YARVINSN_send => Some(gen_send),
//YARVINSN_invokesuper => Some(gen_invokesuper),
YARVINSN_invokesuper => Some(gen_invokesuper),
YARVINSN_leave => Some(gen_leave),

YARVINSN_getglobal => Some(gen_getglobal),
Expand Down

0 comments on commit 813fdfc

Please sign in to comment.