Skip to content

Commit 84205f7

Browse files
committedNov 4, 2023
Draft: opt_new instruction
1 parent a43a52d commit 84205f7

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed
 

‎compile.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ new_insn_body(rb_iseq_t *iseq, const NODE *const line_node, enum ruby_vminsn_typ
14421442
}
14431443

14441444
static const struct rb_callinfo *
1445-
new_callinfo(rb_iseq_t *iseq, ID mid, int argc, unsigned int flag, struct rb_callinfo_kwarg *kw_arg, int has_blockiseq)
1445+
new_callinfo(rb_iseq_t *iseq, ID mid, int argc, unsigned int flag, const struct rb_callinfo_kwarg *kw_arg, int has_blockiseq)
14461446
{
14471447
VM_ASSERT(argc >= 0);
14481448

@@ -3930,8 +3930,15 @@ 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);
3934-
iobj->operand_size = insn_len(iobj->insn_id) - 1;
3933+
switch (vm_ci_mid(ci)) {
3934+
case idNew:
3935+
iobj->insn_id = BIN(opt_new);
3936+
iobj->operands[1] = (VALUE)new_callinfo(iseq, idInitialize, vm_ci_argc(ci), vm_ci_flag(ci) | VM_CALL_FCALL, vm_ci_kwarg(ci), FALSE);
3937+
break;
3938+
default:
3939+
iobj->insn_id = BIN(opt_send_without_block);
3940+
iobj->operand_size = insn_len(iobj->insn_id) - 1;
3941+
}
39353942
}
39363943
}
39373944
#undef SP_INSN

‎defs/id.def

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ firstline, predefined = __LINE__+1, %[\
55
hash
66
freeze
77
nil?
8+
new
89
inspect
910
intern
1011
object_id

‎insns.def

+35
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,41 @@ opt_send_without_block
828828
}
829829
}
830830

831+
/* Invoke constructor */
832+
DEFINE_INSN
833+
opt_new
834+
(CALL_DATA cd, CALL_DATA cd_initialize)
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+
TOPN(vm_ci_argc(cd->ci)) = val;
846+
fprintf(stderr, "opt!\n");
847+
if (vm_sendish(ec, GET_CFP(), cd_initialize, VM_BLOCK_HANDLER_NONE, mexp_search_method) == Qundef) {
848+
val = Qundef;
849+
}
850+
}
851+
852+
if (val == Qundef) {
853+
fprintf(stderr, "de-opt!\n");
854+
855+
VALUE bh = VM_BLOCK_HANDLER_NONE;
856+
val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_method);
857+
JIT_EXEC(ec, val);
858+
859+
if (val == Qundef) {
860+
RESTORE_REGS();
861+
NEXT_INSN();
862+
}
863+
}
864+
}
865+
831866
/* Convert object to string using to_s or equivalent. */
832867
DEFINE_INSN
833868
objtostring

‎internal/object.h

+1
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

+1-1
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

+9
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)
Failed to load comments.