Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Opt send new #544

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ new_insn_body(rb_iseq_t *iseq, const NODE *const line_node, enum ruby_vminsn_typ
}

static const struct rb_callinfo *
new_callinfo(rb_iseq_t *iseq, ID mid, int argc, unsigned int flag, struct rb_callinfo_kwarg *kw_arg, int has_blockiseq)
new_callinfo(rb_iseq_t *iseq, ID mid, int argc, unsigned int flag, const struct rb_callinfo_kwarg *kw_arg, int has_blockiseq)
{
VM_ASSERT(argc >= 0);

Expand Down Expand Up @@ -3930,8 +3930,15 @@ iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
}

if ((vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) == 0 && blockiseq == NULL) {
iobj->insn_id = BIN(opt_send_without_block);
iobj->operand_size = insn_len(iobj->insn_id) - 1;
switch (vm_ci_mid(ci)) {
case idNew:
iobj->insn_id = BIN(opt_new);
iobj->operands[1] = (VALUE)new_callinfo(iseq, idInitialize, vm_ci_argc(ci) - vm_ci_kwarg(ci)->keyword_len, vm_ci_flag(ci) | VM_CALL_FCALL, vm_ci_kwarg(ci), FALSE);
break;
default:
iobj->insn_id = BIN(opt_send_without_block);
iobj->operand_size = insn_len(iobj->insn_id) - 1;
}
}
}
#undef SP_INSN
Expand Down
1 change: 1 addition & 0 deletions defs/id.def
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ firstline, predefined = __LINE__+1, %[\
hash
freeze
nil?
new
inspect
intern
object_id
Expand Down
45 changes: 45 additions & 0 deletions insns.def
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,51 @@ opt_send_without_block
}
}

/* Invoke constructor */
DEFINE_INSN
opt_new
(CALL_DATA cd, CALL_DATA cd_initialize)
(...)
(VALUE val)
// attr bool handles_sp = true;
// attr rb_snum_t sp_inc = sp_inc_of_sendish(cd->ci);
// attr rb_snum_t comptime_sp_inc = sp_inc_of_sendish(ci);
{
int argc = vm_ci_argc(cd->ci);
VM_ASSERT((int)vm_ci_argc(cd_initialize->ci) == argc);

VALUE recv = TOPN(argc);
val = Qundef;

VALUE new_obj = vm_opt_new_alloc(GET_ISEQ(), recv, cd);
if (new_obj != Qundef) {
TOPN(argc) = new_obj;

val = vm_sendish(ec, GET_CFP(), cd_initialize, VM_BLOCK_HANDLER_NONE, mexp_search_method);
// FIXME: somehow the `initialize` return is what end up being the `.new` return, instead of `new_obj`.
// I don't get why.

JIT_EXEC(ec, val);

if (val == Qundef) {
RESTORE_REGS();
NEXT_INSN();
}
else {
val = new_obj;
}
}
else if (val == Qundef) {
val = vm_sendish(ec, GET_CFP(), cd, VM_BLOCK_HANDLER_NONE, mexp_search_method);
JIT_EXEC(ec, val);

if (val == Qundef) {
RESTORE_REGS();
NEXT_INSN();
}
}
}

/* Convert object to string using to_s or equivalent. */
DEFINE_INSN
objtostring
Expand Down
3 changes: 3 additions & 0 deletions internal/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ int rb_bool_expected(VALUE, const char *, int raise);
static inline void RBASIC_CLEAR_CLASS(VALUE obj);
static inline void RBASIC_SET_CLASS_RAW(VALUE obj, VALUE klass);
static inline void RBASIC_SET_CLASS(VALUE obj, VALUE klass);
VALUE rb_class_alloc(VALUE klass);
VALUE rb_obj_initialize(VALUE _self);


RUBY_SYMBOL_EXPORT_BEGIN
/* object.c (export) */
Expand Down
8 changes: 6 additions & 2 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,11 @@ rb_class_search_ancestor(VALUE cl, VALUE c)
*
* Returns a new BasicObject.
*/
#define rb_obj_initialize rb_obj_dummy0
VALUE
rb_obj_initialize(VALUE _self)
{
return Qnil;
}

/*
* Not documented
Expand Down Expand Up @@ -2063,7 +2067,7 @@ rb_class_alloc_m(VALUE klass)
return class_call_alloc_func(allocator, klass);
}

static VALUE
VALUE
rb_class_alloc(VALUE klass)
{
rb_alloc_func_t allocator = class_get_alloc_func(klass);
Expand Down
9 changes: 9 additions & 0 deletions vm_insnhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -6114,6 +6114,15 @@ vm_opt_mod(VALUE recv, VALUE obj)
}
}

static VALUE
vm_opt_new_alloc(const rb_iseq_t *iseq, VALUE recv, CALL_DATA cd)
{
if (RB_TYPE_P(recv, T_CLASS) && vm_method_cfunc_is(iseq, cd, recv, rb_class_new_instance_pass_kw)) {
return rb_class_alloc(recv);
}
return Qundef;
}

static VALUE
vm_opt_neq(const rb_iseq_t *iseq, CALL_DATA cd, CALL_DATA cd_eq, VALUE recv, VALUE obj)
{
Expand Down
Loading