Skip to content

Commit

Permalink
Draft: opt_new instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Nov 4, 2023
1 parent a43a52d commit 84205f7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
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_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
35 changes: 35 additions & 0 deletions insns.def
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,41 @@ 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);
{
VALUE recv = TOPN(vm_ci_argc(cd->ci));

val = vm_opt_new_alloc(GET_ISEQ(), recv, cd);
if (val != Qundef) {
TOPN(vm_ci_argc(cd->ci)) = val;
fprintf(stderr, "opt!\n");
if (vm_sendish(ec, GET_CFP(), cd_initialize, VM_BLOCK_HANDLER_NONE, mexp_search_method) == Qundef) {
val = Qundef;
}
}

if (val == Qundef) {
fprintf(stderr, "de-opt!\n");

VALUE bh = VM_BLOCK_HANDLER_NONE;
val = vm_sendish(ec, GET_CFP(), cd, bh, 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
1 change: 1 addition & 0 deletions internal/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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);

RUBY_SYMBOL_EXPORT_BEGIN
/* object.c (export) */
Expand Down
2 changes: 1 addition & 1 deletion object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,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

0 comments on commit 84205f7

Please sign in to comment.