Ruby 4.1.0dev (2026-11-27 revision 09cfdea5da228b2ec76d1545f55e7557d49dd637)
vm_insnhelper.c (09cfdea5da228b2ec76d1545f55e7557d49dd637)
1/**********************************************************************
2
3 vm_insnhelper.c - instruction helper functions. Included into vm.c.
4
5 $Author$
6
7 Copyright (C) 2007 Koichi Sasada
8
9**********************************************************************/
10
11#include "ruby/internal/config.h"
12
13#include <math.h>
14
15#ifdef HAVE_STDATOMIC_H
16 #include <stdatomic.h>
17#endif
18
19#include "constant.h"
20#include "const_path.h"
21#include "debug_counter.h"
22#include "internal.h"
23#include "internal/class.h"
24#include "internal/compar.h"
25#include "internal/hash.h"
26#include "internal/numeric.h"
27#include "internal/proc.h"
28#include "internal/ractor.h"
29#include "internal/random.h"
30#include "internal/variable.h"
31#include "internal/set.h"
32#include "internal/set_table.h"
33#include "internal/struct.h"
34#include "ruby/thread.h"
35#include "variable.h"
36
37/* finish iseq array */
38#include "insns.inc"
39#include "insns_info.inc"
40
41extern rb_method_definition_t *rb_method_definition_create(rb_method_type_t type, ID mid);
42extern void rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *def, void *opts);
43extern int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2);
44extern VALUE rb_make_no_method_exception(VALUE exc, VALUE format, VALUE obj,
45 int argc, const VALUE *argv, int priv);
46
47/* control stack frame */
48
49static rb_control_frame_t *vm_get_ruby_level_caller_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
50
52ruby_vm_special_exception_copy(VALUE exc)
53{
55 rb_obj_copy_ivar(e, exc);
56 return e;
57}
58
59NORETURN(static void ec_stack_overflow(rb_execution_context_t *ec, int));
60static void
61ec_stack_overflow(rb_execution_context_t *ec, int setup)
62{
63 VALUE mesg = rb_ec_vm_ptr(ec)->special_exceptions[ruby_error_sysstack];
64 ec->raised_flag = RAISED_STACKOVERFLOW;
65 if (setup) {
66 VALUE at = rb_ec_backtrace_object(ec);
67 mesg = ruby_vm_special_exception_copy(mesg);
68 rb_ivar_set(mesg, idBt, at);
69 rb_ivar_set(mesg, idBt_locations, at);
70 }
71 ec->errinfo = mesg;
72 EC_JUMP_TAG(ec, TAG_RAISE);
73}
74
75NORETURN(static void vm_stackoverflow(void));
76
77static void
78vm_stackoverflow(void)
79{
80 ec_stack_overflow(GET_EC(), TRUE);
81}
82
83void
84rb_ec_stack_overflow(rb_execution_context_t *ec, ruby_stack_overflow_critical_level crit)
85{
86 if (rb_during_gc()) {
87 rb_bug("system stack overflow during GC. Faulty native extension?");
88 }
89 if (crit >= rb_stack_overflow_fatal) {
90 ec->raised_flag = RAISED_STACKOVERFLOW;
91 ec->errinfo = rb_ec_vm_ptr(ec)->special_exceptions[ruby_error_stackfatal];
92 EC_JUMP_TAG(ec, TAG_RAISE);
93 }
94 ec_stack_overflow(ec, crit < rb_stack_overflow_signal);
95}
96
97static inline void stack_check(rb_execution_context_t *ec);
98
99#if VM_CHECK_MODE > 0
100static int
101callable_class_p(VALUE klass)
102{
103#if VM_CHECK_MODE >= 2
104 if (!klass) return FALSE;
105 switch (RB_BUILTIN_TYPE(klass)) {
106 default:
107 break;
108 case T_ICLASS:
109 if (!RB_TYPE_P(RCLASS_SUPER(klass), T_MODULE)) break;
110 case T_MODULE:
111 return TRUE;
112 }
113 while (klass) {
114 if (klass == rb_cBasicObject) {
115 return TRUE;
116 }
117 klass = RCLASS_SUPER(klass);
118 }
119 return FALSE;
120#else
121 return klass != 0;
122#endif
123}
124
125static int
126callable_method_entry_p(const rb_callable_method_entry_t *cme)
127{
128 if (cme == NULL) {
129 return TRUE;
130 }
131 else {
132 VM_ASSERT(IMEMO_TYPE_P((VALUE)cme, imemo_ment), "imemo_type:%s", rb_imemo_name(imemo_type((VALUE)cme)));
133
134 if (callable_class_p(cme->defined_class)) {
135 return TRUE;
136 }
137 else {
138 return FALSE;
139 }
140 }
141}
142
143static void
144vm_check_frame_detail(VALUE type, int req_block, int req_me, int req_cref, VALUE specval, VALUE cref_or_me, int is_cframe, const rb_iseq_t *iseq)
145{
146 unsigned int magic = (unsigned int)(type & VM_FRAME_MAGIC_MASK);
147 enum imemo_type cref_or_me_type = imemo_env; /* impossible value */
148
149 if (RB_TYPE_P(cref_or_me, T_IMEMO)) {
150 cref_or_me_type = imemo_type(cref_or_me);
151 }
152 if (type & VM_FRAME_FLAG_BMETHOD) {
153 req_me = TRUE;
154 }
155
156 if (req_block && (type & VM_ENV_FLAG_LOCAL) == 0) {
157 rb_bug("vm_push_frame: specval (%p) should be a block_ptr on %x frame", (void *)specval, magic);
158 }
159 if (!req_block && (type & VM_ENV_FLAG_LOCAL) != 0) {
160 rb_bug("vm_push_frame: specval (%p) should not be a block_ptr on %x frame", (void *)specval, magic);
161 }
162
163 if (req_me) {
164 if (cref_or_me_type != imemo_ment) {
165 rb_bug("vm_push_frame: (%s) should be method entry on %x frame", rb_obj_info(cref_or_me), magic);
166 }
167 }
168 else {
169 if (req_cref && cref_or_me_type != imemo_cref) {
170 rb_bug("vm_push_frame: (%s) should be CREF on %x frame", rb_obj_info(cref_or_me), magic);
171 }
172 else { /* cref or Qfalse */
173 if (cref_or_me != Qfalse && cref_or_me_type != imemo_cref) {
174 if (((type & VM_FRAME_FLAG_LAMBDA) || magic == VM_FRAME_MAGIC_IFUNC || magic == VM_FRAME_MAGIC_DUMMY) && (cref_or_me_type == imemo_ment)) {
175 /* ignore */
176 }
177 else {
178 rb_bug("vm_push_frame: (%s) should be false or cref on %x frame", rb_obj_info(cref_or_me), magic);
179 }
180 }
181 }
182 }
183
184 if (cref_or_me_type == imemo_ment) {
185 const rb_callable_method_entry_t *me = (const rb_callable_method_entry_t *)cref_or_me;
186
187 if (!callable_method_entry_p(me)) {
188 rb_bug("vm_push_frame: ment (%s) should be callable on %x frame.", rb_obj_info(cref_or_me), magic);
189 }
190 }
191
192 if ((type & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY) {
193 VM_ASSERT(iseq == NULL ||
194 RBASIC_CLASS((VALUE)iseq) == 0 || // dummy frame for loading
195 RUBY_VM_NORMAL_ISEQ_P(iseq) //argument error
196 );
197 }
198 else {
199 VM_ASSERT(is_cframe == !RUBY_VM_NORMAL_ISEQ_P(iseq));
200 }
201}
202
203static void
204vm_check_frame(VALUE type,
205 VALUE specval,
206 VALUE cref_or_me,
207 const rb_iseq_t *iseq)
208{
209 VALUE given_magic = type & VM_FRAME_MAGIC_MASK;
210 VM_ASSERT(FIXNUM_P(type));
211
212#define CHECK(magic, req_block, req_me, req_cref, is_cframe) \
213 case magic: \
214 vm_check_frame_detail(type, req_block, req_me, req_cref, \
215 specval, cref_or_me, is_cframe, iseq); \
216 break
217 switch (given_magic) {
218 /* BLK ME CREF CFRAME */
219 CHECK(VM_FRAME_MAGIC_METHOD, TRUE, TRUE, FALSE, FALSE);
220 CHECK(VM_FRAME_MAGIC_CLASS, TRUE, FALSE, TRUE, FALSE);
221 CHECK(VM_FRAME_MAGIC_TOP, TRUE, FALSE, TRUE, FALSE);
222 CHECK(VM_FRAME_MAGIC_CFUNC, TRUE, TRUE, FALSE, TRUE);
223 CHECK(VM_FRAME_MAGIC_BLOCK, FALSE, FALSE, FALSE, FALSE);
224 CHECK(VM_FRAME_MAGIC_IFUNC, FALSE, FALSE, FALSE, TRUE);
225 CHECK(VM_FRAME_MAGIC_EVAL, FALSE, FALSE, FALSE, FALSE);
226 CHECK(VM_FRAME_MAGIC_RESCUE, FALSE, FALSE, FALSE, FALSE);
227 CHECK(VM_FRAME_MAGIC_DUMMY, TRUE, FALSE, FALSE, FALSE);
228 default:
229 rb_bug("vm_push_frame: unknown type (%x)", (unsigned int)given_magic);
230 }
231#undef CHECK
232}
233
234static VALUE vm_stack_canary; /* Initialized later */
235static bool vm_stack_canary_was_born = false;
236
237// Return the index of the instruction right before the given PC.
238// This is needed because insn_entry advances PC before the insn body.
239static unsigned int
240previous_insn_index(const rb_iseq_t *iseq, const VALUE *pc)
241{
242 unsigned int pos = 0;
243 while (pos < ISEQ_BODY(iseq)->iseq_size) {
244 int opcode = rb_vm_insn_addr2opcode((void *)ISEQ_BODY(iseq)->iseq_encoded[pos]);
245 unsigned int next_pos = pos + insn_len(opcode);
246 if (ISEQ_BODY(iseq)->iseq_encoded + next_pos == pc) {
247 return pos;
248 }
249 pos = next_pos;
250 }
251 rb_bug("failed to find the previous insn");
252}
253
254void
255rb_vm_check_canary(const rb_execution_context_t *ec, VALUE *sp)
256{
257 const struct rb_control_frame_struct *reg_cfp = ec->cfp;
258 const struct rb_iseq_struct *iseq;
259
260 if (! LIKELY(vm_stack_canary_was_born)) {
261 return; /* :FIXME: isn't it rather fatal to enter this branch? */
262 }
263 else if ((VALUE *)reg_cfp == ec->vm_stack + ec->vm_stack_size) {
264 /* This is at the very beginning of a thread. cfp does not exist. */
265 return;
266 }
267 else if (! VM_FRAME_RUBYFRAME_P(reg_cfp) || ! (iseq = GET_ISEQ())) {
268 /* The iseq field of an IFUNC frame holds the ifunc. */
269 return;
270 }
271 else if (LIKELY(sp[0] != vm_stack_canary)) {
272 return;
273 }
274 else {
275 /* we are going to call methods below; squash the canary to
276 * prevent infinite loop. */
277 sp[0] = Qundef;
278 }
279
280 const VALUE *orig = rb_iseq_original_iseq(iseq);
281 const VALUE iseqw = rb_iseqw_new(iseq);
282 const VALUE inspection = rb_inspect(iseqw);
283 const char *stri = rb_str_to_cstr(inspection);
284 const VALUE disasm = rb_iseq_disasm(iseq);
285 const char *strd = rb_str_to_cstr(disasm);
286 const ptrdiff_t pos = previous_insn_index(iseq, GET_PC());
287 const enum ruby_vminsn_type insn = (enum ruby_vminsn_type)orig[pos];
288 const char *name = insn_name(insn);
289
290 /* rb_bug() is not capable of outputting this large contents. It
291 is designed to run form a SIGSEGV handler, which tends to be
292 very restricted. */
293 ruby_debug_printf(
294 "We are killing the stack canary set by %s, "
295 "at %s@pc=%"PRIdPTR"\n"
296 "watch out the C stack trace.\n"
297 "%s",
298 name, stri, pos, strd);
299 rb_bug("see above.");
300}
301#define vm_check_canary(ec, sp) rb_vm_check_canary(ec, sp)
302
303#else
304#define vm_check_canary(ec, sp)
305#define vm_check_frame(a, b, c, d)
306#endif /* VM_CHECK_MODE > 0 */
307
308#if USE_DEBUG_COUNTER
309static void
310vm_push_frame_debug_counter_inc(
311 const struct rb_execution_context_struct *ec,
312 const struct rb_control_frame_struct *reg_cfp,
313 VALUE type)
314{
315 const struct rb_control_frame_struct *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(reg_cfp);
316
317 RB_DEBUG_COUNTER_INC(frame_push);
318
319 if (RUBY_VM_END_CONTROL_FRAME(ec) != prev_cfp) {
320 const bool curr = VM_FRAME_RUBYFRAME_P(reg_cfp);
321 const bool prev = VM_FRAME_RUBYFRAME_P(prev_cfp);
322 if (prev) {
323 if (curr) {
324 RB_DEBUG_COUNTER_INC(frame_R2R);
325 }
326 else {
327 RB_DEBUG_COUNTER_INC(frame_R2C);
328 }
329 }
330 else {
331 if (curr) {
332 RB_DEBUG_COUNTER_INC(frame_C2R);
333 }
334 else {
335 RB_DEBUG_COUNTER_INC(frame_C2C);
336 }
337 }
338 }
339
340 switch (type & VM_FRAME_MAGIC_MASK) {
341 case VM_FRAME_MAGIC_METHOD: RB_DEBUG_COUNTER_INC(frame_push_method); return;
342 case VM_FRAME_MAGIC_BLOCK: RB_DEBUG_COUNTER_INC(frame_push_block); return;
343 case VM_FRAME_MAGIC_CLASS: RB_DEBUG_COUNTER_INC(frame_push_class); return;
344 case VM_FRAME_MAGIC_TOP: RB_DEBUG_COUNTER_INC(frame_push_top); return;
345 case VM_FRAME_MAGIC_CFUNC: RB_DEBUG_COUNTER_INC(frame_push_cfunc); return;
346 case VM_FRAME_MAGIC_IFUNC: RB_DEBUG_COUNTER_INC(frame_push_ifunc); return;
347 case VM_FRAME_MAGIC_EVAL: RB_DEBUG_COUNTER_INC(frame_push_eval); return;
348 case VM_FRAME_MAGIC_RESCUE: RB_DEBUG_COUNTER_INC(frame_push_rescue); return;
349 case VM_FRAME_MAGIC_DUMMY: RB_DEBUG_COUNTER_INC(frame_push_dummy); return;
350 }
351
352 rb_bug("unreachable");
353}
354#else
355#define vm_push_frame_debug_counter_inc(ec, cfp, t) /* void */
356#endif
357
358// Return a poison value to be set above the stack top to verify leafness.
359VALUE
360rb_vm_stack_canary(void)
361{
362#if VM_CHECK_MODE > 0
363 return vm_stack_canary;
364#else
365 return 0;
366#endif
367}
368
369STATIC_ASSERT(VM_ENV_DATA_INDEX_ME_CREF, VM_ENV_DATA_INDEX_ME_CREF == -2);
370STATIC_ASSERT(VM_ENV_DATA_INDEX_SPECVAL, VM_ENV_DATA_INDEX_SPECVAL == -1);
371STATIC_ASSERT(VM_ENV_DATA_INDEX_FLAGS, VM_ENV_DATA_INDEX_FLAGS == -0);
372
373static void
374vm_push_frame(rb_execution_context_t *ec,
375 const rb_iseq_t *iseq,
376 VALUE type,
377 VALUE self,
378 VALUE specval,
379 VALUE cref_or_me,
380 const VALUE *pc,
381 VALUE *sp,
382 int local_size,
383 int stack_max)
384{
385 rb_control_frame_t *const cfp = RUBY_VM_NEXT_CONTROL_FRAME(ec->cfp);
386
387 vm_check_frame(type, specval, cref_or_me, iseq);
388 VM_ASSERT(local_size >= 0);
389
390 /* check stack overflow */
391 CHECK_VM_STACK_OVERFLOW0(cfp, sp, local_size + stack_max);
392 vm_check_canary(ec, sp);
393
394 /* setup vm value stack */
395
396 /* initialize local variables */
397 for (int i=0; i < local_size; i++) {
398 *sp++ = Qnil;
399 }
400
401 /* setup ep with managing data */
402 *sp++ = cref_or_me; /* ep[-2] / Qnil or T_IMEMO(cref) or T_IMEMO(ment) */
403 *sp++ = specval /* ep[-1] / block handler or prev env ptr */;
404 *sp++ = type; /* ep[-0] / ENV_FLAGS */
405
406 /* setup new frame */
407 *cfp = (const struct rb_control_frame_struct) {
408 .pc = pc,
409 .sp = sp,
410 ._iseq = iseq,
411 .self = self,
412 .ep = sp - 1,
413 .block_code = NULL,
414#if VM_DEBUG_BP_CHECK
415 .bp_check = sp,
416#endif
417 .jit_return = NULL,
418 };
419
420 /* Ensure the initialization of `*cfp` above never gets reordered with the update of `ec->cfp` below.
421 This is a no-op in all cases we've looked at (https://godbolt.org/z/3oxd1446K), but should guarantee it for all
422 future/untested compilers/platforms. */
423
424 #if defined HAVE_DECL_ATOMIC_SIGNAL_FENCE && HAVE_DECL_ATOMIC_SIGNAL_FENCE
425 atomic_signal_fence(memory_order_seq_cst);
426 #endif
427
428 ec->cfp = cfp;
429
430 if (VMDEBUG == 2) {
431 SDR();
432 }
433 vm_push_frame_debug_counter_inc(ec, cfp, type);
434}
435
436void
437rb_vm_pop_frame_no_int(rb_execution_context_t *ec)
438{
439 rb_control_frame_t *cfp = ec->cfp;
440
441 if (VMDEBUG == 2) SDR();
442
443 ec->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
444}
445
446/* return TRUE if the frame is finished */
447static inline int
448vm_pop_frame(rb_execution_context_t *ec, rb_control_frame_t *cfp, const VALUE *ep)
449{
450 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
451
452 if (VMDEBUG == 2) SDR();
453
454 RUBY_VM_CHECK_INTS(ec);
455 ec->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
456
457 return flags & VM_FRAME_FLAG_FINISH;
458}
459
460void
461rb_vm_pop_frame(rb_execution_context_t *ec)
462{
463 vm_pop_frame(ec, ec->cfp, ec->cfp->ep);
464}
465
466// it pushes pseudo-frame with fname filename.
467VALUE
468rb_vm_push_frame_fname(rb_execution_context_t *ec, VALUE fname)
469{
470 rb_iseq_t *rb_iseq_alloc_with_dummy_path(VALUE fname);
471 rb_iseq_t *dmy_iseq = rb_iseq_alloc_with_dummy_path(fname);
472
473 vm_push_frame(ec,
474 dmy_iseq, //const rb_iseq_t *iseq,
475 VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH, // VALUE type,
476 ec->cfp->self, // VALUE self,
477 VM_BLOCK_HANDLER_NONE, // VALUE specval,
478 Qfalse, // VALUE cref_or_me,
479 NULL, // const VALUE *pc,
480 ec->cfp->sp, // VALUE *sp,
481 0, // int local_size,
482 0); // int stack_max
483
484 return (VALUE)dmy_iseq;
485}
486
487/* method dispatch */
488static inline VALUE
489rb_arity_error_new(int argc, int min, int max)
490{
491 VALUE err_mess = rb_sprintf("wrong number of arguments (given %d, expected %d", argc, min);
492 if (min == max) {
493 /* max is not needed */
494 }
495 else if (max == UNLIMITED_ARGUMENTS) {
496 rb_str_cat_cstr(err_mess, "+");
497 }
498 else {
499 rb_str_catf(err_mess, "..%d", max);
500 }
501 rb_str_cat_cstr(err_mess, ")");
502 return rb_exc_new3(rb_eArgError, err_mess);
503}
504
505void
506rb_error_arity(int argc, int min, int max)
507{
508 rb_exc_raise(rb_arity_error_new(argc, min, max));
509}
510
511/* lvar */
512
513NOINLINE(static void vm_env_write_slowpath(const VALUE *ep, int index, VALUE v));
514
515static void
516vm_env_write_slowpath(const VALUE *ep, int index, VALUE v)
517{
518 const VALUE envval = VM_ENV_ENVVAL(ep);
519
520 if (RB_FL_TEST_RAW(envval, RUBY_FL_SHAREABLE)) {
521 /* Writing to a SHAREABLE env (an isolated proc) can create a shareable ->
522 * unshareable edge; only a full barrier sets the shref bit keeping v alive
523 * through its owner's local GC. WB_REQUIRED stays: later writes need it too. */
524 if (!SPECIAL_CONST_P(v)) {
525 rb_gc_writebarrier(envval, v);
526 }
527 VM_FORCE_WRITE(&ep[index], v);
528 }
529 else {
530 /* remember env value forcely */
531 rb_gc_writebarrier_remember(envval);
532 VM_FORCE_WRITE(&ep[index], v);
533 VM_ENV_FLAGS_UNSET(ep, VM_ENV_FLAG_WB_REQUIRED);
534 }
535 RB_DEBUG_COUNTER_INC(lvar_set_slowpath);
536}
537
538// YJIT assumes this function never runs GC
539static inline void
540vm_env_write(const VALUE *ep, int index, VALUE v)
541{
542 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
543 if (LIKELY((flags & VM_ENV_FLAG_WB_REQUIRED) == 0)) {
544 VM_STACK_ENV_WRITE(ep, index, v);
545 }
546 else {
547 vm_env_write_slowpath(ep, index, v);
548 }
549}
550
551void
552rb_vm_env_write(const VALUE *ep, int index, VALUE v)
553{
554 vm_env_write(ep, index, v);
555}
556
557VALUE
558rb_vm_bh_to_procval(const rb_execution_context_t *ec, VALUE block_handler)
559{
560 if (block_handler == VM_BLOCK_HANDLER_NONE) {
561 return Qnil;
562 }
563 else {
564 switch (vm_block_handler_type(block_handler)) {
565 case block_handler_type_iseq:
566 case block_handler_type_ifunc:
567 return rb_vm_make_proc(ec, VM_BH_TO_CAPT_BLOCK(block_handler), rb_cProc);
568 case block_handler_type_symbol:
569 return rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
570 case block_handler_type_proc:
571 return VM_BH_TO_PROC(block_handler);
572 default:
573 VM_UNREACHABLE(rb_vm_bh_to_procval);
574 }
575 }
576}
577
578/* svar */
579
580#if VM_CHECK_MODE > 0
581static int
582vm_svar_valid_p(VALUE svar)
583{
584 if (RB_TYPE_P((VALUE)svar, T_IMEMO)) {
585 switch (imemo_type(svar)) {
586 case imemo_svar:
587 case imemo_cref:
588 case imemo_ment:
589 return TRUE;
590 default:
591 break;
592 }
593 }
594 rb_bug("vm_svar_valid_p: unknown type: %s", rb_obj_info(svar));
595 return FALSE;
596}
597#endif
598
599/* Should this frame's special variables live in the env's svar slot? A SHAREABLE env
600 * (isolated proc) can run in several Ractors at once, making svar shared mutable state
601 * leaking $~/$_ across Ractors: keep them per-EC instead. */
602static inline bool
603lep_svar_in_env_p(const rb_execution_context_t *ec, const VALUE *lep)
604{
605 if (!lep) return false;
606 if (ec == NULL) return true;
607 if (ec->root_lep == lep) return false;
608 /* lep may be a stale on-stack ep of a frame whose env already escaped: lep[0]
609 * still holds the imemo_env, so flags is not a FIXNUM and VM_ENV_ESCAPED_P
610 * asserts. That is not a live shareable proc's env, so fall back to in-env. */
611 if (FIXNUM_P(lep[VM_ENV_DATA_INDEX_FLAGS]) &&
612 VM_ENV_ESCAPED_P(lep) &&
613 RB_FL_TEST_RAW(VM_ENV_ENVVAL(lep), RUBY_FL_SHAREABLE)) {
614 return false;
615 }
616 return true;
617}
618
619static inline struct vm_svar *
620lep_svar(const rb_execution_context_t *ec, const VALUE *lep)
621{
622 VALUE svar;
623
624 if (lep_svar_in_env_p(ec, lep)) {
625 svar = lep[VM_ENV_DATA_INDEX_ME_CREF];
626 }
627 else {
628 svar = ec->root_svar;
629 }
630
631 VM_ASSERT(svar == Qfalse || vm_svar_valid_p(svar));
632
633 return (struct vm_svar *)svar;
634}
635
636static inline void
637lep_svar_write(const rb_execution_context_t *ec, const VALUE *lep, const struct vm_svar *svar)
638{
639 VM_ASSERT(vm_svar_valid_p((VALUE)svar));
640
641 if (lep_svar_in_env_p(ec, lep)) {
642 vm_env_write(lep, VM_ENV_DATA_INDEX_ME_CREF, (VALUE)svar);
643 }
644 else {
645 RB_OBJ_WRITE(rb_ec_thread_ptr(ec)->self, &ec->root_svar, svar);
646 }
647}
648
649static VALUE
650lep_svar_get(const rb_execution_context_t *ec, const VALUE *lep, rb_num_t key)
651{
652 const struct vm_svar *svar = lep_svar(ec, lep);
653
654 if ((VALUE)svar == Qfalse || imemo_type((VALUE)svar) != imemo_svar) return Qnil;
655
656 switch (key) {
657 case VM_SVAR_LASTLINE:
658 return svar->lastline;
659 case VM_SVAR_BACKREF:
660 return svar->backref;
661 default: {
662 const VALUE ary = svar->others;
663
664 if (NIL_P(ary)) {
665 return Qnil;
666 }
667 else {
668 return rb_ary_entry(ary, key - VM_SVAR_EXTRA_START);
669 }
670 }
671 }
672}
673
674static struct vm_svar *
675svar_new(VALUE obj)
676{
677 struct vm_svar *svar = IMEMO_NEW(struct vm_svar, imemo_svar, obj);
678 *((VALUE *)&svar->lastline) = Qnil;
679 *((VALUE *)&svar->backref) = Qnil;
680 *((VALUE *)&svar->others) = Qnil;
681
682 return svar;
683}
684
685static void
686lep_svar_set(const rb_execution_context_t *ec, const VALUE *lep, rb_num_t key, VALUE val)
687{
688 struct vm_svar *svar = lep_svar(ec, lep);
689
690 if ((VALUE)svar == Qfalse || imemo_type((VALUE)svar) != imemo_svar) {
691 lep_svar_write(ec, lep, svar = svar_new((VALUE)svar));
692 }
693
694 switch (key) {
695 case VM_SVAR_LASTLINE:
696 RB_OBJ_WRITE(svar, &svar->lastline, val);
697 return;
698 case VM_SVAR_BACKREF:
699 RB_OBJ_WRITE(svar, &svar->backref, val);
700 return;
701 default: {
702 VALUE ary = svar->others;
703
704 if (NIL_P(ary)) {
705 RB_OBJ_WRITE(svar, &svar->others, ary = rb_ary_new());
706 }
707 rb_ary_store(ary, key - VM_SVAR_EXTRA_START, val);
708 }
709 }
710}
711
712static inline VALUE
713vm_getspecial(const rb_execution_context_t *ec, const VALUE *lep, rb_num_t key, rb_num_t type)
714{
715 VALUE val;
716
717 if (type == 0) {
718 val = lep_svar_get(ec, lep, key);
719 }
720 else {
721 VALUE backref = lep_svar_get(ec, lep, VM_SVAR_BACKREF);
722
723 if (type & 0x01) {
724 switch (type >> 1) {
725 case '&':
726 val = rb_reg_last_match(backref);
727 break;
728 case '`':
729 val = rb_reg_match_pre(backref);
730 break;
731 case '\'':
732 val = rb_reg_match_post(backref);
733 break;
734 case '+':
735 val = rb_reg_match_last(backref);
736 break;
737 default:
738 rb_bug("unexpected back-ref");
739 }
740 }
741 else {
742 val = rb_reg_nth_match((int)(type >> 1), backref);
743 }
744 }
745 return val;
746}
747
748static inline VALUE
749vm_backref_defined(const rb_execution_context_t *ec, const VALUE *lep, rb_num_t type)
750{
751 VALUE backref = lep_svar_get(ec, lep, VM_SVAR_BACKREF);
752 int nth = 0;
753
754 if (type & 0x01) {
755 switch (type >> 1) {
756 case '&':
757 case '`':
758 case '\'':
759 break;
760 case '+':
761 return rb_reg_last_defined(backref);
762 default:
763 rb_bug("unexpected back-ref");
764 }
765 }
766 else {
767 nth = (int)(type >> 1);
768 }
769 return rb_reg_nth_defined(nth, backref);
770}
771
772PUREFUNC(static rb_callable_method_entry_t *check_method_entry(VALUE obj, int can_be_svar));
773static rb_callable_method_entry_t *
774check_method_entry(VALUE obj, int can_be_svar)
775{
776 if (obj == Qfalse) return NULL;
777
778#if VM_CHECK_MODE > 0
779 if (!RB_TYPE_P(obj, T_IMEMO)) rb_bug("check_method_entry: unknown type: %s", rb_obj_info(obj));
780#endif
781
782 switch (imemo_type(obj)) {
783 case imemo_ment:
784 return (rb_callable_method_entry_t *)obj;
785 case imemo_cref:
786 return NULL;
787 case imemo_svar:
788 if (can_be_svar) {
789 return check_method_entry(((struct vm_svar *)obj)->cref_or_me, FALSE);
790 }
791 default:
792#if VM_CHECK_MODE > 0
793 rb_bug("check_method_entry: svar should not be there:");
794#endif
795 return NULL;
796 }
797}
798
799static rb_callable_method_entry_t *
800env_method_entry_unchecked(VALUE obj, int can_be_svar)
801{
802 if (obj == Qfalse) return NULL;
803
804 switch (imemo_type(obj)) {
805 case imemo_ment:
806 return (rb_callable_method_entry_t *)obj;
807 case imemo_cref:
808 return NULL;
809 case imemo_svar:
810 if (can_be_svar) {
811 return env_method_entry_unchecked(((struct vm_svar *)obj)->cref_or_me, FALSE);
812 }
813 default:
814 return NULL;
815 }
816}
817
818const rb_callable_method_entry_t *
819rb_vm_frame_method_entry(const rb_control_frame_t *cfp)
820{
821 const VALUE *ep = cfp->ep;
822 rb_callable_method_entry_t *me;
823
824 while (!VM_ENV_LOCAL_P(ep)) {
825 if ((me = check_method_entry(ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE)) != NULL) return me;
826 ep = VM_ENV_PREV_EP(ep);
827 }
828
829 return check_method_entry(ep[VM_ENV_DATA_INDEX_ME_CREF], TRUE);
830}
831
832const rb_callable_method_entry_t *
833rb_vm_frame_method_entry_unchecked(const rb_control_frame_t *cfp)
834{
835 const VALUE *ep = cfp->ep;
836 rb_callable_method_entry_t *me;
837
838 while (!VM_ENV_LOCAL_P_UNCHECKED(ep)) {
839 if ((me = env_method_entry_unchecked(ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE)) != NULL) return me;
840 ep = VM_ENV_PREV_EP_UNCHECKED(ep);
841 }
842
843 return env_method_entry_unchecked(ep[VM_ENV_DATA_INDEX_ME_CREF], TRUE);
844}
845
846static const rb_iseq_t *
847method_entry_iseqptr(const rb_callable_method_entry_t *me)
848{
849 switch (me->def->type) {
850 case VM_METHOD_TYPE_ISEQ:
851 return me->def->body.iseq.iseqptr;
852 default:
853 return NULL;
854 }
855}
856
857static rb_cref_t *
858method_entry_cref(const rb_callable_method_entry_t *me)
859{
860 switch (me->def->type) {
861 case VM_METHOD_TYPE_ISEQ:
862 return me->def->body.iseq.cref;
863 default:
864 return NULL;
865 }
866}
867
868#if VM_CHECK_MODE == 0
869PUREFUNC(static rb_cref_t *check_cref(VALUE, int));
870#endif
871static rb_cref_t *
872check_cref(VALUE obj, int can_be_svar)
873{
874 if (obj == Qfalse) return NULL;
875
876#if VM_CHECK_MODE > 0
877 if (!RB_TYPE_P(obj, T_IMEMO)) rb_bug("check_cref: unknown type: %s", rb_obj_info(obj));
878#endif
879
880 switch (imemo_type(obj)) {
881 case imemo_ment:
882 return method_entry_cref((rb_callable_method_entry_t *)obj);
883 case imemo_cref:
884 return (rb_cref_t *)obj;
885 case imemo_svar:
886 if (can_be_svar) {
887 return check_cref(((struct vm_svar *)obj)->cref_or_me, FALSE);
888 }
889 default:
890#if VM_CHECK_MODE > 0
891 rb_bug("check_method_entry: svar should not be there:");
892#endif
893 return NULL;
894 }
895}
896
897static inline rb_cref_t *
898vm_env_cref(const VALUE *ep)
899{
900 rb_cref_t *cref;
901
902 while (!VM_ENV_LOCAL_P(ep)) {
903 if ((cref = check_cref(ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE)) != NULL) return cref;
904 ep = VM_ENV_PREV_EP(ep);
905 }
906
907 return check_cref(ep[VM_ENV_DATA_INDEX_ME_CREF], TRUE);
908}
909
910static int
911is_cref(const VALUE v, int can_be_svar)
912{
913 if (RB_TYPE_P(v, T_IMEMO)) {
914 switch (imemo_type(v)) {
915 case imemo_cref:
916 return TRUE;
917 case imemo_svar:
918 if (can_be_svar) return is_cref(((struct vm_svar *)v)->cref_or_me, FALSE);
919 default:
920 break;
921 }
922 }
923 return FALSE;
924}
925
926static int
927vm_env_cref_by_cref(const VALUE *ep)
928{
929 while (!VM_ENV_LOCAL_P(ep)) {
930 if (is_cref(ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE)) return TRUE;
931 ep = VM_ENV_PREV_EP(ep);
932 }
933 return is_cref(ep[VM_ENV_DATA_INDEX_ME_CREF], TRUE);
934}
935
936static rb_cref_t *
937cref_replace_with_duplicated_cref_each_frame(const VALUE *vptr, int can_be_svar, VALUE parent)
938{
939 const VALUE v = *vptr;
940 rb_cref_t *cref, *new_cref;
941
942 if (RB_TYPE_P(v, T_IMEMO)) {
943 switch (imemo_type(v)) {
944 case imemo_cref:
945 cref = (rb_cref_t *)v;
946 new_cref = rb_vm_cref_dup(cref);
947 if (parent) {
948 RB_OBJ_WRITE(parent, vptr, new_cref);
949 }
950 else {
951 VM_FORCE_WRITE(vptr, (VALUE)new_cref);
952 }
953 return (rb_cref_t *)new_cref;
954 case imemo_svar:
955 if (can_be_svar) {
956 return cref_replace_with_duplicated_cref_each_frame(&((struct vm_svar *)v)->cref_or_me, FALSE, v);
957 }
958 /* fall through */
959 case imemo_ment:
960 rb_bug("cref_replace_with_duplicated_cref_each_frame: unreachable");
961 default:
962 break;
963 }
964 }
965 return NULL;
966}
967
968static rb_cref_t *
969vm_cref_replace_with_duplicated_cref(const VALUE *ep)
970{
971 if (vm_env_cref_by_cref(ep)) {
972 rb_cref_t *cref;
973 VALUE envval;
974
975 while (!VM_ENV_LOCAL_P(ep)) {
976 envval = VM_ENV_ESCAPED_P(ep) ? VM_ENV_ENVVAL(ep) : Qfalse;
977 if ((cref = cref_replace_with_duplicated_cref_each_frame(&ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE, envval)) != NULL) {
978 return cref;
979 }
980 ep = VM_ENV_PREV_EP(ep);
981 }
982 envval = VM_ENV_ESCAPED_P(ep) ? VM_ENV_ENVVAL(ep) : Qfalse;
983 return cref_replace_with_duplicated_cref_each_frame(&ep[VM_ENV_DATA_INDEX_ME_CREF], TRUE, envval);
984 }
985 else {
986 rb_bug("vm_cref_dup: unreachable");
987 }
988}
989
990static rb_cref_t *
991vm_get_cref(const VALUE *ep)
992{
993 rb_cref_t *cref = vm_env_cref(ep);
994
995 if (cref != NULL) {
996 return cref;
997 }
998 else {
999 rb_bug("vm_get_cref: unreachable");
1000 }
1001}
1002
1003rb_cref_t *
1004rb_vm_get_cref(const VALUE *ep)
1005{
1006 return vm_get_cref(ep);
1007}
1008
1009static rb_cref_t *
1010vm_ec_cref(const rb_execution_context_t *ec)
1011{
1012 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1013
1014 if (cfp == NULL) {
1015 return NULL;
1016 }
1017 return vm_get_cref(cfp->ep);
1018}
1019
1020static const rb_cref_t *
1021vm_get_const_key_cref(const VALUE *ep)
1022{
1023 const rb_cref_t *cref = vm_get_cref(ep);
1024 const rb_cref_t *key_cref = cref;
1025
1026 while (cref) {
1027 if (CREF_DYNAMIC(cref)) {
1028 return key_cref;
1029 }
1030 cref = CREF_NEXT(cref);
1031 }
1032
1033 /* no dynamic singleton class or cloned class found */
1034 return NULL;
1035}
1036
1037static rb_cref_t *
1038vm_cref_push(const rb_execution_context_t *ec, VALUE klass, const VALUE *ep, int pushed_by_eval, int singleton)
1039{
1040 rb_cref_t *prev_cref = NULL;
1041
1042 if (ep) {
1043 prev_cref = vm_env_cref(ep);
1044 }
1045 else {
1046 rb_control_frame_t *cfp = vm_get_ruby_level_caller_cfp(ec, ec->cfp);
1047
1048 if (cfp) {
1049 prev_cref = vm_env_cref(cfp->ep);
1050 }
1051 }
1052
1053 return vm_cref_new(klass, METHOD_VISI_PUBLIC, FALSE, prev_cref, pushed_by_eval, singleton);
1054}
1055
1056static inline VALUE
1057vm_get_cbase(const VALUE *ep)
1058{
1059 const rb_cref_t *cref = vm_get_cref(ep);
1060
1061 return CREF_CLASS_FOR_DEFINITION(cref);
1062}
1063
1064static inline VALUE
1065vm_get_const_base(const VALUE *ep)
1066{
1067 const rb_cref_t *cref = vm_get_cref(ep);
1068
1069 while (cref) {
1070 if (!CREF_PUSHED_BY_EVAL(cref)) {
1071 return CREF_CLASS_FOR_DEFINITION(cref);
1072 }
1073 cref = CREF_NEXT(cref);
1074 }
1075
1076 return Qundef;
1077}
1078
1079static inline void
1080vm_check_if_namespace(VALUE klass)
1081{
1082 if (!RB_TYPE_P(klass, T_CLASS) && !RB_TYPE_P(klass, T_MODULE)) {
1083 rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a class/module", klass);
1084 }
1085}
1086
1087static inline void
1088vm_ensure_not_refinement_module(VALUE self)
1089{
1090 if (RB_TYPE_P(self, T_MODULE) && FL_TEST(self, RMODULE_IS_REFINEMENT)) {
1091 rb_warn("not defined at the refinement, but at the outer class/module");
1092 }
1093}
1094
1095static inline VALUE
1096vm_get_iclass(const rb_control_frame_t *cfp, VALUE klass)
1097{
1098 return klass;
1099}
1100
1101static inline VALUE
1102vm_get_ev_const(rb_execution_context_t *ec, VALUE orig_klass, ID id, bool allow_nil, int is_defined)
1103{
1104 void rb_const_warn_if_deprecated(const rb_const_entry_t *ce, VALUE klass, ID id);
1105 VALUE val;
1106
1107 if (NIL_P(orig_klass) && allow_nil) {
1108 /* in current lexical scope */
1109 const rb_cref_t *root_cref = vm_get_cref(ec->cfp->ep);
1110 const rb_cref_t *cref;
1111 VALUE klass = Qnil;
1112
1113 while (root_cref && CREF_PUSHED_BY_EVAL(root_cref)) {
1114 root_cref = CREF_NEXT(root_cref);
1115 }
1116 cref = root_cref;
1117 while (cref && CREF_NEXT(cref)) {
1118 if (CREF_PUSHED_BY_EVAL(cref)) {
1119 klass = Qnil;
1120 }
1121 else {
1122 klass = CREF_CLASS(cref);
1123 }
1124 cref = CREF_NEXT(cref);
1125
1126 if (!NIL_P(klass)) {
1127 VALUE av, am = 0;
1128 rb_const_entry_t entry, *ce = &entry;
1129 search_continue:
1130 if (rb_const_lookup_copy(klass, id, ce)) {
1131 val = ce->value;
1132 // before the warning, which may run Ruby: only this stack holds val until the owner has it
1133 if (!is_defined && !UNDEF_P(val) && UNLIKELY(!rb_class_owned_p(klass)) &&
1134 !rb_class_field_shareable_p(klass, val)) {
1135 rb_const_warn_if_deprecated(ce, klass, id);
1136 rb_const_unshareable_error(klass, id);
1137 }
1138 rb_const_warn_if_deprecated(ce, klass, id);
1139 if (UNDEF_P(val)) {
1140 if (am == klass) break;
1141 am = klass;
1142 if (is_defined) return 1;
1143 if (rb_autoloading_value(klass, id, &av, NULL)) {
1144 if (UNLIKELY(!rb_class_owned_p(klass)) && !rb_class_field_shareable_p(klass, av)) {
1145 rb_const_unshareable_error(klass, id);
1146 }
1147 return av;
1148 }
1149 rb_autoload_load(klass, id);
1150 goto search_continue;
1151 }
1152 else {
1153 if (is_defined) {
1154 return 1;
1155 }
1156 else {
1157 return val;
1158 }
1159 }
1160 }
1161 }
1162 }
1163
1164 /* search self */
1165 if (root_cref && !NIL_P(CREF_CLASS(root_cref))) {
1166 klass = vm_get_iclass(ec->cfp, CREF_CLASS(root_cref));
1167 }
1168 else {
1169 klass = CLASS_OF(ec->cfp->self);
1170 }
1171
1172 if (is_defined) {
1173 return rb_const_defined(klass, id);
1174 }
1175 else {
1176 return rb_const_get(klass, id);
1177 }
1178 }
1179 else {
1180 vm_check_if_namespace(orig_klass);
1181 if (is_defined) {
1182 return rb_public_const_defined_from(orig_klass, id);
1183 }
1184 else {
1185 return rb_public_const_get_from(orig_klass, id);
1186 }
1187 }
1188}
1189
1190VALUE
1191rb_vm_get_ev_const(rb_execution_context_t *ec, VALUE orig_klass, ID id, VALUE allow_nil)
1192{
1193 return vm_get_ev_const(ec, orig_klass, id, allow_nil == Qtrue, 0);
1194}
1195
1196static inline VALUE
1197vm_get_ev_const_chain(rb_execution_context_t *ec, const rb_const_path_t const_path)
1198{
1199 VALUE val = Qnil;
1200 int allow_nil = TRUE;
1201
1202 if (const_path_absolute_p(const_path)) {
1203 val = rb_cObject;
1204 allow_nil = FALSE;
1205 }
1206 size_t i = 0;
1207 ID segment;
1208 while ((segment = const_path_aref(const_path, i))) {
1209 val = vm_get_ev_const(ec, val, segment, allow_nil, 0);
1210 allow_nil = FALSE;
1211 i++;
1212 }
1213 return val;
1214}
1215
1216
1217static inline VALUE
1218vm_get_cvar_base(const rb_cref_t *cref, const rb_control_frame_t *cfp, int top_level_raise)
1219{
1220 VALUE klass;
1221
1222 if (!cref) {
1223 rb_bug("vm_get_cvar_base: no cref");
1224 }
1225
1226 while (CREF_NEXT(cref) &&
1227 (NIL_P(CREF_CLASS(cref)) || RCLASS_SINGLETON_P(CREF_CLASS(cref)) ||
1228 CREF_PUSHED_BY_EVAL(cref) || CREF_SINGLETON(cref))) {
1229 cref = CREF_NEXT(cref);
1230 }
1231 if (top_level_raise && !CREF_NEXT(cref)) {
1232 rb_raise(rb_eRuntimeError, "class variable access from toplevel");
1233 }
1234
1235 klass = vm_get_iclass(cfp, CREF_CLASS(cref));
1236
1237 if (NIL_P(klass)) {
1238 rb_raise(rb_eTypeError, "no class variables available");
1239 }
1240 return klass;
1241}
1242
1243#define ractor_incidental_shareable_p(cond, val) \
1244 (!(cond) || rb_ractor_shareable_p(val))
1245#define ractor_object_incidental_shareable_p(obj, val) \
1246 ractor_incidental_shareable_p(rb_ractor_shareable_p(obj), val)
1247
1248ALWAYS_INLINE(static VALUE vm_getivar(VALUE, ID, const rb_iseq_t *, IVC, const struct rb_callcache *, int, VALUE));
1249static inline VALUE
1250vm_getivar(VALUE obj, ID id, const rb_iseq_t *iseq, IVC ic, const struct rb_callcache *cc, int is_attr, VALUE default_value)
1251{
1252 VALUE fields_obj;
1253#if OPT_IC_FOR_IVAR
1254 if (SPECIAL_CONST_P(obj)) {
1255 return default_value;
1256 }
1257
1258 switch (BUILTIN_TYPE(obj)) {
1259 case T_OBJECT:
1260 fields_obj = ROBJECT_FIELDS_OBJ(obj);
1261 break;
1262 case T_CLASS:
1263 case T_MODULE:
1264 {
1265 if (UNLIKELY(!rb_class_owned_p(obj))) {
1266 // For two reasons we can only use the fast path on the Ractor
1267 // that owns the class.
1268 // First, only the owner Ractor is allowed to set ivars on classes
1269 // and modules. So we can skip locking.
1270 // Second, other ractors need to check the shareability of the
1271 // values returned from the class ivars.
1272
1273 if (default_value == Qundef) { // defined?
1274 return rb_ivar_defined(obj, id) ? Qtrue : Qundef;
1275 }
1276 else {
1277 goto general_path;
1278 }
1279 }
1280
1281 fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1282 break;
1283 }
1284 default:
1285 fields_obj = rb_obj_fields(obj, id);
1286 }
1287
1288 if (!fields_obj) {
1289 return default_value;
1290 }
1291
1292 VALUE val = Qundef;
1293
1294 shape_id_t shape_id = RBASIC_SHAPE_ID_FOR_READ(fields_obj);
1295 VALUE *ivar_list = rb_imemo_fields_ptr(fields_obj);
1296
1297 rb_getivar_cache cache = rb_getivar_cache_unpack(vm_cache_attr_index_atomic_read(is_attr, ic, cc));
1298
1299 if (LIKELY(cache.shape_offset == shape_id)) {
1300 if (cache.index == ATTR_INDEX_NOT_SET) {
1301 return default_value;
1302 }
1303
1304 val = ivar_list[cache.index];
1305#if USE_DEBUG_COUNTER
1306 RB_DEBUG_COUNTER_INC(ivar_get_ic_hit);
1307
1308 if (RB_TYPE_P(obj, T_OBJECT)) {
1309 RB_DEBUG_COUNTER_INC(ivar_get_obj_hit);
1310 }
1311#endif
1312 RUBY_ASSERT(!UNDEF_P(val));
1313 }
1314 else { // cache miss case
1315#if USE_DEBUG_COUNTER
1316 if (is_attr) {
1317 if (cache.shape_offset != INVALID_SHAPE_ID) {
1318 RB_DEBUG_COUNTER_INC(ivar_get_cc_miss_set);
1319 }
1320 else {
1321 RB_DEBUG_COUNTER_INC(ivar_get_cc_miss_unset);
1322 }
1323 }
1324 else {
1325 if (cache.shape_offset != INVALID_SHAPE_ID) {
1326 RB_DEBUG_COUNTER_INC(ivar_get_ic_miss_set);
1327 }
1328 else {
1329 RB_DEBUG_COUNTER_INC(ivar_get_ic_miss_unset);
1330 }
1331 }
1332 RB_DEBUG_COUNTER_INC(ivar_get_ic_miss);
1333
1334 if (RB_TYPE_P(obj, T_OBJECT)) {
1335 RB_DEBUG_COUNTER_INC(ivar_get_obj_miss);
1336 }
1337#endif
1338
1339 if (UNLIKELY(rb_shape_complex_p(shape_id))) {
1340 st_table *table = (st_table *)ivar_list;
1341
1342 RUBY_ASSERT(table);
1343 RUBY_ASSERT(table == rb_imemo_fields_complex_tbl(fields_obj));
1344
1345 if (!st_lookup(table, id, &val)) {
1346 val = default_value;
1347 }
1348 }
1349 else {
1350 shape_id_t previous_cached_offset = cache.shape_offset;
1351 if (rb_shape_get_iv_index_with_hint(shape_id, id, &cache.index, &cache.shape_offset)) {
1352 if (cache.shape_offset != previous_cached_offset) {
1353 RUBY_ASSERT(!rb_shape_complex_p(cache.shape_offset));
1354 RUBY_ASSERT(cache.shape_offset != INVALID_SHAPE_ID);
1355
1356 uint64_t packed_cache = rb_getivar_cache_pack(cache.shape_offset, cache.index);
1357 vm_cache_attr_index_set(is_attr, ic, cc, packed_cache);
1358 }
1359
1360 if (cache.index == ATTR_INDEX_NOT_SET) {
1361 val = default_value;
1362 }
1363 else {
1364 // We fetched the ivar list above
1365 val = ivar_list[cache.index];
1366 RUBY_ASSERT(!UNDEF_P(val));
1367 }
1368 }
1369 else {
1370 vm_cache_attr_index_set(is_attr, ic, cc, rb_getivar_cache_pack(shape_id, ATTR_INDEX_NOT_SET));
1371 val = default_value;
1372 }
1373 }
1374 }
1375
1376 if (!UNDEF_P(default_value)) {
1377 RUBY_ASSERT(!UNDEF_P(val));
1378 }
1379
1380 return val;
1381
1382general_path:
1383#endif /* OPT_IC_FOR_IVAR */
1384 RB_DEBUG_COUNTER_INC(ivar_get_ic_miss);
1385
1386 if (is_attr) {
1387 return rb_attr_get(obj, id);
1388 }
1389 else {
1390 return rb_ivar_get(obj, id);
1391 }
1392}
1393
1394ALWAYS_INLINE(static VALUE vm_setivar_slowpath(VALUE obj, ID id, VALUE val, const rb_iseq_t *iseq, IVC ic, const struct rb_callcache *cc, int is_attr));
1395NOINLINE(static VALUE vm_setivar_slowpath_ivar(VALUE obj, ID id, VALUE val, const rb_iseq_t *iseq, IVC ic));
1396NOINLINE(static VALUE vm_setivar_slowpath_attr(VALUE obj, ID id, VALUE val, const struct rb_callcache *cc));
1397
1398static VALUE
1399vm_setivar_slowpath(VALUE obj, ID id, VALUE val, const rb_iseq_t *iseq, IVC ic, const struct rb_callcache *cc, int is_attr)
1400{
1401#if OPT_IC_FOR_IVAR
1402 RB_DEBUG_COUNTER_INC(ivar_set_ic_miss);
1403
1404 rb_check_ivar_modifiable(obj);
1405
1406 shape_id_t previous_shape_id = RBASIC_SHAPE_ID(obj);
1407 attr_index_t index = rb_ivar_set_index(obj, id, val);
1408 shape_id_t next_shape_id = RBASIC_SHAPE_ID(obj);
1409
1410 if (!rb_shape_complex_p(next_shape_id)) {
1411 uint64_t packed_cache = rb_setivar_cache_pack(RSHAPE_OFFSET(previous_shape_id), RSHAPE_OFFSET(next_shape_id), index);
1412 vm_cache_attr_index_set(is_attr, ic, cc, packed_cache);
1413 }
1414
1415 RB_DEBUG_COUNTER_INC(ivar_set_obj_miss);
1416 return val;
1417#else
1418 return rb_ivar_set(obj, id, val);
1419#endif
1420}
1421
1422static VALUE
1423vm_setivar_slowpath_ivar(VALUE obj, ID id, VALUE val, const rb_iseq_t *iseq, IVC ic)
1424{
1425 return vm_setivar_slowpath(obj, id, val, iseq, ic, NULL, false);
1426}
1427
1428static VALUE
1429vm_setivar_slowpath_attr(VALUE obj, ID id, VALUE val, const struct rb_callcache *cc)
1430{
1431 return vm_setivar_slowpath(obj, id, val, NULL, NULL, cc, true);
1432}
1433
1434NOINLINE(static VALUE vm_setivar_class(VALUE obj, VALUE val, rb_setivar_cache cache));
1435static VALUE
1436vm_setivar_class(VALUE obj, VALUE val, rb_setivar_cache cache)
1437{
1438 if (UNLIKELY(!rb_class_owned_p(obj))) {
1439 return Qundef;
1440 }
1441
1442 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
1443 if (UNLIKELY(!fields_obj)) {
1444 return Qundef;
1445 }
1446
1447 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
1448 shape_id_t dest_shape_id = rb_setivar_cache_revalidate(shape_id, RBASIC_SHAPE_ID(fields_obj), cache);
1449 if (UNLIKELY(dest_shape_id == INVALID_SHAPE_ID)) {
1450 return Qundef;
1451 }
1452
1453 RB_OBJ_WRITE(fields_obj, &rb_imemo_fields_ptr(fields_obj)[cache.index], val);
1454
1455 if (shape_id != dest_shape_id) {
1456 RBASIC_SET_SHAPE_ID_ATOMIC(obj, dest_shape_id);
1457 RBASIC_SET_SHAPE_ID(fields_obj, dest_shape_id);
1458 }
1459
1460 RB_DEBUG_COUNTER_INC(ivar_set_ic_hit);
1461
1462 return val;
1463}
1464
1465NOINLINE(static VALUE vm_setivar_default(VALUE obj, ID id, VALUE val, rb_setivar_cache cache));
1466static VALUE
1467vm_setivar_default(VALUE obj, ID id, VALUE val, rb_setivar_cache cache)
1468{
1469 VALUE fields_obj = rb_obj_fields(obj, id);
1470 if (UNLIKELY(!fields_obj)) {
1471 return Qundef;
1472 }
1473
1474 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1475 shape_id_t dest_shape_id = rb_setivar_cache_revalidate(shape_id, RBASIC_SHAPE_ID(fields_obj), cache);
1476 if (UNLIKELY(dest_shape_id == INVALID_SHAPE_ID)) {
1477 return Qundef;
1478 }
1479
1480 RB_OBJ_WRITE(fields_obj, &rb_imemo_fields_ptr(fields_obj)[cache.index], val);
1481
1482 if (shape_id != dest_shape_id) {
1483 RBASIC_SET_SHAPE_ID(obj, dest_shape_id);
1484 RBASIC_SET_SHAPE_ID(fields_obj, dest_shape_id);
1485 }
1486
1487 RB_DEBUG_COUNTER_INC(ivar_set_ic_hit);
1488
1489 return val;
1490}
1491
1492static inline VALUE
1493vm_setivar(VALUE obj, VALUE val, rb_setivar_cache cache)
1494{
1495#if OPT_IC_FOR_IVAR
1496 switch (BUILTIN_TYPE(obj)) {
1497 case T_OBJECT:
1498 {
1499 VM_ASSERT(!rb_ractor_shareable_p(obj) || rb_obj_frozen_p(obj));
1500
1501 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1502 shape_id_t dest_shape_id = rb_setivar_cache_revalidate(shape_id, shape_id, cache);
1503 if (UNLIKELY(dest_shape_id == INVALID_SHAPE_ID)) {
1504 break;
1505 }
1506
1507 VALUE fields_obj = ROBJECT_FIELDS_OBJ(obj);
1508 RB_OBJ_WRITE(fields_obj, &rb_imemo_fields_ptr(fields_obj)[cache.index], val);
1509 if (shape_id != dest_shape_id) {
1510 RBASIC_SET_SHAPE_ID(obj, dest_shape_id);
1511 if (fields_obj != obj) {
1512 RBASIC_SET_SHAPE_ID(fields_obj, dest_shape_id);
1513 }
1514 }
1515
1516 RB_DEBUG_COUNTER_INC(ivar_set_ic_hit);
1517 RB_DEBUG_COUNTER_INC(ivar_set_obj_hit);
1518 return val;
1519 }
1520 break;
1521 case T_CLASS:
1522 case T_MODULE:
1523 RB_DEBUG_COUNTER_INC(ivar_set_ic_miss_noobject);
1524 default:
1525 break;
1526 }
1527
1528 return Qundef;
1529#endif /* OPT_IC_FOR_IVAR */
1530}
1531
1532static VALUE
1533update_classvariable_cache(const rb_iseq_t *iseq, VALUE klass, ID id, const rb_cref_t * cref, ICVARC ic)
1534{
1535 VALUE defined_class = 0;
1536 VALUE cvar_value = rb_cvar_find(klass, id, &defined_class);
1537
1538 if (RB_TYPE_P(defined_class, T_ICLASS)) {
1539 defined_class = RBASIC(defined_class)->klass;
1540 }
1541
1542 VALUE rb_cvc_tbl = RCLASS_CVC_TBL(defined_class);
1543 if (!rb_cvc_tbl) {
1544 rb_bug("the cvc table should be set");
1545 }
1546
1547 VALUE ent_data;
1548 if (!rb_marked_id_table_lookup(rb_cvc_tbl, id, &ent_data)) {
1549 rb_bug("should have cvar cache entry (%s)", rb_id2name(id));
1550 }
1551
1552 struct rb_cvar_class_tbl_entry *ent = (void *)ent_data;
1553
1554 ent->global_cvar_state = GET_GLOBAL_CVAR_STATE();
1555 RB_OBJ_WRITE((VALUE)ent, &ent->cref, cref);
1556 RB_OBJ_WRITE(iseq, &ic->entry, ent);
1557
1558 RUBY_ASSERT(BUILTIN_TYPE((VALUE)cref) == T_IMEMO && IMEMO_TYPE_P(cref, imemo_cref));
1559
1560 return cvar_value;
1561}
1562
1563static inline VALUE
1564vm_getclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *reg_cfp, ID id, ICVARC ic)
1565{
1566 const rb_cref_t *cref;
1567 cref = vm_get_cref(GET_EP());
1568
1569 // The fast path skips rb_cvar_find's checks, so it is for class_value's owner:
1570 // the sole writer, and the only one allowed to see an unshareable value.
1571 if (ic->entry && ic->entry->global_cvar_state == GET_GLOBAL_CVAR_STATE() && ic->entry->cref == cref &&
1572 LIKELY(rb_class_owned_p(ic->entry->class_value))) {
1573 RB_DEBUG_COUNTER_INC(cvar_read_inline_hit);
1574
1575 VALUE v = rb_ivar_lookup(ic->entry->class_value, id, Qundef);
1576 RUBY_ASSERT(!UNDEF_P(v));
1577
1578 return v;
1579 }
1580
1581 VALUE klass = vm_get_cvar_base(cref, reg_cfp, 1);
1582
1583 return update_classvariable_cache(iseq, klass, id, cref, ic);
1584}
1585
1586VALUE
1587rb_vm_getclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *cfp, ID id, ICVARC ic)
1588{
1589 return vm_getclassvariable(iseq, cfp, id, ic);
1590}
1591
1592static inline void
1593vm_setclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *reg_cfp, ID id, VALUE val, ICVARC ic)
1594{
1595 const rb_cref_t *cref;
1596 cref = vm_get_cref(GET_EP());
1597
1598 // as on the read side: the fast path writes straight into class_value
1599 if (ic->entry && ic->entry->global_cvar_state == GET_GLOBAL_CVAR_STATE() && ic->entry->cref == cref &&
1600 LIKELY(rb_class_owned_p(ic->entry->class_value))) {
1601 RB_DEBUG_COUNTER_INC(cvar_write_inline_hit);
1602
1603 rb_class_ivar_set(ic->entry->class_value, id, val);
1604 return;
1605 }
1606
1607 VALUE klass = vm_get_cvar_base(cref, reg_cfp, 1);
1608
1609 rb_cvar_set(klass, id, val);
1610
1611 update_classvariable_cache(iseq, klass, id, cref, ic);
1612}
1613
1614void
1615rb_vm_setclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *cfp, ID id, VALUE val, ICVARC ic)
1616{
1617 vm_setclassvariable(iseq, cfp, id, val, ic);
1618}
1619
1620ALWAYS_INLINE(static VALUE vm_getinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, IVC ic));
1621static inline VALUE
1622vm_getinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, IVC ic)
1623{
1624 return vm_getivar(obj, id, iseq, ic, NULL, FALSE, Qnil);
1625}
1626
1627static inline void
1628vm_setinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, VALUE val, IVC ic)
1629{
1630 if (RB_SPECIAL_CONST_P(obj)) {
1632 return;
1633 }
1634
1635 rb_setivar_cache cache = rb_setivar_cache_unpack(vm_ic_atomic_cache_read(ic));
1636 if (UNLIKELY(UNDEF_P(vm_setivar(obj, val, cache)))) {
1637 switch (BUILTIN_TYPE(obj)) {
1638 case T_OBJECT:
1639 break;
1640 case T_CLASS:
1641 case T_MODULE:
1642 if (!UNDEF_P(vm_setivar_class(obj, val, cache))) {
1643 return;
1644 }
1645 break;
1646 default:
1647 if (!UNDEF_P(vm_setivar_default(obj, id, val, cache))) {
1648 return;
1649 }
1650 }
1651 vm_setivar_slowpath_ivar(obj, id, val, iseq, ic);
1652 }
1653}
1654
1655void
1656rb_vm_setinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, VALUE val, IVC ic)
1657{
1658 vm_setinstancevariable(iseq, obj, id, val, ic);
1659}
1660
1661VALUE
1662rb_vm_getinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, IVC ic)
1663{
1664 return vm_getinstancevariable(iseq, obj, id, ic);
1665}
1666
1667static VALUE
1668vm_throw_continue(const rb_execution_context_t *ec, VALUE err)
1669{
1670 /* continue throw */
1671
1672 if (FIXNUM_P(err)) {
1673 ec->tag->state = RUBY_TAG_FATAL;
1674 }
1675 else if (SYMBOL_P(err)) {
1676 ec->tag->state = TAG_THROW;
1677 }
1678 else if (THROW_DATA_P(err)) {
1679 ec->tag->state = THROW_DATA_STATE((struct vm_throw_data *)err);
1680 }
1681 else {
1682 ec->tag->state = TAG_RAISE;
1683 }
1684 return err;
1685}
1686
1687static VALUE
1688vm_throw_start(const rb_execution_context_t *ec, rb_control_frame_t *const reg_cfp, enum ruby_tag_type state,
1689 const int flag, const VALUE throwobj)
1690{
1691 const rb_control_frame_t *escape_cfp = NULL;
1692 const rb_control_frame_t * const eocfp = RUBY_VM_END_CONTROL_FRAME(ec); /* end of control frame pointer */
1693
1694 if (flag != 0) {
1695 /* do nothing */
1696 }
1697 else if (state == TAG_BREAK) {
1698 int is_orphan = 1;
1699 const VALUE *ep = GET_EP();
1700 const rb_iseq_t *base_iseq = GET_ISEQ();
1701 escape_cfp = reg_cfp;
1702
1703 while (ISEQ_BODY(base_iseq)->type != ISEQ_TYPE_BLOCK) {
1704 if (ISEQ_BODY(CFP_ISEQ(escape_cfp))->type == ISEQ_TYPE_CLASS) {
1705 escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
1706 ep = escape_cfp->ep;
1707 base_iseq = CFP_ISEQ(escape_cfp);
1708 }
1709 else {
1710 ep = VM_ENV_PREV_EP(ep);
1711 base_iseq = ISEQ_BODY(base_iseq)->parent_iseq;
1712 escape_cfp = rb_vm_search_cf_from_ep(ec, escape_cfp, ep);
1713 VM_ASSERT(CFP_ISEQ(escape_cfp) == base_iseq);
1714 }
1715 }
1716
1717 if (VM_FRAME_LAMBDA_P(escape_cfp)) {
1718 /* lambda{... break ...} */
1719 is_orphan = 0;
1720 state = TAG_RETURN;
1721 }
1722 else {
1723 ep = VM_ENV_PREV_EP(ep);
1724
1725 while (escape_cfp < eocfp) {
1726 if (escape_cfp->ep == ep) {
1727 const rb_iseq_t *const iseq = CFP_ISEQ(escape_cfp);
1728 const VALUE epc = CFP_PC(escape_cfp) - ISEQ_BODY(iseq)->iseq_encoded;
1729 const struct iseq_catch_table *const ct = ISEQ_BODY(iseq)->catch_table;
1730 unsigned int i;
1731
1732 if (!ct) break;
1733 for (i=0; i < ct->size; i++) {
1734 const struct iseq_catch_table_entry *const entry =
1735 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1736
1737 if (entry->type == CATCH_TYPE_BREAK &&
1738 entry->iseq == base_iseq &&
1739 entry->start < epc && entry->end >= epc) {
1740 if (entry->cont == epc) { /* found! */
1741 is_orphan = 0;
1742 }
1743 break;
1744 }
1745 }
1746 break;
1747 }
1748
1749 escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
1750 }
1751 }
1752
1753 if (is_orphan) {
1754 rb_vm_localjump_error("break from proc-closure", throwobj, TAG_BREAK);
1755 }
1756 }
1757 else if (state == TAG_RETRY) {
1758 const VALUE *ep = VM_ENV_PREV_EP(GET_EP());
1759
1760 escape_cfp = rb_vm_search_cf_from_ep(ec, reg_cfp, ep);
1761 }
1762 else if (state == TAG_RETURN) {
1763 const VALUE *current_ep = GET_EP();
1764 const VALUE *target_ep = NULL, *target_lep, *ep = current_ep;
1765 int in_class_frame = 0;
1766 int toplevel = 1;
1767 escape_cfp = reg_cfp;
1768
1769 // find target_lep, target_ep
1770 while (!VM_ENV_LOCAL_P(ep)) {
1771 if (VM_ENV_FLAGS(ep, VM_FRAME_FLAG_LAMBDA) && target_ep == NULL) {
1772 target_ep = ep;
1773 }
1774 ep = VM_ENV_PREV_EP(ep);
1775 }
1776 target_lep = ep;
1777
1778 while (escape_cfp < eocfp) {
1779 const VALUE *lep = VM_CF_LEP(escape_cfp);
1780
1781 if (!target_lep) {
1782 target_lep = lep;
1783 }
1784
1785 if (lep == target_lep &&
1786 VM_FRAME_RUBYFRAME_P(escape_cfp) &&
1787 ISEQ_BODY(CFP_ISEQ(escape_cfp))->type == ISEQ_TYPE_CLASS) {
1788 in_class_frame = 1;
1789 target_lep = 0;
1790 }
1791
1792 if (lep == target_lep) {
1793 if (VM_FRAME_LAMBDA_P(escape_cfp)) {
1794 toplevel = 0;
1795 if (in_class_frame) {
1796 /* lambda {class A; ... return ...; end} */
1797 goto valid_return;
1798 }
1799 else {
1800 const VALUE *tep = current_ep;
1801
1802 while (target_lep != tep) {
1803 if (escape_cfp->ep == tep) {
1804 /* in lambda */
1805 if (tep == target_ep) {
1806 goto valid_return;
1807 }
1808 else {
1809 goto unexpected_return;
1810 }
1811 }
1812 tep = VM_ENV_PREV_EP(tep);
1813 }
1814 }
1815 }
1816 else if (VM_FRAME_RUBYFRAME_P(escape_cfp)) {
1817 switch (ISEQ_BODY(CFP_ISEQ(escape_cfp))->type) {
1818 case ISEQ_TYPE_TOP:
1819 case ISEQ_TYPE_MAIN:
1820 if (toplevel) {
1821 if (in_class_frame) goto unexpected_return;
1822 if (target_ep == NULL) {
1823 goto valid_return;
1824 }
1825 else {
1826 goto unexpected_return;
1827 }
1828 }
1829 break;
1830 case ISEQ_TYPE_EVAL: {
1831 const rb_iseq_t *is = CFP_ISEQ(escape_cfp);
1832 enum rb_iseq_type t = ISEQ_BODY(is)->type;
1833 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE || t == ISEQ_TYPE_EVAL) {
1834 if (!(is = ISEQ_BODY(is)->parent_iseq)) break;
1835 t = ISEQ_BODY(is)->type;
1836 }
1837 toplevel = t == ISEQ_TYPE_TOP || t == ISEQ_TYPE_MAIN;
1838 break;
1839 }
1840 case ISEQ_TYPE_CLASS:
1841 toplevel = 0;
1842 break;
1843 default:
1844 break;
1845 }
1846 }
1847 }
1848
1849 if (escape_cfp->ep == target_lep && ISEQ_BODY(CFP_ISEQ(escape_cfp))->type == ISEQ_TYPE_METHOD) {
1850 if (target_ep == NULL) {
1851 goto valid_return;
1852 }
1853 else {
1854 goto unexpected_return;
1855 }
1856 }
1857
1858 escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
1859 }
1860 unexpected_return:;
1861 rb_vm_localjump_error("unexpected return", throwobj, TAG_RETURN);
1862
1863 valid_return:;
1864 /* do nothing */
1865 }
1866 else {
1867 rb_bug("isns(throw): unsupported throw type");
1868 }
1869
1870 ec->tag->state = state;
1871 return (VALUE)THROW_DATA_NEW(throwobj, escape_cfp, state);
1872}
1873
1874static VALUE
1875vm_throw(const rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
1876 rb_num_t throw_state, VALUE throwobj)
1877{
1878 const int state = (int)(throw_state & VM_THROW_STATE_MASK);
1879 const int flag = (int)(throw_state & VM_THROW_NO_ESCAPE_FLAG);
1880
1881 if (state != 0) {
1882 return vm_throw_start(ec, reg_cfp, state, flag, throwobj);
1883 }
1884 else {
1885 return vm_throw_continue(ec, throwobj);
1886 }
1887}
1888
1889// Fallback for YJIT. Prepare throw data and return it.
1890VALUE
1891rb_vm_throw(const rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t throw_state, VALUE throwobj)
1892{
1893 return vm_throw(ec, reg_cfp, throw_state, throwobj);
1894}
1895
1896NORETURN(VALUE rb_zjit_throw(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t throw_state, VALUE throwobj));
1897
1898// Fallback for ZJIT. Make a longjmp and unwind to the most recent vm_exec().
1899VALUE
1900rb_zjit_throw(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t throw_state, VALUE throwobj)
1901{
1902 VALUE val = vm_throw(ec, reg_cfp, throw_state, throwobj);
1903
1904 // vm_throw() has set ec->tag->state. On the longjmp path, vm_exec() reads the throw data from
1905 // ec->errinfo instead of vm_exec_core()'s return value like THROW_EXCEPTION()'s path does, so
1906 // we need to put it there instead.
1907 enum ruby_tag_type state = ec->tag->state;
1908 ec->errinfo = val;
1909 EC_JUMP_TAG(ec, state);
1910
1912}
1913
1914static inline void
1915vm_expandarray(struct rb_control_frame_struct *cfp, VALUE ary, rb_num_t num, int flag)
1916{
1917 int is_splat = flag & 0x01;
1918 const VALUE *ptr;
1919 rb_num_t len;
1920 const VALUE obj = ary;
1921
1922 if (!RB_TYPE_P(ary, T_ARRAY) && NIL_P(ary = rb_check_array_type(ary))) {
1923 ary = obj;
1924 ptr = &ary;
1925 len = 1;
1926 }
1927 else {
1928 ptr = RARRAY_CONST_PTR(ary);
1929 len = (rb_num_t)RARRAY_LEN(ary);
1930 }
1931
1932 if (num + is_splat == 0) {
1933 /* no space left on stack */
1934 }
1935 else if (flag & 0x02) {
1936 /* post: ..., nil ,ary[-1], ..., ary[0..-num] # top */
1937 rb_num_t i = 0, j;
1938
1939 if (len < num) {
1940 for (i = 0; i < num - len; i++) {
1941 *cfp->sp++ = Qnil;
1942 }
1943 }
1944
1945 for (j = 0; i < num; i++, j++) {
1946 VALUE v = ptr[len - j - 1];
1947 *cfp->sp++ = v;
1948 }
1949
1950 if (is_splat) {
1951 *cfp->sp++ = rb_ary_new4(len - j, ptr);
1952 }
1953 }
1954 else {
1955 /* normal: ary[num..-1], ary[num-2], ary[num-3], ..., ary[0] # top */
1956 if (is_splat) {
1957 if (num > len) {
1958 *cfp->sp++ = rb_ary_new();
1959 }
1960 else {
1961 *cfp->sp++ = rb_ary_new4(len - num, ptr + num);
1962 }
1963 }
1964
1965 if (num > len) {
1966 rb_num_t i = 0;
1967 for (; i < num - len; i++) {
1968 *cfp->sp++ = Qnil;
1969 }
1970
1971 for (rb_num_t j = 0; i < num; i++, j++) {
1972 *cfp->sp++ = ptr[len - j - 1];
1973 }
1974 }
1975 else {
1976 for (rb_num_t j = 0; j < num; j++) {
1977 *cfp->sp++ = ptr[num - j - 1];
1978 }
1979 }
1980 }
1981
1982 RB_GC_GUARD(ary);
1983}
1984
1985static VALUE vm_call_general(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling);
1986
1987static VALUE vm_mtbl_dump(VALUE klass, ID target_mid);
1988
1989static struct rb_class_cc_entries *
1990vm_ccs_create(VALUE klass, VALUE cc_tbl, ID mid, const rb_callable_method_entry_t *cme)
1991{
1992 int initial_capa = 2;
1993 struct rb_class_cc_entries *ccs = ruby_xmalloc(vm_ccs_alloc_size(initial_capa));
1994#if VM_CHECK_MODE > 0
1995 ccs->debug_sig = ~(VALUE)ccs;
1996#endif
1997 ccs->capa = initial_capa;
1998 ccs->len = 0;
1999 ccs->cme = cme;
2000 METHOD_ENTRY_CACHED_SET((rb_callable_method_entry_t *)cme);
2001
2002 rb_managed_id_table_insert(cc_tbl, mid, (VALUE)ccs);
2003 RB_OBJ_WRITTEN(cc_tbl, Qundef, cme);
2004 return ccs;
2005}
2006
2007static void
2008vm_ccs_push(VALUE cc_tbl, ID mid, struct rb_class_cc_entries *ccs, const struct rb_callinfo *ci, const struct rb_callcache *cc)
2009{
2010 if (! vm_cc_markable(cc)) {
2011 return;
2012 }
2013
2014 if (UNLIKELY(ccs->len == ccs->capa)) {
2015 RUBY_ASSERT(ccs->capa > 0);
2016 ccs->capa *= 2;
2017 ccs = ruby_xrealloc(ccs, vm_ccs_alloc_size(ccs->capa));
2018#if VM_CHECK_MODE > 0
2019 ccs->debug_sig = ~(VALUE)ccs;
2020#endif
2021 // GC?
2022 rb_managed_id_table_insert(cc_tbl, mid, (VALUE)ccs);
2023 }
2024 VM_ASSERT(ccs->len < ccs->capa);
2025
2026 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(ci);
2027 const int pos = ccs->len++;
2028 ccs->entries[pos].argc = vm_ci_argc(ci);
2029 ccs->entries[pos].flag = (unsigned short)vm_ci_flag(ci);
2030 ccs->entries[pos].kw_len = (unsigned short)(kwarg ? kwarg->keyword_len : 0);
2031 RB_OBJ_WRITE(cc_tbl, &ccs->entries[pos].cc, cc);
2032
2033 if (RB_DEBUG_COUNTER_SETMAX(ccs_maxlen, ccs->len)) {
2034 // for tuning
2035 // vm_mtbl_dump(klass, 0);
2036 }
2037}
2038
2039#if VM_CHECK_MODE > 0
2040void
2041rb_vm_ccs_dump(struct rb_class_cc_entries *ccs)
2042{
2043 ruby_debug_printf("ccs:%p (%d,%d)\n", (void *)ccs, ccs->len, ccs->capa);
2044 for (int i=0; i<ccs->len; i++) {
2045 ruby_debug_printf("CCS CI ID:flag:%x argc:%u kw_len:%u\n",
2046 ccs->entries[i].flag,
2047 ccs->entries[i].argc,
2048 ccs->entries[i].kw_len);
2049 rp(ccs->entries[i].cc);
2050 }
2051}
2052
2053static int
2054vm_ccs_verify(struct rb_class_cc_entries *ccs, ID mid, VALUE klass)
2055{
2056 VM_ASSERT(vm_ccs_p(ccs));
2057 VM_ASSERT(ccs->len <= ccs->capa);
2058
2059 for (int i=0; i<ccs->len; i++) {
2060 const struct rb_callcache *cc = ccs->entries[i].cc;
2061
2062 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
2063 VM_ASSERT(vm_cc_class_check(cc, klass));
2064 VM_ASSERT(vm_cc_check_cme(cc, ccs->cme));
2065 VM_ASSERT(!vm_cc_super_p(cc));
2066 VM_ASSERT(!vm_cc_refinement_p(cc));
2067 }
2068 return TRUE;
2069}
2070#endif
2071
2072const rb_callable_method_entry_t *rb_check_overloaded_cme(const rb_callable_method_entry_t *cme, const struct rb_callinfo * const ci);
2073
2074static void
2075vm_evict_cc(VALUE klass, VALUE cc_tbl, ID mid)
2076{
2077 ASSERT_vm_locking();
2078
2079 if (rb_multi_ractor_p()) {
2080 if (RCLASS_WRITABLE_CC_TBL(klass) != cc_tbl) {
2081 // Another ractor updated the CC table while we were waiting on the VM lock.
2082 // We have to retry.
2083 return;
2084 }
2085
2086 VALUE ccs_obj = 0;
2087 rb_managed_id_table_lookup(cc_tbl, mid, &ccs_obj);
2088 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_obj;
2089
2090 if (!ccs || !METHOD_ENTRY_INVALIDATED(ccs->cme)) {
2091 // Another ractor replaced that entry while we were waiting on the VM lock.
2092 return;
2093 }
2094
2095 VALUE new_table = rb_vm_cc_table_dup(cc_tbl);
2096 rb_vm_cc_table_delete(new_table, mid);
2097 RB_OBJ_ATOMIC_WRITE(klass, &RCLASS_WRITABLE_CC_TBL(klass), new_table);
2098 }
2099 else {
2100 rb_vm_cc_table_delete(cc_tbl, mid);
2101 }
2102}
2103
2104static const struct rb_callcache *
2105vm_populate_cc(VALUE klass, const struct rb_callinfo * const ci, ID mid)
2106{
2107 ASSERT_vm_locking();
2108
2109 RB_DEBUG_COUNTER_INC(cc_not_found_in_ccs);
2110
2111 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, mid);
2112
2113 VM_ASSERT(cme == NULL || IMEMO_TYPE_P(cme, imemo_ment));
2114
2115 if (cme == NULL) {
2116 // undef or not found: can't cache the information
2117 VM_ASSERT(vm_cc_cme(&rb_vm_empty_cc) == NULL);
2118 return &rb_vm_empty_cc;
2119 }
2120
2121 if (UNLIKELY(cme->def->uncached && !(vm_ci_flag(ci) & VM_CALL_SUPER))) {
2122 // Leaving a cc here would cost a ccs and a cc table entry on klass, which
2123 // is usually a metaclass, so the cost would scale with the number of
2124 // classes. invokesuper is excluded because vm_search_super_method needs a
2125 // cme in the cc it gets back.
2126 return &rb_vm_uncached_cc;
2127 }
2128
2129 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
2130 const VALUE original_cc_table = cc_tbl;
2131 if (!cc_tbl) {
2132 // Is this possible after rb_callable_method_entry ?
2133 cc_tbl = rb_vm_cc_table_create(1);
2134 }
2135 else if (rb_multi_ractor_p()) {
2136 cc_tbl = rb_vm_cc_table_dup(cc_tbl);
2137 }
2138
2139 VM_ASSERT(cme == rb_callable_method_entry(klass, mid));
2140
2141 METHOD_ENTRY_CACHED_SET((struct rb_callable_method_entry_struct *)cme);
2142
2143 VM_ASSERT(cc_tbl);
2144
2145 struct rb_class_cc_entries *ccs = NULL;
2146 {
2147 VALUE ccs_obj;
2148 if (UNLIKELY(rb_managed_id_table_lookup(cc_tbl, mid, &ccs_obj))) {
2149 ccs = (struct rb_class_cc_entries *)ccs_obj;
2150 }
2151 else {
2152 // TODO: required?
2153 ccs = vm_ccs_create(klass, cc_tbl, mid, cme);
2154 }
2155 }
2156
2157 cme = rb_check_overloaded_cme(cme, ci);
2158
2159 const struct rb_callcache *cc = vm_cc_new(klass, cme, vm_call_general, cc_type_normal);
2160 vm_ccs_push(cc_tbl, mid, ccs, ci, cc);
2161
2162 VM_ASSERT(vm_cc_cme(cc) != NULL);
2163 VM_ASSERT(cme->called_id == mid);
2164 VM_ASSERT(vm_cc_cme(cc)->called_id == mid);
2165
2166 if (original_cc_table != cc_tbl) {
2167 RB_OBJ_ATOMIC_WRITE(klass, &RCLASS_WRITABLE_CC_TBL(klass), cc_tbl);
2168 }
2169
2170 return cc;
2171}
2172
2173static const struct rb_callcache *
2174vm_lookup_cc(const VALUE klass, const struct rb_callinfo * const ci, ID mid)
2175{
2176 VALUE cc_tbl;
2177 struct rb_class_cc_entries *ccs;
2178retry:
2179 cc_tbl = RUBY_ATOMIC_VALUE_LOAD(RCLASS_WRITABLE_CC_TBL(klass));
2180 ccs = NULL;
2181
2182 if (cc_tbl) {
2183 // CCS data is keyed on method id, so we don't need the method id
2184 // for doing comparisons in the `for` loop below.
2185
2186 VALUE ccs_obj;
2187 if (rb_managed_id_table_lookup(cc_tbl, mid, &ccs_obj)) {
2188 ccs = (struct rb_class_cc_entries *)ccs_obj;
2189 const int ccs_len = ccs->len;
2190
2191 if (UNLIKELY(METHOD_ENTRY_INVALIDATED(ccs->cme))) {
2192 RB_VM_LOCKING() {
2193 vm_evict_cc(klass, cc_tbl, mid);
2194 }
2195 goto retry;
2196 }
2197 else if (UNDEFINED_METHOD_ENTRY_P(ccs->cme)) {
2198 // vm_populate_cc would return the empty cc for this too
2199 return &rb_vm_empty_cc;
2200 }
2201 else {
2202 VM_ASSERT(vm_ccs_verify(ccs, mid, klass));
2203
2204 // We already know the method id is correct because we had
2205 // to look up the ccs_data by method id. We compare argc and
2206 // flag, plus the keyword argument count: two call sites with
2207 // the same argc and flag can still differ in how many of the
2208 // arguments are keywords (e.g. `m(a: 1, b: 2)` vs `m(1, b: 2)`),
2209 // which selects a different argument setup fastpath.
2210 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(ci);
2211 unsigned int argc = vm_ci_argc(ci);
2212 unsigned int flag = vm_ci_flag(ci);
2213 unsigned int kw_len = kwarg ? kwarg->keyword_len : 0;
2214
2215 for (int i=0; i<ccs_len; i++) {
2216 unsigned int ccs_ci_argc = ccs->entries[i].argc;
2217 unsigned int ccs_ci_flag = ccs->entries[i].flag;
2218 unsigned int ccs_ci_kw_len = ccs->entries[i].kw_len;
2219 const struct rb_callcache *ccs_cc = ccs->entries[i].cc;
2220
2221 VM_ASSERT(IMEMO_TYPE_P(ccs_cc, imemo_callcache));
2222
2223 if (ccs_ci_argc == argc && ccs_ci_flag == flag && ccs_ci_kw_len == kw_len) {
2224 RB_DEBUG_COUNTER_INC(cc_found_in_ccs);
2225
2226 VM_ASSERT(vm_cc_cme(ccs_cc)->called_id == mid);
2227 VM_ASSERT(ccs_cc->klass == klass);
2228 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(vm_cc_cme(ccs_cc)));
2229
2230 return ccs_cc;
2231 }
2232 }
2233 }
2234 }
2235 }
2236
2237 RB_GC_GUARD(cc_tbl);
2238 return NULL;
2239}
2240
2241static const struct rb_callcache *
2242vm_search_cc(const VALUE klass, const struct rb_callinfo * const ci)
2243{
2244 const ID mid = vm_ci_mid(ci);
2245
2246 const struct rb_callcache *cc = vm_lookup_cc(klass, ci, mid);
2247 if (cc) {
2248 return cc;
2249 }
2250
2251 RB_VM_LOCKING() {
2252 if (rb_multi_ractor_p()) {
2253 // The CC may have been populated by another ractor while we were waiting on the lock,
2254 // so we must lookup a second time.
2255 cc = vm_lookup_cc(klass, ci, mid);
2256 }
2257
2258 if (!cc) {
2259 cc = vm_populate_cc(klass, ci, mid);
2260 }
2261 }
2262
2263 return cc;
2264}
2265
2266#if VM_CHECK_MODE > 0
2267static bool
2268vm_cc_resolved_p(const struct rb_callcache *cc)
2269{
2270 return cc != &rb_vm_empty_cc && cc != &rb_vm_uncached_cc;
2271}
2272#endif
2273
2274const struct rb_callcache *
2275rb_vm_search_method_slowpath(const struct rb_callinfo *ci, VALUE klass)
2276{
2277 const struct rb_callcache *cc;
2278
2279 VM_ASSERT(!SPECIAL_CONST_P(klass));
2280
2281 if (RB_BUILTIN_TYPE(klass) == T_NONE) {
2282 // If we find a T_NONE here, it's most likely we called CLASS_OF(obj) on a
2283 // garbage collected object (the freelist is stored in the class pointer),
2284 // but it's possible that just the class was GC'd.
2285 // This message intentionally tries to imply the former, but make an
2286 // accurate statement for either case.
2287 rb_bug("attempted to search method '%s' on a garbage collected object",
2288 rb_id2name(vm_ci_mid(ci)));
2289 }
2290
2291 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
2292
2293 cc = vm_search_cc(klass, ci);
2294
2295 VM_ASSERT(cc);
2296 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
2297 VM_ASSERT(!vm_cc_resolved_p(cc) || cc->klass == klass);
2298 VM_ASSERT(!vm_cc_resolved_p(cc) || callable_method_entry_p(vm_cc_cme(cc)));
2299 VM_ASSERT(!vm_cc_resolved_p(cc) || !METHOD_ENTRY_INVALIDATED(vm_cc_cme(cc)));
2300 VM_ASSERT(!vm_cc_resolved_p(cc) || vm_cc_cme(cc)->called_id == vm_ci_mid(ci));
2301
2302 return cc;
2303}
2304
2305#if VM_CHECK_MODE > 0
2306static bool
2307vm_cd_owned_by_iseq_p(const struct rb_call_data *cd, const rb_iseq_t *iseq)
2308{
2309 const struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
2310 return cd >= body->call_data && cd < body->call_data + body->ci_size;
2311}
2312#endif
2313
2314static const struct rb_callcache *
2315vm_search_method_slowpath0(VALUE cd_owner, struct rb_call_data *cd, VALUE klass)
2316{
2317#if USE_DEBUG_COUNTER
2318 const struct rb_callcache *old_cc = cd->cc;
2319 const rb_callable_method_entry_t *const old_cme = old_cc ? old_cc->cme_ : NULL;
2320#endif
2321
2322 const struct rb_callcache *cc = rb_vm_search_method_slowpath(cd->ci, klass);
2323 const rb_callable_method_entry_t *const new_cme = vm_cc_cme(cc);
2324
2325#if OPT_INLINE_METHOD_CACHE
2326 cd->cc = cc;
2327
2328 if (cd_owner && vm_cc_markable(cc)) {
2329 // invokesuper dispatches with a cd on the machine stack, which has no
2330 // owning iseq to remember and keeps its ci out of the GC.
2331 VM_ASSERT(!vm_ci_markable(cd->ci) ||
2332 vm_cd_owned_by_iseq_p(cd, (const rb_iseq_t *)cd_owner));
2333 RB_OBJ_WRITTEN(cd_owner, Qundef, cc);
2334 }
2335
2336#if USE_DEBUG_COUNTER
2337 const struct rb_callcache *empty_cc = &rb_vm_empty_cc;
2338 if (!old_cc || old_cc == empty_cc) {
2339 // empty
2340 RB_DEBUG_COUNTER_INC(mc_inline_miss_empty);
2341 }
2342 else if (old_cc == cc) {
2343 RB_DEBUG_COUNTER_INC(mc_inline_miss_same_cc);
2344 }
2345 else if (old_cme == new_cme) {
2346 RB_DEBUG_COUNTER_INC(mc_inline_miss_same_cme);
2347 }
2348 else if (old_cme && new_cme && old_cme->def == new_cme->def) {
2349 RB_DEBUG_COUNTER_INC(mc_inline_miss_same_def);
2350 }
2351 else {
2352 RB_DEBUG_COUNTER_INC(mc_inline_miss_diff);
2353 }
2354#endif
2355#endif // OPT_INLINE_METHOD_CACHE
2356
2357 if (new_cme) VM_ASSERT(new_cme->called_id == vm_ci_mid(cd->ci));
2358
2359 return cc;
2360}
2361
2362ALWAYS_INLINE(static bool vm_cc_hit_p(const struct rb_callcache *cc, const struct rb_call_data *cd, VALUE klass));
2363static bool
2364vm_cc_hit_p(const struct rb_callcache *cc, const struct rb_call_data *cd, VALUE klass)
2365{
2366 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
2367
2368#if OPT_INLINE_METHOD_CACHE
2369 if (LIKELY(vm_cc_class_check(cc, klass))) {
2370 if (LIKELY(!METHOD_ENTRY_INVALIDATED(vm_cc_cme(cc)))) {
2371 VM_ASSERT(callable_method_entry_p(vm_cc_cme(cc)));
2372 RB_DEBUG_COUNTER_INC(mc_inline_hit);
2373 VM_ASSERT(vm_cc_cme(cc) == NULL || // not found
2374 (vm_ci_flag(cd->ci) & VM_CALL_SUPER) || // search_super w/ define_method
2375 vm_cc_cme(cc)->called_id == vm_ci_mid(cd->ci)); // cme->called_id == ci->mid
2376
2377 return true;
2378 }
2379 RB_DEBUG_COUNTER_INC(mc_inline_miss_invalidated);
2380 }
2381 else {
2382 RB_DEBUG_COUNTER_INC(mc_inline_miss_klass);
2383 }
2384#endif
2385
2386 return false;
2387}
2388
2389ALWAYS_INLINE(static const struct rb_callcache *vm_search_method_fastpath(const struct rb_control_frame_struct *reg_cfp, struct rb_call_data *cd, VALUE klass));
2390static const struct rb_callcache *
2391vm_search_method_fastpath(const struct rb_control_frame_struct *reg_cfp, struct rb_call_data *cd, VALUE klass)
2392{
2393 const struct rb_callcache *cc = cd->cc;
2394 if (vm_cc_hit_p(cc, cd, klass)) {
2395 return cc;
2396 }
2397 if (UNLIKELY(cc == &rb_vm_uncached_cc)) {
2398 return cc;
2399 }
2400
2401 return vm_search_method_slowpath0((VALUE)CFP_ISEQ(reg_cfp), cd, klass);
2402}
2403
2404static const struct rb_callable_method_entry_struct *
2405vm_search_method(struct rb_control_frame_struct *reg_cfp, struct rb_call_data *cd, VALUE recv)
2406{
2407 VALUE klass = CLASS_OF(recv);
2408 VM_ASSERT(klass != Qfalse);
2409 VM_ASSERT(RBASIC_CLASS(klass) == 0 || rb_obj_is_kind_of(klass, rb_cClass));
2410
2411 const struct rb_callcache *cc = vm_search_method_fastpath(reg_cfp, cd, klass);
2412 return vm_cc_cme(cc);
2413}
2414
2415static VALUE
2416vm_call_uncached(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
2417{
2418 RB_DEBUG_COUNTER_INC(ccf_uncached);
2419
2420 const struct rb_call_data *cd = calling->cd;
2421 const VALUE klass = CLASS_OF(calling->recv);
2422 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, vm_ci_mid(cd->ci));
2423
2424 if (UNLIKELY(cme == NULL || !cme->def->uncached)) {
2425 // A site that saw an uncached method is now seeing a receiver whose
2426 // method is cacheable, so let it have a real cc after all.
2427 const struct rb_callcache *cc =
2428 vm_search_method_slowpath0((VALUE)CFP_ISEQ(cfp), (struct rb_call_data *)cd, klass);
2429 calling->cc = cc;
2430 return vm_cc_call(cc)(ec, cfp, calling);
2431 }
2432
2433 calling->cc = &VM_CC_ON_STACK(klass, vm_call_general, {{ 0 }},
2434 rb_check_overloaded_cme(cme, cd->ci));
2435 return vm_call_general(ec, cfp, calling);
2436}
2437
2439rb_zjit_vm_search_method(VALUE cd_owner, struct rb_call_data *cd, VALUE recv)
2440{
2441 // Called from ZJIT with the compile-time iseq, which may differ from
2442 // the iseq on the current CFP.
2443 VALUE klass = CLASS_OF(recv);
2444 const struct rb_callcache *cc = cd->cc;
2445 if (!vm_cc_hit_p(cc, cd, klass)) {
2446 if (UNLIKELY(cc == &rb_vm_uncached_cc)) {
2447 return rb_callable_method_entry(klass, vm_ci_mid(cd->ci));
2448 }
2449 cc = vm_search_method_slowpath0(cd_owner, cd, klass);
2450 }
2451 return vm_cc_cme(cc);
2452}
2453
2454#if __has_attribute(transparent_union)
2455typedef union {
2456 VALUE (*anyargs)(ANYARGS);
2457 VALUE (*f00)(VALUE);
2458 VALUE (*f01)(VALUE, VALUE);
2459 VALUE (*f02)(VALUE, VALUE, VALUE);
2460 VALUE (*f03)(VALUE, VALUE, VALUE, VALUE);
2461 VALUE (*f04)(VALUE, VALUE, VALUE, VALUE, VALUE);
2462 VALUE (*f05)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE);
2463 VALUE (*f06)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE);
2464 VALUE (*f07)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE);
2473 VALUE (*fm1)(int, union { VALUE *x; const VALUE *y; } __attribute__((__transparent_union__)), VALUE);
2474} __attribute__((__transparent_union__)) cfunc_type;
2475# define make_cfunc_type(f) (cfunc_type){.anyargs = (VALUE (*)(ANYARGS))(f)}
2476#else
2477typedef VALUE (*cfunc_type)(ANYARGS);
2478# define make_cfunc_type(f) (cfunc_type)(f)
2479#endif
2480
2481static inline bool
2482cfunc_p(const rb_callable_method_entry_t *me)
2483{
2484 if (! me) {
2485 return false;
2486 }
2487
2488 VM_ASSERT(IMEMO_TYPE_P(me, imemo_ment));
2489 VM_ASSERT(callable_method_entry_p(me));
2490 VM_ASSERT(me->def);
2491
2492 return me->def->type == VM_METHOD_TYPE_CFUNC;
2493}
2494
2495static inline bool
2496cfunc_equal(const rb_callable_method_entry_t *me, cfunc_type func)
2497{
2498 VM_ASSERT(cfunc_p(me));
2499
2500#if __has_attribute(transparent_union)
2501 return me->def->body.cfunc.func == func.anyargs;
2502#else
2503 return me->def->body.cfunc.func == func;
2504#endif
2505}
2506
2507static inline bool
2508check_cfunc(const rb_callable_method_entry_t *me, cfunc_type func)
2509{
2510 return cfunc_p(me) && cfunc_equal(me, func);
2511}
2512
2513static inline int
2514check_method_basic_definition(const rb_callable_method_entry_t *me)
2515{
2516 return me && METHOD_ENTRY_BASIC(me);
2517}
2518
2519static inline int
2520vm_method_cfunc_is(struct rb_control_frame_struct *reg_cfp, CALL_DATA cd, VALUE recv, cfunc_type func)
2521{
2522 VM_ASSERT(reg_cfp != NULL);
2523 const struct rb_callable_method_entry_struct *cme = vm_search_method(reg_cfp, cd, recv);
2524 return check_cfunc(cme, func);
2525}
2526
2527bool
2528rb_zjit_cme_is_cfunc(const rb_callable_method_entry_t *me, const cfunc_type func)
2529{
2530 return check_cfunc(me, func);
2531}
2532
2533int
2534rb_vm_method_cfunc_is(const rb_iseq_t *iseq, CALL_DATA cd, VALUE recv, cfunc_type func)
2535{
2536 const struct rb_callable_method_entry_struct *cme = rb_zjit_vm_search_method((VALUE)iseq, cd, recv);
2537 return check_cfunc(cme, func);
2538}
2539
2540#define check_cfunc(me, func) check_cfunc(me, make_cfunc_type(func))
2541#define vm_method_cfunc_is(reg_cfp, cd, recv, func) vm_method_cfunc_is(reg_cfp, cd, recv, make_cfunc_type(func))
2542
2543#define OP_UNREDEFINED_P(op, t) BASIC_OP_UNREDEFINED_P(BOP_##op, t##_REDEFINED_OP_FLAG)
2544#define EQ_UNREDEFINED_P(t) OP_UNREDEFINED_P(EQ, t)
2545
2546static inline bool
2547FIXNUM_2_P(VALUE a, VALUE b)
2548{
2549 /* FIXNUM_P(a) && FIXNUM_P(b)
2550 * == ((a & 1) && (b & 1))
2551 * == a & b & 1 */
2552 SIGNED_VALUE x = a;
2553 SIGNED_VALUE y = b;
2554 SIGNED_VALUE z = x & y & 1;
2555 return z == 1;
2556}
2557
2558static inline bool
2559FLONUM_2_P(VALUE a, VALUE b)
2560{
2561#if USE_FLONUM
2562 /* FLONUM_P(a) && FLONUM_P(b)
2563 * == ((a & 3) == 2) && ((b & 3) == 2)
2564 * == ! ((a ^ 2) | (b ^ 2) & 3)
2565 */
2566 SIGNED_VALUE x = a;
2567 SIGNED_VALUE y = b;
2568 SIGNED_VALUE z = ((x ^ 2) | (y ^ 2)) & 3;
2569 return !z;
2570#else
2571 return false;
2572#endif
2573}
2574
2575static inline VALUE
2576opt_equality_specialized(VALUE recv, VALUE obj)
2577{
2578 if (recv == obj) {
2579 if (OP_UNREDEFINED_P(EQ, ANY)) {
2580 // Two identical immediate VALUE are always equal
2581 if (SPECIAL_CONST_P(recv)) {
2582 return Qtrue;
2583 }
2584 return Qundef;
2585 }
2586 return Qundef;
2587 }
2588
2589 return opt_equality_specialized_miss(recv, obj);
2590}
2591
2592static VALUE
2593opt_equality(struct rb_control_frame_struct *reg_cfp, VALUE recv, VALUE obj, CALL_DATA cd)
2594{
2595 VM_ASSERT(reg_cfp != NULL);
2596
2597 VALUE val = opt_equality_specialized(recv, obj);
2598 if (!UNDEF_P(val)) return val;
2599
2600 if (!vm_method_cfunc_is(reg_cfp, cd, recv, rb_obj_equal)) {
2601 return Qundef;
2602 }
2603 else {
2604 return RBOOL(recv == obj);
2605 }
2606}
2607
2608static inline const struct rb_callcache *gccct_method_search(rb_execution_context_t *ec, VALUE recv, ID mid, const struct rb_callinfo *ci); // vm_eval.c
2609
2610static inline VALUE
2611opt_equality_by_mid(VALUE recv, VALUE obj, ID mid)
2612{
2613 const struct rb_callcache *cc = gccct_method_search(GET_EC(), recv, mid, &VM_CI_ON_STACK(mid, 0, 1, NULL));
2614
2615 if (cc && check_cfunc(vm_cc_cme(cc), rb_obj_equal)) {
2616 return RBOOL(recv == obj);
2617 }
2618 else {
2619 return RBOOL(RTEST(rb_funcall(recv, mid, 1, obj)));
2620 }
2621}
2622
2623VALUE
2624rb_equal_opt(VALUE obj1, VALUE obj2)
2625{
2626 return opt_equality_by_mid(obj1, obj2, idEq);
2627}
2628
2629VALUE
2630rb_eql_opt(VALUE obj1, VALUE obj2)
2631{
2632 return opt_equality_by_mid(obj1, obj2, idEqlP);
2633}
2634
2635extern VALUE rb_vm_call0(rb_execution_context_t *ec, VALUE, ID, int, const VALUE*, const rb_callable_method_entry_t *, int kw_splat);
2636extern VALUE rb_vm_call_with_refinements(rb_execution_context_t *, VALUE, ID, int, const VALUE *, int);
2637
2638static VALUE
2639check_match(rb_execution_context_t *ec, VALUE pattern, VALUE target, enum vm_check_match_type type)
2640{
2641 switch (type) {
2642 case VM_CHECKMATCH_TYPE_WHEN:
2643 return pattern;
2644 case VM_CHECKMATCH_TYPE_RESCUE:
2645 if (!rb_obj_is_kind_of(pattern, rb_cModule)) {
2646 rb_raise(rb_eTypeError, "class or module required for rescue clause");
2647 }
2648 /* fall through */
2649 case VM_CHECKMATCH_TYPE_CASE: {
2650 return rb_vm_call_with_refinements(ec, pattern, idEqq, 1, &target, RB_NO_KEYWORDS);
2651 }
2652 default:
2653 rb_bug("check_match: unreachable");
2654 }
2655}
2656
2657
2658static inline VALUE
2659double_cmp_lt(double a, double b)
2660{
2661 return RBOOL(a < b);
2662}
2663
2664static inline VALUE
2665double_cmp_le(double a, double b)
2666{
2667 return RBOOL(a <= b);
2668}
2669
2670static inline VALUE
2671double_cmp_gt(double a, double b)
2672{
2673 return RBOOL(a > b);
2674}
2675
2676static inline VALUE
2677double_cmp_ge(double a, double b)
2678{
2679 return RBOOL(a >= b);
2680}
2681
2682// Copied by vm_dump.c
2683static inline VALUE *
2684vm_base_ptr(const rb_control_frame_t *cfp)
2685{
2686 const rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2687
2688 if (CFP_ISEQ(cfp) && VM_FRAME_RUBYFRAME_P(cfp)) {
2689 VALUE *bp = prev_cfp->sp + ISEQ_BODY(CFP_ISEQ(cfp))->local_table_size + VM_ENV_DATA_SIZE;
2690
2691 if (ISEQ_BODY(CFP_ISEQ(cfp))->param.flags.forwardable && VM_ENV_LOCAL_P(cfp->ep)) {
2692 int lts = ISEQ_BODY(CFP_ISEQ(cfp))->local_table_size;
2693 int params = ISEQ_BODY(CFP_ISEQ(cfp))->param.size;
2694
2695 CALL_INFO ci = (CALL_INFO)cfp->ep[-(VM_ENV_DATA_SIZE + (lts - params))]; // skip EP stuff, CI should be last local
2696 bp += vm_ci_argc(ci);
2697 }
2698
2699 if (ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_METHOD || VM_FRAME_BMETHOD_P(cfp)) {
2700 /* adjust `self' */
2701 bp += 1;
2702 }
2703#if VM_DEBUG_BP_CHECK
2704 if (bp != cfp->bp_check) {
2705 ruby_debug_printf("bp_check: %ld, bp: %ld\n",
2706 (long)(cfp->bp_check - GET_EC()->vm_stack),
2707 (long)(bp - GET_EC()->vm_stack));
2708 rb_bug("vm_base_ptr: unreachable");
2709 }
2710#endif
2711 return bp;
2712 }
2713 else {
2714 return NULL;
2715 }
2716}
2717
2718VALUE *
2719rb_vm_base_ptr(const rb_control_frame_t *cfp)
2720{
2721 return vm_base_ptr(cfp);
2722}
2723
2724/* method call processes with call_info */
2725
2726#include "vm_args.c"
2727
2728static inline VALUE vm_call_iseq_setup_2(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling, int opt_pc, unsigned int param_size, unsigned int local_size);
2729ALWAYS_INLINE(static VALUE vm_call_iseq_setup_normal(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling, const rb_callable_method_entry_t *me, int opt_pc, int param_size, int local_size));
2730static inline VALUE vm_call_iseq_setup_tailcall(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling, int opt_pc, unsigned int param_size, unsigned int local_size);
2731static VALUE vm_call_super_method(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling);
2732static VALUE vm_call_method_nome(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling);
2733static VALUE vm_call_method_each_type(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling);
2734static inline VALUE vm_call_method(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling);
2735
2736static vm_call_handler vm_call_iseq_setup_func(const struct rb_callinfo *ci, const int param_size, const int local_size);
2737
2738static VALUE
2739vm_call_iseq_setup_tailcall_0start(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
2740{
2741 RB_DEBUG_COUNTER_INC(ccf_iseq_setup_tailcall_0start);
2742
2743 const struct rb_callcache *cc = calling->cc;
2744 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
2745 unsigned int param = ISEQ_BODY(iseq)->param.size;
2746 unsigned int local = ISEQ_BODY(iseq)->local_table_size;
2747 return vm_call_iseq_setup_tailcall(ec, cfp, calling, 0, param, local);
2748}
2749
2750static VALUE
2751vm_call_iseq_setup_normal_0start(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
2752{
2753 RB_DEBUG_COUNTER_INC(ccf_iseq_setup_0start);
2754
2755 const struct rb_callcache *cc = calling->cc;
2756 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
2757 int param = ISEQ_BODY(iseq)->param.size;
2758 int local = ISEQ_BODY(iseq)->local_table_size;
2759 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), 0, param, local);
2760}
2761
2762bool
2763rb_simple_iseq_p(const rb_iseq_t *iseq)
2764{
2765 return ISEQ_BODY(iseq)->param.flags.has_opt == FALSE &&
2766 ISEQ_BODY(iseq)->param.flags.has_rest == FALSE &&
2767 ISEQ_BODY(iseq)->param.flags.has_post == FALSE &&
2768 ISEQ_BODY(iseq)->param.flags.has_kw == FALSE &&
2769 ISEQ_BODY(iseq)->param.flags.has_kwrest == FALSE &&
2770 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg == FALSE &&
2771 ISEQ_BODY(iseq)->param.flags.forwardable == FALSE &&
2772 ISEQ_BODY(iseq)->param.flags.has_block == FALSE &&
2773 ISEQ_BODY(iseq)->param.flags.accepts_no_block == FALSE;
2774}
2775
2776bool
2777rb_iseq_only_optparam_p(const rb_iseq_t *iseq)
2778{
2779 return ISEQ_BODY(iseq)->param.flags.has_opt == TRUE &&
2780 ISEQ_BODY(iseq)->param.flags.has_rest == FALSE &&
2781 ISEQ_BODY(iseq)->param.flags.has_post == FALSE &&
2782 ISEQ_BODY(iseq)->param.flags.has_kw == FALSE &&
2783 ISEQ_BODY(iseq)->param.flags.has_kwrest == FALSE &&
2784 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg == FALSE &&
2785 ISEQ_BODY(iseq)->param.flags.forwardable == FALSE &&
2786 ISEQ_BODY(iseq)->param.flags.has_block == FALSE &&
2787 ISEQ_BODY(iseq)->param.flags.accepts_no_block == FALSE;
2788}
2789
2790bool
2791rb_iseq_only_kwparam_p(const rb_iseq_t *iseq)
2792{
2793 return ISEQ_BODY(iseq)->param.flags.has_opt == FALSE &&
2794 ISEQ_BODY(iseq)->param.flags.has_rest == FALSE &&
2795 ISEQ_BODY(iseq)->param.flags.has_post == FALSE &&
2796 ISEQ_BODY(iseq)->param.flags.has_kw == TRUE &&
2797 ISEQ_BODY(iseq)->param.flags.has_kwrest == FALSE &&
2798 ISEQ_BODY(iseq)->param.flags.forwardable == FALSE &&
2799 ISEQ_BODY(iseq)->param.flags.has_block == FALSE &&
2800 ISEQ_BODY(iseq)->param.flags.accepts_no_block == FALSE;
2801}
2802
2803#define ALLOW_HEAP_ARGV (-2)
2804#define ALLOW_HEAP_ARGV_KEEP_KWSPLAT (-3)
2805
2806static inline bool
2807vm_caller_setup_arg_splat(rb_control_frame_t *cfp, struct rb_calling_info *calling, VALUE ary, int max_args)
2808{
2809 vm_check_canary(GET_EC(), cfp->sp);
2810 bool ret = false;
2811
2812 if (!NIL_P(ary)) {
2813 const VALUE *ptr = RARRAY_CONST_PTR(ary);
2814 rb_len_t len = RARRAY_LEN(ary);
2815 int argc = calling->argc;
2816
2817 if (UNLIKELY(max_args <= ALLOW_HEAP_ARGV && len + argc > VM_ARGC_STACK_MAX)) {
2818 /* Avoid SystemStackError when splatting large arrays by storing arguments in
2819 * a temporary array, instead of trying to keeping arguments on the VM stack.
2820 */
2821 VALUE *argv = cfp->sp - argc;
2822 VALUE argv_ary = rb_ary_hidden_new(len + argc + 1);
2823 rb_ary_cat(argv_ary, argv, argc);
2824 rb_ary_cat(argv_ary, ptr, len);
2825 cfp->sp -= argc - 1;
2826 cfp->sp[-1] = argv_ary;
2827 calling->argc = 1;
2828 calling->heap_argv = argv_ary;
2829 RB_GC_GUARD(ary);
2830 }
2831 else {
2832 long i;
2833
2834 if (max_args >= 0 && len + argc > max_args) {
2835 /* If only a given max_args is allowed, copy up to max args.
2836 * Used by vm_callee_setup_block_arg for non-lambda blocks,
2837 * where additional arguments are ignored.
2838 *
2839 * Also, copy up to one more argument than the maximum,
2840 * in case it is an empty keyword hash that will be removed.
2841 */
2842 calling->argc += len - (max_args - argc + 1);
2843 len = max_args - argc + 1;
2844 ret = true;
2845 }
2846 else {
2847 /* Unset heap_argv if set originally. Can happen when
2848 * forwarding modified arguments, where heap_argv was used
2849 * originally, but heap_argv not supported by the forwarded
2850 * method in all cases.
2851 */
2852 calling->heap_argv = 0;
2853 }
2854 CHECK_VM_STACK_OVERFLOW(cfp, len);
2855
2856 for (i = 0; i < len; i++) {
2857 *cfp->sp++ = ptr[i];
2858 }
2859 calling->argc += i;
2860 }
2861 }
2862
2863 return ret;
2864}
2865
2866static inline void
2867vm_caller_setup_arg_kw(rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_callinfo *ci)
2868{
2869 const VALUE *const passed_keywords = vm_ci_kwarg(ci)->keywords;
2870 const int kw_len = vm_ci_kwarg(ci)->keyword_len;
2871 const VALUE h = rb_hash_new_capa(kw_len);
2872 VALUE *sp = cfp->sp;
2873 int i;
2874
2875 for (i=0; i<kw_len; i++) {
2876 rb_hash_aset(h, passed_keywords[i], (sp - kw_len)[i]);
2877 }
2878 (sp-kw_len)[0] = h;
2879
2880 cfp->sp -= kw_len - 1;
2881 calling->argc -= kw_len - 1;
2882 calling->kw_splat = 1;
2883}
2884
2885static inline VALUE
2886vm_caller_setup_keyword_hash(const struct rb_callinfo *ci, VALUE keyword_hash)
2887{
2888 if (UNLIKELY(!RB_TYPE_P(keyword_hash, T_HASH))) {
2889 if (keyword_hash != Qnil) {
2890 /* Convert a non-hash keyword splat to a new hash */
2891 keyword_hash = rb_hash_dup(rb_to_hash_type(keyword_hash));
2892 }
2893 }
2894 else if (!IS_ARGS_KW_SPLAT_MUT(ci) && !RHASH_EMPTY_P(keyword_hash)) {
2895 /* Convert a hash keyword splat to a new hash unless
2896 * a mutable keyword splat was passed.
2897 * Skip allocating new hash for empty keyword splat, as empty
2898 * keyword splat will be ignored by both callers.
2899 */
2900 keyword_hash = rb_hash_dup(keyword_hash);
2901 }
2902 return keyword_hash;
2903}
2904
2905static inline void
2906CALLER_SETUP_ARG(struct rb_control_frame_struct *restrict cfp,
2907 struct rb_calling_info *restrict calling,
2908 const struct rb_callinfo *restrict ci, int max_args)
2909{
2910 if (UNLIKELY(IS_ARGS_SPLAT(ci))) {
2911 if (IS_ARGS_KW_SPLAT(ci)) {
2912 // f(*a, **kw)
2913 VM_ASSERT(calling->kw_splat == 1);
2914
2915 cfp->sp -= 2;
2916 calling->argc -= 2;
2917 VALUE ary = cfp->sp[0];
2918 VALUE kwh = vm_caller_setup_keyword_hash(ci, cfp->sp[1]);
2919
2920 // splat a
2921 if (vm_caller_setup_arg_splat(cfp, calling, ary, max_args)) return;
2922
2923 // put kw
2924 if (kwh != Qnil && !RHASH_EMPTY_P(kwh)) {
2925 if (UNLIKELY(calling->heap_argv)) {
2926 rb_ary_push(calling->heap_argv, kwh);
2927 ((struct RHash *)kwh)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
2928 if (max_args != ALLOW_HEAP_ARGV_KEEP_KWSPLAT) {
2929 calling->kw_splat = 0;
2930 }
2931 }
2932 else {
2933 cfp->sp[0] = kwh;
2934 cfp->sp++;
2935 calling->argc++;
2936
2937 VM_ASSERT(calling->kw_splat == 1);
2938 }
2939 }
2940 else {
2941 calling->kw_splat = 0;
2942 }
2943 }
2944 else {
2945 // f(*a)
2946 VM_ASSERT(calling->kw_splat == 0);
2947
2948 cfp->sp -= 1;
2949 calling->argc -= 1;
2950 VALUE ary = cfp->sp[0];
2951
2952 if (vm_caller_setup_arg_splat(cfp, calling, ary, max_args)) {
2953 goto check_keyword;
2954 }
2955
2956 // check the last argument
2957 VALUE last_hash, argv_ary;
2958 if (UNLIKELY(argv_ary = calling->heap_argv)) {
2959 if (!IS_ARGS_KEYWORD(ci) &&
2960 RARRAY_LEN(argv_ary) > 0 &&
2961 RB_TYPE_P((last_hash = rb_ary_last(0, NULL, argv_ary)), T_HASH) &&
2962 (((struct RHash *)last_hash)->basic.flags & RHASH_PASS_AS_KEYWORDS)) {
2963
2964 rb_ary_pop(argv_ary);
2965 if (!RHASH_EMPTY_P(last_hash)) {
2966 rb_ary_push(argv_ary, rb_hash_dup(last_hash));
2967 calling->kw_splat = 1;
2968 }
2969 }
2970 }
2971 else {
2972check_keyword:
2973 if (!IS_ARGS_KEYWORD(ci) &&
2974 calling->argc > 0 &&
2975 RB_TYPE_P((last_hash = cfp->sp[-1]), T_HASH) &&
2976 (((struct RHash *)last_hash)->basic.flags & RHASH_PASS_AS_KEYWORDS)) {
2977
2978 if (RHASH_EMPTY_P(last_hash)) {
2979 calling->argc--;
2980 cfp->sp -= 1;
2981 }
2982 else {
2983 cfp->sp[-1] = rb_hash_dup(last_hash);
2984 calling->kw_splat = 1;
2985 }
2986 }
2987 }
2988 }
2989 }
2990 else if (UNLIKELY(IS_ARGS_KW_SPLAT(ci))) {
2991 // f(**kw)
2992 VM_ASSERT(calling->kw_splat == 1);
2993 VALUE kwh = vm_caller_setup_keyword_hash(ci, cfp->sp[-1]);
2994
2995 if (kwh == Qnil || RHASH_EMPTY_P(kwh)) {
2996 cfp->sp--;
2997 calling->argc--;
2998 calling->kw_splat = 0;
2999 }
3000 else {
3001 cfp->sp[-1] = kwh;
3002 }
3003 }
3004 else if (UNLIKELY(IS_ARGS_KEYWORD(ci))) {
3005 // f(k1:1, k2:2)
3006 VM_ASSERT(calling->kw_splat == 0);
3007
3008 /* This converts VM_CALL_KWARG style to VM_CALL_KW_SPLAT style
3009 * by creating a keyword hash.
3010 * So, vm_ci_flag(ci) & VM_CALL_KWARG is now inconsistent.
3011 */
3012 vm_caller_setup_arg_kw(cfp, calling, ci);
3013 }
3014}
3015
3016#define USE_OPT_HIST 0
3017
3018#if USE_OPT_HIST
3019#define OPT_HIST_MAX 64
3020static int opt_hist[OPT_HIST_MAX+1];
3021
3022__attribute__((destructor))
3023static void
3024opt_hist_show_results_at_exit(void)
3025{
3026 for (int i=0; i<OPT_HIST_MAX; i++) {
3027 ruby_debug_printf("opt_hist\t%d\t%d\n", i, opt_hist[i]);
3028 }
3029}
3030#endif
3031
3032static VALUE
3033vm_call_iseq_setup_normal_opt_start(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3034 struct rb_calling_info *calling)
3035{
3036 const struct rb_callcache *cc = calling->cc;
3037 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3038 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3039 const int opt = calling->argc - lead_num;
3040 const int opt_num = ISEQ_BODY(iseq)->param.opt_num;
3041 const int opt_pc = (int)ISEQ_BODY(iseq)->param.opt_table[opt];
3042 const int param = ISEQ_BODY(iseq)->param.size;
3043 const int local = ISEQ_BODY(iseq)->local_table_size;
3044 const int delta = opt_num - opt;
3045
3046 RB_DEBUG_COUNTER_INC(ccf_iseq_opt);
3047
3048#if USE_OPT_HIST
3049 if (opt_pc < OPT_HIST_MAX) {
3050 opt_hist[opt]++;
3051 }
3052 else {
3053 opt_hist[OPT_HIST_MAX]++;
3054 }
3055#endif
3056
3057 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), opt_pc, param - delta, local);
3058}
3059
3060static VALUE
3061vm_call_iseq_setup_tailcall_opt_start(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3062 struct rb_calling_info *calling)
3063{
3064 const struct rb_callcache *cc = calling->cc;
3065 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3066 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3067 const int opt = calling->argc - lead_num;
3068 const int opt_pc = (int)ISEQ_BODY(iseq)->param.opt_table[opt];
3069 unsigned const int param = ISEQ_BODY(iseq)->param.size;
3070 unsigned const int local = ISEQ_BODY(iseq)->local_table_size;
3071
3072 RB_DEBUG_COUNTER_INC(ccf_iseq_opt);
3073
3074#if USE_OPT_HIST
3075 if (opt_pc < OPT_HIST_MAX) {
3076 opt_hist[opt]++;
3077 }
3078 else {
3079 opt_hist[OPT_HIST_MAX]++;
3080 }
3081#endif
3082
3083 return vm_call_iseq_setup_tailcall(ec, cfp, calling, opt_pc, param, local);
3084}
3085
3086static void
3087args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *const iseq, const rb_callable_method_entry_t *cme,
3088 VALUE *const passed_values, const int passed_keyword_len, const VALUE *const passed_keywords,
3089 VALUE *const locals);
3090
3091static VALUE
3092vm_call_iseq_forwardable(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3093 struct rb_calling_info *calling)
3094{
3095 const struct rb_callcache *cc = calling->cc;
3096 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3097 int param_size = ISEQ_BODY(iseq)->param.size;
3098 int local_size = ISEQ_BODY(iseq)->local_table_size;
3099
3100 // Setting up local size and param size
3101 VM_ASSERT(ISEQ_BODY(iseq)->param.flags.forwardable);
3102
3103 local_size = local_size + vm_ci_argc(calling->cd->ci);
3104 param_size = param_size + vm_ci_argc(calling->cd->ci);
3105
3106 cfp->sp[0] = (VALUE)calling->cd->ci;
3107
3108 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), 0, param_size, local_size);
3109}
3110
3111static VALUE
3112vm_call_iseq_setup_kwparm_kwarg(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3113 struct rb_calling_info *calling)
3114{
3115 const struct rb_callinfo *ci = calling->cd->ci;
3116 const struct rb_callcache *cc = calling->cc;
3117
3118 VM_ASSERT(vm_ci_flag(ci) & VM_CALL_KWARG);
3119 RB_DEBUG_COUNTER_INC(ccf_iseq_kw1);
3120
3121 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3122 const struct rb_iseq_param_keyword *kw_param = ISEQ_BODY(iseq)->param.keyword;
3123 const struct rb_callinfo_kwarg *kw_arg = vm_ci_kwarg(ci);
3124 const int ci_kw_len = kw_arg->keyword_len;
3125 const VALUE * const ci_keywords = kw_arg->keywords;
3126 VALUE *argv = cfp->sp - calling->argc;
3127 VALUE *const klocals = argv + kw_param->bits_start - kw_param->num;
3128 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3129 VALUE * const ci_kws = ALLOCA_N(VALUE, ci_kw_len);
3130 MEMCPY(ci_kws, argv + lead_num, VALUE, ci_kw_len);
3131 args_setup_kw_parameters(ec, iseq, vm_cc_cme(cc), ci_kws, ci_kw_len, ci_keywords, klocals);
3132
3133 int param = ISEQ_BODY(iseq)->param.size;
3134 int local = ISEQ_BODY(iseq)->local_table_size;
3135 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), 0, param, local);
3136}
3137
3138static VALUE
3139vm_call_iseq_setup_kwparm_nokwarg(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3140 struct rb_calling_info *calling)
3141{
3142 const struct rb_callinfo *MAYBE_UNUSED(ci) = calling->cd->ci;
3143 const struct rb_callcache *cc = calling->cc;
3144
3145 VM_ASSERT((vm_ci_flag(ci) & VM_CALL_KWARG) == 0);
3146 RB_DEBUG_COUNTER_INC(ccf_iseq_kw2);
3147
3148 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3149 const struct rb_iseq_param_keyword *kw_param = ISEQ_BODY(iseq)->param.keyword;
3150 VALUE * const argv = cfp->sp - calling->argc;
3151 VALUE * const klocals = argv + kw_param->bits_start - kw_param->num;
3152
3153 int i;
3154 for (i=0; i<kw_param->num; i++) {
3155 klocals[i] = kw_param->default_values[i];
3156 }
3157 klocals[i] = INT2FIX(0); // kw specify flag
3158 // NOTE:
3159 // nobody check this value, but it should be cleared because it can
3160 // points invalid VALUE (T_NONE objects, raw pointer and so on).
3161
3162 int param = ISEQ_BODY(iseq)->param.size;
3163 int local = ISEQ_BODY(iseq)->local_table_size;
3164 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), 0, param, local);
3165}
3166
3167static VALUE builtin_invoker0(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr);
3168
3169static VALUE
3170vm_call_single_noarg_leaf_builtin(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3171 struct rb_calling_info *calling)
3172{
3173 const struct rb_callcache *cc = calling->cc;
3174 // Pairs with the release store in vm_callee_setup_arg so aux_.bf is visible.
3175 vm_call_handler call = (vm_call_handler)(uintptr_t)rbimpl_atomic_ptr_load((void **)&cc->call_, RBIMPL_ATOMIC_ACQUIRE);
3176 if (UNLIKELY(call != vm_call_single_noarg_leaf_builtin)) {
3177 return call(ec, cfp, calling);
3178 }
3179 const struct rb_builtin_function *bf = cc->aux_.bf;
3180 cfp->sp -= (calling->argc + 1);
3181 rb_insn_func_t func_ptr = (rb_insn_func_t)(uintptr_t)bf->func_ptr;
3182 return builtin_invoker0(ec, calling->recv, NULL, func_ptr);
3183}
3184
3185VALUE rb_gen_method_name(VALUE owner, VALUE name); // in vm_backtrace.c
3186
3187static void
3188warn_unused_block(const rb_callable_method_entry_t *cme, const rb_iseq_t *iseq, void *pc)
3189{
3190 rb_vm_t *vm = GET_VM();
3191 set_table *dup_check_table = &vm->unused_block_warning_table;
3192 st_data_t key;
3193 bool strict_unused_block = rb_warning_category_enabled_p(RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK);
3194
3195 union {
3196 VALUE v;
3197 unsigned char b[SIZEOF_VALUE];
3198 } k1 = {
3199 .v = (VALUE)pc,
3200 }, k2 = {
3201 .v = (VALUE)cme->def,
3202 };
3203
3204 // relax check
3205 if (!strict_unused_block) {
3206 key = (st_data_t)cme->def->original_id;
3207
3208 if (set_table_lookup(dup_check_table, key)) {
3209 return;
3210 }
3211 }
3212
3213 // strict check
3214 // make unique key from pc and me->def pointer
3215 key = 0;
3216 for (int i=0; i<SIZEOF_VALUE; i++) {
3217 // fprintf(stderr, "k1:%3d k2:%3d\n", k1.b[i], k2.b[SIZEOF_VALUE-1-i]);
3218 key |= (st_data_t)(k1.b[i] ^ k2.b[SIZEOF_VALUE-1-i]) << (8 * i);
3219 }
3220
3221 if (0) {
3222 fprintf(stderr, "SIZEOF_VALUE:%d\n", SIZEOF_VALUE);
3223 fprintf(stderr, "pc:%p def:%p\n", pc, (void *)cme->def);
3224 fprintf(stderr, "key:%p\n", (void *)key);
3225 }
3226
3227 // duplication check
3228 if (set_insert(dup_check_table, key)) {
3229 // already shown
3230 }
3231 else if (RTEST(ruby_verbose) || strict_unused_block) {
3232 VALUE m_loc = rb_method_entry_location((const rb_method_entry_t *)cme);
3233 VALUE name = rb_gen_method_name(cme->defined_class, rb_iseq_base_label(iseq));
3234
3235 if (!NIL_P(m_loc)) {
3236 rb_warn("the block passed to '%"PRIsVALUE"' defined at %"PRIsVALUE":%"PRIsVALUE" may be ignored",
3237 name, RARRAY_AREF(m_loc, 0), RARRAY_AREF(m_loc, 1));
3238 }
3239 else {
3240 rb_warn("the block may be ignored because '%"PRIsVALUE"' does not use a block", name);
3241 }
3242 }
3243}
3244
3245static inline int
3246vm_callee_setup_arg(rb_execution_context_t *ec, struct rb_calling_info *calling,
3247 const rb_iseq_t *iseq, VALUE *argv, int param_size, int local_size)
3248{
3249 const struct rb_callinfo *ci = calling->cd->ci;
3250 const struct rb_callcache *cc = calling->cc;
3251
3252 VM_ASSERT((vm_ci_argc(ci), 1));
3253 VM_ASSERT(vm_cc_cme(cc) != NULL);
3254
3255 if (UNLIKELY(!ISEQ_BODY(iseq)->param.flags.use_block &&
3256 calling->block_handler != VM_BLOCK_HANDLER_NONE &&
3257 !(vm_ci_flag(calling->cd->ci) & (VM_CALL_OPT_SEND | VM_CALL_SUPER)))) {
3258 warn_unused_block(vm_cc_cme(cc), iseq, (void *)CFP_PC(ec->cfp));
3259 }
3260
3261 if (LIKELY(!(vm_ci_flag(ci) & VM_CALL_KW_SPLAT))) {
3262 if (LIKELY(rb_simple_iseq_p(iseq))) {
3263 rb_control_frame_t *cfp = ec->cfp;
3264 int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3265 CALLER_SETUP_ARG(cfp, calling, ci, lead_num);
3266
3267 if (calling->argc != lead_num) {
3268 argument_arity_error(ec, iseq, vm_cc_cme(cc), calling->argc, lead_num, lead_num);
3269 }
3270
3271 //VM_ASSERT(ci == calling->cd->ci);
3272 VM_ASSERT(cc == calling->cc);
3273
3274 if (vm_call_iseq_optimizable_p(ci, cc)) {
3275 if ((ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_SINGLE_NOARG_LEAF) && ruby_vm_c_events_enabled == 0) {
3276 VM_ASSERT(ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF);
3277 vm_cc_bf_set(cc, (void *)ISEQ_BODY(iseq)->iseq_encoded[1]);
3278 rbimpl_atomic_ptr_store((volatile void **)&cc->call_,
3279 (void *)(uintptr_t)vm_call_single_noarg_leaf_builtin,
3280 RBIMPL_ATOMIC_RELEASE);
3281 }
3282 else {
3283 CC_SET_FASTPATH(cc, vm_call_iseq_setup_func(ci, param_size, local_size), true);
3284 }
3285 }
3286 return 0;
3287 }
3288 else if (rb_iseq_only_optparam_p(iseq)) {
3289 rb_control_frame_t *cfp = ec->cfp;
3290
3291 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3292 const int opt_num = ISEQ_BODY(iseq)->param.opt_num;
3293
3294 CALLER_SETUP_ARG(cfp, calling, ci, lead_num + opt_num);
3295 const int argc = calling->argc;
3296 const int opt = argc - lead_num;
3297
3298 if (opt < 0 || opt > opt_num) {
3299 argument_arity_error(ec, iseq, vm_cc_cme(cc), argc, lead_num, lead_num + opt_num);
3300 }
3301
3302 if (LIKELY(!(vm_ci_flag(ci) & VM_CALL_TAILCALL))) {
3303 CC_SET_FASTPATH(cc, vm_call_iseq_setup_normal_opt_start,
3304 !IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) &&
3305 vm_call_cacheable(ci, cc));
3306 }
3307 else {
3308 CC_SET_FASTPATH(cc, vm_call_iseq_setup_tailcall_opt_start,
3309 !IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) &&
3310 vm_call_cacheable(ci, cc));
3311 }
3312
3313 /* initialize opt vars for self-references */
3314 VM_ASSERT((int)ISEQ_BODY(iseq)->param.size == lead_num + opt_num);
3315 for (int i=argc; i<lead_num + opt_num; i++) {
3316 argv[i] = Qnil;
3317 }
3318 return (int)ISEQ_BODY(iseq)->param.opt_table[opt];
3319 }
3320 else if (rb_iseq_only_kwparam_p(iseq) && !IS_ARGS_SPLAT(ci)) {
3321 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3322 const int argc = calling->argc;
3323 const struct rb_iseq_param_keyword *kw_param = ISEQ_BODY(iseq)->param.keyword;
3324
3325 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3326 const struct rb_callinfo_kwarg *kw_arg = vm_ci_kwarg(ci);
3327
3328 if (argc - kw_arg->keyword_len == lead_num) {
3329 const int ci_kw_len = kw_arg->keyword_len;
3330 const VALUE * const ci_keywords = kw_arg->keywords;
3331 VALUE * const ci_kws = ALLOCA_N(VALUE, ci_kw_len);
3332 MEMCPY(ci_kws, argv + lead_num, VALUE, ci_kw_len);
3333
3334 VALUE *const klocals = argv + kw_param->bits_start - kw_param->num;
3335 args_setup_kw_parameters(ec, iseq, vm_cc_cme(cc), ci_kws, ci_kw_len, ci_keywords, klocals);
3336
3337 CC_SET_FASTPATH(cc, vm_call_iseq_setup_kwparm_kwarg,
3338 vm_call_cacheable(ci, cc));
3339
3340 return 0;
3341 }
3342 }
3343 else if (argc == lead_num) {
3344 /* no kwarg */
3345 VALUE *const klocals = argv + kw_param->bits_start - kw_param->num;
3346 args_setup_kw_parameters(ec, iseq, vm_cc_cme(cc), NULL, 0, NULL, klocals);
3347
3348 if (klocals[kw_param->num] == INT2FIX(0)) {
3349 /* copy from default_values */
3350 CC_SET_FASTPATH(cc, vm_call_iseq_setup_kwparm_nokwarg,
3351 vm_call_cacheable(ci, cc));
3352 }
3353
3354 return 0;
3355 }
3356 }
3357 }
3358
3359 // Called iseq is using ... param
3360 // def foo(...) # <- iseq for foo will have "forwardable"
3361 //
3362 // We want to set the `...` local to the caller's CI
3363 // foo(1, 2) # <- the ci for this should end up as `...`
3364 //
3365 // So hopefully the stack looks like:
3366 //
3367 // => 1
3368 // => 2
3369 // => *
3370 // => **
3371 // => &
3372 // => ... # <- points at `foo`s CI
3373 // => cref_or_me
3374 // => specval
3375 // => type
3376 //
3377 if (ISEQ_BODY(iseq)->param.flags.forwardable) {
3378 bool can_fastpath = true;
3379
3380 if ((vm_ci_flag(ci) & VM_CALL_FORWARDING)) {
3381 struct rb_forwarding_call_data * forward_cd = (struct rb_forwarding_call_data *)calling->cd;
3382 if (vm_ci_argc(ci) != vm_ci_argc(forward_cd->caller_ci)) {
3383 ci = vm_ci_new_runtime(
3384 vm_ci_mid(ci),
3385 vm_ci_flag(ci),
3386 vm_ci_argc(ci),
3387 vm_ci_kwarg(ci));
3388 }
3389 else {
3390 ci = forward_cd->caller_ci;
3391 }
3392 can_fastpath = false;
3393 }
3394 // C functions calling iseqs will stack allocate a CI,
3395 // so we need to convert it to heap allocated
3396 if (!vm_ci_markable(ci)) {
3397 ci = vm_ci_new_runtime(
3398 vm_ci_mid(ci),
3399 vm_ci_flag(ci),
3400 vm_ci_argc(ci),
3401 vm_ci_kwarg(ci));
3402 can_fastpath = false;
3403 }
3404 argv[param_size - 1] = (VALUE)ci;
3405 CC_SET_FASTPATH(cc, vm_call_iseq_forwardable, can_fastpath);
3406 return 0;
3407 }
3408
3409 return setup_parameters_complex(ec, iseq, calling, ci, argv, arg_setup_method);
3410}
3411
3412static void
3413vm_adjust_stack_forwarding(const struct rb_execution_context_struct *ec, struct rb_control_frame_struct *cfp, int argc, VALUE splat)
3414{
3415 // This case is when the caller is using a ... parameter.
3416 // For example `bar(...)`. The call info will have VM_CALL_FORWARDING
3417 // In this case the caller's caller's CI will be on the stack.
3418 //
3419 // For example:
3420 //
3421 // def bar(a, b); a + b; end
3422 // def foo(...); bar(...); end
3423 // foo(1, 2) # <- this CI will be on the stack when we call `bar(...)`
3424 //
3425 // Stack layout will be:
3426 //
3427 // > 1
3428 // > 2
3429 // > CI for foo(1, 2)
3430 // > cref_or_me
3431 // > specval
3432 // > type
3433 // > receiver
3434 // > CI for foo(1, 2), via `getlocal ...`
3435 // > ( SP points here )
3436 const VALUE * lep = VM_CF_LEP(cfp);
3437
3438 const rb_iseq_t *iseq;
3439
3440 // If we're in an escaped environment (lambda for example), get the iseq
3441 // from the captured env.
3442 if (VM_ENV_FLAGS(lep, VM_ENV_FLAG_ESCAPED)) {
3443 rb_env_t * env = (rb_env_t *)lep[VM_ENV_DATA_INDEX_ENV];
3444 iseq = env->iseq;
3445 }
3446 else { // Otherwise use the lep to find the caller
3447 iseq = CFP_ISEQ(rb_vm_search_cf_from_ep(ec, cfp, lep));
3448 }
3449
3450 // Our local storage is below the args we need to copy
3451 int local_size = ISEQ_BODY(iseq)->local_table_size + argc;
3452
3453 const VALUE * from = lep - (local_size + VM_ENV_DATA_SIZE - 1); // 2 for EP values
3454 VALUE * to = cfp->sp - 1; // clobber the CI
3455
3456 if (RTEST(splat)) {
3457 to -= 1; // clobber the splat array
3458 CHECK_VM_STACK_OVERFLOW0(cfp, to, RARRAY_LEN(splat));
3459 MEMCPY(to, RARRAY_CONST_PTR(splat), VALUE, RARRAY_LEN(splat));
3460 to += RARRAY_LEN(splat);
3461 }
3462
3463 CHECK_VM_STACK_OVERFLOW0(cfp, to, argc);
3464 MEMCPY(to, from, VALUE, argc);
3465 cfp->sp = to + argc;
3466
3467 // Stack layout should now be:
3468 //
3469 // > 1
3470 // > 2
3471 // > CI for foo(1, 2)
3472 // > cref_or_me
3473 // > specval
3474 // > type
3475 // > receiver
3476 // > 1
3477 // > 2
3478 // > ( SP points here )
3479}
3480
3481static VALUE
3482vm_call_iseq_setup(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
3483{
3484 RB_DEBUG_COUNTER_INC(ccf_iseq_setup);
3485
3486 const struct rb_callcache *cc = calling->cc;
3487 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3488 unsigned int param_size = ISEQ_BODY(iseq)->param.size;
3489 unsigned int local_size = ISEQ_BODY(iseq)->local_table_size;
3490
3491 RUBY_ASSERT(!ISEQ_BODY(iseq)->param.flags.forwardable);
3492
3493 const int opt_pc = vm_callee_setup_arg(ec, calling, iseq, cfp->sp - calling->argc, param_size, local_size);
3494 return vm_call_iseq_setup_2(ec, cfp, calling, opt_pc, param_size, local_size);
3495}
3496
3497static VALUE
3498vm_call_iseq_fwd_setup(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
3499{
3500 RB_DEBUG_COUNTER_INC(ccf_iseq_setup);
3501
3502 const struct rb_callcache *cc = calling->cc;
3503 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3504 unsigned int param_size = ISEQ_BODY(iseq)->param.size;
3505 unsigned int local_size = ISEQ_BODY(iseq)->local_table_size;
3506
3507 RUBY_ASSERT(ISEQ_BODY(iseq)->param.flags.forwardable);
3508
3509 // Setting up local size and param size
3510 local_size = local_size + vm_ci_argc(calling->cd->ci);
3511 param_size = param_size + vm_ci_argc(calling->cd->ci);
3512
3513 const int opt_pc = vm_callee_setup_arg(ec, calling, iseq, cfp->sp - calling->argc, param_size, local_size);
3514 return vm_call_iseq_setup_2(ec, cfp, calling, opt_pc, param_size, local_size);
3515}
3516
3517static inline VALUE
3518vm_call_iseq_setup_2(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling,
3519 int opt_pc, unsigned int param_size, unsigned int local_size)
3520{
3521 const struct rb_callinfo *ci = calling->cd->ci;
3522 const struct rb_callcache *cc = calling->cc;
3523
3524 if (LIKELY(!(vm_ci_flag(ci) & VM_CALL_TAILCALL))) {
3525 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), opt_pc, param_size, local_size);
3526 }
3527 else {
3528 return vm_call_iseq_setup_tailcall(ec, cfp, calling, opt_pc, param_size, local_size);
3529 }
3530}
3531
3532static inline VALUE
3533vm_call_iseq_setup_normal(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling, const rb_callable_method_entry_t *me,
3534 int opt_pc, int param_size, int local_size)
3535{
3536 const rb_iseq_t *iseq = def_iseq_ptr(me->def);
3537 VALUE *argv = cfp->sp - calling->argc;
3538 VALUE *sp = argv + param_size;
3539 cfp->sp = argv - 1 /* recv */;
3540
3541 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_METHOD | VM_ENV_FLAG_LOCAL, calling->recv,
3542 calling->block_handler, (VALUE)me,
3543 ISEQ_BODY(iseq)->iseq_encoded + opt_pc, sp,
3544 local_size - param_size,
3545 ISEQ_BODY(iseq)->stack_max);
3546 return Qundef;
3547}
3548
3549static inline VALUE
3550vm_call_iseq_setup_tailcall(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling,
3551 int opt_pc, unsigned int param_size, unsigned int local_size)
3552{
3553 const struct rb_callcache *cc = calling->cc;
3554 VALUE *argv = cfp->sp - calling->argc;
3555 const rb_callable_method_entry_t *me = vm_cc_cme(cc);
3556 const rb_iseq_t *iseq = def_iseq_ptr(me->def);
3557 VALUE *src_argv = argv;
3558 VALUE *sp_orig, *sp;
3559 VALUE finish_flag = VM_FRAME_FINISHED_P(cfp) ? VM_FRAME_FLAG_FINISH : 0;
3560
3561 if (VM_BH_FROM_CFP_P(calling->block_handler, cfp)) {
3562 struct rb_captured_block *dst_captured = VM_CFP_TO_CAPTURED_BLOCK(RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
3563 const struct rb_captured_block *src_captured = VM_BH_TO_CAPT_BLOCK(calling->block_handler);
3564 dst_captured->code.val = src_captured->code.val;
3565 if (VM_BH_ISEQ_BLOCK_P(calling->block_handler)) {
3566 calling->block_handler = VM_BH_FROM_ISEQ_BLOCK(dst_captured);
3567 }
3568 else {
3569 calling->block_handler = VM_BH_FROM_IFUNC_BLOCK(dst_captured);
3570 }
3571 }
3572
3573 vm_pop_frame(ec, cfp, cfp->ep);
3574 cfp = ec->cfp;
3575
3576 sp_orig = sp = cfp->sp;
3577
3578 /* push self */
3579 sp[0] = calling->recv;
3580 sp++;
3581
3582 /* copy arguments */
3583 for (unsigned int i = 0; i < param_size; i++) {
3584 *sp++ = src_argv[i];
3585 }
3586
3587 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_METHOD | VM_ENV_FLAG_LOCAL | finish_flag,
3588 calling->recv, calling->block_handler, (VALUE)me,
3589 ISEQ_BODY(iseq)->iseq_encoded + opt_pc, sp,
3590 local_size - param_size,
3591 ISEQ_BODY(iseq)->stack_max);
3592
3593 cfp->sp = sp_orig;
3594
3595 return Qundef;
3596}
3597
3598static void
3599ractor_unsafe_check(void)
3600{
3601 if (!rb_ractor_main_p()) {
3602 rb_raise(rb_eRactorUnsafeError, "ractor unsafe method called from not main ractor");
3603 }
3604}
3605
3606static VALUE
3607call_cfunc_m2(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3608{
3609 ractor_unsafe_check();
3610 VALUE(*f)(VALUE, VALUE) = (VALUE(*)(VALUE, VALUE))func;
3611 return (*f)(recv, rb_ary_new4(argc, argv));
3612}
3613
3614static VALUE
3615call_cfunc_m1(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3616{
3617 ractor_unsafe_check();
3618 VALUE(*f)(int, const VALUE *, VALUE) = (VALUE(*)(int, const VALUE *, VALUE))func;
3619 return (*f)(argc, argv, recv);
3620}
3621
3622static VALUE
3623call_cfunc_0(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3624{
3625 ractor_unsafe_check();
3626 VALUE(*f)(VALUE) = (VALUE(*)(VALUE))func;
3627 return (*f)(recv);
3628}
3629
3630static VALUE
3631call_cfunc_1(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3632{
3633 ractor_unsafe_check();
3634 VALUE(*f)(VALUE, VALUE) = (VALUE(*)(VALUE, VALUE))func;
3635 return (*f)(recv, argv[0]);
3636}
3637
3638static VALUE
3639call_cfunc_2(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3640{
3641 ractor_unsafe_check();
3642 VALUE(*f)(VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE))func;
3643 return (*f)(recv, argv[0], argv[1]);
3644}
3645
3646static VALUE
3647call_cfunc_3(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3648{
3649 ractor_unsafe_check();
3650 VALUE(*f)(VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE))func;
3651 return (*f)(recv, argv[0], argv[1], argv[2]);
3652}
3653
3654static VALUE
3655call_cfunc_4(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3656{
3657 ractor_unsafe_check();
3658 VALUE(*f)(VALUE, VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE, VALUE))func;
3659 return (*f)(recv, argv[0], argv[1], argv[2], argv[3]);
3660}
3661
3662static VALUE
3663call_cfunc_5(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3664{
3665 ractor_unsafe_check();
3666 VALUE(*f)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE))func;
3667 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4]);
3668}
3669
3670static VALUE
3671call_cfunc_6(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3672{
3673 ractor_unsafe_check();
3675 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
3676}
3677
3678static VALUE
3679call_cfunc_7(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3680{
3681 ractor_unsafe_check();
3683 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
3684}
3685
3686static VALUE
3687call_cfunc_8(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3688{
3689 ractor_unsafe_check();
3691 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
3692}
3693
3694static VALUE
3695call_cfunc_9(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3696{
3697 ractor_unsafe_check();
3699 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
3700}
3701
3702static VALUE
3703call_cfunc_10(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3704{
3705 ractor_unsafe_check();
3707 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
3708}
3709
3710static VALUE
3711call_cfunc_11(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3712{
3713 ractor_unsafe_check();
3715 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]);
3716}
3717
3718static VALUE
3719call_cfunc_12(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3720{
3721 ractor_unsafe_check();
3723 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]);
3724}
3725
3726static VALUE
3727call_cfunc_13(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3728{
3729 ractor_unsafe_check();
3731 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12]);
3732}
3733
3734static VALUE
3735call_cfunc_14(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3736{
3737 ractor_unsafe_check();
3739 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13]);
3740}
3741
3742static VALUE
3743call_cfunc_15(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3744{
3745 ractor_unsafe_check();
3747 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14]);
3748}
3749
3750static VALUE
3751ractor_safe_call_cfunc_m2(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3752{
3753 VALUE(*f)(VALUE, VALUE) = (VALUE(*)(VALUE, VALUE))func;
3754 return (*f)(recv, rb_ary_new4(argc, argv));
3755}
3756
3757static VALUE
3758ractor_safe_call_cfunc_m1(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3759{
3760 VALUE(*f)(int, const VALUE *, VALUE) = (VALUE(*)(int, const VALUE *, VALUE))func;
3761 return (*f)(argc, argv, recv);
3762}
3763
3764static VALUE
3765ractor_safe_call_cfunc_0(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3766{
3767 VALUE(*f)(VALUE) = (VALUE(*)(VALUE))func;
3768 return (*f)(recv);
3769}
3770
3771static VALUE
3772ractor_safe_call_cfunc_1(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3773{
3774 VALUE(*f)(VALUE, VALUE) = (VALUE(*)(VALUE, VALUE))func;
3775 return (*f)(recv, argv[0]);
3776}
3777
3778static VALUE
3779ractor_safe_call_cfunc_2(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3780{
3781 VALUE(*f)(VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE))func;
3782 return (*f)(recv, argv[0], argv[1]);
3783}
3784
3785static VALUE
3786ractor_safe_call_cfunc_3(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3787{
3788 VALUE(*f)(VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE))func;
3789 return (*f)(recv, argv[0], argv[1], argv[2]);
3790}
3791
3792static VALUE
3793ractor_safe_call_cfunc_4(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3794{
3795 VALUE(*f)(VALUE, VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE, VALUE))func;
3796 return (*f)(recv, argv[0], argv[1], argv[2], argv[3]);
3797}
3798
3799static VALUE
3800ractor_safe_call_cfunc_5(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3801{
3802 VALUE(*f)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE))func;
3803 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4]);
3804}
3805
3806static VALUE
3807ractor_safe_call_cfunc_6(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3808{
3810 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
3811}
3812
3813static VALUE
3814ractor_safe_call_cfunc_7(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3815{
3817 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
3818}
3819
3820static VALUE
3821ractor_safe_call_cfunc_8(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3822{
3824 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
3825}
3826
3827static VALUE
3828ractor_safe_call_cfunc_9(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3829{
3831 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
3832}
3833
3834static VALUE
3835ractor_safe_call_cfunc_10(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3836{
3838 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
3839}
3840
3841static VALUE
3842ractor_safe_call_cfunc_11(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3843{
3845 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]);
3846}
3847
3848static VALUE
3849ractor_safe_call_cfunc_12(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3850{
3852 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]);
3853}
3854
3855static VALUE
3856ractor_safe_call_cfunc_13(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3857{
3859 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12]);
3860}
3861
3862static VALUE
3863ractor_safe_call_cfunc_14(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3864{
3866 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13]);
3867}
3868
3869static VALUE
3870ractor_safe_call_cfunc_15(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3871{
3873 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14]);
3874}
3875
3876static inline int
3877vm_cfp_consistent_p(rb_execution_context_t *ec, const rb_control_frame_t *reg_cfp)
3878{
3879 const int ov_flags = RAISED_STACKOVERFLOW;
3880 if (LIKELY(reg_cfp == ec->cfp + 1)) return TRUE;
3881 if (rb_ec_raised_p(ec, ov_flags)) {
3882 rb_ec_raised_reset(ec, ov_flags);
3883 return TRUE;
3884 }
3885 return FALSE;
3886}
3887
3888#define CHECK_CFP_CONSISTENCY(func) \
3889 (LIKELY(vm_cfp_consistent_p(ec, reg_cfp)) ? (void)0 : \
3890 rb_bug(func ": cfp consistency error (%p, %p)", (void *)reg_cfp, (void *)(ec->cfp+1)))
3891
3892static inline
3893const rb_method_cfunc_t *
3894vm_method_cfunc_entry(const rb_callable_method_entry_t *me)
3895{
3896#if VM_DEBUG_VERIFY_METHOD_CACHE
3897 switch (me->def->type) {
3898 case VM_METHOD_TYPE_CFUNC:
3899 case VM_METHOD_TYPE_NOTIMPLEMENTED:
3900 break;
3901# define METHOD_BUG(t) case VM_METHOD_TYPE_##t: rb_bug("wrong method type: " #t)
3902 METHOD_BUG(ISEQ);
3903 METHOD_BUG(ATTRSET);
3904 METHOD_BUG(IVAR);
3905 METHOD_BUG(BMETHOD);
3906 METHOD_BUG(ZSUPER);
3907 METHOD_BUG(UNDEF);
3908 METHOD_BUG(OPTIMIZED);
3909 METHOD_BUG(MISSING);
3910 METHOD_BUG(REFINED);
3911 METHOD_BUG(ALIAS);
3912# undef METHOD_BUG
3913 default:
3914 rb_bug("wrong method type: %d", me->def->type);
3915 }
3916#endif
3917 return UNALIGNED_MEMBER_PTR(me->def, body.cfunc);
3918}
3919
3920static VALUE
3921vm_call_cfunc_with_frame_(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling,
3922 int argc, VALUE *argv, VALUE *stack_bottom)
3923{
3924 RB_DEBUG_COUNTER_INC(ccf_cfunc_with_frame);
3925 const struct rb_callinfo *ci = calling->cd->ci;
3926 const struct rb_callcache *cc = calling->cc;
3927 VALUE val;
3928 const rb_callable_method_entry_t *me = vm_cc_cme(cc);
3929 const rb_method_cfunc_t *cfunc = vm_method_cfunc_entry(me);
3930
3931 VALUE recv = calling->recv;
3932 VALUE block_handler = calling->block_handler;
3933 VALUE frame_type = VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL;
3934
3935 if (UNLIKELY(calling->kw_splat)) {
3936 frame_type |= VM_FRAME_FLAG_CFRAME_KW;
3937 }
3938
3939 VM_ASSERT(reg_cfp == ec->cfp);
3940
3941 RUBY_DTRACE_CMETHOD_ENTRY_HOOK(ec, me->owner, me->def->original_id);
3942 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, recv, me->def->original_id, vm_ci_mid(ci), me->owner, Qundef);
3943
3944 vm_push_frame(ec, NULL, frame_type, recv,
3945 block_handler, (VALUE)me,
3946 0, ec->cfp->sp, 0, 0);
3947
3948 int len = cfunc->argc;
3949 if (len >= 0) rb_check_arity(argc, len, len);
3950
3951 reg_cfp->sp = stack_bottom;
3952 val = (*cfunc->invoker)(recv, argc, argv, cfunc->func);
3953
3954 CHECK_CFP_CONSISTENCY("vm_call_cfunc");
3955
3956 rb_vm_pop_frame(ec);
3957
3958 VM_ASSERT(ec->cfp->sp == stack_bottom);
3959
3960 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, recv, me->def->original_id, vm_ci_mid(ci), me->owner, val);
3961 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
3962
3963 return val;
3964}
3965
3966// Push a C method frame for a given cme. This is called when JIT code skipped
3967// pushing a frame but the C method reached a point where a frame is needed.
3968void
3969rb_vm_push_cfunc_frame(const rb_callable_method_entry_t *cme, int recv_idx)
3970{
3971 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_CFUNC);
3972 rb_execution_context_t *ec = GET_EC();
3973 VALUE *sp = ec->cfp->sp;
3974 VALUE recv = *(sp - recv_idx - 1);
3975 VALUE frame_type = VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL;
3976 VALUE block_handler = VM_BLOCK_HANDLER_NONE;
3977#if VM_CHECK_MODE > 0
3978 // Clean up the stack canary since we're about to satisfy the "leaf or lazy push" assumption
3979 *(GET_EC()->cfp->sp) = Qfalse;
3980#endif
3981 vm_push_frame(ec, NULL, frame_type, recv, block_handler, (VALUE)cme, 0, ec->cfp->sp, 0, 0);
3982}
3983
3984// If true, cc->call needs to include `CALLER_SETUP_ARG` (i.e. can't be skipped in fastpath)
3985bool
3986rb_splat_or_kwargs_p(const struct rb_callinfo *restrict ci)
3987{
3988 return IS_ARGS_SPLAT(ci) || IS_ARGS_KW_OR_KW_SPLAT(ci);
3989}
3990
3991static VALUE
3992vm_call_get_kwarg(rb_control_frame_t *reg_cfp, CALL_DATA cd, ID kw)
3993{
3994
3995 VALUE sym = ID2SYM(kw);
3996 if (vm_ci_flag(cd->ci) & VM_CALL_KW_SPLAT) {
3997 VALUE keywords = TOPN(0);
3998 if (UNLIKELY(!RB_TYPE_P(keywords, T_HASH))) {
3999 TOPN(0) = keywords = rb_to_hash_type(keywords);
4000 }
4001 return rb_hash_lookup2(keywords, sym, Qundef);
4002 }
4003 else {
4004 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(cd->ci);
4005 if (kwarg) {
4006 for (int index = 0; index < kwarg->keyword_len; index++) {
4007 if (kwarg->keywords[index] == sym) {
4008 return TOPN(kwarg->keyword_len - index - 1);
4009 }
4010 }
4011 }
4012 }
4013 return Qundef;
4014}
4015
4016static VALUE
4017vm_call_cfunc_with_frame(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4018{
4019 int argc = calling->argc;
4020 VALUE *stack_bottom = reg_cfp->sp - argc - 1;
4021 VALUE *argv = &stack_bottom[1];
4022
4023 return vm_call_cfunc_with_frame_(ec, reg_cfp, calling, argc, argv, stack_bottom);
4024}
4025
4026static VALUE
4027vm_call_cfunc_other(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4028{
4029 const struct rb_callinfo *ci = calling->cd->ci;
4030 RB_DEBUG_COUNTER_INC(ccf_cfunc_other);
4031
4032 CALLER_SETUP_ARG(reg_cfp, calling, ci, ALLOW_HEAP_ARGV_KEEP_KWSPLAT);
4033 VALUE argv_ary;
4034 if (UNLIKELY(argv_ary = calling->heap_argv)) {
4035 VM_ASSERT(!IS_ARGS_KEYWORD(ci));
4036 int argc = RARRAY_LENINT(argv_ary);
4037 VALUE *argv = (VALUE *)RARRAY_CONST_PTR(argv_ary);
4038 VALUE *stack_bottom = reg_cfp->sp - 2;
4039
4040 VM_ASSERT(calling->argc == 1);
4041 VM_ASSERT(RB_TYPE_P(argv_ary, T_ARRAY));
4042 VM_ASSERT(RBASIC_CLASS(argv_ary) == 0); // hidden ary
4043
4044 return vm_call_cfunc_with_frame_(ec, reg_cfp, calling, argc, argv, stack_bottom);
4045 }
4046 else {
4047 CC_SET_FASTPATH(calling->cc, vm_call_cfunc_with_frame, !rb_splat_or_kwargs_p(ci) && !calling->kw_splat && !(vm_ci_flag(ci) & VM_CALL_FORWARDING));
4048
4049 return vm_call_cfunc_with_frame(ec, reg_cfp, calling);
4050 }
4051}
4052
4053static inline VALUE
4054vm_call_cfunc_array_argv(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, int stack_offset, int argc_offset)
4055{
4056 VALUE argv_ary = reg_cfp->sp[-1 - stack_offset];
4057 int argc = RARRAY_LENINT(argv_ary) - argc_offset;
4058
4059 if (UNLIKELY(argc > VM_ARGC_STACK_MAX)) {
4060 return vm_call_cfunc_other(ec, reg_cfp, calling);
4061 }
4062
4063 VALUE *argv = (VALUE *)RARRAY_CONST_PTR(argv_ary);
4064 calling->kw_splat = 0;
4065 int i;
4066 VALUE *stack_bottom = reg_cfp->sp - 2 - stack_offset;
4067 VALUE *sp = stack_bottom;
4068 CHECK_VM_STACK_OVERFLOW(reg_cfp, argc);
4069 for(i = 0; i < argc; i++) {
4070 *++sp = argv[i];
4071 }
4072 reg_cfp->sp = sp+1;
4073
4074 return vm_call_cfunc_with_frame_(ec, reg_cfp, calling, argc, stack_bottom+1, stack_bottom);
4075}
4076
4077static inline VALUE
4078vm_call_cfunc_only_splat(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4079{
4080 RB_DEBUG_COUNTER_INC(ccf_cfunc_only_splat);
4081 VALUE argv_ary = reg_cfp->sp[-1];
4082 int argc = RARRAY_LENINT(argv_ary);
4083 VALUE *argv = (VALUE *)RARRAY_CONST_PTR(argv_ary);
4084 VALUE last_hash;
4085 int argc_offset = 0;
4086
4087 if (UNLIKELY(argc > 0 &&
4088 RB_TYPE_P((last_hash = argv[argc-1]), T_HASH) &&
4089 (((struct RHash *)last_hash)->basic.flags & RHASH_PASS_AS_KEYWORDS))) {
4090 if (!RHASH_EMPTY_P(last_hash)) {
4091 return vm_call_cfunc_other(ec, reg_cfp, calling);
4092 }
4093 argc_offset++;
4094 }
4095 return vm_call_cfunc_array_argv(ec, reg_cfp, calling, 0, argc_offset);
4096}
4097
4098static inline VALUE
4099vm_call_cfunc_only_splat_kw(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4100{
4101 RB_DEBUG_COUNTER_INC(ccf_cfunc_only_splat_kw);
4102 VALUE keyword_hash = reg_cfp->sp[-1];
4103
4104 if (keyword_hash == Qnil || (RB_TYPE_P(keyword_hash, T_HASH) && RHASH_EMPTY_P(keyword_hash))) {
4105 return vm_call_cfunc_array_argv(ec, reg_cfp, calling, 1, 0);
4106 }
4107
4108 return vm_call_cfunc_other(ec, reg_cfp, calling);
4109}
4110
4111static VALUE
4112vm_call_cfunc(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4113{
4114 const struct rb_callinfo *ci = calling->cd->ci;
4115 RB_DEBUG_COUNTER_INC(ccf_cfunc);
4116
4117 if (IS_ARGS_SPLAT(ci) && !(vm_ci_flag(ci) & VM_CALL_FORWARDING)) {
4118 if (!IS_ARGS_KW_SPLAT(ci) && vm_ci_argc(ci) == 1) {
4119 // f(*a)
4120 CC_SET_FASTPATH(calling->cc, vm_call_cfunc_only_splat, TRUE);
4121 return vm_call_cfunc_only_splat(ec, reg_cfp, calling);
4122 }
4123 if (IS_ARGS_KW_SPLAT(ci) && vm_ci_argc(ci) == 2) {
4124 // f(*a, **kw)
4125 CC_SET_FASTPATH(calling->cc, vm_call_cfunc_only_splat_kw, TRUE);
4126 return vm_call_cfunc_only_splat_kw(ec, reg_cfp, calling);
4127 }
4128 }
4129
4130 CC_SET_FASTPATH(calling->cc, vm_call_cfunc_other, TRUE);
4131 return vm_call_cfunc_other(ec, reg_cfp, calling);
4132}
4133
4134static VALUE
4135vm_call_ivar(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4136{
4137 const struct rb_callcache *cc = calling->cc;
4138 RB_DEBUG_COUNTER_INC(ccf_ivar);
4139 cfp->sp -= 1;
4140 VALUE ivar = vm_getivar(calling->recv, vm_cc_cme(cc)->def->body.attr.id, NULL, NULL, cc, TRUE, Qnil);
4141 return ivar;
4142}
4143
4144static VALUE
4145vm_call_attrset_direct(rb_execution_context_t *ec, rb_control_frame_t *cfp, const struct rb_callcache *cc, VALUE obj)
4146{
4147 RB_DEBUG_COUNTER_INC(ccf_attrset);
4148 VALUE val = *(cfp->sp - 1);
4149 cfp->sp -= 2;
4150 rb_setivar_cache cache = rb_setivar_cache_unpack(vm_cc_atomic_cache_read(cc));
4151 ID id = vm_cc_cme(cc)->def->body.attr.id;
4152 rb_check_frozen(obj);
4153 VALUE res = vm_setivar(obj, val, cache);
4154 if (UNDEF_P(res)) {
4155 switch (BUILTIN_TYPE(obj)) {
4156 case T_OBJECT:
4157 break;
4158 case T_CLASS:
4159 case T_MODULE:
4160 {
4161 res = vm_setivar_class(obj, val, cache);
4162 if (!UNDEF_P(res)) {
4163 return res;
4164 }
4165 }
4166 break;
4167 default:
4168 {
4169 res = vm_setivar_default(obj, id, val, cache);
4170 if (!UNDEF_P(res)) {
4171 return res;
4172 }
4173 }
4174 }
4175 res = vm_setivar_slowpath_attr(obj, id, val, cc);
4176 }
4177 return res;
4178}
4179
4180static VALUE
4181vm_call_attrset(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4182{
4183 return vm_call_attrset_direct(ec, cfp, calling->cc, calling->recv);
4184}
4185
4186static inline VALUE
4187vm_call_bmethod_body(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv)
4188{
4189 rb_proc_t *proc;
4190 VALUE val;
4191 const struct rb_callcache *cc = calling->cc;
4192 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4193 VALUE procv = cme->def->body.bmethod.proc;
4194
4195 if (!RB_OBJ_SHAREABLE_P(procv) &&
4196 cme->def->body.bmethod.defined_ractor_id != rb_ec_ractor_id(ec)) {
4197 rb_raise(rb_eRuntimeError, "defined with an un-shareable Proc in a different Ractor");
4198 }
4199
4200 /* control block frame */
4201 GetProcPtr(procv, proc);
4202 val = vm_invoke_bmethod(ec, proc, calling->recv, CALLING_ARGC(calling), argv, calling->kw_splat, calling->block_handler, vm_cc_cme(cc));
4203
4204 return val;
4205}
4206
4207static int vm_callee_setup_block_arg(rb_execution_context_t *ec, struct rb_calling_info *calling, const struct rb_callinfo *ci, const rb_iseq_t *iseq, VALUE *argv, const enum arg_setup_type arg_setup_type);
4208
4209static VALUE
4210vm_call_iseq_bmethod(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4211{
4212 RB_DEBUG_COUNTER_INC(ccf_iseq_bmethod);
4213
4214 const struct rb_callcache *cc = calling->cc;
4215 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4216 VALUE procv = cme->def->body.bmethod.proc;
4217
4218 if (!RB_OBJ_SHAREABLE_P(procv) &&
4219 cme->def->body.bmethod.defined_ractor_id != rb_ec_ractor_id(ec)) {
4220 rb_raise(rb_eRuntimeError, "defined with an un-shareable Proc in a different Ractor");
4221 }
4222
4223 rb_proc_t *proc;
4224 GetProcPtr(procv, proc);
4225 const struct rb_block *block = &proc->block;
4226
4227 while (vm_block_type(block) == block_type_proc) {
4228 block = vm_proc_block(block->as.proc);
4229 }
4230 VM_ASSERT(vm_block_type(block) == block_type_iseq);
4231
4232 const struct rb_captured_block *captured = &block->as.captured;
4233 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
4234 VALUE * const argv = cfp->sp - calling->argc;
4235 const int arg_size = ISEQ_BODY(iseq)->param.size;
4236
4237 int opt_pc;
4238 if (vm_ci_flag(calling->cd->ci) & VM_CALL_ARGS_SIMPLE) {
4239 opt_pc = vm_callee_setup_block_arg(ec, calling, calling->cd->ci, iseq, argv, arg_setup_method);
4240 }
4241 else {
4242 opt_pc = setup_parameters_complex(ec, iseq, calling, calling->cd->ci, argv, arg_setup_method);
4243 }
4244
4245 cfp->sp = argv - 1; // -1 for the receiver
4246
4247 vm_push_frame(ec, iseq,
4248 VM_FRAME_MAGIC_BLOCK | VM_FRAME_FLAG_BMETHOD | VM_FRAME_FLAG_LAMBDA,
4249 calling->recv,
4250 VM_GUARDED_PREV_EP(captured->ep),
4251 (VALUE)cme,
4252 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
4253 argv + arg_size,
4254 ISEQ_BODY(iseq)->local_table_size - arg_size,
4255 ISEQ_BODY(iseq)->stack_max);
4256
4257 return Qundef;
4258}
4259
4260static VALUE
4261vm_call_noniseq_bmethod(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4262{
4263 RB_DEBUG_COUNTER_INC(ccf_noniseq_bmethod);
4264
4265 VALUE *argv;
4266 int argc;
4267 CALLER_SETUP_ARG(cfp, calling, calling->cd->ci, ALLOW_HEAP_ARGV);
4268 if (UNLIKELY(calling->heap_argv)) {
4269 argv = RARRAY_PTR(calling->heap_argv);
4270 cfp->sp -= 2;
4271 }
4272 else {
4273 argc = calling->argc;
4274 argv = ALLOCA_N(VALUE, argc);
4275 MEMCPY(argv, cfp->sp - argc, VALUE, argc);
4276 cfp->sp += - argc - 1;
4277 }
4278
4279 return vm_call_bmethod_body(ec, calling, argv);
4280}
4281
4282static VALUE
4283vm_call_bmethod(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4284{
4285 RB_DEBUG_COUNTER_INC(ccf_bmethod);
4286
4287 const struct rb_callcache *cc = calling->cc;
4288 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4289 VALUE procv = cme->def->body.bmethod.proc;
4290 rb_proc_t *proc;
4291 GetProcPtr(procv, proc);
4292 const struct rb_block *block = &proc->block;
4293
4294 while (vm_block_type(block) == block_type_proc) {
4295 block = vm_proc_block(block->as.proc);
4296 }
4297 if (vm_block_type(block) == block_type_iseq) {
4298 CC_SET_FASTPATH(cc, vm_call_iseq_bmethod, TRUE);
4299 return vm_call_iseq_bmethod(ec, cfp, calling);
4300 }
4301
4302 CC_SET_FASTPATH(cc, vm_call_noniseq_bmethod, TRUE);
4303 return vm_call_noniseq_bmethod(ec, cfp, calling);
4304}
4305
4306VALUE
4307rb_find_defined_class_by_owner(VALUE current_class, VALUE target_owner)
4308{
4309 VALUE klass = current_class;
4310
4311 /* for prepended Module, then start from cover class */
4312 if (RB_TYPE_P(klass, T_ICLASS) && RICLASS_IS_ORIGIN_P(klass) &&
4313 RB_TYPE_P(RBASIC_CLASS(klass), T_CLASS)) {
4314 klass = RBASIC_CLASS(klass);
4315 }
4316
4317 while (RTEST(klass)) {
4318 VALUE owner = RB_TYPE_P(klass, T_ICLASS) ? RBASIC_CLASS(klass) : klass;
4319 if (owner == target_owner) {
4320 return klass;
4321 }
4322 klass = RCLASS_SUPER(klass);
4323 }
4324
4325 return current_class; /* maybe module function */
4326}
4327
4328static const rb_callable_method_entry_t *
4329aliased_callable_method_entry(const rb_callable_method_entry_t *me)
4330{
4331 const rb_method_entry_t *orig_me =
4332 rb_method_alias_original_me(rb_method_entry_def_acquire((const rb_method_entry_t *)me));
4333 const rb_callable_method_entry_t *cme = NULL;
4334
4335 if (orig_me->defined_class == 0) { // only true on first resolution
4336 // Resolving the alias mutates `me` (and may replace me->def, dropping a
4337 // reference to the shared definition), so it has to be serialized. Two
4338 // ractors calling the same aliased method would otherwise both release
4339 // the same definition which could result in a UAF.
4340 RB_VM_LOCKING() {
4341 orig_me = me->def->body.alias.original_me;
4342 // me is the ALIAS me. It has a def that's different from orig_me->def.
4343 if (orig_me->defined_class == 0) {
4344 // orig_me is the unresolved me clone that rb_add_alias stored.
4345 VALUE defined_class = rb_find_defined_class_by_owner(me->defined_class, orig_me->owner);
4346 VM_ASSERT_TYPE(orig_me->owner, T_MODULE);
4347 cme = rb_method_entry_complement_defined_class(orig_me, me->called_id, defined_class);
4348
4349 if (me->def->reference_count == 1) {
4350 rbimpl_atomic_ptr_store((volatile void **)&me->def->body.alias.original_me,
4351 (void *)cme, RBIMPL_ATOMIC_RELEASE);
4352 RB_OBJ_WRITTEN(me, Qundef, cme);
4353 }
4354 else {
4355 rb_method_definition_t *def =
4356 rb_method_definition_create(VM_METHOD_TYPE_ALIAS, me->def->original_id);
4357 def->body.alias.original_me = (rb_method_entry_t *)cme;
4358 def->original_module = cme->def->original_module;
4359 rb_method_definition_set((rb_method_entry_t *)me, def, NULL);
4360 RB_OBJ_WRITTEN(me, Qundef, cme);
4361 RB_OBJ_WRITTEN(me, Qundef, def->original_module);
4362 }
4363 }
4364 else {
4365 cme = (const rb_callable_method_entry_t *)orig_me;
4366 }
4367 }
4368 }
4369 else {
4370 cme = (const rb_callable_method_entry_t *)orig_me;
4371 }
4372
4373 VM_ASSERT(callable_method_entry_p(cme));
4374 return cme;
4375}
4376
4377const rb_callable_method_entry_t *
4378rb_aliased_callable_method_entry(const rb_callable_method_entry_t *me)
4379{
4380 return aliased_callable_method_entry(me);
4381}
4382
4383static VALUE
4384vm_call_alias(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4385{
4386 calling->cc = &VM_CC_ON_STACK(Qundef,
4387 vm_call_general,
4388 {{0}},
4389 aliased_callable_method_entry(vm_cc_cme(calling->cc)));
4390
4391 return vm_call_method_each_type(ec, cfp, calling);
4392}
4393
4394static enum method_missing_reason
4395ci_missing_reason(const struct rb_callinfo *ci)
4396{
4397 enum method_missing_reason stat = MISSING_NOENTRY;
4398 if (vm_ci_flag(ci) & VM_CALL_VCALL && !(vm_ci_flag(ci) & VM_CALL_FORWARDING)) stat |= MISSING_VCALL;
4399 if (vm_ci_flag(ci) & VM_CALL_FCALL) stat |= MISSING_FCALL;
4400 if (vm_ci_flag(ci) & VM_CALL_SUPER) stat |= MISSING_SUPER;
4401 return stat;
4402}
4403
4404static VALUE vm_call_method_missing(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling);
4405
4406static VALUE
4407vm_call_symbol(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
4408 struct rb_calling_info *calling, const struct rb_callinfo *ci, VALUE symbol, int flags)
4409{
4410 ASSUME(calling->argc >= 0);
4411
4412 enum method_missing_reason missing_reason = MISSING_NOENTRY;
4413 int argc = calling->argc;
4414 VALUE recv = calling->recv;
4415 VALUE klass = CLASS_OF(recv);
4416 ID mid = rb_check_id(&symbol);
4417 flags |= VM_CALL_OPT_SEND;
4418
4419 if (UNLIKELY(! mid)) {
4420 mid = idMethodMissing;
4421 missing_reason = ci_missing_reason(ci);
4422 ec->method_missing_reason = missing_reason;
4423
4424 VALUE argv_ary;
4425 if (UNLIKELY(argv_ary = calling->heap_argv)) {
4426 if (rb_method_basic_definition_p(klass, idMethodMissing)) {
4427 rb_ary_unshift(argv_ary, symbol);
4428
4429 /* Inadvertent symbol creation shall be forbidden, see [Feature #5112] */
4430 int priv = vm_ci_flag(ci) & (VM_CALL_FCALL | VM_CALL_VCALL);
4431 VALUE exc = rb_make_no_method_exception(
4432 rb_eNoMethodError, 0, recv, RARRAY_LENINT(argv_ary), RARRAY_CONST_PTR(argv_ary), priv);
4433
4434 rb_exc_raise(exc);
4435 }
4436 rb_ary_unshift(argv_ary, rb_str_intern(symbol));
4437 }
4438 else {
4439 /* E.g. when argc == 2
4440 *
4441 * | | | | TOPN
4442 * | | +------+
4443 * | | +---> | arg1 | 0
4444 * +------+ | +------+
4445 * | arg1 | -+ +-> | arg0 | 1
4446 * +------+ | +------+
4447 * | arg0 | ---+ | sym | 2
4448 * +------+ +------+
4449 * | recv | | recv | 3
4450 * --+------+--------+------+------
4451 */
4452 int i = argc;
4453 CHECK_VM_STACK_OVERFLOW(reg_cfp, 1);
4454 INC_SP(1);
4455 MEMMOVE(&TOPN(i - 1), &TOPN(i), VALUE, i);
4456 argc = ++calling->argc;
4457
4458 if (rb_method_basic_definition_p(klass, idMethodMissing)) {
4459 /* Inadvertent symbol creation shall be forbidden, see [Feature #5112] */
4460 TOPN(i) = symbol;
4461 int priv = vm_ci_flag(ci) & (VM_CALL_FCALL | VM_CALL_VCALL);
4462 const VALUE *argv = STACK_ADDR_FROM_TOP(argc);
4463 VALUE exc = rb_make_no_method_exception(
4464 rb_eNoMethodError, 0, recv, argc, argv, priv);
4465
4466 rb_exc_raise(exc);
4467 }
4468 else {
4469 TOPN(i) = rb_str_intern(symbol);
4470 }
4471 }
4472 }
4473
4474 struct rb_forwarding_call_data new_fcd = {
4475 .cd = {
4476 .ci = &VM_CI_ON_STACK(mid, flags, argc, vm_ci_kwarg(ci)),
4477 .cc = NULL,
4478 },
4479 .caller_ci = NULL,
4480 };
4481
4482 if (!(vm_ci_flag(ci) & VM_CALL_FORWARDING)) {
4483 calling->cd = &new_fcd.cd;
4484 }
4485 else {
4486 const struct rb_callinfo *caller_ci = ((struct rb_forwarding_call_data *)calling->cd)->caller_ci;
4487 VM_ASSERT((vm_ci_argc(caller_ci), 1));
4488 new_fcd.caller_ci = caller_ci;
4489 calling->cd = (struct rb_call_data *)&new_fcd;
4490 }
4491 calling->cc = &VM_CC_ON_STACK(klass,
4492 vm_call_general,
4493 { .method_missing_reason = missing_reason },
4494 rb_callable_method_entry_with_refinements(klass, mid, NULL));
4495
4496 if (flags & VM_CALL_FCALL) {
4497 return vm_call_method(ec, reg_cfp, calling);
4498 }
4499
4500 const struct rb_callcache *cc = calling->cc;
4501 VM_ASSERT(callable_method_entry_p(vm_cc_cme(cc)));
4502
4503 if (vm_cc_cme(cc) != NULL) {
4504 switch (METHOD_ENTRY_VISI(vm_cc_cme(cc))) {
4505 case METHOD_VISI_PUBLIC: /* likely */
4506 return vm_call_method_each_type(ec, reg_cfp, calling);
4507 case METHOD_VISI_PRIVATE:
4508 vm_cc_method_missing_reason_set(cc, MISSING_PRIVATE);
4509 break;
4510 case METHOD_VISI_PROTECTED:
4511 vm_cc_method_missing_reason_set(cc, MISSING_PROTECTED);
4512 break;
4513 default:
4514 VM_UNREACHABLE(vm_call_method);
4515 }
4516 return vm_call_method_missing(ec, reg_cfp, calling);
4517 }
4518
4519 return vm_call_method_nome(ec, reg_cfp, calling);
4520}
4521
4522static VALUE
4523vm_call_opt_send0(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, int flags)
4524{
4525 const struct rb_callinfo *ci = calling->cd->ci;
4526 int i;
4527 VALUE sym;
4528
4529 i = calling->argc - 1;
4530
4531 if (calling->argc == 0) {
4532 rb_raise(rb_eArgError, "no method name given");
4533 }
4534
4535 sym = TOPN(i);
4536 /* E.g. when i == 2
4537 *
4538 * | | | | TOPN
4539 * +------+ | |
4540 * | arg1 | ---+ | | 0
4541 * +------+ | +------+
4542 * | arg0 | -+ +-> | arg1 | 1
4543 * +------+ | +------+
4544 * | sym | +---> | arg0 | 2
4545 * +------+ +------+
4546 * | recv | | recv | 3
4547 * --+------+--------+------+------
4548 */
4549 /* shift arguments */
4550 if (i > 0) {
4551 MEMMOVE(&TOPN(i), &TOPN(i-1), VALUE, i);
4552 }
4553 calling->argc -= 1;
4554 DEC_SP(1);
4555
4556 return vm_call_symbol(ec, reg_cfp, calling, ci, sym, flags);
4557}
4558
4559static VALUE
4560vm_call_opt_send_complex(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4561{
4562 RB_DEBUG_COUNTER_INC(ccf_opt_send_complex);
4563 const struct rb_callinfo *ci = calling->cd->ci;
4564 int flags = VM_CALL_FCALL;
4565 VALUE sym;
4566
4567 VALUE argv_ary;
4568 CALLER_SETUP_ARG(reg_cfp, calling, ci, ALLOW_HEAP_ARGV);
4569 if (UNLIKELY(argv_ary = calling->heap_argv)) {
4570 sym = rb_ary_shift(argv_ary);
4571 flags |= VM_CALL_ARGS_SPLAT;
4572 if (calling->kw_splat) {
4573 VALUE last_hash = rb_ary_last(0, NULL, argv_ary);
4574 ((struct RHash *)last_hash)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
4575 calling->kw_splat = 0;
4576 }
4577 return vm_call_symbol(ec, reg_cfp, calling, ci, sym, flags);
4578 }
4579
4580 if (calling->kw_splat) flags |= VM_CALL_KW_SPLAT;
4581 return vm_call_opt_send0(ec, reg_cfp, calling, flags);
4582}
4583
4584static VALUE
4585vm_call_opt_send_simple(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4586{
4587 RB_DEBUG_COUNTER_INC(ccf_opt_send_simple);
4588 return vm_call_opt_send0(ec, reg_cfp, calling, vm_ci_flag(calling->cd->ci) | VM_CALL_FCALL);
4589}
4590
4591static VALUE
4592vm_call_opt_send(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4593{
4594 RB_DEBUG_COUNTER_INC(ccf_opt_send);
4595
4596 const struct rb_callinfo *ci = calling->cd->ci;
4597 int flags = vm_ci_flag(ci);
4598
4599 if (UNLIKELY((flags & VM_CALL_FORWARDING) || (!(flags & VM_CALL_ARGS_SIMPLE) &&
4600 ((calling->argc == 1 && (flags & (VM_CALL_ARGS_SPLAT | VM_CALL_KW_SPLAT))) ||
4601 (calling->argc == 2 && (flags & VM_CALL_ARGS_SPLAT) && (flags & VM_CALL_KW_SPLAT)) ||
4602 ((flags & VM_CALL_KWARG) && (vm_ci_kwarg(ci)->keyword_len == calling->argc)))))) {
4603 CC_SET_FASTPATH(calling->cc, vm_call_opt_send_complex, TRUE);
4604 return vm_call_opt_send_complex(ec, reg_cfp, calling);
4605 }
4606
4607 CC_SET_FASTPATH(calling->cc, vm_call_opt_send_simple, TRUE);
4608 return vm_call_opt_send_simple(ec, reg_cfp, calling);
4609}
4610
4611static VALUE
4612vm_call_method_missing_body(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling,
4613 const struct rb_callinfo *orig_ci, enum method_missing_reason reason)
4614{
4615 RB_DEBUG_COUNTER_INC(ccf_method_missing);
4616
4617 VALUE *argv = STACK_ADDR_FROM_TOP(calling->argc);
4618 unsigned int argc, flag;
4619
4620 flag = VM_CALL_FCALL | VM_CALL_OPT_SEND | vm_ci_flag(orig_ci);
4621 argc = ++calling->argc;
4622
4623 /* shift arguments: m(a, b, c) #=> method_missing(:m, a, b, c) */
4624 CHECK_VM_STACK_OVERFLOW(reg_cfp, 1);
4625 vm_check_canary(ec, reg_cfp->sp);
4626 if (argc > 1) {
4627 MEMMOVE(argv+1, argv, VALUE, argc-1);
4628 }
4629 argv[0] = ID2SYM(vm_ci_mid(orig_ci));
4630 INC_SP(1);
4631
4632 ec->method_missing_reason = reason;
4633
4634 struct rb_forwarding_call_data new_fcd = {
4635 .cd = {
4636 .ci = &VM_CI_ON_STACK(idMethodMissing, flag, argc, vm_ci_kwarg(orig_ci)),
4637 .cc = NULL,
4638 },
4639 .caller_ci = NULL,
4640 };
4641
4642 if (!(flag & VM_CALL_FORWARDING)) {
4643 calling->cd = &new_fcd.cd;
4644 }
4645 else {
4646 const struct rb_callinfo *caller_ci = ((struct rb_forwarding_call_data *)calling->cd)->caller_ci;
4647 VM_ASSERT((vm_ci_argc(caller_ci), 1));
4648 new_fcd.caller_ci = caller_ci;
4649 calling->cd = (struct rb_call_data *)&new_fcd;
4650 }
4651
4652 calling->cc = &VM_CC_ON_STACK(Qundef, vm_call_general, {{ 0 }},
4653 rb_callable_method_entry_without_refinements(CLASS_OF(calling->recv), idMethodMissing, NULL));
4654 return vm_call_method(ec, reg_cfp, calling);
4655}
4656
4657static VALUE
4658vm_call_method_missing(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4659{
4660 return vm_call_method_missing_body(ec, reg_cfp, calling, calling->cd->ci, vm_cc_cmethod_missing_reason(calling->cc));
4661}
4662
4663static const rb_callable_method_entry_t *refined_method_callable_without_refinement(const rb_callable_method_entry_t *me);
4664static VALUE
4665vm_call_zsuper(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling, VALUE klass)
4666{
4667 klass = RCLASS_SUPER(klass);
4668
4669 const rb_callable_method_entry_t *cme = klass ? rb_callable_method_entry(klass, vm_ci_mid(calling->cd->ci)) : NULL;
4670 if (cme == NULL) {
4671 return vm_call_method_nome(ec, cfp, calling);
4672 }
4673 if (cme->def->type == VM_METHOD_TYPE_REFINED &&
4674 cme->def->body.refined.orig_me) {
4675 cme = refined_method_callable_without_refinement(cme);
4676 }
4677
4678 calling->cc = &VM_CC_ON_STACK(Qundef, vm_call_general, {{ 0 }}, cme);
4679
4680 return vm_call_method_each_type(ec, cfp, calling);
4681}
4682
4683static inline VALUE
4684find_refinement(VALUE refinements, VALUE klass)
4685{
4686 if (NIL_P(refinements)) {
4687 return Qnil;
4688 }
4689 return rb_hash_lookup(refinements, klass);
4690}
4691
4692PUREFUNC(static rb_control_frame_t * current_method_entry(const rb_execution_context_t *ec, rb_control_frame_t *cfp));
4693static rb_control_frame_t *
4694current_method_entry(const rb_execution_context_t *ec, rb_control_frame_t *cfp)
4695{
4696 rb_control_frame_t *top_cfp = cfp;
4697
4698 if (CFP_ISEQ(cfp) && !VM_FRAME_BMETHOD_P(cfp) && ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_BLOCK) {
4699 const rb_iseq_t *local_iseq = ISEQ_BODY(CFP_ISEQ(cfp))->local_iseq;
4700
4701 do {
4702 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
4703 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
4704 /* TODO: orphan block */
4705 return top_cfp;
4706 }
4707 } while (CFP_ISEQ(cfp) != local_iseq && !VM_FRAME_BMETHOD_P(cfp));
4708 }
4709 return cfp;
4710}
4711
4712static const rb_callable_method_entry_t *
4713refined_method_callable_without_refinement(const rb_callable_method_entry_t *me)
4714{
4715 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me;
4716 const rb_callable_method_entry_t *cme;
4717
4718 if (orig_me->defined_class == 0) {
4719 cme = NULL;
4721 }
4722 else {
4723 cme = (const rb_callable_method_entry_t *)orig_me;
4724 }
4725
4726 VM_ASSERT(callable_method_entry_p(cme));
4727
4728 if (UNDEFINED_METHOD_ENTRY_P(cme)) {
4729 cme = NULL;
4730 }
4731
4732 return cme;
4733}
4734
4735// Construct correct super chain for module refinements.
4736// Each class including/prepending a module has a weak map mapping refinement
4737// iclasses to refinement module iclasses with the correct super. Like
4738// singleton classes, these are lazily initialized (both the map and
4739// the refinement module iclasses).
4740static VALUE
4741module_refinement_iclass(VALUE refinement_iclass, VALUE defined_class)
4742{
4743 VALUE module_ref_iclass = Qundef;
4744
4745 RB_VM_LOCKING() {
4746 VALUE cache = RCLASS_MODULE_REFINEMENT_ICLASSES(defined_class);
4747
4748 if (LIKELY(cache)) {
4749 module_ref_iclass = rb_wmap_lookup(cache, refinement_iclass);
4750 }
4751 else {
4752 cache = rb_wmap_new_hidden();
4753 RCLASS_SET_MODULE_REFINEMENT_ICLASSES(defined_class, cache);
4754 }
4755
4756 if (UNDEF_P(module_ref_iclass)) {
4757 VALUE super;
4758 VALUE refinement_iclass_super = RCLASS_SUPER(refinement_iclass);
4759
4760 if (RICLASS_FOR_REFINEMENT_P(refinement_iclass_super)) {
4761 super = module_refinement_iclass(refinement_iclass_super, defined_class);
4762 }
4763 else {
4764 super = defined_class;
4765 }
4766
4767 module_ref_iclass = rb_include_class_new(RBASIC(refinement_iclass)->klass, super);
4768 RCLASS_SET_REFINED_CLASS(module_ref_iclass, RCLASS_REFINED_CLASS(refinement_iclass));
4769 rb_class_subclass_add(RCLASS_REFINED_CLASS(refinement_iclass), module_ref_iclass);
4770 rb_wmap_aset(cache, refinement_iclass, module_ref_iclass);
4771 }
4772 }
4773
4774 return module_ref_iclass;
4775}
4776
4777VALUE
4778rb_vm_module_refinement_iclass(VALUE refinement_iclass, VALUE defined_class)
4779{
4780 return module_refinement_iclass(refinement_iclass, defined_class);
4781}
4782
4783static VALUE
4784refinement_iclass_for_cme(VALUE refinement_iclass, const rb_callable_method_entry_t *cme)
4785{
4786 if (RB_TYPE_P(cme->owner, T_MODULE) && RB_TYPE_P(cme->defined_class, T_ICLASS)) {
4787 refinement_iclass = module_refinement_iclass(refinement_iclass, cme->defined_class);
4788 }
4789 return refinement_iclass;
4790}
4791
4792VALUE
4793rb_vm_refinement_iclass_for_cme(VALUE refinement_iclass, const rb_callable_method_entry_t *cme)
4794{
4795 return refinement_iclass_for_cme(refinement_iclass, cme);
4796}
4797
4798static const rb_callable_method_entry_t *
4799search_refined_method(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4800{
4801 const struct rb_callinfo *ci = calling->cd->ci;
4802 ID mid = vm_ci_mid(ci);
4803 // The initialize call of opt_new must behave like Class#new, which does not see the caller's refinements. [Bug #22501]
4804 const rb_cref_t *top_cref = (vm_ci_flag(ci) & VM_CALL_IGNORE_REFINEMENTS) ? NULL : vm_get_cref(cfp->ep);
4805 const struct rb_callcache * const cc = calling->cc;
4806 const rb_callable_method_entry_t *refined_cme = vm_cc_cme(cc);
4807 const rb_callable_method_entry_t *cme;
4808 bool skip_current_method = vm_cc_call(cc) == vm_call_super_method;
4809
4810 retry:
4811 cme = refined_cme;
4812
4813 for (const rb_cref_t *cref = top_cref; cref; cref = CREF_NEXT(cref)) {
4814 VALUE refinement = find_refinement(CREF_REFINEMENTS(cref), refined_cme->owner);
4815 if (NIL_P(refinement)) continue;
4816
4817 refinement = refinement_iclass_for_cme(refinement, refined_cme);
4818
4819 const rb_callable_method_entry_t *const ref_me =
4820 rb_callable_method_entry(refinement, mid);
4821
4822 if (ref_me) {
4823 if (skip_current_method) {
4824 const rb_control_frame_t *top_cfp = current_method_entry(ec, cfp);
4825 const rb_callable_method_entry_t *top_me = rb_vm_frame_method_entry(top_cfp);
4826 if (top_me && rb_method_definition_eq(ref_me->def, top_me->def)) {
4827 continue;
4828 }
4829 }
4830
4831 if (cme->def->type != VM_METHOD_TYPE_REFINED ||
4832 cme->def != ref_me->def) {
4833 cme = ref_me;
4834 }
4835 if (ref_me->def->type != VM_METHOD_TYPE_REFINED) {
4836 return cme;
4837 }
4838 }
4839 else {
4840 return NULL;
4841 }
4842 }
4843
4844 if (refined_cme->def->body.refined.orig_me) {
4845 return refined_method_callable_without_refinement(refined_cme);
4846 }
4847 else {
4848 VALUE klass = RCLASS_SUPER(refined_cme->defined_class);
4849 cme = klass ? rb_callable_method_entry(klass, mid) : NULL;
4850 if (cme && cme->def->type == VM_METHOD_TYPE_REFINED) {
4851 // Resolve it here; a second pass of vm_call_refined would cache
4852 // the result for this entry's iclass.
4853 refined_cme = cme;
4854 skip_current_method = false;
4855 goto retry;
4856 }
4857 return cme;
4858 }
4859}
4860
4861static VALUE
4862vm_call_refined(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4863{
4864 const rb_callable_method_entry_t *ref_cme = search_refined_method(ec, cfp, calling);
4865
4866 if (ref_cme) {
4867 if (calling->cd->cc) {
4868 const struct rb_callcache *cc = calling->cc = vm_cc_new(vm_cc_cme(calling->cc)->defined_class, ref_cme, vm_call_general, cc_type_refinement);
4869 RB_OBJ_WRITE(CFP_ISEQ(cfp), &calling->cd->cc, cc);
4870 return vm_call_method(ec, cfp, calling);
4871 }
4872 else {
4873 struct rb_callcache *ref_cc = &VM_CC_ON_STACK(Qundef, vm_call_general, {{ 0 }}, ref_cme);
4874 calling->cc= ref_cc;
4875 return vm_call_method(ec, cfp, calling);
4876 }
4877 }
4878 else {
4879 return vm_call_method_nome(ec, cfp, calling);
4880 }
4881}
4882
4883static inline VALUE vm_invoke_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_callinfo *ci, bool is_lambda, VALUE block_handler);
4884
4885NOINLINE(static VALUE
4886 vm_invoke_block_opt_call(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
4887 struct rb_calling_info *calling, const struct rb_callinfo *ci, VALUE block_handler));
4888
4889static VALUE
4890vm_invoke_block_opt_call(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
4891 struct rb_calling_info *calling, const struct rb_callinfo *ci, VALUE block_handler)
4892{
4893 int argc = calling->argc;
4894
4895 /* remove self */
4896 if (argc > 0) MEMMOVE(&TOPN(argc), &TOPN(argc-1), VALUE, argc);
4897 DEC_SP(1);
4898
4899 return vm_invoke_block(ec, reg_cfp, calling, ci, false, block_handler);
4900}
4901
4902static VALUE
4903vm_call_opt_call(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4904{
4905 RB_DEBUG_COUNTER_INC(ccf_opt_call);
4906
4907 const struct rb_callinfo *ci = calling->cd->ci;
4908 VALUE procval = calling->recv;
4909 return vm_invoke_block_opt_call(ec, reg_cfp, calling, ci, VM_BH_FROM_PROC(procval));
4910}
4911
4912static VALUE
4913vm_call_opt_block_call(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4914{
4915 RB_DEBUG_COUNTER_INC(ccf_opt_block_call);
4916
4917 VALUE block_handler = VM_ENV_BLOCK_HANDLER(VM_CF_LEP(reg_cfp));
4918 const struct rb_callinfo *ci = calling->cd->ci;
4919
4920 enum ruby_basic_operators bop;
4921 switch (vm_ci_mid(ci)) {
4922 case idAREF: bop = BOP_AREF; break;
4923 case idYield: bop = BOP_YIELD; break;
4924 case idEqq: bop = BOP_EQQ; break;
4925 default: bop = BOP_CALL; break;
4926 }
4927
4928 if (BASIC_OP_UNREDEFINED_P(bop, PROC_REDEFINED_OP_FLAG)) {
4929 return vm_invoke_block_opt_call(ec, reg_cfp, calling, ci, block_handler);
4930 }
4931 else {
4932 calling->recv = rb_vm_bh_to_procval(ec, block_handler);
4933 calling->cc = rb_vm_search_method_slowpath(ci, CLASS_OF(calling->recv));
4934 return vm_call_general(ec, reg_cfp, calling);
4935 }
4936}
4937
4938static VALUE
4939vm_call_opt_struct_aref0(rb_execution_context_t *ec, struct rb_calling_info *calling)
4940{
4941 VALUE recv = calling->recv;
4942
4943 VM_ASSERT(RB_TYPE_P(recv, T_STRUCT));
4944 VM_ASSERT(vm_cc_cme(calling->cc)->def->type == VM_METHOD_TYPE_OPTIMIZED);
4945 VM_ASSERT(vm_cc_cme(calling->cc)->def->body.optimized.type == OPTIMIZED_METHOD_TYPE_STRUCT_AREF);
4946
4947 const unsigned int off = vm_cc_cme(calling->cc)->def->body.optimized.index;
4948 return RSTRUCT_GET_RAW(recv, off);
4949}
4950
4951static VALUE
4952vm_call_opt_struct_aref(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4953{
4954 RB_DEBUG_COUNTER_INC(ccf_opt_struct_aref);
4955
4956 VALUE ret = vm_call_opt_struct_aref0(ec, calling);
4957 reg_cfp->sp -= 1;
4958 return ret;
4959}
4960
4961static VALUE
4962vm_call_opt_struct_aset0(rb_execution_context_t *ec, struct rb_calling_info *calling, VALUE val)
4963{
4964 VALUE recv = calling->recv;
4965
4966 VM_ASSERT(RB_TYPE_P(recv, T_STRUCT));
4967 VM_ASSERT(vm_cc_cme(calling->cc)->def->type == VM_METHOD_TYPE_OPTIMIZED);
4968 VM_ASSERT(vm_cc_cme(calling->cc)->def->body.optimized.type == OPTIMIZED_METHOD_TYPE_STRUCT_ASET);
4969
4970 rb_check_frozen(recv);
4971
4972 const unsigned int off = vm_cc_cme(calling->cc)->def->body.optimized.index;
4973 RSTRUCT_SET_RAW(recv, off, val);
4974
4975 return val;
4976}
4977
4978static VALUE
4979vm_call_opt_struct_aset(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4980{
4981 RB_DEBUG_COUNTER_INC(ccf_opt_struct_aset);
4982
4983 VALUE ret = vm_call_opt_struct_aset0(ec, calling, *(reg_cfp->sp - 1));
4984 reg_cfp->sp -= 2;
4985 return ret;
4986}
4987
4988NOINLINE(static VALUE vm_call_optimized(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling,
4989 const struct rb_callinfo *ci, const struct rb_callcache *cc));
4990
4991#define VM_CALL_METHOD_ATTR(var, func, nohook) \
4992 if (UNLIKELY(ruby_vm_c_events_enabled > 0)) { \
4993 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, calling->recv, vm_cc_cme(cc)->def->original_id, \
4994 vm_ci_mid(ci), vm_cc_cme(cc)->owner, Qundef); \
4995 var = func; \
4996 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, calling->recv, vm_cc_cme(cc)->def->original_id, \
4997 vm_ci_mid(ci), vm_cc_cme(cc)->owner, (var)); \
4998 } \
4999 else { \
5000 nohook; \
5001 var = func; \
5002 }
5003
5004static VALUE
5005vm_call_optimized(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling,
5006 const struct rb_callinfo *ci, const struct rb_callcache *cc)
5007{
5008 switch (vm_cc_cme(cc)->def->body.optimized.type) {
5009 case OPTIMIZED_METHOD_TYPE_SEND:
5010 CC_SET_FASTPATH(cc, vm_call_opt_send, TRUE);
5011 return vm_call_opt_send(ec, cfp, calling);
5012 case OPTIMIZED_METHOD_TYPE_CALL:
5013 CC_SET_FASTPATH(cc, vm_call_opt_call, TRUE);
5014 return vm_call_opt_call(ec, cfp, calling);
5015 case OPTIMIZED_METHOD_TYPE_BLOCK_CALL:
5016 CC_SET_FASTPATH(cc, vm_call_opt_block_call, TRUE);
5017 return vm_call_opt_block_call(ec, cfp, calling);
5018 case OPTIMIZED_METHOD_TYPE_STRUCT_AREF: {
5019 CALLER_SETUP_ARG(cfp, calling, ci, 0);
5020 rb_check_arity(calling->argc, 0, 0);
5021
5022 VALUE v;
5023 VM_CALL_METHOD_ATTR(v,
5024 vm_call_opt_struct_aref(ec, cfp, calling),
5025 set_vm_cc_ivar(cc); \
5026 CC_SET_FASTPATH(cc, vm_call_opt_struct_aref, (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)))
5027 return v;
5028 }
5029 case OPTIMIZED_METHOD_TYPE_STRUCT_ASET: {
5030 CALLER_SETUP_ARG(cfp, calling, ci, 1);
5031 rb_check_arity(calling->argc, 1, 1);
5032
5033 VALUE v;
5034 VM_CALL_METHOD_ATTR(v,
5035 vm_call_opt_struct_aset(ec, cfp, calling),
5036 set_vm_cc_ivar(cc); \
5037 CC_SET_FASTPATH(cc, vm_call_opt_struct_aset, (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)))
5038 return v;
5039 }
5040 default:
5041 rb_bug("vm_call_method: unsupported optimized method type (%d)", vm_cc_cme(cc)->def->body.optimized.type);
5042 }
5043}
5044
5045static VALUE
5046vm_call_method_each_type(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
5047{
5048 const struct rb_callinfo *ci = calling->cd->ci;
5049 const struct rb_callcache *cc = calling->cc;
5050 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
5051 VALUE v;
5052
5053 VM_ASSERT(! METHOD_ENTRY_INVALIDATED(cme));
5054
5055 switch (cme->def->type) {
5056 case VM_METHOD_TYPE_ISEQ:
5057 if (ISEQ_BODY(def_iseq_ptr(cme->def))->param.flags.forwardable) {
5058 CC_SET_FASTPATH(cc, vm_call_iseq_fwd_setup, TRUE);
5059 return vm_call_iseq_fwd_setup(ec, cfp, calling);
5060 }
5061 else {
5062 CC_SET_FASTPATH(cc, vm_call_iseq_setup, TRUE);
5063 return vm_call_iseq_setup(ec, cfp, calling);
5064 }
5065
5066 case VM_METHOD_TYPE_NOTIMPLEMENTED:
5067 case VM_METHOD_TYPE_CFUNC:
5068 CC_SET_FASTPATH(cc, vm_call_cfunc, TRUE);
5069 return vm_call_cfunc(ec, cfp, calling);
5070
5071 case VM_METHOD_TYPE_ATTRSET:
5072 CALLER_SETUP_ARG(cfp, calling, ci, 1);
5073
5074 rb_check_arity(calling->argc, 1, 1);
5075
5076 const unsigned int aset_mask = (VM_CALL_ARGS_SPLAT | VM_CALL_KW_SPLAT | VM_CALL_KWARG | VM_CALL_FORWARDING);
5077
5078 if (vm_cc_markable(cc)) {
5079 vm_cc_attr_index_set(cc, IVAR_CACHE_INIT);
5080 VM_CALL_METHOD_ATTR(v,
5081 vm_call_attrset_direct(ec, cfp, cc, calling->recv),
5082 CC_SET_FASTPATH(cc, vm_call_attrset, !(vm_ci_flag(ci) & aset_mask)));
5083 }
5084 else {
5085 cc = &((struct rb_callcache) {
5086 .flags = T_IMEMO |
5087 (imemo_callcache << FL_USHIFT) |
5088 VM_CALLCACHE_UNMARKABLE |
5089 VM_CALLCACHE_ON_STACK,
5090 .klass = cc->klass,
5091 .cme_ = cc->cme_,
5092 .call_ = cc->call_,
5093 .aux_ = {
5094 .attr = {
5095 .value = IVAR_CACHE_INIT,
5096 }
5097 },
5098 });
5099
5100 VM_CALL_METHOD_ATTR(v,
5101 vm_call_attrset_direct(ec, cfp, cc, calling->recv),
5102 CC_SET_FASTPATH(cc, vm_call_attrset, !(vm_ci_flag(ci) & aset_mask)));
5103 }
5104 return v;
5105
5106 case VM_METHOD_TYPE_IVAR:
5107 CALLER_SETUP_ARG(cfp, calling, ci, 0);
5108 rb_check_arity(calling->argc, 0, 0);
5109 vm_cc_attr_index_set(cc, rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET));
5110 const unsigned int ivar_mask = (VM_CALL_ARGS_SPLAT | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING);
5111 VM_CALL_METHOD_ATTR(v,
5112 vm_call_ivar(ec, cfp, calling),
5113 CC_SET_FASTPATH(cc, vm_call_ivar, !(vm_ci_flag(ci) & ivar_mask)));
5114 return v;
5115
5116 case VM_METHOD_TYPE_MISSING:
5117 vm_cc_method_missing_reason_set(cc, 0);
5118 CC_SET_FASTPATH(cc, vm_call_method_missing, TRUE);
5119 return vm_call_method_missing(ec, cfp, calling);
5120
5121 case VM_METHOD_TYPE_BMETHOD:
5122 CC_SET_FASTPATH(cc, vm_call_bmethod, TRUE);
5123 return vm_call_bmethod(ec, cfp, calling);
5124
5125 case VM_METHOD_TYPE_ALIAS:
5126 CC_SET_FASTPATH(cc, vm_call_alias, TRUE);
5127 return vm_call_alias(ec, cfp, calling);
5128
5129 case VM_METHOD_TYPE_OPTIMIZED:
5130 return vm_call_optimized(ec, cfp, calling, ci, cc);
5131
5132 case VM_METHOD_TYPE_UNDEF:
5133 break;
5134
5135 case VM_METHOD_TYPE_ZSUPER:
5136 return vm_call_zsuper(ec, cfp, calling, RCLASS_ORIGIN(vm_cc_cme(cc)->defined_class));
5137
5138 case VM_METHOD_TYPE_REFINED:
5139 // CC_SET_FASTPATH(cc, vm_call_refined, TRUE);
5140 // should not set FASTPATH since vm_call_refined assumes cc->call is vm_call_super_method on invokesuper.
5141 return vm_call_refined(ec, cfp, calling);
5142 }
5143
5144 rb_bug("vm_call_method: unsupported method type (%d)", vm_cc_cme(cc)->def->type);
5145}
5146
5147NORETURN(static void vm_raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE obj, int call_status));
5148
5149static VALUE
5150vm_call_method_nome(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
5151{
5152 /* method missing */
5153 const struct rb_callinfo *ci = calling->cd->ci;
5154 const int stat = ci_missing_reason(ci);
5155
5156 if (vm_ci_mid(ci) == idMethodMissing) {
5157 if (UNLIKELY(calling->heap_argv)) {
5158 vm_raise_method_missing(ec, RARRAY_LENINT(calling->heap_argv), RARRAY_CONST_PTR(calling->heap_argv), calling->recv, stat);
5159 }
5160 else {
5161 rb_control_frame_t *reg_cfp = cfp;
5162 VALUE *argv = STACK_ADDR_FROM_TOP(calling->argc);
5163 vm_raise_method_missing(ec, calling->argc, argv, calling->recv, stat);
5164 }
5165 }
5166 else {
5167 return vm_call_method_missing_body(ec, cfp, calling, ci, stat);
5168 }
5169}
5170
5171/* Protected method calls and super invocations need to check that the receiver
5172 * (self for super) inherits the module on which the method is defined.
5173 * In the case of refinements, it should consider the original class not the
5174 * refinement.
5175 */
5176static VALUE
5177vm_defined_class_for_protected_call(const rb_callable_method_entry_t *me)
5178{
5179 VALUE defined_class = me->defined_class;
5180 VALUE refined_class = RCLASS_REFINED_CLASS(defined_class);
5181 return NIL_P(refined_class) ? defined_class : refined_class;
5182}
5183
5184static inline VALUE
5185vm_call_method(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
5186{
5187 const struct rb_callinfo *ci = calling->cd->ci;
5188 const struct rb_callcache *cc = calling->cc;
5189
5190 VM_ASSERT(callable_method_entry_p(vm_cc_cme(cc)));
5191
5192 if (vm_cc_cme(cc) != NULL) {
5193 switch (METHOD_ENTRY_VISI(vm_cc_cme(cc))) {
5194 case METHOD_VISI_PUBLIC: /* likely */
5195 return vm_call_method_each_type(ec, cfp, calling);
5196
5197 case METHOD_VISI_PRIVATE:
5198 if (!(vm_ci_flag(ci) & VM_CALL_FCALL)) {
5199 enum method_missing_reason stat = MISSING_PRIVATE;
5200 if (vm_ci_flag(ci) & VM_CALL_VCALL) stat |= MISSING_VCALL;
5201
5202 vm_cc_method_missing_reason_set(cc, stat);
5203 CC_SET_FASTPATH(cc, vm_call_method_missing, TRUE);
5204 return vm_call_method_missing(ec, cfp, calling);
5205 }
5206 return vm_call_method_each_type(ec, cfp, calling);
5207
5208 case METHOD_VISI_PROTECTED:
5209 if (!(vm_ci_flag(ci) & (VM_CALL_OPT_SEND | VM_CALL_FCALL))) {
5210 VALUE defined_class = vm_defined_class_for_protected_call(vm_cc_cme(cc));
5211 if (!rb_obj_is_kind_of(cfp->self, defined_class)) {
5212 vm_cc_method_missing_reason_set(cc, MISSING_PROTECTED);
5213 return vm_call_method_missing(ec, cfp, calling);
5214 }
5215 else {
5216 /* caching method info to dummy cc */
5217 VM_ASSERT(vm_cc_cme(cc) != NULL);
5218 struct rb_callcache cc_on_stack = *cc;
5219 FL_SET_RAW((VALUE)&cc_on_stack, VM_CALLCACHE_UNMARKABLE);
5220 calling->cc = &cc_on_stack;
5221 return vm_call_method_each_type(ec, cfp, calling);
5222 }
5223 }
5224 return vm_call_method_each_type(ec, cfp, calling);
5225
5226 default:
5227 rb_bug("unreachable");
5228 }
5229 }
5230 else {
5231 return vm_call_method_nome(ec, cfp, calling);
5232 }
5233}
5234
5235static VALUE
5236vm_call_general(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
5237{
5238 RB_DEBUG_COUNTER_INC(ccf_general);
5239 return vm_call_method(ec, reg_cfp, calling);
5240}
5241
5242void
5243rb_vm_cc_general(const struct rb_callcache *cc)
5244{
5245 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
5246 VM_ASSERT(cc != vm_cc_empty());
5247
5248 *(vm_call_handler *)&cc->call_ = vm_call_general;
5249}
5250
5251static VALUE
5252vm_call_super_method(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
5253{
5254 RB_DEBUG_COUNTER_INC(ccf_super_method);
5255
5256 // This line is introduced to make different from `vm_call_general` because some compilers (VC we found)
5257 // can merge the function and the address of the function becomes same.
5258 // The address of `vm_call_super_method` is used in `search_refined_method`, so it should be different.
5259 if (ec == NULL) rb_bug("unreachable");
5260
5261 /* this check is required to distinguish with other functions. */
5262 VM_ASSERT(vm_cc_call(calling->cc) == vm_call_super_method);
5263 return vm_call_method(ec, reg_cfp, calling);
5264}
5265
5266/* super */
5267
5268static inline VALUE
5269vm_search_normal_superclass(VALUE klass)
5270{
5271 if (RICLASS_FOR_REFINEMENT_P(klass)) {
5272 if (RB_TYPE_P(RCLASS_REFINED_CLASS(klass), T_MODULE)) {
5273 do {
5274 klass = RCLASS_SUPER(klass);
5275 } while (RICLASS_FOR_REFINEMENT_P(klass));
5276
5277 return klass;
5278 }
5279
5280 klass = RBASIC(klass)->klass;
5281 }
5282 klass = RCLASS_ORIGIN(klass);
5283 return RCLASS_SUPER(klass);
5284}
5285
5286NORETURN(static void vm_super_outside(void));
5287
5288static void
5289vm_super_outside(void)
5290{
5291 rb_raise(rb_eNoMethodError, "super called outside of method");
5292}
5293
5294static const struct rb_callcache *
5295empty_cc_for_super(void)
5296{
5297 return &rb_vm_empty_cc_for_super;
5298}
5299
5300static const struct rb_callcache *
5301vm_search_super_method(const rb_control_frame_t *reg_cfp, struct rb_call_data *cd, VALUE recv)
5302{
5303 VALUE current_defined_class;
5304 const rb_iseq_t *iseq = CFP_ISEQ(reg_cfp);
5305 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(reg_cfp);
5306
5307 if (!me) {
5308 vm_super_outside();
5309 }
5310
5311 current_defined_class = vm_defined_class_for_protected_call(me);
5312
5313 if (BUILTIN_TYPE(current_defined_class) != T_MODULE &&
5314 iseq != method_entry_iseqptr(me) &&
5315 !rb_obj_is_kind_of(recv, current_defined_class)) {
5316 VALUE m = RB_TYPE_P(current_defined_class, T_ICLASS) ?
5317 RCLASS_INCLUDER(current_defined_class) : current_defined_class;
5318
5319 if (m) { /* not bound UnboundMethod */
5320 rb_raise(rb_eTypeError,
5321 "self has wrong type to call super in this context: "
5322 "%"PRIsVALUE" (expected %"PRIsVALUE")",
5323 rb_obj_class(recv), m);
5324 }
5325 }
5326
5327 if (me->def->type == VM_METHOD_TYPE_BMETHOD && (vm_ci_flag(cd->ci) & VM_CALL_ZSUPER)) {
5328 rb_raise(rb_eRuntimeError,
5329 "implicit argument passing of super from method defined"
5330 " by define_method() is not supported."
5331 " Specify all arguments explicitly.");
5332 }
5333
5334 ID mid = me->def->original_id;
5335
5336 if (!vm_ci_markable(cd->ci)) {
5337 VM_FORCE_WRITE((const VALUE *)&cd->ci->mid, (VALUE)mid);
5338 }
5339 else {
5340 // update iseq. really? (TODO)
5341 cd->ci = vm_ci_new_runtime(mid,
5342 vm_ci_flag(cd->ci),
5343 vm_ci_argc(cd->ci),
5344 vm_ci_kwarg(cd->ci));
5345
5346 RB_OBJ_WRITTEN(iseq, Qundef, cd->ci);
5347 }
5348
5349 const struct rb_callcache *cc;
5350
5351 VALUE klass = vm_search_normal_superclass(me->defined_class);
5352
5353 if (!klass) {
5354 /* bound instance method of module */
5355 cc = vm_cc_new(Qundef, NULL, vm_call_method_missing, cc_type_super);
5356 RB_OBJ_WRITE(iseq, &cd->cc, cc);
5357 }
5358 else {
5359 cc = vm_search_method_fastpath(reg_cfp, cd, klass);
5360 VM_ASSERT(cc != &rb_vm_uncached_cc);
5361 const rb_callable_method_entry_t *cached_cme = vm_cc_cme(cc);
5362
5363 // define_method can cache for different method id
5364 if (cached_cme == NULL) {
5365 // empty_cc_for_super is not markable object
5366 cd->cc = empty_cc_for_super();
5367 }
5368 else if (cached_cme->called_id != mid) {
5369 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, mid);
5370 if (cme) {
5371 cc = vm_cc_new(klass, cme, vm_call_super_method, cc_type_super);
5372 RB_OBJ_WRITE(iseq, &cd->cc, cc);
5373 }
5374 else {
5375 cd->cc = cc = empty_cc_for_super();
5376 }
5377 }
5378 else {
5379 switch (cached_cme->def->type) {
5380 // vm_call_refined (search_refined_method) assumes cc->call is vm_call_super_method on invokesuper
5381 case VM_METHOD_TYPE_REFINED:
5382 // cc->klass is superclass of receiver class. Checking cc->klass is not enough to invalidate IVC for the receiver class.
5383 case VM_METHOD_TYPE_ATTRSET:
5384 case VM_METHOD_TYPE_IVAR:
5385 vm_cc_call_set(cc, vm_call_super_method); // invalidate fastpath
5386 break;
5387 default:
5388 break; // use fastpath
5389 }
5390 }
5391 }
5392
5393 VM_ASSERT((vm_cc_cme(cc), true));
5394
5395 return cc;
5396}
5397
5398/* yield */
5399
5400static inline int
5401block_proc_is_lambda(const VALUE procval)
5402{
5403 rb_proc_t *proc;
5404
5405 if (procval) {
5406 GetProcPtr(procval, proc);
5407 return proc->header.is_lambda;
5408 }
5409 else {
5410 return 0;
5411 }
5412}
5413
5414static VALUE
5415vm_yield_with_cfunc(rb_execution_context_t *ec,
5416 const struct rb_captured_block *captured,
5417 VALUE self, int argc, const VALUE *argv, int kw_splat, VALUE block_handler,
5418 const rb_callable_method_entry_t *me, int stack_argc)
5419{
5420 int is_lambda = FALSE; /* TODO */
5421 VALUE val, arg, blockarg;
5422 int frame_flag;
5423 const struct vm_ifunc *ifunc = captured->code.ifunc;
5424
5425 if (is_lambda) {
5426 arg = rb_ary_new4(argc, argv);
5427 }
5428 else if (argc == 0) {
5429 arg = Qnil;
5430 }
5431 else {
5432 arg = argv[0];
5433 }
5434
5435 blockarg = rb_vm_bh_to_procval(ec, block_handler);
5436
5437 frame_flag = VM_FRAME_MAGIC_IFUNC | VM_FRAME_FLAG_CFRAME | (me ? VM_FRAME_FLAG_BMETHOD : 0);
5438 if (kw_splat) {
5439 frame_flag |= VM_FRAME_FLAG_CFRAME_KW;
5440 }
5441
5442 rb_control_frame_t *cfp = ec->cfp;
5443 vm_push_frame(ec, (const rb_iseq_t *)captured->code.ifunc,
5444 frame_flag,
5445 self,
5446 VM_GUARDED_PREV_EP(captured->ep),
5447 (VALUE)me,
5448 0, cfp->sp, 0, 0);
5449 /* The new frame keeps the arguments live. Pop them from the caller
5450 * before the callback can exit via break. */
5451 cfp->sp -= stack_argc;
5452 val = (*ifunc->func)(arg, (VALUE)ifunc->data, argc, argv, blockarg);
5453 rb_vm_pop_frame(ec);
5454
5455 return val;
5456}
5457
5458VALUE
5459rb_vm_yield_with_cfunc(rb_execution_context_t *ec, const struct rb_captured_block *captured, int argc, const VALUE *argv)
5460{
5461 return vm_yield_with_cfunc(ec, captured, captured->self, argc, argv, 0, VM_BLOCK_HANDLER_NONE, NULL, 0);
5462}
5463
5464static VALUE
5465vm_yield_with_symbol(rb_execution_context_t *ec, VALUE symbol, int argc, const VALUE *argv, int kw_splat, VALUE block_handler)
5466{
5467 VALUE passed_proc = rb_vm_bh_to_procval(ec, block_handler);
5468
5469 if (!rb_box_available()) {
5470 return rb_sym_proc_call(SYM2ID(symbol), argc, argv, kw_splat, passed_proc);
5471 }
5472
5473 const rb_control_frame_t *reg_cfp = ec->cfp;
5474 const rb_control_frame_t *ruby_cfp = rb_vm_get_ruby_level_next_cfp(ec, reg_cfp);
5475 const rb_box_t *box = NULL;
5476
5477 /*
5478 * Traverse the frames until a user box (Main or Optional Box) is found.
5479 * Frames for built-in methods like <internal:xxx> run in the Root or Master Box,
5480 */
5481 while (ruby_cfp) {
5482 box = current_box_on_cfp(ec, ruby_cfp);
5483 if (BOX_USER_P(box)) {
5484 break;
5485 }
5486 ruby_cfp = rb_vm_get_ruby_level_next_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(ruby_cfp));
5487 }
5488
5489 // Fallback to the normal call if no user box is found
5490 if (!ruby_cfp || !box) {
5491 return rb_sym_proc_call(SYM2ID(symbol), argc, argv, kw_splat, passed_proc);
5492 }
5493
5494 /*
5495 * Push a dummy block frame whose outer env is `ruby_cfp` so that the
5496 * method resolves in the caller's box (via the ep chain) and the
5497 * backtrace reads like a usual block invocation at the caller's site.
5498 */
5499 const rb_iseq_t *caller_iseq = CFP_ISEQ(ruby_cfp);
5500 VALUE name = rb_sprintf("block in %"PRIsVALUE, rb_iseq_label(caller_iseq));
5501 const rb_iseq_t *iseq = rb_iseq_new_with_opt(Qnil, name,
5502 rb_iseq_path(caller_iseq), rb_iseq_realpath(caller_iseq),
5503 rb_vm_get_sourceline(ruby_cfp), caller_iseq, 0,
5504 ISEQ_TYPE_BLOCK, NULL, Qnil);
5505 volatile VALUE val = Qnil;
5506 enum ruby_tag_type state;
5507
5508 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_BLOCK | VM_FRAME_FLAG_FINISH,
5509 ruby_cfp->self, VM_GUARDED_PREV_EP(ruby_cfp->ep),
5510 Qfalse, /* cref or me */
5511 ISEQ_BODY(iseq)->iseq_encoded, reg_cfp->sp,
5512 ISEQ_BODY(iseq)->local_table_size, ISEQ_BODY(iseq)->stack_max);
5513
5514 /*
5515 * The pushed frame is finished (owned by no vm_exec loop), so catch the
5516 * non-local exit here, pop the frame, and let the caller's handlers see
5517 * the exception.
5518 */
5519 EC_PUSH_TAG(ec);
5520 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
5521 val = rb_sym_proc_call(SYM2ID(symbol), argc, argv, kw_splat, passed_proc);
5522 }
5523 EC_POP_TAG();
5524
5525 if (state != TAG_NONE) {
5526 rb_vm_rewind_cfp(ec, (rb_control_frame_t *)reg_cfp);
5527 EC_JUMP_TAG(ec, state);
5528 }
5529
5530 rb_vm_pop_frame(ec);
5531
5532 return val;
5533}
5534
5535static inline int
5536vm_callee_setup_block_arg_arg0_splat(rb_control_frame_t *cfp, const rb_iseq_t *iseq, VALUE *argv, VALUE ary)
5537{
5538 int i;
5539 rb_len_t len = RARRAY_LEN(ary);
5540
5541 CHECK_VM_STACK_OVERFLOW(cfp, ISEQ_BODY(iseq)->param.lead_num);
5542
5543 for (i=0; i<len && i<ISEQ_BODY(iseq)->param.lead_num; i++) {
5544 argv[i] = RARRAY_AREF(ary, i);
5545 }
5546
5547 return i;
5548}
5549
5550static inline VALUE
5551vm_callee_setup_block_arg_arg0_check(VALUE *argv)
5552{
5553 VALUE ary, arg0 = argv[0];
5554 ary = rb_check_array_type(arg0);
5555#if 0
5556 argv[0] = arg0;
5557#else
5558 VM_ASSERT(argv[0] == arg0);
5559#endif
5560 return ary;
5561}
5562
5563static int
5564vm_callee_setup_block_arg(rb_execution_context_t *ec, struct rb_calling_info *calling, const struct rb_callinfo *ci, const rb_iseq_t *iseq, VALUE *argv, const enum arg_setup_type arg_setup_type)
5565{
5566 if (rb_simple_iseq_p(iseq)) {
5567 rb_control_frame_t *cfp = ec->cfp;
5568 VALUE arg0;
5569
5570 CALLER_SETUP_ARG(cfp, calling, ci, ISEQ_BODY(iseq)->param.lead_num);
5571
5572 if (arg_setup_type == arg_setup_block &&
5573 calling->argc == 1 &&
5574 ISEQ_BODY(iseq)->param.flags.has_lead &&
5575 !ISEQ_BODY(iseq)->param.flags.ambiguous_param0 &&
5576 !NIL_P(arg0 = vm_callee_setup_block_arg_arg0_check(argv))) {
5577 calling->argc = vm_callee_setup_block_arg_arg0_splat(cfp, iseq, argv, arg0);
5578 }
5579
5580 if (calling->argc != ISEQ_BODY(iseq)->param.lead_num) {
5581 if (arg_setup_type == arg_setup_block) {
5582 if (calling->argc < ISEQ_BODY(iseq)->param.lead_num) {
5583 int i;
5584 CHECK_VM_STACK_OVERFLOW(cfp, ISEQ_BODY(iseq)->param.lead_num);
5585 for (i=calling->argc; i<ISEQ_BODY(iseq)->param.lead_num; i++) argv[i] = Qnil;
5586 calling->argc = ISEQ_BODY(iseq)->param.lead_num; /* fill rest parameters */
5587 }
5588 else if (calling->argc > ISEQ_BODY(iseq)->param.lead_num) {
5589 calling->argc = ISEQ_BODY(iseq)->param.lead_num; /* simply truncate arguments */
5590 }
5591 }
5592 else {
5593 argument_arity_error(ec, iseq, NULL, calling->argc, ISEQ_BODY(iseq)->param.lead_num, ISEQ_BODY(iseq)->param.lead_num);
5594 }
5595 }
5596
5597 return 0;
5598 }
5599 else {
5600 return setup_parameters_complex(ec, iseq, calling, ci, argv, arg_setup_type);
5601 }
5602}
5603
5604static int
5605vm_yield_setup_args(rb_execution_context_t *ec, const rb_iseq_t *iseq, const int argc, VALUE *argv, int flags, VALUE block_handler, enum arg_setup_type arg_setup_type)
5606{
5607 struct rb_calling_info calling_entry, *calling;
5608
5609 calling = &calling_entry;
5610 calling->argc = argc;
5611 calling->block_handler = block_handler;
5612 calling->kw_splat = (flags & VM_CALL_KW_SPLAT) ? 1 : 0;
5613 calling->recv = Qundef;
5614 calling->heap_argv = 0;
5615 calling->cc = NULL;
5616 struct rb_callinfo dummy_ci = VM_CI_ON_STACK(0, flags, 0, 0);
5617
5618 return vm_callee_setup_block_arg(ec, calling, &dummy_ci, iseq, argv, arg_setup_type);
5619}
5620
5621/* ruby iseq -> ruby block */
5622
5623static VALUE
5624vm_invoke_iseq_block_with_cref(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5625 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5626 bool is_lambda, VALUE block_handler, const rb_cref_t *cref)
5627{
5628 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
5629 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
5630 const int arg_size = ISEQ_BODY(iseq)->param.size;
5631 VALUE * const rsp = GET_SP() - calling->argc;
5632 VALUE * const argv = rsp;
5633 int opt_pc = vm_callee_setup_block_arg(ec, calling, ci, iseq, argv, is_lambda ? arg_setup_method : arg_setup_block);
5634 int frame_flag = VM_FRAME_MAGIC_BLOCK | (is_lambda ? VM_FRAME_FLAG_LAMBDA : 0);
5635
5636 SET_SP(rsp);
5637
5638 /* cref is normally 0; Proc#refined supplies a refinement cref */
5639 vm_push_frame(ec, iseq,
5640 frame_flag,
5641 captured->self,
5642 VM_GUARDED_PREV_EP(captured->ep), (VALUE)cref,
5643 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
5644 rsp + arg_size,
5645 ISEQ_BODY(iseq)->local_table_size - arg_size, ISEQ_BODY(iseq)->stack_max);
5646
5647 return Qundef;
5648}
5649
5650static VALUE
5651vm_invoke_iseq_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5652 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5653 bool is_lambda, VALUE block_handler)
5654{
5655 return vm_invoke_iseq_block_with_cref(ec, reg_cfp, calling, ci, is_lambda, block_handler, NULL);
5656}
5657
5658static VALUE
5659vm_invoke_symbol_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5660 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5661 MAYBE_UNUSED(bool is_lambda), VALUE block_handler)
5662{
5663 VALUE symbol = VM_BH_TO_SYMBOL(block_handler);
5664 int flags = vm_ci_flag(ci);
5665
5666 if (UNLIKELY(!(flags & VM_CALL_ARGS_SIMPLE) &&
5667 ((calling->argc == 0) ||
5668 (calling->argc == 1 && (flags & (VM_CALL_ARGS_SPLAT | VM_CALL_KW_SPLAT))) ||
5669 (calling->argc == 2 && (flags & VM_CALL_ARGS_SPLAT) && (flags & VM_CALL_KW_SPLAT)) ||
5670 ((flags & VM_CALL_KWARG) && (vm_ci_kwarg(ci)->keyword_len == calling->argc))))) {
5671 CALLER_SETUP_ARG(reg_cfp, calling, ci, ALLOW_HEAP_ARGV);
5672 flags = 0;
5673 if (UNLIKELY(calling->heap_argv)) {
5674#if VM_ARGC_STACK_MAX < 0
5675 if (RARRAY_LEN(calling->heap_argv) < 1) {
5676 rb_raise(rb_eArgError, "no receiver given");
5677 }
5678#endif
5679 calling->recv = rb_ary_shift(calling->heap_argv);
5680 // Modify stack to avoid cfp consistency error
5681 reg_cfp->sp++;
5682 reg_cfp->sp[-1] = reg_cfp->sp[-2];
5683 reg_cfp->sp[-2] = calling->recv;
5684 flags |= VM_CALL_ARGS_SPLAT;
5685 }
5686 else {
5687 if (calling->argc < 1) {
5688 rb_raise(rb_eArgError, "no receiver given");
5689 }
5690 calling->recv = TOPN(--calling->argc);
5691 }
5692 if (calling->kw_splat) {
5693 flags |= VM_CALL_KW_SPLAT;
5694 }
5695 }
5696 else {
5697 if (calling->argc < 1) {
5698 rb_raise(rb_eArgError, "no receiver given");
5699 }
5700 calling->recv = TOPN(--calling->argc);
5701 }
5702
5703 return vm_call_symbol(ec, reg_cfp, calling, ci, symbol, flags);
5704}
5705
5706static VALUE
5707vm_invoke_ifunc_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5708 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5709 MAYBE_UNUSED(bool is_lambda), VALUE block_handler)
5710{
5711 const struct rb_captured_block *captured = VM_BH_TO_IFUNC_BLOCK(block_handler);
5712 CALLER_SETUP_ARG(reg_cfp, calling, ci, ALLOW_HEAP_ARGV_KEEP_KWSPLAT);
5713 const VALUE *argv = calling->heap_argv ? RARRAY_CONST_PTR(calling->heap_argv) : STACK_ADDR_FROM_TOP(calling->argc);
5714 return vm_yield_with_cfunc(ec, captured, captured->self, CALLING_ARGC(calling), argv,
5715 calling->kw_splat, calling->block_handler, NULL, calling->argc);
5716}
5717
5718static inline VALUE
5719vm_block_to_block_handler(const struct rb_block *block)
5720{
5721 switch (vm_block_type(block)) {
5722 case block_type_iseq:
5723 return VM_BH_FROM_ISEQ_BLOCK(&block->as.captured);
5724 case block_type_ifunc:
5725 return VM_BH_FROM_IFUNC_BLOCK(&block->as.captured);
5726 case block_type_symbol:
5727 return VM_BH_FROM_SYMBOL(block->as.symbol);
5728 case block_type_proc:
5729 return VM_BH_FROM_PROC(block->as.proc);
5730 }
5731 VM_UNREACHABLE(vm_yield_with_proc);
5732 return Qundef;
5733}
5734
5735static VALUE
5736vm_proc_to_block_handler(VALUE procval)
5737{
5738 return vm_block_to_block_handler(vm_proc_block(procval));
5739}
5740
5741NOINLINE(static VALUE
5742vm_invoke_proc_block_with_cref(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5743 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5744 bool is_lambda, VALUE block_handler, VALUE refined_procval));
5745static VALUE
5746vm_invoke_proc_block_with_cref(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5747 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5748 bool is_lambda, VALUE block_handler, VALUE refined_procval)
5749{
5750 const rb_cref_t *cref = rb_proc_refinements_cref_for_call(refined_procval);
5751 return vm_invoke_iseq_block_with_cref(ec, reg_cfp, calling, ci, is_lambda, block_handler, cref);
5752}
5753
5754static VALUE
5755vm_invoke_proc_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5756 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5757 bool is_lambda, VALUE block_handler)
5758{
5759 VALUE refined_procval = 0;
5760
5761 while (vm_block_handler_type(block_handler) == block_handler_type_proc) {
5762 VALUE procval = VM_BH_TO_PROC(block_handler);
5763 rb_proc_t *po;
5764 GetProcPtr(procval, po);
5765 if (po->header.is_refined) refined_procval = procval;
5766 is_lambda = po->header.is_lambda;
5767 block_handler = vm_block_to_block_handler(&po->block);
5768 }
5769
5770 if (UNLIKELY(refined_procval) && vm_block_handler_type(block_handler) == block_handler_type_iseq) {
5771 /* should not be inlined so that vm_invoke_proc_block stays leaf/frameless. */
5772 return vm_invoke_proc_block_with_cref(ec, reg_cfp, calling, ci, is_lambda, block_handler,
5773 refined_procval);
5774 }
5775
5776 return vm_invoke_block(ec, reg_cfp, calling, ci, is_lambda, block_handler);
5777}
5778
5779static inline VALUE
5780vm_invoke_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5781 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5782 bool is_lambda, VALUE block_handler)
5783{
5784 VALUE (*func)(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5785 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5786 bool is_lambda, VALUE block_handler);
5787
5788 switch (vm_block_handler_type(block_handler)) {
5789 case block_handler_type_iseq: func = vm_invoke_iseq_block; break;
5790 case block_handler_type_ifunc: func = vm_invoke_ifunc_block; break;
5791 case block_handler_type_proc: func = vm_invoke_proc_block; break;
5792 case block_handler_type_symbol: func = vm_invoke_symbol_block; break;
5793 default: rb_bug("vm_invoke_block: unreachable");
5794 }
5795
5796 return func(ec, reg_cfp, calling, ci, is_lambda, block_handler);
5797}
5798
5799static VALUE
5800vm_make_proc_with_iseq(const rb_iseq_t *blockiseq)
5801{
5802 const rb_execution_context_t *ec = GET_EC();
5803 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
5804 struct rb_captured_block *captured;
5805
5806 if (cfp == 0) {
5807 rb_bug("vm_make_proc_with_iseq: unreachable");
5808 }
5809
5810 captured = VM_CFP_TO_CAPTURED_BLOCK(cfp);
5811 captured->code.iseq = blockiseq;
5812
5813 return rb_vm_make_proc(ec, captured, rb_cProc);
5814}
5815
5816static VALUE
5817vm_once_exec(VALUE iseq)
5818{
5819 VALUE proc = vm_make_proc_with_iseq((rb_iseq_t *)iseq);
5820 return rb_proc_call_with_block(proc, 0, 0, Qnil);
5821}
5822
5823#define RUNNING_THREAD_ONCE_DONE ((rb_thread_t *)0x1)
5824
5826 rb_vm_t *vm;
5827 union iseq_inline_storage_entry *is;
5828};
5829
5830static void
5831vm_once_broadcast(rb_vm_t *vm)
5832{
5833 rb_native_mutex_lock(&vm->once_lock);
5834 rb_native_cond_broadcast(&vm->once_cond);
5835 rb_native_mutex_unlock(&vm->once_lock);
5836}
5837
5838static void *
5839vm_once_wait_no_gvl(void *ptr)
5840{
5841 struct vm_once_wait_arg *arg = (struct vm_once_wait_arg *)ptr;
5842 rb_vm_t *vm = arg->vm;
5843 rb_thread_t *running_th;
5844
5845 rb_native_mutex_lock(&vm->once_lock);
5846 running_th = rbimpl_atomic_ptr_load((void **)&arg->is->once.running_thread, RBIMPL_ATOMIC_ACQUIRE);
5847 if (running_th != NULL && running_th != RUNNING_THREAD_ONCE_DONE) {
5848 rb_native_cond_wait(&vm->once_cond, &vm->once_lock);
5849 }
5850 rb_native_mutex_unlock(&vm->once_lock);
5851 return NULL;
5852}
5853
5854static void
5855vm_once_wait_ubf(void *ptr)
5856{
5857 vm_once_broadcast((rb_vm_t *)ptr);
5858}
5859
5860static VALUE
5861vm_once_clear(VALUE data)
5862{
5863 union iseq_inline_storage_entry *is = (union iseq_inline_storage_entry *)data;
5864 rbimpl_atomic_ptr_store((volatile void **)&is->once.running_thread, NULL, RBIMPL_ATOMIC_RELEASE);
5865 vm_once_broadcast(GET_VM());
5866 return Qnil;
5867}
5868
5869/* defined insn */
5870
5871static bool
5872check_respond_to_missing(VALUE obj, VALUE v)
5873{
5874 VALUE args[2];
5875 VALUE r;
5876
5877 args[0] = obj; args[1] = Qfalse;
5878 r = rb_check_funcall(v, idRespond_to_missing, 2, args);
5879 if (!UNDEF_P(r) && RTEST(r)) {
5880 return true;
5881 }
5882 else {
5883 return false;
5884 }
5885}
5886
5887static bool
5888vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE obj, VALUE v)
5889{
5890 VALUE klass;
5891 enum defined_type type = (enum defined_type)op_type;
5892
5893 switch (type) {
5894 case DEFINED_IVAR:
5895 return rb_ivar_defined(GET_SELF(), SYM2ID(obj));
5896 break;
5897 case DEFINED_GVAR:
5898 return rb_gvar_defined(SYM2ID(obj));
5899 break;
5900 case DEFINED_CVAR: {
5901 const rb_cref_t *cref = vm_get_cref(GET_EP());
5902 klass = vm_get_cvar_base(cref, GET_CFP(), 0);
5903 return rb_cvar_defined(klass, SYM2ID(obj));
5904 break;
5905 }
5906 case DEFINED_CONST:
5907 case DEFINED_CONST_FROM: {
5908 bool allow_nil = type == DEFINED_CONST;
5909 klass = v;
5910 return vm_get_ev_const(ec, klass, SYM2ID(obj), allow_nil, true);
5911 break;
5912 }
5913 case DEFINED_FUNC:
5914 klass = CLASS_OF(v);
5915 return rb_ec_obj_respond_to(ec, v, SYM2ID(obj), TRUE);
5916 break;
5917 case DEFINED_METHOD:{
5918 VALUE klass = CLASS_OF(v);
5919 const rb_callable_method_entry_t *cme = rb_callable_method_entry_with_refinements(klass, SYM2ID(obj), NULL);
5920
5921 if (cme) {
5922 switch (METHOD_ENTRY_VISI(cme)) {
5923 case METHOD_VISI_PRIVATE:
5924 break;
5925 case METHOD_VISI_PROTECTED:
5926 if (!rb_obj_is_kind_of(GET_SELF(), vm_defined_class_for_protected_call(cme))) {
5927 break;
5928 }
5929 case METHOD_VISI_PUBLIC:
5930 return true;
5931 break;
5932 default:
5933 rb_bug("vm_defined: unreachable: %u", (unsigned int)METHOD_ENTRY_VISI(cme));
5934 }
5935 }
5936 else {
5937 return check_respond_to_missing(obj, v);
5938 }
5939 break;
5940 }
5941 case DEFINED_YIELD:
5942 if (GET_BLOCK_HANDLER() != VM_BLOCK_HANDLER_NONE) {
5943 return true;
5944 }
5945 break;
5946 case DEFINED_ZSUPER:
5947 {
5948 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(GET_CFP());
5949
5950 if (me) {
5951 VALUE klass = vm_search_normal_superclass(me->defined_class);
5952 if (!klass) return false;
5953
5954 ID id = me->def->original_id;
5955
5956 return rb_method_boundp(klass, id, 0);
5957 }
5958 }
5959 break;
5960 case DEFINED_REF:
5961 return RTEST(vm_backref_defined(ec, GET_LEP(), FIX2INT(obj)));
5962 default:
5963 rb_bug("unimplemented defined? type (VM)");
5964 break;
5965 }
5966
5967 return false;
5968}
5969
5970bool
5971rb_vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE obj, VALUE v)
5972{
5973 return vm_defined(ec, reg_cfp, op_type, obj, v);
5974}
5975
5976static const VALUE *
5977vm_get_ep(const VALUE *const reg_ep, rb_num_t lv)
5978{
5979 rb_num_t i;
5980 const VALUE *ep = reg_ep;
5981 for (i = 0; i < lv; i++) {
5982 ep = GET_PREV_EP(ep);
5983 }
5984 return ep;
5985}
5986
5987static VALUE
5988vm_get_special_object(const VALUE *const reg_ep,
5989 enum vm_special_object_type type)
5990{
5991 switch (type) {
5992 case VM_SPECIAL_OBJECT_VMCORE:
5993 return rb_mRubyVMFrozenCore;
5994 case VM_SPECIAL_OBJECT_CBASE:
5995 return vm_get_cbase(reg_ep);
5996 case VM_SPECIAL_OBJECT_CONST_BASE:
5997 return vm_get_const_base(reg_ep);
5998 default:
5999 rb_bug("putspecialobject insn: unknown value_type %d", type);
6000 }
6001}
6002
6003// ZJIT implementation is using the C function
6004// and needs to call a non-static function
6005VALUE
6006rb_vm_get_special_object(const VALUE *reg_ep, enum vm_special_object_type type)
6007{
6008 return vm_get_special_object(reg_ep, type);
6009}
6010
6011static VALUE
6012vm_concat_array(VALUE ary1, VALUE ary2st)
6013{
6014 const VALUE ary2 = ary2st;
6015 VALUE tmp1 = rb_check_to_array(ary1);
6016 VALUE tmp2 = rb_check_to_array(ary2);
6017
6018 if (NIL_P(tmp1)) {
6019 tmp1 = rb_ary_new3(1, ary1);
6020 }
6021 if (tmp1 == ary1) {
6022 tmp1 = rb_ary_dup(ary1);
6023 }
6024
6025 if (NIL_P(tmp2)) {
6026 return rb_ary_push(tmp1, ary2);
6027 }
6028 else {
6029 return rb_ary_concat(tmp1, tmp2);
6030 }
6031}
6032
6033static VALUE
6034vm_concat_to_array(VALUE ary1, VALUE ary2st)
6035{
6036 /* ary1 must be a newly created array */
6037 const VALUE ary2 = ary2st;
6038
6039 if (NIL_P(ary2)) return ary1;
6040
6041 VALUE tmp2 = rb_check_to_array(ary2);
6042
6043 if (NIL_P(tmp2)) {
6044 return rb_ary_push(ary1, ary2);
6045 }
6046 else {
6047 return rb_ary_concat(ary1, tmp2);
6048 }
6049}
6050
6051// YJIT implementation is using the C function
6052// and needs to call a non-static function
6053VALUE
6054rb_vm_concat_array(VALUE ary1, VALUE ary2st)
6055{
6056 return vm_concat_array(ary1, ary2st);
6057}
6058
6059VALUE
6060rb_vm_concat_to_array(VALUE ary1, VALUE ary2st)
6061{
6062 return vm_concat_to_array(ary1, ary2st);
6063}
6064
6065static VALUE
6066vm_splat_array(VALUE flag, VALUE ary)
6067{
6068 if (NIL_P(ary)) {
6069 return RTEST(flag) ? rb_ary_new() : rb_cArray_empty_frozen;
6070 }
6071 VALUE tmp = rb_check_to_array(ary);
6072 if (NIL_P(tmp)) {
6073 return rb_ary_new3(1, ary);
6074 }
6075 else if (RTEST(flag)) {
6076 return rb_ary_dup(tmp);
6077 }
6078 else {
6079 return tmp;
6080 }
6081}
6082
6083// YJIT implementation is using the C function
6084// and needs to call a non-static function
6085VALUE
6086rb_vm_splat_array(VALUE flag, VALUE ary)
6087{
6088 return vm_splat_array(flag, ary);
6089}
6090
6091static VALUE
6092vm_check_match(rb_execution_context_t *ec, VALUE target, VALUE pattern, rb_num_t flag)
6093{
6094 enum vm_check_match_type type = ((int)flag) & VM_CHECKMATCH_TYPE_MASK;
6095
6096 if (flag & VM_CHECKMATCH_ARRAY) {
6097 rb_len_t i;
6098 const rb_len_t n = RARRAY_LEN(pattern);
6099
6100 for (i = 0; i < n; i++) {
6101 VALUE v = RARRAY_AREF(pattern, i);
6102 VALUE c = check_match(ec, v, target, type);
6103
6104 if (RTEST(c)) {
6105 return c;
6106 }
6107 }
6108 return Qfalse;
6109 }
6110 else {
6111 return check_match(ec, pattern, target, type);
6112 }
6113}
6114
6115VALUE
6116rb_vm_check_match(rb_execution_context_t *ec, VALUE target, VALUE pattern, rb_num_t flag)
6117{
6118 return vm_check_match(ec, target, pattern, flag);
6119}
6120
6121static VALUE
6122vm_check_keyword(lindex_t bits, lindex_t idx, const VALUE *ep)
6123{
6124 const VALUE kw_bits = *(ep - bits);
6125
6126 if (FIXNUM_P(kw_bits)) {
6127 unsigned int b = (unsigned int)FIX2ULONG(kw_bits);
6128 if ((idx < VM_KW_SPECIFIED_BITS_MAX) && (b & (0x01 << idx)))
6129 return Qfalse;
6130 }
6131 else {
6132 VM_ASSERT(rb_set_p(kw_bits), "%s", rb_obj_info(kw_bits));
6133 if (rb_set_lookup(kw_bits, INT2FIX(idx))) return Qfalse;
6134 }
6135 return Qtrue;
6136}
6137
6138static void
6139vm_dtrace(rb_event_flag_t flag, rb_execution_context_t *ec)
6140{
6141 if (RUBY_DTRACE_METHOD_ENTRY_ENABLED() ||
6142 RUBY_DTRACE_METHOD_RETURN_ENABLED() ||
6143 RUBY_DTRACE_CMETHOD_ENTRY_ENABLED() ||
6144 RUBY_DTRACE_CMETHOD_RETURN_ENABLED()) {
6145
6146 switch (flag) {
6147 case RUBY_EVENT_CALL:
6148 RUBY_DTRACE_METHOD_ENTRY_HOOK(ec, 0, 0);
6149 return;
6150 case RUBY_EVENT_C_CALL:
6151 RUBY_DTRACE_CMETHOD_ENTRY_HOOK(ec, 0, 0);
6152 return;
6153 case RUBY_EVENT_RETURN:
6154 RUBY_DTRACE_METHOD_RETURN_HOOK(ec, 0, 0);
6155 return;
6157 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, 0, 0);
6158 return;
6159 }
6160 }
6161}
6162
6163static VALUE
6164vm_const_get_under(ID id, rb_num_t flags, VALUE cbase)
6165{
6166 if (!rb_const_defined_at(cbase, id)) {
6167 return 0;
6168 }
6169 else if (VM_DEFINECLASS_SCOPED_P(flags)) {
6170 return rb_public_const_get_at(cbase, id);
6171 }
6172 else {
6173 return rb_const_get_at(cbase, id);
6174 }
6175}
6176
6177static VALUE
6178vm_check_if_class(ID id, rb_num_t flags, VALUE super, VALUE klass)
6179{
6180 if (!RB_TYPE_P(klass, T_CLASS)) {
6181 return 0;
6182 }
6183 else if (VM_DEFINECLASS_HAS_SUPERCLASS_P(flags)) {
6184 VALUE tmp = rb_class_real(RCLASS_SUPER(klass));
6185
6186 if (tmp != super) {
6187 rb_raise(rb_eTypeError,
6188 "superclass mismatch for class %"PRIsVALUE"",
6189 rb_id2str(id));
6190 }
6191 else {
6192 return klass;
6193 }
6194 }
6195 else {
6196 return klass;
6197 }
6198}
6199
6200static VALUE
6201vm_check_if_module(ID id, VALUE mod)
6202{
6203 if (!RB_TYPE_P(mod, T_MODULE)) {
6204 return 0;
6205 }
6206 else {
6207 return mod;
6208 }
6209}
6210
6211static VALUE
6212declare_under(ID id, VALUE cbase, VALUE c)
6213{
6214 rb_set_class_path_string(c, cbase, rb_id2str(id));
6215 rb_const_set(cbase, id, c);
6216 return c;
6217}
6218
6219static VALUE
6220vm_declare_class(ID id, rb_num_t flags, VALUE cbase, VALUE super)
6221{
6222 /* new class declaration */
6223 VALUE s = VM_DEFINECLASS_HAS_SUPERCLASS_P(flags) ? super : rb_cObject;
6224 VALUE c = declare_under(id, cbase, rb_define_class_id(id, s));
6225 rb_class_inherited(s, c);
6226 return c;
6227}
6228
6229static VALUE
6230vm_declare_module(ID id, VALUE cbase)
6231{
6232 /* new module declaration */
6233 return declare_under(id, cbase, rb_module_new());
6234}
6235
6236NORETURN(static void unmatched_redefinition(const char *type, VALUE cbase, ID id, VALUE old));
6237static void
6238unmatched_redefinition(const char *type, VALUE cbase, ID id, VALUE old)
6239{
6240 VALUE name = rb_id2str(id);
6241 VALUE message = rb_sprintf("%"PRIsVALUE" is not a %s",
6242 name, type);
6243 VALUE location = rb_const_source_location_at(cbase, id);
6244 if (!NIL_P(location)) {
6245 rb_str_catf(message, "\n%"PRIsVALUE":%"PRIsVALUE":"
6246 " previous definition of %"PRIsVALUE" was here",
6247 rb_ary_entry(location, 0), rb_ary_entry(location, 1), name);
6248 }
6250}
6251
6252static VALUE
6253vm_define_class(ID id, rb_num_t flags, VALUE cbase, VALUE super)
6254{
6255 VALUE klass;
6256
6257 if (VM_DEFINECLASS_HAS_SUPERCLASS_P(flags) && !RB_TYPE_P(super, T_CLASS)) {
6258 rb_raise(rb_eTypeError,
6259 "superclass must be an instance of Class (given an instance of %"PRIsVALUE")",
6260 rb_obj_class(super));
6261 }
6262
6263 vm_check_if_namespace(cbase);
6264
6265 /* find klass */
6266 rb_autoload_load(cbase, id);
6267
6268 if ((klass = vm_const_get_under(id, flags, cbase)) != 0) {
6269 if (!vm_check_if_class(id, flags, super, klass))
6270 unmatched_redefinition("class", cbase, id, klass);
6271 return klass;
6272 }
6273 else {
6274 return vm_declare_class(id, flags, cbase, super);
6275 }
6276}
6277
6278static VALUE
6279vm_define_module(ID id, rb_num_t flags, VALUE cbase)
6280{
6281 VALUE mod;
6282
6283 vm_check_if_namespace(cbase);
6284 if ((mod = vm_const_get_under(id, flags, cbase)) != 0) {
6285 if (!vm_check_if_module(id, mod))
6286 unmatched_redefinition("module", cbase, id, mod);
6287 return mod;
6288 }
6289 else {
6290 return vm_declare_module(id, cbase);
6291 }
6292}
6293
6294static VALUE
6295vm_find_or_create_class_by_id(ID id,
6296 rb_num_t flags,
6297 VALUE cbase,
6298 VALUE super)
6299{
6300 rb_vm_defineclass_type_t type = VM_DEFINECLASS_TYPE(flags);
6301
6302 switch (type) {
6303 case VM_DEFINECLASS_TYPE_CLASS:
6304 /* classdef returns class scope value */
6305 return vm_define_class(id, flags, cbase, super);
6306
6307 case VM_DEFINECLASS_TYPE_SINGLETON_CLASS:
6308 /* classdef returns class scope value */
6309 return rb_singleton_class(cbase);
6310
6311 case VM_DEFINECLASS_TYPE_MODULE:
6312 /* classdef returns class scope value */
6313 return vm_define_module(id, flags, cbase);
6314
6315 default:
6316 rb_bug("unknown defineclass type: %d", (int)type);
6317 }
6318}
6319
6320static rb_method_visibility_t
6321vm_scope_visibility_get(const rb_execution_context_t *ec)
6322{
6323 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
6324
6325 if (!vm_env_cref_by_cref(cfp->ep)) {
6326 return METHOD_VISI_PUBLIC;
6327 }
6328 else {
6329 return CREF_SCOPE_VISI(vm_ec_cref(ec))->method_visi;
6330 }
6331}
6332
6333static int
6334vm_scope_module_func_check(const rb_execution_context_t *ec)
6335{
6336 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
6337
6338 if (!vm_env_cref_by_cref(cfp->ep)) {
6339 return FALSE;
6340 }
6341 else {
6342 return CREF_SCOPE_VISI(vm_ec_cref(ec))->module_func;
6343 }
6344}
6345
6346static void
6347vm_define_method(const rb_execution_context_t *ec, VALUE obj, ID id, VALUE iseqval, int is_singleton)
6348{
6349 VALUE klass;
6350 rb_method_visibility_t visi;
6351 rb_cref_t *cref = vm_ec_cref(ec);
6352
6353 if (is_singleton) {
6354 klass = rb_singleton_class(obj); /* class and frozen checked in this API */
6355 visi = METHOD_VISI_PUBLIC;
6356 }
6357 else {
6358 klass = CREF_CLASS_FOR_DEFINITION(cref);
6359 visi = vm_scope_visibility_get(ec);
6360 }
6361
6362 if (NIL_P(klass)) {
6363 rb_raise(rb_eTypeError, "no class/module to add method");
6364 }
6365
6366 rb_add_method_iseq(klass, id, (const rb_iseq_t *)iseqval, cref, visi);
6367 // Set max_iv_count on klasses based on number of ivar sets that are in the initialize method
6368 if (id == idInitialize && RB_TYPE_P(klass, T_CLASS) &&
6369 !RCLASS_SINGLETON_P(klass) && !RCLASS_EXPECT_NO_IVAR(klass) &&
6370 (rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
6371 RCLASS_SET_MAX_IV_COUNT(klass, rb_estimate_iv_count(klass, (const rb_iseq_t *)iseqval));
6372 }
6373
6374 if (!is_singleton && vm_scope_module_func_check(ec)) {
6375 klass = rb_singleton_class(klass);
6376 rb_add_method_iseq(klass, id, (const rb_iseq_t *)iseqval, cref, METHOD_VISI_PUBLIC);
6377 }
6378}
6379
6380// Return the untagged block handler:
6381// * If it's VM_BLOCK_HANDLER_NONE, return nil
6382// * If it's an ISEQ or an IFUNC, fetch it from its rb_captured_block
6383// * If it's a PROC or SYMBOL, return it as is
6384VALUE
6385rb_vm_untag_block_handler(VALUE block_handler)
6386{
6387 if (VM_BLOCK_HANDLER_NONE == block_handler) return Qnil;
6388
6389 switch (vm_block_handler_type(block_handler)) {
6390 case block_handler_type_iseq:
6391 case block_handler_type_ifunc: {
6392 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
6393 return captured->code.val;
6394 }
6395 case block_handler_type_proc:
6396 case block_handler_type_symbol:
6397 return block_handler;
6398 default:
6399 rb_bug("rb_vm_untag_block_handler: unreachable");
6400 }
6401}
6402
6403VALUE
6404rb_vm_get_untagged_block_handler(rb_control_frame_t *reg_cfp)
6405{
6406 return rb_vm_untag_block_handler(VM_CF_BLOCK_HANDLER(reg_cfp));
6407}
6408
6409static VALUE
6410vm_invokeblock_i(struct rb_execution_context_struct *ec,
6411 struct rb_control_frame_struct *reg_cfp,
6412 struct rb_calling_info *calling)
6413{
6414 const struct rb_callinfo *ci = calling->cd->ci;
6415 VALUE block_handler = VM_CF_BLOCK_HANDLER(GET_CFP());
6416
6417 if (block_handler == VM_BLOCK_HANDLER_NONE) {
6418 rb_vm_localjump_error("no block given (yield)", Qnil, 0);
6419 }
6420 else {
6421 return vm_invoke_block(ec, GET_CFP(), calling, ci, false, block_handler);
6422 }
6423}
6424
6425enum method_explorer_type {
6426 mexp_search_method,
6427 mexp_search_invokeblock,
6428 mexp_search_super,
6429};
6430
6431ALWAYS_INLINE(static VALUE vm_sendish(struct rb_execution_context_struct *ec,
6432 struct rb_control_frame_struct *reg_cfp,
6433 struct rb_call_data *cd, VALUE block_handler,
6434 enum method_explorer_type method_explorer));
6435static inline VALUE
6436vm_sendish(
6437 struct rb_execution_context_struct *ec,
6438 struct rb_control_frame_struct *reg_cfp,
6439 struct rb_call_data *cd,
6440 VALUE block_handler,
6441 enum method_explorer_type method_explorer
6442) {
6443 VALUE val = Qundef;
6444 const struct rb_callinfo *ci = cd->ci;
6445 const struct rb_callcache *cc;
6446 int argc = vm_ci_argc(ci);
6447 VALUE recv = TOPN(argc);
6448 struct rb_calling_info calling = {
6449 .block_handler = block_handler,
6450 .kw_splat = IS_ARGS_KW_SPLAT(ci) > 0,
6451 .recv = recv,
6452 .argc = argc,
6453 .cd = cd,
6454 };
6455
6456 switch (method_explorer) {
6457 case mexp_search_method:
6458 calling.cc = cc = vm_search_method_fastpath(reg_cfp, cd, CLASS_OF(recv));
6459 val = vm_cc_call(cc)(ec, GET_CFP(), &calling);
6460 break;
6461 case mexp_search_super:
6462 calling.cc = cc = vm_search_super_method(reg_cfp, cd, recv);
6463 val = vm_cc_call(cc)(ec, GET_CFP(), &calling);
6464 break;
6465 case mexp_search_invokeblock:
6466 val = vm_invokeblock_i(ec, GET_CFP(), &calling);
6467 break;
6468 }
6469 return val;
6470}
6471
6472VALUE
6473rb_vm_send(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd, ISEQ blockiseq)
6474{
6475 stack_check(ec);
6476 VALUE bh = vm_caller_setup_arg_block(ec, GET_CFP(), cd->ci, blockiseq, false);
6477 VALUE val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_method);
6478 VM_EXEC(ec, val);
6479 return val;
6480}
6481
6482// Fallback for YJIT/ZJIT, not used by the interpreter
6483VALUE
6484rb_vm_sendforward(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd, ISEQ blockiseq)
6485{
6486 stack_check(ec);
6487
6488 struct rb_forwarding_call_data adjusted_cd;
6489 struct rb_callinfo adjusted_ci;
6490
6491 VALUE bh = vm_caller_setup_fwd_args(GET_EC(), GET_CFP(), cd, blockiseq, false, &adjusted_cd, &adjusted_ci);
6492
6493 VALUE val = vm_sendish(ec, GET_CFP(), &adjusted_cd.cd, bh, mexp_search_method);
6494
6495 if (cd->cc != adjusted_cd.cd.cc && vm_cc_markable(adjusted_cd.cd.cc)) {
6496 RB_OBJ_WRITE(CFP_ISEQ(GET_CFP()), &cd->cc, adjusted_cd.cd.cc);
6497 }
6498
6499 VM_EXEC(ec, val);
6500 return val;
6501}
6502
6503VALUE
6504rb_vm_opt_send_without_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd)
6505{
6506 stack_check(ec);
6507 VALUE bh = VM_BLOCK_HANDLER_NONE;
6508 VALUE val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_method);
6509 VM_EXEC(ec, val);
6510 return val;
6511}
6512
6513VALUE
6514rb_vm_invokesuper(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd, ISEQ blockiseq)
6515{
6516 stack_check(ec);
6517
6518 struct rb_callinfo adjusted_ci = VM_CI_ON_STACK(vm_ci_mid(cd->ci),
6519 vm_ci_flag(cd->ci),
6520 vm_ci_argc(cd->ci),
6521 vm_ci_kwarg(cd->ci));
6522 const struct rb_callcache *original_cc = rbimpl_atomic_ptr_load((void **)&cd->cc, RBIMPL_ATOMIC_ACQUIRE);
6523 struct rb_call_data adjusted_cd = {
6524 .ci = &adjusted_ci,
6525 .cc = original_cc,
6526 };
6527
6528 VALUE bh = vm_caller_setup_arg_block(ec, GET_CFP(), adjusted_cd.ci, blockiseq, true);
6529 VALUE val = vm_sendish(ec, GET_CFP(), &adjusted_cd, bh, mexp_search_super);
6530
6531 if (original_cc != adjusted_cd.cc && vm_cc_markable(adjusted_cd.cc)) {
6532 rbimpl_atomic_ptr_store((volatile void **)&cd->cc, (void *)adjusted_cd.cc, RBIMPL_ATOMIC_RELEASE);
6533 RB_OBJ_WRITTEN(CFP_ISEQ(GET_CFP()), Qundef, adjusted_cd.cc);
6534 }
6535
6536 VM_EXEC(ec, val);
6537 return val;
6538}
6539
6540// Fallback for YJIT/ZJIT, not used by the interpreter
6541VALUE
6542rb_vm_invokesuperforward(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd, ISEQ blockiseq)
6543{
6544 stack_check(ec);
6545 struct rb_forwarding_call_data adjusted_cd;
6546 struct rb_callinfo adjusted_ci;
6547
6548 VALUE bh = vm_caller_setup_fwd_args(GET_EC(), GET_CFP(), cd, blockiseq, true, &adjusted_cd, &adjusted_ci);
6549
6550 VALUE val = vm_sendish(ec, GET_CFP(), &adjusted_cd.cd, bh, mexp_search_super);
6551
6552 if (cd->cc != adjusted_cd.cd.cc && vm_cc_markable(adjusted_cd.cd.cc)) {
6553 RB_OBJ_WRITE(CFP_ISEQ(GET_CFP()), &cd->cc, adjusted_cd.cd.cc);
6554 }
6555
6556 VM_EXEC(ec, val);
6557 return val;
6558}
6559
6560VALUE
6561rb_vm_invokeblock(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd)
6562{
6563 stack_check(ec);
6564 VALUE bh = VM_BLOCK_HANDLER_NONE;
6565 VALUE val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_invokeblock);
6566 VM_EXEC(ec, val);
6567 return val;
6568}
6569
6570/* object.c */
6571VALUE rb_nil_to_s(VALUE);
6572VALUE rb_true_to_s(VALUE);
6573VALUE rb_false_to_s(VALUE);
6574/* numeric.c */
6575VALUE rb_int_to_s(int argc, VALUE *argv, VALUE x);
6576VALUE rb_fix_to_s(VALUE);
6577/* variable.c */
6578VALUE rb_mod_to_s(VALUE);
6580
6581static VALUE
6582vm_objtostring(struct rb_control_frame_struct *reg_cfp, VALUE recv, CALL_DATA cd)
6583{
6584 int type = TYPE(recv);
6585 if (type == T_STRING) {
6586 return recv;
6587 }
6588
6589 const struct rb_callable_method_entry_struct *cme = vm_search_method(reg_cfp, cd, recv);
6590
6591 switch (type) {
6592 case T_SYMBOL:
6593 if (check_method_basic_definition(cme)) {
6594 return rb_sym2str(recv);
6595 }
6596 break;
6597 case T_MODULE:
6598 case T_CLASS:
6599 if (check_cfunc(cme, rb_mod_to_s)) {
6600 // rb_mod_to_s() allocates a mutable string, but since we are only
6601 // going to use this string for interpolation, it's fine to use the
6602 // frozen string.
6603 VALUE val = rb_mod_name(recv);
6604 if (NIL_P(val)) {
6605 val = rb_mod_to_s(recv);
6606 }
6607 return val;
6608 }
6609 break;
6610 case T_NIL:
6611 if (check_cfunc(cme, rb_nil_to_s)) {
6612 return rb_nil_to_s(recv);
6613 }
6614 break;
6615 case T_TRUE:
6616 if (check_cfunc(cme, rb_true_to_s)) {
6617 return rb_true_to_s(recv);
6618 }
6619 break;
6620 case T_FALSE:
6621 if (check_cfunc(cme, rb_false_to_s)) {
6622 return rb_false_to_s(recv);
6623 }
6624 break;
6625 case T_FIXNUM:
6626 if (check_cfunc(cme, rb_int_to_s)) {
6627 return rb_fix_to_s(recv);
6628 }
6629 break;
6630 }
6631 return Qundef;
6632}
6633
6634// ZJIT implementation is using the C function
6635// and needs to call a non-static function
6636VALUE
6637rb_vm_objtostring(struct rb_control_frame_struct *reg_cfp, VALUE recv, CALL_DATA cd)
6638{
6639 return vm_objtostring(reg_cfp, recv, cd);
6640}
6641
6642static VALUE
6643vm_opt_ary_freeze(VALUE ary, int bop, ID id)
6644{
6645 if (BASIC_OP_UNREDEFINED_P(bop, ARRAY_REDEFINED_OP_FLAG)) {
6646 return ary;
6647 }
6648 else {
6649 return Qundef;
6650 }
6651}
6652
6653static VALUE
6654vm_opt_hash_freeze(VALUE hash, int bop, ID id)
6655{
6656 if (BASIC_OP_UNREDEFINED_P(bop, HASH_REDEFINED_OP_FLAG)) {
6657 return hash;
6658 }
6659 else {
6660 return Qundef;
6661 }
6662}
6663
6664static VALUE
6665vm_opt_str_freeze(VALUE str, int bop, ID id)
6666{
6667 if (BASIC_OP_UNREDEFINED_P(bop, STRING_REDEFINED_OP_FLAG)) {
6668 return str;
6669 }
6670 else {
6671 return Qundef;
6672 }
6673}
6674
6675/* this macro is mandatory to use OPTIMIZED_CMP. What a design! */
6676#define id_cmp idCmp
6677
6678static VALUE
6679vm_opt_duparray_include_p(rb_execution_context_t *ec, const VALUE ary, VALUE target)
6680{
6681 if (OP_UNREDEFINED_P(INCLUDE_P, ARRAY)) {
6682 return rb_ary_includes(ary, target);
6683 }
6684 else {
6685 VALUE args[1] = {target};
6686
6687 // duparray
6688 RUBY_DTRACE_CREATE_HOOK(ARRAY, RARRAY_LEN(ary));
6689 VALUE dupary = rb_ary_resurrect(ary);
6690
6691 return rb_vm_call_with_refinements(ec, dupary, idIncludeP, 1, args, RB_NO_KEYWORDS);
6692 }
6693}
6694
6695VALUE
6696rb_vm_opt_duparray_include_p(rb_execution_context_t *ec, const VALUE ary, VALUE target)
6697{
6698 return vm_opt_duparray_include_p(ec, ary, target);
6699}
6700
6701static VALUE
6702vm_opt_newarray_max(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6703{
6704 if (OP_UNREDEFINED_P(MAX, ARRAY)) {
6705 if (array_len == 0) {
6706 return Qnil;
6707 }
6708 else {
6709 VALUE result = *ptr;
6710 rb_snum_t i = array_len - 1;
6711 while (i-- > 0) {
6712 const VALUE v = *++ptr;
6713 if (OPTIMIZED_CMP(v, result) > 0) {
6714 result = v;
6715 }
6716 }
6717 return result;
6718 }
6719 }
6720 else {
6721 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idMax, 0, NULL, RB_NO_KEYWORDS);
6722 }
6723}
6724
6725VALUE
6726rb_vm_opt_newarray_max(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6727{
6728 return vm_opt_newarray_max(ec, array_len, ptr);
6729}
6730
6731static VALUE
6732vm_opt_newarray_min(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6733{
6734 if (OP_UNREDEFINED_P(MIN, ARRAY)) {
6735 if (array_len == 0) {
6736 return Qnil;
6737 }
6738 else {
6739 VALUE result = *ptr;
6740 rb_snum_t i = array_len - 1;
6741 while (i-- > 0) {
6742 const VALUE v = *++ptr;
6743 if (OPTIMIZED_CMP(v, result) < 0) {
6744 result = v;
6745 }
6746 }
6747 return result;
6748 }
6749 }
6750 else {
6751 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idMin, 0, NULL, RB_NO_KEYWORDS);
6752 }
6753}
6754
6755VALUE
6756rb_vm_opt_newarray_min(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6757{
6758 return vm_opt_newarray_min(ec, array_len, ptr);
6759}
6760
6761static VALUE
6762vm_opt_newarray_hash(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6763{
6764 // If Array#hash is _not_ monkeypatched, use the optimized call
6765 if (OP_UNREDEFINED_P(HASH, ARRAY)) {
6766 return rb_ary_hash_values(array_len, ptr);
6767 }
6768 else {
6769 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idHash, 0, NULL, RB_NO_KEYWORDS);
6770 }
6771}
6772
6773VALUE
6774rb_vm_opt_newarray_hash(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6775{
6776 return vm_opt_newarray_hash(ec, array_len, ptr);
6777}
6778
6779VALUE rb_setup_fake_ary(struct RArray *fake_ary, const VALUE *list, rb_len_t len);
6780VALUE rb_ec_pack_ary(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer);
6781
6782static VALUE
6783vm_opt_newarray_include_p(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr, VALUE target)
6784{
6785 if (OP_UNREDEFINED_P(INCLUDE_P, ARRAY)) {
6786 struct RArray fake_ary = {RBASIC_INIT};
6787 VALUE ary = rb_setup_fake_ary(&fake_ary, ptr, array_len);
6788 return rb_ary_includes(ary, target);
6789 }
6790 else {
6791 VALUE args[1] = {target};
6792 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idIncludeP, 1, args, RB_NO_KEYWORDS);
6793 }
6794}
6795
6796VALUE
6797rb_vm_opt_newarray_include_p(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr, VALUE target)
6798{
6799 return vm_opt_newarray_include_p(ec, array_len, ptr, target);
6800}
6801
6802static VALUE
6803vm_opt_newarray_pack_buffer(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr, VALUE fmt, VALUE buffer)
6804{
6805 if (OP_UNREDEFINED_P(PACK, ARRAY)) {
6806 struct RArray fake_ary = {RBASIC_INIT};
6807 VALUE ary = rb_setup_fake_ary(&fake_ary, ptr, array_len);
6808 return rb_ec_pack_ary(ec, ary, fmt, (UNDEF_P(buffer) ? Qnil : buffer));
6809 }
6810 else {
6811 // The opt_newarray_send insn drops the keyword args so we need to rebuild them.
6812 // Setup an array with room for keyword hash.
6813 VALUE args[2];
6814 args[0] = fmt;
6815 int kw_splat = RB_NO_KEYWORDS;
6816 int argc = 1;
6817
6818 if (!UNDEF_P(buffer)) {
6819 args[1] = rb_hash_new_capa(1);
6820 rb_hash_aset(args[1], ID2SYM(idBuffer), buffer);
6821 kw_splat = RB_PASS_KEYWORDS;
6822 argc++;
6823 }
6824
6825 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idPack, argc, args, kw_splat);
6826 }
6827}
6828
6829VALUE
6830rb_vm_opt_newarray_pack_buffer(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr, VALUE fmt, VALUE buffer)
6831{
6832 return vm_opt_newarray_pack_buffer(ec, array_len, ptr, fmt, buffer);
6833}
6834
6835
6836#undef id_cmp
6837
6838static void
6839vm_track_constant_cache(ID id, void *ic)
6840{
6841 rb_vm_t *vm = GET_VM();
6842 struct rb_id_table *const_cache = &vm->constant_cache;
6843 VALUE lookup_result;
6844 set_table *ics;
6845
6846 if (rb_id_table_lookup(const_cache, id, &lookup_result)) {
6847 ics = (set_table *)lookup_result;
6848 }
6849 else {
6850 ics = set_init_numtable();
6851 rb_id_table_insert(const_cache, id, (VALUE)ics);
6852 }
6853
6854 /* The call below to st_insert could allocate which could trigger a GC.
6855 * If it triggers a GC, it may free an iseq that also holds a cache to this
6856 * constant. If that iseq is the last iseq with a cache to this constant, then
6857 * it will free this ST table, which would cause an use-after-free during this
6858 * st_insert.
6859 *
6860 * So to fix this issue, we store the ID that is currently being inserted
6861 * and, in remove_from_constant_cache, we don't free the ST table for ID
6862 * equal to this one.
6863 *
6864 * See [Bug #20921].
6865 */
6866 vm->inserting_constant_cache_id = id;
6867
6868 set_insert(ics, (st_data_t)ic);
6869
6870 vm->inserting_constant_cache_id = (ID)0;
6871}
6872
6873static void
6874vm_ic_track_const_chain(rb_control_frame_t *cfp, IC ic, const rb_const_path_t const_path)
6875{
6876 RB_VM_LOCKING() {
6877 size_t i = 0;
6878 ID segment;
6879 while (((segment = const_path_aref(const_path, i)))) {
6880 vm_track_constant_cache(segment, ic);
6881 i++;
6882 }
6883 }
6884}
6885
6886// For JIT inlining
6887static inline bool
6888vm_inlined_ic_hit_p(VALUE flags, VALUE value, const rb_cref_t *ic_cref, rb_serial_t ractor_id, const VALUE *reg_ep)
6889{
6890 // Not rb_ractor_main_p(): an owner Ractor may now cache an unshareable constant
6891 // of its own class, and only it may be handed that value back.
6892 if ((flags & IMEMO_CONST_CACHE_SHAREABLE) || ractor_id == rb_ractor_id(GET_RACTOR())) {
6893 VM_ASSERT(ractor_incidental_shareable_p(flags & IMEMO_CONST_CACHE_SHAREABLE, value));
6894
6895 return (ic_cref == NULL || // no need to check CREF
6896 ic_cref == vm_get_cref(reg_ep));
6897 }
6898 return false;
6899}
6900
6901static bool
6902vm_ic_hit_p(const struct iseq_inline_constant_cache_entry *ice, const VALUE *reg_ep)
6903{
6904 VM_ASSERT(IMEMO_TYPE_P(ice, imemo_constcache));
6905 return vm_inlined_ic_hit_p(ice->flags, ice->value, ice->ic_cref, ice->ractor_id, reg_ep);
6906}
6907
6908// YJIT needs this function to never allocate and never raise
6909bool
6910rb_vm_ic_hit_p(IC ic, const VALUE *reg_ep)
6911{
6912 return ic->entry && vm_ic_hit_p(ic->entry, reg_ep);
6913}
6914
6915static void
6916vm_ic_update(const rb_execution_context_t *ec, const rb_iseq_t *iseq, IC ic, VALUE val, const VALUE *reg_ep, const VALUE *pc)
6917{
6918 if (ruby_vm_const_missing_count > 0) {
6919 ruby_vm_const_missing_count = 0;
6920 ic->entry = NULL;
6921 return;
6922 }
6923
6924 struct iseq_inline_constant_cache_entry *ice = SHAREABLE_IMEMO_NEW(struct iseq_inline_constant_cache_entry, imemo_constcache, 0);
6925 RB_OBJ_WRITE(ice, &ice->value, val);
6926 ice->ic_cref = vm_get_const_key_cref(reg_ep);
6927 ice->ractor_id = rb_ractor_id(rb_ec_ractor_ptr(ec));
6928
6929 if (rb_ractor_shareable_p(val)) {
6930 RUBY_ASSERT((rb_gc_verify_shareable(val), 1));
6931 ice->flags |= IMEMO_CONST_CACHE_SHAREABLE;
6932 }
6933 RB_OBJ_WRITE(iseq, &ic->entry, ice);
6934 RUBY_ASSERT(pc >= ISEQ_BODY(iseq)->iseq_encoded);
6935 unsigned pos = (unsigned)(pc - ISEQ_BODY(iseq)->iseq_encoded);
6936 rb_yjit_constant_ic_update(iseq, ic, pos);
6937}
6938
6939VALUE
6940rb_vm_opt_getconstant_path(rb_execution_context_t *ec, rb_control_frame_t *const reg_cfp, IC ic)
6941{
6942 VALUE val;
6943 const rb_const_path_t const_path = ic->const_path;
6944 struct iseq_inline_constant_cache_entry *ice = ic->entry;
6945
6946 if (ice && vm_ic_hit_p(ice, GET_EP())) {
6947 val = ice->value;
6948
6949 // On a non-main ractor another ractor can concurrently reassign a
6950 // shareable constant (which invalidates ICs asynchronously), so the
6951 // cached-but-still-shareable value may legitimately differ from a
6952 // freshly recomputed chain. Only assert equality when single-ractor.
6953 VM_ASSERT(val == vm_get_ev_const_chain(ec, const_path) || rb_multi_ractor_p());
6954 }
6955 else {
6956 ruby_vm_constant_cache_misses++;
6957 val = vm_get_ev_const_chain(ec, const_path);
6958 vm_ic_track_const_chain(GET_CFP(), ic, const_path);
6959 // Undo the PC increment to get the address to this instruction
6960 // INSN_ATTR(width) == 2
6961 vm_ic_update(ec, CFP_ISEQ(GET_CFP()), ic, val, GET_EP(), CFP_PC(GET_CFP()) - 2);
6962 }
6963 return val;
6964}
6965
6966// Return true if the once value is already computed and set *result to the value.
6967// Otherwise, return false.
6968// Used for ZJIT. Keep in sync with `vm_once_dispatch` below.
6969bool
6970rb_vm_once_done_value(ISE is, VALUE *result)
6971{
6972 rb_thread_t *running_th = rbimpl_atomic_ptr_load((void**)&is->once.running_thread, RBIMPL_ATOMIC_ACQUIRE);
6973 if (running_th == RUNNING_THREAD_ONCE_DONE) {
6974 *result = is->once.value;
6975 return true;
6976 }
6977 return false;
6978}
6979
6980static VALUE
6981vm_once_dispatch(rb_execution_context_t *ec, ISEQ iseq, ISE is)
6982{
6983 rb_thread_t *th = rb_ec_thread_ptr(ec);
6984 rb_thread_t *running_th;
6985
6986 again:
6987 running_th = rbimpl_atomic_ptr_load((void**)&is->once.running_thread, RBIMPL_ATOMIC_ACQUIRE);
6988 if (running_th == RUNNING_THREAD_ONCE_DONE) {
6989 return is->once.value;
6990 }
6991 else if (running_th == NULL) {
6992 VALUE val = Qundef;
6993 enum ruby_tag_type state;
6994 if (rbimpl_atomic_ptr_cas((void**)&is->once.running_thread, running_th, th, RBIMPL_ATOMIC_RELEASE, RBIMPL_ATOMIC_RELAXED) != running_th) {
6995 goto again;
6996 }
6997 EC_PUSH_TAG(ec);
6998 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
6999 val = vm_once_exec((VALUE)iseq);
7000 }
7001 EC_POP_TAG();
7002 if (state != TAG_NONE) {
7003 vm_once_clear((VALUE)is);
7004 EC_JUMP_TAG(ec, state);
7005 }
7006
7007 // TODO: confirm that it is shareable
7008 if (RB_FL_ABLE(val)) {
7010 }
7011
7012 RB_OBJ_WRITE(CFP_ISEQ(ec->cfp), &is->once.value, val);
7013
7014 rbimpl_atomic_ptr_store((volatile void**)&is->once.running_thread, RUNNING_THREAD_ONCE_DONE, RBIMPL_ATOMIC_RELEASE); /* success */
7015 vm_once_broadcast(rb_ec_vm_ptr(ec));
7016 return val;
7017 }
7018 else if (running_th == th) {
7019 /* recursive once */
7020 return vm_once_exec((VALUE)iseq);
7021 }
7022 else {
7023 /* wait for the winner to finish */
7024 struct vm_once_wait_arg arg = { .vm = rb_ec_vm_ptr(ec), .is = is };
7025 rb_nogvl(vm_once_wait_no_gvl, &arg, vm_once_wait_ubf, arg.vm,
7027 RUBY_VM_CHECK_INTS(ec);
7028 goto again;
7029 }
7030}
7031
7032static OFFSET
7033vm_case_dispatch(CDHASH hash, OFFSET else_offset, VALUE key)
7034{
7035 switch (OBJ_BUILTIN_TYPE(key)) {
7036 case -1:
7037 case T_FLOAT:
7038 case T_SYMBOL:
7039 case T_BIGNUM:
7040 case T_STRING:
7041 if (BASIC_OP_UNREDEFINED_P(BOP_EQQ,
7042 SYMBOL_REDEFINED_OP_FLAG |
7043 INTEGER_REDEFINED_OP_FLAG |
7044 FLOAT_REDEFINED_OP_FLAG |
7045 NIL_REDEFINED_OP_FLAG |
7046 TRUE_REDEFINED_OP_FLAG |
7047 FALSE_REDEFINED_OP_FLAG |
7048 STRING_REDEFINED_OP_FLAG)) {
7049 st_data_t val;
7050 if (RB_FLOAT_TYPE_P(key)) {
7051 double kval = RFLOAT_VALUE(key);
7052 if (!isinf(kval) && modf(kval, &kval) == 0.0) {
7053 key = FIXABLE(kval) ? LONG2FIX((long)kval) : rb_dbl2big(kval);
7054 }
7055 }
7056 if (st_lookup(rb_imemo_cdhash_tbl(hash), key, &val)) {
7057 return (VALUE)val;
7058 }
7059 else {
7060 return else_offset;
7061 }
7062 }
7063 }
7064 return 0;
7065}
7066
7067NORETURN(static void
7068 vm_stack_consistency_error(const rb_execution_context_t *ec,
7069 const rb_control_frame_t *,
7070 const VALUE *));
7071static void
7072vm_stack_consistency_error(const rb_execution_context_t *ec,
7073 const rb_control_frame_t *cfp,
7074 const VALUE *bp)
7075{
7076 const ptrdiff_t nsp = VM_SP_CNT(ec, cfp->sp);
7077 const ptrdiff_t nbp = VM_SP_CNT(ec, bp);
7078 static const char stack_consistency_error[] =
7079 "Stack consistency error (sp: %"PRIdPTRDIFF", bp: %"PRIdPTRDIFF")";
7080#if defined RUBY_DEVEL
7081 VALUE mesg = rb_sprintf(stack_consistency_error, nsp, nbp);
7082 rb_str_cat_cstr(mesg, "\n");
7083 rb_str_append(mesg, rb_iseq_disasm(CFP_ISEQ(cfp)));
7085#else
7086 rb_bug(stack_consistency_error, nsp, nbp);
7087#endif
7088}
7089
7090static VALUE
7091vm_opt_plus(VALUE recv, VALUE obj)
7092{
7093 if (FIXNUM_2_P(recv, obj) &&
7094 OP_UNREDEFINED_P(PLUS, INTEGER)) {
7095 return rb_fix_plus_fix(recv, obj);
7096 }
7097 else if (FLONUM_2_P(recv, obj) &&
7098 OP_UNREDEFINED_P(PLUS, FLOAT)) {
7099 return DBL2NUM(RFLOAT_VALUE(recv) + RFLOAT_VALUE(obj));
7100 }
7101 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7102 return Qundef;
7103 }
7104 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7105 RBASIC_CLASS(obj) == rb_cFloat &&
7106 OP_UNREDEFINED_P(PLUS, FLOAT)) {
7107 return DBL2NUM(RFLOAT_VALUE(recv) + RFLOAT_VALUE(obj));
7108 }
7109 else if (RBASIC_CLASS(recv) == rb_cString &&
7110 RBASIC_CLASS(obj) == rb_cString &&
7111 OP_UNREDEFINED_P(PLUS, STRING)) {
7112 return rb_str_opt_plus(recv, obj);
7113 }
7114 else if (RBASIC_CLASS(recv) == rb_cArray &&
7115 RBASIC_CLASS(obj) == rb_cArray &&
7116 OP_UNREDEFINED_P(PLUS, ARRAY)) {
7117 return rb_ary_plus(recv, obj);
7118 }
7119 else {
7120 return Qundef;
7121 }
7122}
7123
7124static VALUE
7125vm_opt_minus(VALUE recv, VALUE obj)
7126{
7127 if (FIXNUM_2_P(recv, obj) &&
7128 OP_UNREDEFINED_P(MINUS, INTEGER)) {
7129 return rb_fix_minus_fix(recv, obj);
7130 }
7131 else if (FLONUM_2_P(recv, obj) &&
7132 OP_UNREDEFINED_P(MINUS, FLOAT)) {
7133 return DBL2NUM(RFLOAT_VALUE(recv) - RFLOAT_VALUE(obj));
7134 }
7135 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7136 return Qundef;
7137 }
7138 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7139 RBASIC_CLASS(obj) == rb_cFloat &&
7140 OP_UNREDEFINED_P(MINUS, FLOAT)) {
7141 return DBL2NUM(RFLOAT_VALUE(recv) - RFLOAT_VALUE(obj));
7142 }
7143 else {
7144 return Qundef;
7145 }
7146}
7147
7148static VALUE
7149vm_opt_mult(VALUE recv, VALUE obj)
7150{
7151 if (FIXNUM_2_P(recv, obj) &&
7152 OP_UNREDEFINED_P(MULT, INTEGER)) {
7153 return rb_fix_mul_fix(recv, obj);
7154 }
7155 else if (FLONUM_2_P(recv, obj) &&
7156 OP_UNREDEFINED_P(MULT, FLOAT)) {
7157 return DBL2NUM(RFLOAT_VALUE(recv) * RFLOAT_VALUE(obj));
7158 }
7159 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7160 return Qundef;
7161 }
7162 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7163 RBASIC_CLASS(obj) == rb_cFloat &&
7164 OP_UNREDEFINED_P(MULT, FLOAT)) {
7165 return DBL2NUM(RFLOAT_VALUE(recv) * RFLOAT_VALUE(obj));
7166 }
7167 else {
7168 return Qundef;
7169 }
7170}
7171
7172static VALUE
7173vm_opt_div(VALUE recv, VALUE obj)
7174{
7175 if (FIXNUM_2_P(recv, obj) &&
7176 OP_UNREDEFINED_P(DIV, INTEGER)) {
7177 return (FIX2LONG(obj) == 0) ? Qundef : rb_fix_div_fix(recv, obj);
7178 }
7179 else if (FLONUM_2_P(recv, obj) &&
7180 OP_UNREDEFINED_P(DIV, FLOAT)) {
7181 return rb_flo_div_flo(recv, obj);
7182 }
7183 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7184 return Qundef;
7185 }
7186 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7187 RBASIC_CLASS(obj) == rb_cFloat &&
7188 OP_UNREDEFINED_P(DIV, FLOAT)) {
7189 return rb_flo_div_flo(recv, obj);
7190 }
7191 else {
7192 return Qundef;
7193 }
7194}
7195
7196static VALUE
7197vm_opt_mod(VALUE recv, VALUE obj)
7198{
7199 if (FIXNUM_2_P(recv, obj) &&
7200 OP_UNREDEFINED_P(MOD, INTEGER)) {
7201 return (FIX2LONG(obj) == 0) ? Qundef : rb_fix_mod_fix(recv, obj);
7202 }
7203 else if (FLONUM_2_P(recv, obj) &&
7204 OP_UNREDEFINED_P(MOD, FLOAT)) {
7205 return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(recv), RFLOAT_VALUE(obj)));
7206 }
7207 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7208 return Qundef;
7209 }
7210 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7211 RBASIC_CLASS(obj) == rb_cFloat &&
7212 OP_UNREDEFINED_P(MOD, FLOAT)) {
7213 return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(recv), RFLOAT_VALUE(obj)));
7214 }
7215 else {
7216 return Qundef;
7217 }
7218}
7219
7220static VALUE
7221vm_opt_neq(struct rb_control_frame_struct *reg_cfp, CALL_DATA cd, CALL_DATA cd_eq, VALUE recv, VALUE obj)
7222{
7223 if (vm_method_cfunc_is(reg_cfp, cd, recv, rb_obj_not_equal)) {
7224 VALUE val = opt_equality(reg_cfp, recv, obj, cd_eq);
7225
7226 if (!UNDEF_P(val)) {
7227 return RBOOL(!RTEST(val));
7228 }
7229 }
7230
7231 return Qundef;
7232}
7233
7234static VALUE
7235vm_opt_lt(VALUE recv, VALUE obj)
7236{
7237 if (FIXNUM_2_P(recv, obj) &&
7238 OP_UNREDEFINED_P(LT, INTEGER)) {
7239 return RBOOL((SIGNED_VALUE)recv < (SIGNED_VALUE)obj);
7240 }
7241 else if (FLONUM_2_P(recv, obj) &&
7242 OP_UNREDEFINED_P(LT, FLOAT)) {
7243 return RBOOL(RFLOAT_VALUE(recv) < RFLOAT_VALUE(obj));
7244 }
7245 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7246 return Qundef;
7247 }
7248 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7249 RBASIC_CLASS(obj) == rb_cFloat &&
7250 OP_UNREDEFINED_P(LT, FLOAT)) {
7251 return RBOOL(RFLOAT_VALUE(recv) < RFLOAT_VALUE(obj));
7252 }
7253 else {
7254 return Qundef;
7255 }
7256}
7257
7258static VALUE
7259vm_opt_le(VALUE recv, VALUE obj)
7260{
7261 if (FIXNUM_2_P(recv, obj) &&
7262 OP_UNREDEFINED_P(LE, INTEGER)) {
7263 return RBOOL((SIGNED_VALUE)recv <= (SIGNED_VALUE)obj);
7264 }
7265 else if (FLONUM_2_P(recv, obj) &&
7266 OP_UNREDEFINED_P(LE, FLOAT)) {
7267 return RBOOL(RFLOAT_VALUE(recv) <= RFLOAT_VALUE(obj));
7268 }
7269 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7270 return Qundef;
7271 }
7272 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7273 RBASIC_CLASS(obj) == rb_cFloat &&
7274 OP_UNREDEFINED_P(LE, FLOAT)) {
7275 return RBOOL(RFLOAT_VALUE(recv) <= RFLOAT_VALUE(obj));
7276 }
7277 else {
7278 return Qundef;
7279 }
7280}
7281
7282static VALUE
7283vm_opt_gt(VALUE recv, VALUE obj)
7284{
7285 if (FIXNUM_2_P(recv, obj) &&
7286 OP_UNREDEFINED_P(GT, INTEGER)) {
7287 return RBOOL((SIGNED_VALUE)recv > (SIGNED_VALUE)obj);
7288 }
7289 else if (FLONUM_2_P(recv, obj) &&
7290 OP_UNREDEFINED_P(GT, FLOAT)) {
7291 return RBOOL(RFLOAT_VALUE(recv) > RFLOAT_VALUE(obj));
7292 }
7293 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7294 return Qundef;
7295 }
7296 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7297 RBASIC_CLASS(obj) == rb_cFloat &&
7298 OP_UNREDEFINED_P(GT, FLOAT)) {
7299 return RBOOL(RFLOAT_VALUE(recv) > RFLOAT_VALUE(obj));
7300 }
7301 else {
7302 return Qundef;
7303 }
7304}
7305
7306static VALUE
7307vm_opt_ge(VALUE recv, VALUE obj)
7308{
7309 if (FIXNUM_2_P(recv, obj) &&
7310 OP_UNREDEFINED_P(GE, INTEGER)) {
7311 return RBOOL((SIGNED_VALUE)recv >= (SIGNED_VALUE)obj);
7312 }
7313 else if (FLONUM_2_P(recv, obj) &&
7314 OP_UNREDEFINED_P(GE, FLOAT)) {
7315 return RBOOL(RFLOAT_VALUE(recv) >= RFLOAT_VALUE(obj));
7316 }
7317 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7318 return Qundef;
7319 }
7320 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7321 RBASIC_CLASS(obj) == rb_cFloat &&
7322 OP_UNREDEFINED_P(GE, FLOAT)) {
7323 return RBOOL(RFLOAT_VALUE(recv) >= RFLOAT_VALUE(obj));
7324 }
7325 else {
7326 return Qundef;
7327 }
7328}
7329
7330
7331static VALUE
7332vm_opt_ltlt(VALUE recv, VALUE obj)
7333{
7334 if (SPECIAL_CONST_P(recv)) {
7335 return Qundef;
7336 }
7337 else if (RBASIC_CLASS(recv) == rb_cString &&
7338 OP_UNREDEFINED_P(LTLT, STRING)) {
7339 if (LIKELY(RB_TYPE_P(obj, T_STRING))) {
7340 return rb_str_buf_append(recv, obj);
7341 }
7342 else {
7343 return rb_str_concat(recv, obj);
7344 }
7345 }
7346 else if (RBASIC_CLASS(recv) == rb_cArray &&
7347 OP_UNREDEFINED_P(LTLT, ARRAY)) {
7348 return rb_ary_push(recv, obj);
7349 }
7350 else {
7351 return Qundef;
7352 }
7353}
7354
7355static VALUE
7356vm_opt_and(VALUE recv, VALUE obj)
7357{
7358 // If recv and obj are both fixnums, then the bottom tag bit
7359 // will be 1 on both. 1 & 1 == 1, so the result value will also
7360 // be a fixnum. If either side is *not* a fixnum, then the tag bit
7361 // will be 0, and we return Qundef.
7362 VALUE ret = ((SIGNED_VALUE) recv) & ((SIGNED_VALUE) obj);
7363
7364 if (FIXNUM_P(ret) &&
7365 OP_UNREDEFINED_P(AND, INTEGER)) {
7366 return ret;
7367 }
7368 else {
7369 return Qundef;
7370 }
7371}
7372
7373static VALUE
7374vm_opt_or(VALUE recv, VALUE obj)
7375{
7376 if (FIXNUM_2_P(recv, obj) &&
7377 OP_UNREDEFINED_P(OR, INTEGER)) {
7378 return recv | obj;
7379 }
7380 else {
7381 return Qundef;
7382 }
7383}
7384
7385static VALUE
7386vm_opt_aref(VALUE recv, VALUE obj)
7387{
7388 if (SPECIAL_CONST_P(recv)) {
7389 if (FIXNUM_2_P(recv, obj) &&
7390 OP_UNREDEFINED_P(AREF, INTEGER)) {
7391 return rb_fix_aref(recv, obj);
7392 }
7393 return Qundef;
7394 }
7395 else if (RBASIC_CLASS(recv) == rb_cArray &&
7396 OP_UNREDEFINED_P(AREF, ARRAY)) {
7397 if (FIXNUM_P(obj)) {
7398 return rb_ary_entry_internal(recv, FIX2LONG(obj));
7399 }
7400 else {
7401 return rb_ary_aref1(recv, obj);
7402 }
7403 }
7404 else if (RBASIC_CLASS(recv) == rb_cHash &&
7405 OP_UNREDEFINED_P(AREF, HASH)) {
7406 return rb_hash_aref(recv, obj);
7407 }
7408 else {
7409 return Qundef;
7410 }
7411}
7412
7413static VALUE
7414vm_opt_aset(VALUE recv, VALUE obj, VALUE set)
7415{
7416 if (SPECIAL_CONST_P(recv)) {
7417 return Qundef;
7418 }
7419 else if (RBASIC_CLASS(recv) == rb_cArray &&
7420 OP_UNREDEFINED_P(ASET, ARRAY) &&
7421 FIXNUM_P(obj)) {
7422 rb_ary_store(recv, FIX2LONG(obj), set);
7423 return set;
7424 }
7425 else if (RBASIC_CLASS(recv) == rb_cHash &&
7426 OP_UNREDEFINED_P(ASET, HASH)) {
7427 rb_hash_aset(recv, obj, set);
7428 return set;
7429 }
7430 else {
7431 return Qundef;
7432 }
7433}
7434
7435static VALUE
7436vm_opt_length(VALUE recv, int bop)
7437{
7438 if (SPECIAL_CONST_P(recv)) {
7439 return Qundef;
7440 }
7441 else if (RBASIC_CLASS(recv) == rb_cString &&
7442 BASIC_OP_UNREDEFINED_P(bop, STRING_REDEFINED_OP_FLAG)) {
7443 if (bop == BOP_EMPTY_P) {
7444 return LEN2NUM(RSTRING_LEN(recv));
7445 }
7446 else {
7447 return rb_str_length(recv);
7448 }
7449 }
7450 else if (RBASIC_CLASS(recv) == rb_cArray &&
7451 BASIC_OP_UNREDEFINED_P(bop, ARRAY_REDEFINED_OP_FLAG)) {
7452 return LEN2NUM(RARRAY_LEN(recv));
7453 }
7454 else if (RBASIC_CLASS(recv) == rb_cHash &&
7455 BASIC_OP_UNREDEFINED_P(bop, HASH_REDEFINED_OP_FLAG)) {
7456 return INT2FIX(RHASH_SIZE(recv));
7457 }
7458 else {
7459 return Qundef;
7460 }
7461}
7462
7463static VALUE
7464vm_opt_empty_p(VALUE recv)
7465{
7466 switch (vm_opt_length(recv, BOP_EMPTY_P)) {
7467 case Qundef: return Qundef;
7468 case INT2FIX(0): return Qtrue;
7469 default: return Qfalse;
7470 }
7471}
7472
7473VALUE rb_false(VALUE obj);
7474
7475static VALUE
7476vm_opt_nil_p(struct rb_control_frame_struct *reg_cfp, CALL_DATA cd, VALUE recv)
7477{
7478 if (NIL_P(recv) &&
7479 OP_UNREDEFINED_P(NIL_P, NIL)) {
7480 return Qtrue;
7481 }
7482 else if (vm_method_cfunc_is(reg_cfp, cd, recv, rb_false)) {
7483 return Qfalse;
7484 }
7485 else {
7486 return Qundef;
7487 }
7488}
7489
7490static VALUE
7491fix_succ(VALUE x)
7492{
7493 switch (x) {
7494 case ~0UL:
7495 /* 0xFFFF_FFFF == INT2FIX(-1)
7496 * `-1.succ` is of course 0. */
7497 return INT2FIX(0);
7498 case RSHIFT(~0UL, 1):
7499 /* 0x7FFF_FFFF == LONG2FIX(0x3FFF_FFFF)
7500 * 0x3FFF_FFFF + 1 == 0x4000_0000, which is a Bignum. */
7501 return rb_uint2big(1UL << (SIZEOF_LONG * CHAR_BIT - 2));
7502 default:
7503 /* LONG2FIX(FIX2LONG(x)+FIX2LONG(y))
7504 * == ((lx*2+1)/2 + (ly*2+1)/2)*2+1
7505 * == lx*2 + ly*2 + 1
7506 * == (lx*2+1) + (ly*2+1) - 1
7507 * == x + y - 1
7508 *
7509 * Here, if we put y := INT2FIX(1):
7510 *
7511 * == x + INT2FIX(1) - 1
7512 * == x + 2 .
7513 */
7514 return x + 2;
7515 }
7516}
7517
7518static VALUE
7519vm_opt_succ(VALUE recv)
7520{
7521 if (FIXNUM_P(recv) &&
7522 OP_UNREDEFINED_P(SUCC, INTEGER)) {
7523 return fix_succ(recv);
7524 }
7525 else if (SPECIAL_CONST_P(recv)) {
7526 return Qundef;
7527 }
7528 else if (RBASIC_CLASS(recv) == rb_cString &&
7529 OP_UNREDEFINED_P(SUCC, STRING)) {
7530 return rb_str_succ(recv);
7531 }
7532 else {
7533 return Qundef;
7534 }
7535}
7536
7537static VALUE
7538vm_opt_not(struct rb_control_frame_struct *reg_cfp, CALL_DATA cd, VALUE recv)
7539{
7540 if (vm_method_cfunc_is(reg_cfp, cd, recv, rb_obj_not)) {
7541 return RBOOL(!RTEST(recv));
7542 }
7543 else {
7544 return Qundef;
7545 }
7546}
7547
7548static VALUE
7549vm_opt_regexpmatch2(VALUE recv, VALUE obj)
7550{
7551 if (SPECIAL_CONST_P(recv)) {
7552 return Qundef;
7553 }
7554 else if (RBASIC_CLASS(recv) == rb_cString &&
7555 CLASS_OF(obj) == rb_cRegexp &&
7556 OP_UNREDEFINED_P(MATCH, STRING)) {
7557 return rb_reg_match(obj, recv);
7558 }
7559 else if (RBASIC_CLASS(recv) == rb_cRegexp &&
7560 OP_UNREDEFINED_P(MATCH, REGEXP)) {
7561 return rb_reg_match(recv, obj);
7562 }
7563 else {
7564 return Qundef;
7565 }
7566}
7567
7568#undef EQ_UNREDEFINED_P
7569#undef OP_UNREDEFINED_P
7570
7571rb_event_flag_t rb_iseq_event_flags(const rb_iseq_t *iseq, size_t pos);
7572
7573NOINLINE(static void vm_trace(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp));
7574
7575static inline void
7576vm_trace_hook(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const VALUE *pc,
7577 rb_event_flag_t pc_events, rb_event_flag_t target_event,
7578 rb_hook_list_t *global_hooks, rb_hook_list_t *local_hooks, VALUE val)
7579{
7580 rb_event_flag_t event = pc_events & target_event;
7581 VALUE self = GET_SELF();
7582
7583 VM_ASSERT(rb_popcount64((uint64_t)event) == 1);
7584
7585 if (local_hooks) local_hooks->running++; // make sure they don't get deleted while global hooks run
7586
7587 if (event & global_hooks->events) {
7588 /* increment PC because source line is calculated with PC-1 */
7589 reg_cfp->pc++;
7590 vm_dtrace(event, ec);
7591 rb_exec_event_hook_orig(ec, global_hooks, event, self, 0, 0, 0 , val, 0);
7592 reg_cfp->pc--;
7593 }
7594
7595 if (local_hooks) local_hooks->running--;
7596 if (local_hooks != NULL) {
7597 if (event & local_hooks->events) {
7598 /* increment PC because source line is calculated with PC-1 */
7599 reg_cfp->pc++;
7600 rb_exec_event_hook_orig(ec, local_hooks, event, self, 0, 0, 0 , val, 0);
7601 reg_cfp->pc--;
7602 }
7603 }
7604}
7605
7606/* Re-fetch the iseq-local hook list before each event: a hook fired by an
7607 * earlier event at this pc can disable the last targeted TracePoint on this
7608 * iseq, which frees local_hooks. Reload from its stable source (the ractor's
7609 * targeted-hooks table) so we never dereference a dangling pointer. */
7610#define VM_TRACE_HOOK(target_event, val) do { \
7611 if ((pc_events & (target_event)) & enabled_flags) { \
7612 if (local_hooks_cnt > 0) local_hooks = rb_iseq_local_hooks(iseq, r, false); \
7613 vm_trace_hook(ec, reg_cfp, pc, pc_events, (target_event), global_hooks, local_hooks, (val)); \
7614 } \
7615} while (0)
7616
7617static VALUE
7618rescue_errinfo(rb_execution_context_t *ec, rb_control_frame_t *cfp)
7619{
7620 VM_ASSERT(VM_FRAME_RUBYFRAME_P(cfp));
7621 VM_ASSERT(ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_RESCUE);
7622 return cfp->ep[VM_ENV_INDEX_LAST_LVAR];
7623}
7624
7625static void
7626vm_trace(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp)
7627{
7628 const VALUE *pc = reg_cfp->pc;
7629 rb_ractor_t *r = rb_ec_ractor_ptr(ec);
7630 rb_event_flag_t enabled_flags = r->pub.hooks.events & ISEQ_TRACE_EVENTS;
7631 rb_event_flag_t ractor_events = enabled_flags;
7632
7633 if (enabled_flags == 0 && rb_ractor_targeted_hooks_cnt(r) == 0) {
7634 return;
7635 }
7636 else {
7637 const rb_iseq_t *iseq = CFP_ISEQ(reg_cfp);
7638 size_t pos = pc - ISEQ_BODY(iseq)->iseq_encoded;
7639 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pos);
7640 unsigned int local_hooks_cnt = iseq->aux.exec.local_hooks_cnt;
7641 rb_hook_list_t *local_hooks = NULL;
7642 if (RB_UNLIKELY(local_hooks_cnt > 0)) {
7643 st_data_t val;
7644 if (st_lookup(rb_ractor_targeted_hooks(r), (st_data_t)iseq, &val)) {
7645 local_hooks = (rb_hook_list_t*)val;
7646 }
7647 }
7648 rb_event_flag_t iseq_local_events = local_hooks != NULL ? local_hooks->events : 0;
7649
7650 rb_hook_list_t *bmethod_local_hooks = NULL;
7651 rb_event_flag_t bmethod_local_events = 0;
7652 unsigned int bmethod_hooks_cnt = 0;
7653 rb_method_definition_t *bmethod_def = NULL;
7654 const bool bmethod_frame = VM_FRAME_BMETHOD_P(reg_cfp);
7655 enabled_flags |= iseq_local_events;
7656
7657 VM_ASSERT((iseq_local_events & ~ISEQ_TRACE_EVENTS) == 0);
7658
7659 if (bmethod_frame) {
7660 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(reg_cfp);
7661 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
7662 bmethod_def = me->def;
7663 bmethod_hooks_cnt = me->def->body.bmethod.local_hooks_cnt;
7664 if (RB_UNLIKELY(bmethod_hooks_cnt > 0)) {
7665 st_data_t val;
7666 if (st_lookup(rb_ractor_targeted_hooks(r), (st_data_t)me->def, &val)) {
7667 bmethod_local_hooks = (rb_hook_list_t*)val;
7668 }
7669 if (bmethod_local_hooks) {
7670 bmethod_local_events = bmethod_local_hooks->events;
7671 }
7672 }
7673 }
7674
7675 if ((pc_events & enabled_flags) == 0 && !bmethod_frame) {
7676#if 0
7677 /* disable trace */
7678 /* TODO: incomplete */
7679 rb_iseq_trace_set(iseq, vm_event_flags & ISEQ_TRACE_EVENTS);
7680#else
7681 /* do not disable trace because of performance problem
7682 * (re-enable overhead)
7683 */
7684#endif
7685 return;
7686 }
7687 else if (ec->trace_arg != NULL) {
7688 /* already tracing */
7689 return;
7690 }
7691 else {
7692 rb_hook_list_t *global_hooks = rb_ec_ractor_hooks(ec);
7693 /* Note, not considering iseq local events here since the same
7694 * iseq could be used in multiple bmethods. */
7695 rb_event_flag_t bmethod_events = ractor_events | bmethod_local_events;
7696
7697 if (0) {
7698 VALUE path = rb_iseq_path(iseq);
7699 VALUE label = rb_iseq_label(iseq);
7700 ruby_debug_printf("vm_trace>>%4d (%4x) - %.*s:%d %.*s\n",
7701 (int)pos,
7702 (int)pc_events,
7703 RSTRING_LENINT(path), RSTRING_PTR(path),
7704 (int)rb_iseq_line_no(iseq, pos),
7705 RSTRING_LENINT(label), RSTRING_PTR(label));
7706 }
7707 VM_ASSERT(reg_cfp->pc == pc);
7708 VM_ASSERT(pc_events != 0);
7709
7710 /* check traces */
7711 if ((pc_events & RUBY_EVENT_B_CALL) && bmethod_frame && (bmethod_events & RUBY_EVENT_CALL)) {
7712 /* b_call instruction running as a method. Fire call event. */
7713 vm_trace_hook(ec, reg_cfp, pc, RUBY_EVENT_CALL, RUBY_EVENT_CALL, global_hooks, bmethod_local_hooks, Qundef);
7714 }
7716 VM_TRACE_HOOK(RUBY_EVENT_RESCUE, rescue_errinfo(ec, reg_cfp));
7717 VM_TRACE_HOOK(RUBY_EVENT_LINE, Qundef);
7718 VM_TRACE_HOOK(RUBY_EVENT_COVERAGE_LINE, Qundef);
7719 VM_TRACE_HOOK(RUBY_EVENT_COVERAGE_BRANCH, Qundef);
7720 VM_TRACE_HOOK(RUBY_EVENT_END | RUBY_EVENT_RETURN | RUBY_EVENT_B_RETURN, TOPN(0));
7721 if ((pc_events & RUBY_EVENT_B_RETURN) && bmethod_frame && (bmethod_events & RUBY_EVENT_RETURN)) {
7722 /* b_return instruction running as a method. Fire return event. */
7723 /* An earlier event's hook at this pc may have freed bmethod_local_hooks
7724 * (see VM_TRACE_HOOK); reload it from its stable source. */
7725 if (bmethod_hooks_cnt > 0) bmethod_local_hooks = rb_method_def_local_hooks(bmethod_def, r, false);
7726 vm_trace_hook(ec, reg_cfp, pc, RUBY_EVENT_RETURN, RUBY_EVENT_RETURN, global_hooks, bmethod_local_hooks, TOPN(0));
7727 }
7728 }
7729 }
7730}
7731#undef VM_TRACE_HOOK
7732
7733#if VM_CHECK_MODE > 0
7734NORETURN( NOINLINE( COLDFUNC
7735void rb_vm_canary_is_found_dead(enum ruby_vminsn_type i, VALUE c)));
7736
7737void
7738Init_vm_stack_canary(void)
7739{
7740 /* This has to be called _after_ our PRNG is properly set up. */
7741 int n = ruby_fill_random_bytes(&vm_stack_canary, sizeof vm_stack_canary, false);
7742 /* Make it a large positive Fixnum. Arguments a block does not take
7743 * stay at the stack top, where vm_push_frame checks the canary. */
7744 vm_stack_canary >>= 2;
7745 vm_stack_canary |= (VALUE)1 << (SIZEOF_VALUE * CHAR_BIT - 2);
7746 vm_stack_canary |= 0x01; // valid VALUE (Fixnum)
7747
7748 vm_stack_canary_was_born = true;
7749 VM_ASSERT(n == 0);
7750}
7751
7752void
7753rb_vm_canary_is_found_dead(enum ruby_vminsn_type i, VALUE c)
7754{
7755 /* Because a method has already been called, why not call
7756 * another one. */
7757 const char *insn = rb_insns_name(i);
7758 VALUE inspection = rb_inspect(c);
7759 const char *str = StringValueCStr(inspection);
7760
7761 rb_bug("dead canary found at %s: %s", insn, str);
7762}
7763
7764#else
7765void Init_vm_stack_canary(void) { /* nothing to do */ }
7766#endif
7767
7768
7769/* a part of the following code is generated by this ruby script:
7770
777116.times{|i|
7772 typedef_args = (0...i).map{|j| "VALUE v#{j+1}"}.join(", ")
7773 typedef_args.prepend(", ") if i != 0
7774 call_args = (0...i).map{|j| "argv[#{j}]"}.join(", ")
7775 call_args.prepend(", ") if i != 0
7776 puts %Q{
7777static VALUE
7778builtin_invoker#{i}(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7779{
7780 typedef VALUE (*rb_invoke_funcptr#{i}_t)(rb_execution_context_t *ec, VALUE self#{typedef_args});
7781 return (*(rb_invoke_funcptr#{i}_t)funcptr)(ec, self#{call_args});
7782}}
7783}
7784
7785puts
7786puts "static VALUE (* const cfunc_invokers[])(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr) = {"
778716.times{|i|
7788 puts " builtin_invoker#{i},"
7789}
7790puts "};"
7791*/
7792
7793static VALUE
7794builtin_invoker0(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7795{
7796 typedef VALUE (*rb_invoke_funcptr0_t)(rb_execution_context_t *ec, VALUE self);
7797 return (*(rb_invoke_funcptr0_t)funcptr)(ec, self);
7798}
7799
7800static VALUE
7801builtin_invoker1(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7802{
7803 typedef VALUE (*rb_invoke_funcptr1_t)(rb_execution_context_t *ec, VALUE self, VALUE v1);
7804 return (*(rb_invoke_funcptr1_t)funcptr)(ec, self, argv[0]);
7805}
7806
7807static VALUE
7808builtin_invoker2(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7809{
7810 typedef VALUE (*rb_invoke_funcptr2_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2);
7811 return (*(rb_invoke_funcptr2_t)funcptr)(ec, self, argv[0], argv[1]);
7812}
7813
7814static VALUE
7815builtin_invoker3(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7816{
7817 typedef VALUE (*rb_invoke_funcptr3_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3);
7818 return (*(rb_invoke_funcptr3_t)funcptr)(ec, self, argv[0], argv[1], argv[2]);
7819}
7820
7821static VALUE
7822builtin_invoker4(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7823{
7824 typedef VALUE (*rb_invoke_funcptr4_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4);
7825 return (*(rb_invoke_funcptr4_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3]);
7826}
7827
7828static VALUE
7829builtin_invoker5(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7830{
7831 typedef VALUE (*rb_invoke_funcptr5_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5);
7832 return (*(rb_invoke_funcptr5_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4]);
7833}
7834
7835static VALUE
7836builtin_invoker6(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7837{
7838 typedef VALUE (*rb_invoke_funcptr6_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6);
7839 return (*(rb_invoke_funcptr6_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
7840}
7841
7842static VALUE
7843builtin_invoker7(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7844{
7845 typedef VALUE (*rb_invoke_funcptr7_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6, VALUE v7);
7846 return (*(rb_invoke_funcptr7_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
7847}
7848
7849static VALUE
7850builtin_invoker8(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7851{
7852 typedef VALUE (*rb_invoke_funcptr8_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6, VALUE v7, VALUE v8);
7853 return (*(rb_invoke_funcptr8_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
7854}
7855
7856static VALUE
7857builtin_invoker9(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7858{
7859 typedef VALUE (*rb_invoke_funcptr9_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6, VALUE v7, VALUE v8, VALUE v9);
7860 return (*(rb_invoke_funcptr9_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
7861}
7862
7863static VALUE
7864builtin_invoker10(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7865{
7866 typedef VALUE (*rb_invoke_funcptr10_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6, VALUE v7, VALUE v8, VALUE v9, VALUE v10);
7867 return (*(rb_invoke_funcptr10_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
7868}
7869
7870static VALUE
7871builtin_invoker11(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7872{
7873 typedef VALUE (*rb_invoke_funcptr11_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6, VALUE v7, VALUE v8, VALUE v9, VALUE v10, VALUE v11);
7874 return (*(rb_invoke_funcptr11_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]);
7875}
7876
7877static VALUE
7878builtin_invoker12(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7879{
7880 typedef VALUE (*rb_invoke_funcptr12_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6, VALUE v7, VALUE v8, VALUE v9, VALUE v10, VALUE v11, VALUE v12);
7881 return (*(rb_invoke_funcptr12_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]);
7882}
7883
7884static VALUE
7885builtin_invoker13(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7886{
7887 typedef VALUE (*rb_invoke_funcptr13_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6, VALUE v7, VALUE v8, VALUE v9, VALUE v10, VALUE v11, VALUE v12, VALUE v13);
7888 return (*(rb_invoke_funcptr13_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12]);
7889}
7890
7891static VALUE
7892builtin_invoker14(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7893{
7894 typedef VALUE (*rb_invoke_funcptr14_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6, VALUE v7, VALUE v8, VALUE v9, VALUE v10, VALUE v11, VALUE v12, VALUE v13, VALUE v14);
7895 return (*(rb_invoke_funcptr14_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13]);
7896}
7897
7898static VALUE
7899builtin_invoker15(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7900{
7901 typedef VALUE (*rb_invoke_funcptr15_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5, VALUE v6, VALUE v7, VALUE v8, VALUE v9, VALUE v10, VALUE v11, VALUE v12, VALUE v13, VALUE v14, VALUE v15);
7902 return (*(rb_invoke_funcptr15_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14]);
7903}
7904
7905typedef VALUE (*builtin_invoker)(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr);
7906
7907static builtin_invoker
7908lookup_builtin_invoker(int argc)
7909{
7910 static const builtin_invoker invokers[] = {
7911 builtin_invoker0,
7912 builtin_invoker1,
7913 builtin_invoker2,
7914 builtin_invoker3,
7915 builtin_invoker4,
7916 builtin_invoker5,
7917 builtin_invoker6,
7918 builtin_invoker7,
7919 builtin_invoker8,
7920 builtin_invoker9,
7921 builtin_invoker10,
7922 builtin_invoker11,
7923 builtin_invoker12,
7924 builtin_invoker13,
7925 builtin_invoker14,
7926 builtin_invoker15,
7927 };
7928
7929 return invokers[argc];
7930}
7931
7932static inline VALUE
7933invoke_bf(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const struct rb_builtin_function* bf, const VALUE *argv)
7934{
7935 const bool canary_p = ISEQ_BODY(CFP_ISEQ(reg_cfp))->builtin_attrs & BUILTIN_ATTR_LEAF; // Verify an assumption of `Primitive.attr! :leaf`
7936 SETUP_CANARY(canary_p);
7937 rb_insn_func_t func_ptr = (rb_insn_func_t)(uintptr_t)bf->func_ptr;
7938 VALUE ret = (*lookup_builtin_invoker(bf->argc))(ec, reg_cfp->self, argv, func_ptr);
7939 CHECK_CANARY(canary_p, BIN(invokebuiltin));
7940 return ret;
7941}
7942
7943static VALUE
7944vm_invoke_builtin(rb_execution_context_t *ec, rb_control_frame_t *cfp, const struct rb_builtin_function* bf, const VALUE *argv)
7945{
7946 return invoke_bf(ec, cfp, bf, argv);
7947}
7948
7949static VALUE
7950vm_invoke_builtin_delegate(rb_execution_context_t *ec, rb_control_frame_t *cfp, const struct rb_builtin_function *bf, unsigned int start_index)
7951{
7952 if (0) { // debug print
7953 fputs("vm_invoke_builtin_delegate: passing -> ", stderr);
7954 for (int i=0; i<bf->argc; i++) {
7955 ruby_debug_printf(":%s ", rb_id2name(ISEQ_BODY(CFP_ISEQ(cfp))->local_table[i+start_index]));
7956 }
7957 ruby_debug_printf("\n" "%s %s(%d):%p\n", RUBY_FUNCTION_NAME_STRING, bf->name, bf->argc,
7958 (void *)(uintptr_t)bf->func_ptr);
7959 }
7960
7961 if (bf->argc == 0) {
7962 return invoke_bf(ec, cfp, bf, NULL);
7963 }
7964 else {
7965 const VALUE *argv = cfp->ep - ISEQ_BODY(CFP_ISEQ(cfp))->local_table_size - VM_ENV_DATA_SIZE + 1 + start_index;
7966 return invoke_bf(ec, cfp, bf, argv);
7967 }
7968}
7969
7970// for __builtin_inline!()
7971
7972VALUE
7973rb_vm_lvar_exposed(rb_execution_context_t *ec, int index)
7974{
7975 const rb_control_frame_t *cfp = ec->cfp;
7976 return cfp->ep[index];
7977}
#define RUBY_EVENT_END
Encountered an end of a class clause.
Definition event.h:40
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
Definition event.h:43
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
Definition event.h:56
#define RUBY_EVENT_CLASS
Encountered a new class.
Definition event.h:39
#define RUBY_EVENT_LINE
Encountered a new line.
Definition event.h:38
#define RUBY_EVENT_RETURN
Encountered a return statement.
Definition event.h:42
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition event.h:44
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
Definition event.h:55
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
Definition event.h:41
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
Definition event.h:61
static VALUE RB_FL_TEST_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_TEST().
Definition fl_type.h:407
static bool RB_FL_ABLE(VALUE obj)
Checks if the object is flaggable.
Definition fl_type.h:384
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:253
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:3061
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1665
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:1565
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:1544
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define LEN2NUM
Old name of RB_LEN2NUM.
Definition len_t.h:33
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define T_NONE
Old name of RUBY_T_NONE.
Definition value_type.h:74
#define rb_ary_new4
Old name of rb_ary_new_from_values.
Definition array.h:659
#define FIXABLE
Old name of RB_FIXABLE.
Definition fixnum.h:25
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define FIX2INT
Old name of RB_FIX2INT.
Definition int.h:41
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
#define FIX2ULONG
Old name of RB_FIX2ULONG.
Definition long.h:47
#define T_TRUE
Old name of RUBY_T_TRUE.
Definition value_type.h:81
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#define rb_exc_new3
Old name of rb_exc_new_str.
Definition error.h:38
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:127
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
Definition fl_type.h:67
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
#define rb_notimplement()
Definition flock.c:3
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:476
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1457
VALUE rb_eFatal
fatal exception.
Definition error.c:1453
VALUE rb_eNoMethodError
NoMethodError exception.
Definition error.c:1465
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
Definition eval.c:691
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1455
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:464
void rb_error_frozen_object(VALUE frozen_obj)
Identical to rb_error_frozen(), except it takes arbitrary Ruby object instead of C's string.
Definition error.c:4305
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1508
@ RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK
Warning is for checking unused block strictly.
Definition error.h:57
VALUE rb_cClass
Class class.
Definition object.c:62
VALUE rb_cArray
Array class.
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2264
VALUE rb_cRegexp
Regexp class.
Definition re.c:3624
VALUE rb_obj_frozen_p(VALUE obj)
Same as RB_OBJ_FROZEN(), but returns Qtrue/Qfalse instead of bool.
Definition object.c:1312
VALUE rb_cHash
Hash class.
Definition hash.c:123
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:230
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:665
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:58
VALUE rb_cModule
Module class.
Definition object.c:61
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:221
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:902
VALUE rb_cFloat
Float class.
Definition numeric.c:201
VALUE rb_cProc
Proc class.
Definition proc.c:46
VALUE rb_cString
String class.
Definition string.c:85
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:504
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:492
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1149
int rb_during_gc(void)
Queries if the GC is busy.
Definition gc.c:5713
VALUE rb_ary_concat(VALUE lhs, VALUE rhs)
Destructively appends the contents of latter into the end of former.
VALUE rb_ary_shift(VALUE ary)
Destructively deletes an element from the beginning of the passed array and returns what was deleted.
VALUE rb_ary_resurrect(VALUE ary)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
VALUE rb_ary_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_includes(VALUE ary, VALUE elem)
Queries if the passed array has the passed entry.
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_ary_plus(VALUE lhs, VALUE rhs)
Creates a new array, concatenating the former to the latter.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, rb_len_t len)
Destructively appends multiple elements at the end of the array.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc)
Identical to rb_proc_call(), except you can additionally pass another proc object,...
Definition proc.c:1774
VALUE rb_reg_last_match(VALUE md)
This just returns the argument, stringified.
Definition re.c:2736
VALUE rb_reg_match(VALUE re, VALUE str)
This is the match operator.
Definition re.c:4818
VALUE rb_reg_nth_match(int n, VALUE md)
Queries the nth captured substring.
Definition re.c:2712
VALUE rb_reg_match_post(VALUE md)
The portion of the original string after the given match.
Definition re.c:2794
VALUE rb_reg_nth_defined(int n, VALUE md)
Identical to rb_reg_nth_match(), except it just returns Boolean.
Definition re.c:2696
VALUE rb_reg_match_pre(VALUE md)
The portion of the original string before the given match.
Definition re.c:2762
VALUE rb_reg_match_last(VALUE md)
The portion of the original string that captured at the very last.
Definition re.c:2823
bool rb_set_lookup(VALUE set, VALUE element)
Whether the set contains the given element.
Definition set.c:2387
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3995
VALUE rb_str_succ(VALUE orig)
Searches for the "successor" of a string.
Definition string.c:5733
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
Definition string.c:3959
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition string.c:4244
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1681
VALUE rb_str_length(VALUE)
Identical to rb_str_strlen(), except it returns the value in rb_cInteger.
Definition string.c:2522
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:1097
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3552
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:2185
void rb_cvar_set(VALUE klass, ID name, VALUE val)
Assigns a value to a class variable.
Definition variable.c:4346
VALUE rb_cvar_find(VALUE klass, ID name, VALUE *front)
Identical to rb_cvar_get(), except it takes additional "front" pointer.
Definition variable.c:4407
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1665
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:4035
VALUE rb_autoload_load(VALUE space, ID name)
Kicks the autoload procedure as if it was "touched".
Definition variable.c:3373
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
Definition variable.c:151
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:3558
void rb_set_class_path_string(VALUE klass, VALUE space, VALUE name)
Identical to rb_set_class_path(), except it accepts the name as Ruby's string instead of C's.
Definition variable.c:440
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition variable.c:2245
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition variable.c:3893
VALUE rb_cvar_defined(VALUE klass, ID name)
Queries if the given class has the given class variable.
Definition variable.c:4432
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition variable.c:3887
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well... Let us hesitate from describing what a "basic definition" is.
Definition vm_method.c:3643
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:716
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
Definition vm_method.c:1913
int rb_method_boundp(VALUE klass, ID id, int ex)
Queries if the klass has this method.
Definition vm_method.c:2535
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1302
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:1161
int off
Offset inside of ptr.
Definition io.h:5
int len
Length of the buffer.
Definition io.h:8
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
Definition ractor.h:290
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:269
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:255
#define RB_NOGVL_UBF_ASYNC_SAFE
Passing this flag to rb_nogvl() indicates that the passed UBF is async-signal-safe.
Definition thread.h:71
#define RB_NOGVL_INTR_FAIL
Passing this flag to rb_nogvl() prevents it from processing interrupts after the given function retur...
Definition thread.h:50
void * rb_nogvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int flags)
Identical to rb_thread_call_without_gvl(), except it additionally takes "flags" that change the behav...
Definition thread.c:1775
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:375
#define ALLOCA_N(type, n)
Definition memory.h:295
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
Definition memory.h:387
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:256
static VALUE * RARRAY_PTR(VALUE ary)
Wild use of a C pointer.
Definition rarray.h:341
#define RARRAY_AREF(a, i)
References an object in an array.
Definition rarray.h:398
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:57
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:67
static int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
Definition rstring.h:438
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
#define RB_PASS_KEYWORDS
Pass keywords, final argument must be a hash of keywords.
Definition scan_args.h:72
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition stdarg.h:64
Ruby's array.
Definition rarray.h:128
const VALUE ary[1]
Embedded elements.
Definition rarray.h:188
Definition hash.h:54
Definition iseq.h:360
Definition vm_core.h:263
Definition method.h:63
Internal header for Class.
Definition class.h:31
rb_cref_t * cref
class reference, should be marked
Definition method.h:144
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:143
IFUNC (Internal FUNCtion).
Definition imemo.h:89
SVAR (Special VARiable).
Definition imemo.h:54
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition imemo.h:56
THROW_DATA.
Definition imemo.h:63
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_cond_broadcast(rb_nativethread_cond_t *cond)
Signals a condition variable.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
Waits for the passed condition variable to be signalled.
Definition vm_core.h:290
Represent a constant's path, e.g.
Definition const_path.h:15
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
Definition value.h:69
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:182
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition value_type.h:264
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376