Skip to content

Commit f86909e

Browse files
committed
Draft: opt_new instruction
1 parent a43a52d commit f86909e

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

compile.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3930,7 +3930,14 @@ iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
39303930
}
39313931

39323932
if ((vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) == 0 && blockiseq == NULL) {
3933-
iobj->insn_id = BIN(opt_send_without_block);
3933+
switch (vm_ci_mid(ci)) {
3934+
case idNew:
3935+
iobj->insn_id = BIN(opt_new);
3936+
// TODO: add a second call_data for `#initialize`
3937+
break;
3938+
default:
3939+
iobj->insn_id = BIN(opt_send_without_block);
3940+
}
39343941
iobj->operand_size = insn_len(iobj->insn_id) - 1;
39353942
}
39363943
}

insns.def

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,37 @@ opt_send_without_block
828828
}
829829
}
830830

831+
/* Invoke constructor */
832+
DEFINE_INSN
833+
opt_new
834+
(CALL_DATA cd)
835+
(...)
836+
(VALUE val)
837+
// attr bool handles_sp = true;
838+
// attr rb_snum_t sp_inc = sp_inc_of_sendish(cd->ci);
839+
// attr rb_snum_t comptime_sp_inc = sp_inc_of_sendish(ci);
840+
{
841+
VALUE recv = TOPN(vm_ci_argc(cd->ci));
842+
843+
val = vm_opt_new_alloc(GET_ISEQ(), recv, cd);
844+
if (val != Qundef) {
845+
fprintf(stderr, "opt!\n");
846+
// TODO: call initialize (need a second CALL_DATA).
847+
}
848+
else {
849+
fprintf(stderr, "de-opt!\n");
850+
}
851+
852+
VALUE bh = VM_BLOCK_HANDLER_NONE;
853+
val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_method);
854+
JIT_EXEC(ec, val);
855+
856+
if (val == Qundef) {
857+
RESTORE_REGS();
858+
NEXT_INSN();
859+
}
860+
}
861+
831862
/* Convert object to string using to_s or equivalent. */
832863
DEFINE_INSN
833864
objtostring

internal/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ int rb_bool_expected(VALUE, const char *, int raise);
2121
static inline void RBASIC_CLEAR_CLASS(VALUE obj);
2222
static inline void RBASIC_SET_CLASS_RAW(VALUE obj, VALUE klass);
2323
static inline void RBASIC_SET_CLASS(VALUE obj, VALUE klass);
24+
VALUE rb_class_alloc(VALUE klass);
2425

2526
RUBY_SYMBOL_EXPORT_BEGIN
2627
/* object.c (export) */

object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ rb_class_alloc_m(VALUE klass)
20632063
return class_call_alloc_func(allocator, klass);
20642064
}
20652065

2066-
static VALUE
2066+
VALUE
20672067
rb_class_alloc(VALUE klass)
20682068
{
20692069
rb_alloc_func_t allocator = class_get_alloc_func(klass);

vm_insnhelper.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6114,6 +6114,15 @@ vm_opt_mod(VALUE recv, VALUE obj)
61146114
}
61156115
}
61166116

6117+
static VALUE
6118+
vm_opt_new_alloc(const rb_iseq_t *iseq, VALUE recv, CALL_DATA cd)
6119+
{
6120+
if (RB_TYPE_P(recv, T_CLASS) && vm_method_cfunc_is(iseq, cd, recv, rb_class_new_instance_pass_kw)) {
6121+
return rb_class_alloc(recv);
6122+
}
6123+
return Qundef;
6124+
}
6125+
61176126
static VALUE
61186127
vm_opt_neq(const rb_iseq_t *iseq, CALL_DATA cd, CALL_DATA cd_eq, VALUE recv, VALUE obj)
61196128
{

0 commit comments

Comments
 (0)