Ruby 4.1.0dev (2026-12-03 revision 75078c9cc813acc3e097cde5614e37455c775ca4)
vm_insnhelper.c (75078c9cc813acc3e097cde5614e37455c775ca4)
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 /* Not `*cfp->sp++ = ...`: the compiler may bump sp before the
1952 * call, and a GC run by the allocation would then mark the
1953 * still unwritten slot. */
1954 *cfp->sp = rb_ary_new4(len - j, ptr);
1955 cfp->sp++;
1956 }
1957 }
1958 else {
1959 /* normal: ary[num..-1], ary[num-2], ary[num-3], ..., ary[0] # top */
1960 if (is_splat) {
1961 if (num > len) {
1962 *cfp->sp = rb_ary_new();
1963 }
1964 else {
1965 *cfp->sp = rb_ary_new4(len - num, ptr + num);
1966 }
1967 cfp->sp++;
1968 }
1969
1970 if (num > len) {
1971 rb_num_t i = 0;
1972 for (; i < num - len; i++) {
1973 *cfp->sp++ = Qnil;
1974 }
1975
1976 for (rb_num_t j = 0; i < num; i++, j++) {
1977 *cfp->sp++ = ptr[len - j - 1];
1978 }
1979 }
1980 else {
1981 for (rb_num_t j = 0; j < num; j++) {
1982 *cfp->sp++ = ptr[num - j - 1];
1983 }
1984 }
1985 }
1986
1987 RB_GC_GUARD(ary);
1988}
1989
1990static VALUE vm_call_general(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling);
1991
1992static VALUE vm_mtbl_dump(VALUE klass, ID target_mid);
1993
1994static struct rb_class_cc_entries *
1995vm_ccs_create(VALUE klass, VALUE cc_tbl, ID mid, const rb_callable_method_entry_t *cme)
1996{
1997 int initial_capa = 2;
1998 struct rb_class_cc_entries *ccs = ruby_xmalloc(vm_ccs_alloc_size(initial_capa));
1999#if VM_CHECK_MODE > 0
2000 ccs->debug_sig = ~(VALUE)ccs;
2001#endif
2002 ccs->capa = initial_capa;
2003 ccs->len = 0;
2004 ccs->cme = cme;
2005 METHOD_ENTRY_CACHED_SET((rb_callable_method_entry_t *)cme);
2006
2007 rb_managed_id_table_insert(cc_tbl, mid, (VALUE)ccs);
2008 RB_OBJ_WRITTEN(cc_tbl, Qundef, cme);
2009 return ccs;
2010}
2011
2012static void
2013vm_ccs_push(VALUE cc_tbl, ID mid, struct rb_class_cc_entries *ccs, const struct rb_callinfo *ci, const struct rb_callcache *cc)
2014{
2015 if (! vm_cc_markable(cc)) {
2016 return;
2017 }
2018
2019 if (UNLIKELY(ccs->len == ccs->capa)) {
2020 RUBY_ASSERT(ccs->capa > 0);
2021 ccs->capa *= 2;
2022 ccs = ruby_xrealloc(ccs, vm_ccs_alloc_size(ccs->capa));
2023#if VM_CHECK_MODE > 0
2024 ccs->debug_sig = ~(VALUE)ccs;
2025#endif
2026 // GC?
2027 rb_managed_id_table_insert(cc_tbl, mid, (VALUE)ccs);
2028 }
2029 VM_ASSERT(ccs->len < ccs->capa);
2030
2031 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(ci);
2032 const int pos = ccs->len++;
2033 ccs->entries[pos].argc = vm_ci_argc(ci);
2034 ccs->entries[pos].flag = (unsigned short)vm_ci_flag(ci);
2035 ccs->entries[pos].kw_len = (unsigned short)(kwarg ? kwarg->keyword_len : 0);
2036 RB_OBJ_WRITE(cc_tbl, &ccs->entries[pos].cc, cc);
2037
2038 if (RB_DEBUG_COUNTER_SETMAX(ccs_maxlen, ccs->len)) {
2039 // for tuning
2040 // vm_mtbl_dump(klass, 0);
2041 }
2042}
2043
2044#if VM_CHECK_MODE > 0
2045void
2046rb_vm_ccs_dump(struct rb_class_cc_entries *ccs)
2047{
2048 ruby_debug_printf("ccs:%p (%d,%d)\n", (void *)ccs, ccs->len, ccs->capa);
2049 for (int i=0; i<ccs->len; i++) {
2050 ruby_debug_printf("CCS CI ID:flag:%x argc:%u kw_len:%u\n",
2051 ccs->entries[i].flag,
2052 ccs->entries[i].argc,
2053 ccs->entries[i].kw_len);
2054 rp(ccs->entries[i].cc);
2055 }
2056}
2057
2058static int
2059vm_ccs_verify(struct rb_class_cc_entries *ccs, ID mid, VALUE klass)
2060{
2061 VM_ASSERT(vm_ccs_p(ccs));
2062 VM_ASSERT(ccs->len <= ccs->capa);
2063
2064 for (int i=0; i<ccs->len; i++) {
2065 const struct rb_callcache *cc = ccs->entries[i].cc;
2066
2067 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
2068 VM_ASSERT(vm_cc_class_check(cc, klass));
2069 VM_ASSERT(vm_cc_check_cme(cc, ccs->cme));
2070 VM_ASSERT(!vm_cc_super_p(cc));
2071 VM_ASSERT(!vm_cc_refinement_p(cc));
2072 }
2073 return TRUE;
2074}
2075#endif
2076
2077const rb_callable_method_entry_t *rb_check_overloaded_cme(const rb_callable_method_entry_t *cme, const struct rb_callinfo * const ci);
2078
2079static void
2080vm_evict_cc(VALUE klass, VALUE cc_tbl, ID mid)
2081{
2082 ASSERT_vm_locking();
2083
2084 if (rb_multi_ractor_p()) {
2085 if (RCLASS_WRITABLE_CC_TBL(klass) != cc_tbl) {
2086 // Another ractor updated the CC table while we were waiting on the VM lock.
2087 // We have to retry.
2088 return;
2089 }
2090
2091 VALUE ccs_obj = 0;
2092 rb_managed_id_table_lookup(cc_tbl, mid, &ccs_obj);
2093 struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_obj;
2094
2095 if (!ccs || !METHOD_ENTRY_INVALIDATED(ccs->cme)) {
2096 // Another ractor replaced that entry while we were waiting on the VM lock.
2097 return;
2098 }
2099
2100 VALUE new_table = rb_vm_cc_table_dup(cc_tbl);
2101 rb_vm_cc_table_delete(new_table, mid);
2102 RB_OBJ_ATOMIC_WRITE(klass, &RCLASS_WRITABLE_CC_TBL(klass), new_table);
2103 }
2104 else {
2105 rb_vm_cc_table_delete(cc_tbl, mid);
2106 }
2107}
2108
2109static const struct rb_callcache *
2110vm_populate_cc(VALUE klass, const struct rb_callinfo * const ci, ID mid)
2111{
2112 ASSERT_vm_locking();
2113
2114 RB_DEBUG_COUNTER_INC(cc_not_found_in_ccs);
2115
2116 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, mid);
2117
2118 VM_ASSERT(cme == NULL || IMEMO_TYPE_P(cme, imemo_ment));
2119
2120 if (cme == NULL) {
2121 // undef or not found: can't cache the information
2122 VM_ASSERT(vm_cc_cme(&rb_vm_empty_cc) == NULL);
2123 return &rb_vm_empty_cc;
2124 }
2125
2126 if (UNLIKELY(cme->def->uncached && !(vm_ci_flag(ci) & VM_CALL_SUPER))) {
2127 // Leaving a cc here would cost a ccs and a cc table entry on klass, which
2128 // is usually a metaclass, so the cost would scale with the number of
2129 // classes. invokesuper is excluded because vm_search_super_method needs a
2130 // cme in the cc it gets back.
2131 return &rb_vm_uncached_cc;
2132 }
2133
2134 VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
2135 const VALUE original_cc_table = cc_tbl;
2136 if (!cc_tbl) {
2137 // Is this possible after rb_callable_method_entry ?
2138 cc_tbl = rb_vm_cc_table_create(1);
2139 }
2140 else if (rb_multi_ractor_p()) {
2141 cc_tbl = rb_vm_cc_table_dup(cc_tbl);
2142 }
2143
2144 VM_ASSERT(cme == rb_callable_method_entry(klass, mid));
2145
2146 METHOD_ENTRY_CACHED_SET((struct rb_callable_method_entry_struct *)cme);
2147
2148 VM_ASSERT(cc_tbl);
2149
2150 struct rb_class_cc_entries *ccs = NULL;
2151 {
2152 VALUE ccs_obj;
2153 if (UNLIKELY(rb_managed_id_table_lookup(cc_tbl, mid, &ccs_obj))) {
2154 ccs = (struct rb_class_cc_entries *)ccs_obj;
2155 }
2156 else {
2157 // TODO: required?
2158 ccs = vm_ccs_create(klass, cc_tbl, mid, cme);
2159 }
2160 }
2161
2162 cme = rb_check_overloaded_cme(cme, ci);
2163
2164 const struct rb_callcache *cc = vm_cc_new(klass, cme, vm_call_general, cc_type_normal);
2165 vm_ccs_push(cc_tbl, mid, ccs, ci, cc);
2166
2167 VM_ASSERT(vm_cc_cme(cc) != NULL);
2168 VM_ASSERT(cme->called_id == mid);
2169 VM_ASSERT(vm_cc_cme(cc)->called_id == mid);
2170
2171 if (original_cc_table != cc_tbl) {
2172 RB_OBJ_ATOMIC_WRITE(klass, &RCLASS_WRITABLE_CC_TBL(klass), cc_tbl);
2173 }
2174
2175 return cc;
2176}
2177
2178static const struct rb_callcache *
2179vm_lookup_cc(const VALUE klass, const struct rb_callinfo * const ci, ID mid)
2180{
2181 VALUE cc_tbl;
2182 struct rb_class_cc_entries *ccs;
2183retry:
2184 cc_tbl = RUBY_ATOMIC_VALUE_LOAD(RCLASS_WRITABLE_CC_TBL(klass));
2185 ccs = NULL;
2186
2187 if (cc_tbl) {
2188 // CCS data is keyed on method id, so we don't need the method id
2189 // for doing comparisons in the `for` loop below.
2190
2191 VALUE ccs_obj;
2192 if (rb_managed_id_table_lookup(cc_tbl, mid, &ccs_obj)) {
2193 ccs = (struct rb_class_cc_entries *)ccs_obj;
2194 const int ccs_len = ccs->len;
2195
2196 if (UNLIKELY(METHOD_ENTRY_INVALIDATED(ccs->cme))) {
2197 RB_VM_LOCKING() {
2198 vm_evict_cc(klass, cc_tbl, mid);
2199 }
2200 goto retry;
2201 }
2202 else if (UNDEFINED_METHOD_ENTRY_P(ccs->cme)) {
2203 // vm_populate_cc would return the empty cc for this too
2204 return &rb_vm_empty_cc;
2205 }
2206 else {
2207 VM_ASSERT(vm_ccs_verify(ccs, mid, klass));
2208
2209 // We already know the method id is correct because we had
2210 // to look up the ccs_data by method id. We compare argc and
2211 // flag, plus the keyword argument count: two call sites with
2212 // the same argc and flag can still differ in how many of the
2213 // arguments are keywords (e.g. `m(a: 1, b: 2)` vs `m(1, b: 2)`),
2214 // which selects a different argument setup fastpath.
2215 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(ci);
2216 unsigned int argc = vm_ci_argc(ci);
2217 unsigned int flag = vm_ci_flag(ci);
2218 unsigned int kw_len = kwarg ? kwarg->keyword_len : 0;
2219
2220 for (int i=0; i<ccs_len; i++) {
2221 unsigned int ccs_ci_argc = ccs->entries[i].argc;
2222 unsigned int ccs_ci_flag = ccs->entries[i].flag;
2223 unsigned int ccs_ci_kw_len = ccs->entries[i].kw_len;
2224 const struct rb_callcache *ccs_cc = ccs->entries[i].cc;
2225
2226 VM_ASSERT(IMEMO_TYPE_P(ccs_cc, imemo_callcache));
2227
2228 if (ccs_ci_argc == argc && ccs_ci_flag == flag && ccs_ci_kw_len == kw_len) {
2229 RB_DEBUG_COUNTER_INC(cc_found_in_ccs);
2230
2231 VM_ASSERT(vm_cc_cme(ccs_cc)->called_id == mid);
2232 VM_ASSERT(ccs_cc->klass == klass);
2233 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(vm_cc_cme(ccs_cc)));
2234
2235 return ccs_cc;
2236 }
2237 }
2238 }
2239 }
2240 }
2241
2242 RB_GC_GUARD(cc_tbl);
2243 return NULL;
2244}
2245
2246static const struct rb_callcache *
2247vm_search_cc(const VALUE klass, const struct rb_callinfo * const ci)
2248{
2249 const ID mid = vm_ci_mid(ci);
2250
2251 const struct rb_callcache *cc = vm_lookup_cc(klass, ci, mid);
2252 if (cc) {
2253 return cc;
2254 }
2255
2256 RB_VM_LOCKING() {
2257 if (rb_multi_ractor_p()) {
2258 // The CC may have been populated by another ractor while we were waiting on the lock,
2259 // so we must lookup a second time.
2260 cc = vm_lookup_cc(klass, ci, mid);
2261 }
2262
2263 if (!cc) {
2264 cc = vm_populate_cc(klass, ci, mid);
2265 }
2266 }
2267
2268 return cc;
2269}
2270
2271#if VM_CHECK_MODE > 0
2272static bool
2273vm_cc_resolved_p(const struct rb_callcache *cc)
2274{
2275 return cc != &rb_vm_empty_cc && cc != &rb_vm_uncached_cc;
2276}
2277#endif
2278
2279const struct rb_callcache *
2280rb_vm_search_method_slowpath(const struct rb_callinfo *ci, VALUE klass)
2281{
2282 const struct rb_callcache *cc;
2283
2284 VM_ASSERT(!SPECIAL_CONST_P(klass));
2285
2286 if (RB_BUILTIN_TYPE(klass) == T_NONE) {
2287 // If we find a T_NONE here, it's most likely we called CLASS_OF(obj) on a
2288 // garbage collected object (the freelist is stored in the class pointer),
2289 // but it's possible that just the class was GC'd.
2290 // This message intentionally tries to imply the former, but make an
2291 // accurate statement for either case.
2292 rb_bug("attempted to search method '%s' on a garbage collected object",
2293 rb_id2name(vm_ci_mid(ci)));
2294 }
2295
2296 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
2297
2298 cc = vm_search_cc(klass, ci);
2299
2300 VM_ASSERT(cc);
2301 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
2302 VM_ASSERT(!vm_cc_resolved_p(cc) || cc->klass == klass);
2303 VM_ASSERT(!vm_cc_resolved_p(cc) || callable_method_entry_p(vm_cc_cme(cc)));
2304 VM_ASSERT(!vm_cc_resolved_p(cc) || !METHOD_ENTRY_INVALIDATED(vm_cc_cme(cc)));
2305 VM_ASSERT(!vm_cc_resolved_p(cc) || vm_cc_cme(cc)->called_id == vm_ci_mid(ci));
2306
2307 return cc;
2308}
2309
2310#if VM_CHECK_MODE > 0
2311static bool
2312vm_cd_owned_by_iseq_p(const struct rb_call_data *cd, const rb_iseq_t *iseq)
2313{
2314 const struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
2315 return cd >= body->call_data && cd < body->call_data + body->ci_size;
2316}
2317#endif
2318
2319static const struct rb_callcache *
2320vm_search_method_slowpath0(VALUE cd_owner, struct rb_call_data *cd, VALUE klass)
2321{
2322#if USE_DEBUG_COUNTER
2323 const struct rb_callcache *old_cc = cd->cc;
2324 const rb_callable_method_entry_t *const old_cme = old_cc ? old_cc->cme_ : NULL;
2325#endif
2326
2327 const struct rb_callcache *cc = rb_vm_search_method_slowpath(cd->ci, klass);
2328 const rb_callable_method_entry_t *const new_cme = vm_cc_cme(cc);
2329
2330#if OPT_INLINE_METHOD_CACHE
2331 cd->cc = cc;
2332
2333 if (cd_owner && vm_cc_markable(cc)) {
2334 // invokesuper dispatches with a cd on the machine stack, which has no
2335 // owning iseq to remember and keeps its ci out of the GC.
2336 VM_ASSERT(!vm_ci_markable(cd->ci) ||
2337 vm_cd_owned_by_iseq_p(cd, (const rb_iseq_t *)cd_owner));
2338 RB_OBJ_WRITTEN(cd_owner, Qundef, cc);
2339 }
2340
2341#if USE_DEBUG_COUNTER
2342 const struct rb_callcache *empty_cc = &rb_vm_empty_cc;
2343 if (!old_cc || old_cc == empty_cc) {
2344 // empty
2345 RB_DEBUG_COUNTER_INC(mc_inline_miss_empty);
2346 }
2347 else if (old_cc == cc) {
2348 RB_DEBUG_COUNTER_INC(mc_inline_miss_same_cc);
2349 }
2350 else if (old_cme == new_cme) {
2351 RB_DEBUG_COUNTER_INC(mc_inline_miss_same_cme);
2352 }
2353 else if (old_cme && new_cme && old_cme->def == new_cme->def) {
2354 RB_DEBUG_COUNTER_INC(mc_inline_miss_same_def);
2355 }
2356 else {
2357 RB_DEBUG_COUNTER_INC(mc_inline_miss_diff);
2358 }
2359#endif
2360#endif // OPT_INLINE_METHOD_CACHE
2361
2362 if (new_cme) VM_ASSERT(new_cme->called_id == vm_ci_mid(cd->ci));
2363
2364 return cc;
2365}
2366
2367ALWAYS_INLINE(static bool vm_cc_hit_p(const struct rb_callcache *cc, const struct rb_call_data *cd, VALUE klass));
2368static bool
2369vm_cc_hit_p(const struct rb_callcache *cc, const struct rb_call_data *cd, VALUE klass)
2370{
2371 VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
2372
2373#if OPT_INLINE_METHOD_CACHE
2374 if (LIKELY(vm_cc_class_check(cc, klass))) {
2375 if (LIKELY(!METHOD_ENTRY_INVALIDATED(vm_cc_cme(cc)))) {
2376 VM_ASSERT(callable_method_entry_p(vm_cc_cme(cc)));
2377 RB_DEBUG_COUNTER_INC(mc_inline_hit);
2378 VM_ASSERT(vm_cc_cme(cc) == NULL || // not found
2379 (vm_ci_flag(cd->ci) & VM_CALL_SUPER) || // search_super w/ define_method
2380 vm_cc_cme(cc)->called_id == vm_ci_mid(cd->ci)); // cme->called_id == ci->mid
2381
2382 return true;
2383 }
2384 RB_DEBUG_COUNTER_INC(mc_inline_miss_invalidated);
2385 }
2386 else {
2387 RB_DEBUG_COUNTER_INC(mc_inline_miss_klass);
2388 }
2389#endif
2390
2391 return false;
2392}
2393
2394ALWAYS_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));
2395static const struct rb_callcache *
2396vm_search_method_fastpath(const struct rb_control_frame_struct *reg_cfp, struct rb_call_data *cd, VALUE klass)
2397{
2398 const struct rb_callcache *cc = cd->cc;
2399 if (vm_cc_hit_p(cc, cd, klass)) {
2400 return cc;
2401 }
2402 if (UNLIKELY(cc == &rb_vm_uncached_cc)) {
2403 return cc;
2404 }
2405
2406 return vm_search_method_slowpath0((VALUE)CFP_ISEQ(reg_cfp), cd, klass);
2407}
2408
2409static const struct rb_callable_method_entry_struct *
2410vm_search_method(struct rb_control_frame_struct *reg_cfp, struct rb_call_data *cd, VALUE recv)
2411{
2412 VALUE klass = CLASS_OF(recv);
2413 VM_ASSERT(klass != Qfalse);
2414 VM_ASSERT(RBASIC_CLASS(klass) == 0 || rb_obj_is_kind_of(klass, rb_cClass));
2415
2416 const struct rb_callcache *cc = vm_search_method_fastpath(reg_cfp, cd, klass);
2417 return vm_cc_cme(cc);
2418}
2419
2420static VALUE
2421vm_call_uncached(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
2422{
2423 RB_DEBUG_COUNTER_INC(ccf_uncached);
2424
2425 const struct rb_call_data *cd = calling->cd;
2426 const VALUE klass = CLASS_OF(calling->recv);
2427 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, vm_ci_mid(cd->ci));
2428
2429 if (UNLIKELY(cme == NULL || !cme->def->uncached)) {
2430 // A site that saw an uncached method is now seeing a receiver whose
2431 // method is cacheable, so let it have a real cc after all.
2432 const struct rb_callcache *cc =
2433 vm_search_method_slowpath0((VALUE)CFP_ISEQ(cfp), (struct rb_call_data *)cd, klass);
2434 calling->cc = cc;
2435 return vm_cc_call(cc)(ec, cfp, calling);
2436 }
2437
2438 calling->cc = &VM_CC_ON_STACK(klass, vm_call_general, {{ 0 }},
2439 rb_check_overloaded_cme(cme, cd->ci));
2440 return vm_call_general(ec, cfp, calling);
2441}
2442
2444rb_zjit_vm_search_method(VALUE cd_owner, struct rb_call_data *cd, VALUE recv)
2445{
2446 // Called from ZJIT with the compile-time iseq, which may differ from
2447 // the iseq on the current CFP.
2448 VALUE klass = CLASS_OF(recv);
2449 const struct rb_callcache *cc = cd->cc;
2450 if (!vm_cc_hit_p(cc, cd, klass)) {
2451 if (UNLIKELY(cc == &rb_vm_uncached_cc)) {
2452 return rb_callable_method_entry(klass, vm_ci_mid(cd->ci));
2453 }
2454 cc = vm_search_method_slowpath0(cd_owner, cd, klass);
2455 }
2456 return vm_cc_cme(cc);
2457}
2458
2459#if __has_attribute(transparent_union)
2460typedef union {
2461 VALUE (*anyargs)(ANYARGS);
2462 VALUE (*f00)(VALUE);
2463 VALUE (*f01)(VALUE, VALUE);
2464 VALUE (*f02)(VALUE, VALUE, VALUE);
2465 VALUE (*f03)(VALUE, VALUE, VALUE, VALUE);
2466 VALUE (*f04)(VALUE, VALUE, VALUE, VALUE, VALUE);
2467 VALUE (*f05)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE);
2468 VALUE (*f06)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE);
2469 VALUE (*f07)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE);
2478 VALUE (*fm1)(int, union { VALUE *x; const VALUE *y; } __attribute__((__transparent_union__)), VALUE);
2479} __attribute__((__transparent_union__)) cfunc_type;
2480# define make_cfunc_type(f) (cfunc_type){.anyargs = (VALUE (*)(ANYARGS))(f)}
2481#else
2482typedef VALUE (*cfunc_type)(ANYARGS);
2483# define make_cfunc_type(f) (cfunc_type)(f)
2484#endif
2485
2486static inline bool
2487cfunc_p(const rb_callable_method_entry_t *me)
2488{
2489 if (! me) {
2490 return false;
2491 }
2492
2493 VM_ASSERT(IMEMO_TYPE_P(me, imemo_ment));
2494 VM_ASSERT(callable_method_entry_p(me));
2495 VM_ASSERT(me->def);
2496
2497 return me->def->type == VM_METHOD_TYPE_CFUNC;
2498}
2499
2500static inline bool
2501cfunc_equal(const rb_callable_method_entry_t *me, cfunc_type func)
2502{
2503 VM_ASSERT(cfunc_p(me));
2504
2505#if __has_attribute(transparent_union)
2506 return me->def->body.cfunc.func == func.anyargs;
2507#else
2508 return me->def->body.cfunc.func == func;
2509#endif
2510}
2511
2512static inline bool
2513check_cfunc(const rb_callable_method_entry_t *me, cfunc_type func)
2514{
2515 return cfunc_p(me) && cfunc_equal(me, func);
2516}
2517
2518static inline int
2519check_method_basic_definition(const rb_callable_method_entry_t *me)
2520{
2521 return me && METHOD_ENTRY_BASIC(me);
2522}
2523
2524static inline int
2525vm_method_cfunc_is(struct rb_control_frame_struct *reg_cfp, CALL_DATA cd, VALUE recv, cfunc_type func)
2526{
2527 VM_ASSERT(reg_cfp != NULL);
2528 const struct rb_callable_method_entry_struct *cme = vm_search_method(reg_cfp, cd, recv);
2529 return check_cfunc(cme, func);
2530}
2531
2532bool
2533rb_zjit_cme_is_cfunc(const rb_callable_method_entry_t *me, const cfunc_type func)
2534{
2535 return check_cfunc(me, func);
2536}
2537
2538int
2539rb_vm_method_cfunc_is(const rb_iseq_t *iseq, CALL_DATA cd, VALUE recv, cfunc_type func)
2540{
2541 const struct rb_callable_method_entry_struct *cme = rb_zjit_vm_search_method((VALUE)iseq, cd, recv);
2542 return check_cfunc(cme, func);
2543}
2544
2545#define check_cfunc(me, func) check_cfunc(me, make_cfunc_type(func))
2546#define vm_method_cfunc_is(reg_cfp, cd, recv, func) vm_method_cfunc_is(reg_cfp, cd, recv, make_cfunc_type(func))
2547
2548#define OP_UNREDEFINED_P(op, t) BASIC_OP_UNREDEFINED_P(BOP_##op, t##_REDEFINED_OP_FLAG)
2549#define EQ_UNREDEFINED_P(t) OP_UNREDEFINED_P(EQ, t)
2550
2551static inline bool
2552FIXNUM_2_P(VALUE a, VALUE b)
2553{
2554 /* FIXNUM_P(a) && FIXNUM_P(b)
2555 * == ((a & 1) && (b & 1))
2556 * == a & b & 1 */
2557 SIGNED_VALUE x = a;
2558 SIGNED_VALUE y = b;
2559 SIGNED_VALUE z = x & y & 1;
2560 return z == 1;
2561}
2562
2563static inline bool
2564FLONUM_2_P(VALUE a, VALUE b)
2565{
2566#if USE_FLONUM
2567 /* FLONUM_P(a) && FLONUM_P(b)
2568 * == ((a & 3) == 2) && ((b & 3) == 2)
2569 * == ! ((a ^ 2) | (b ^ 2) & 3)
2570 */
2571 SIGNED_VALUE x = a;
2572 SIGNED_VALUE y = b;
2573 SIGNED_VALUE z = ((x ^ 2) | (y ^ 2)) & 3;
2574 return !z;
2575#else
2576 return false;
2577#endif
2578}
2579
2580static inline VALUE
2581opt_equality_specialized(VALUE recv, VALUE obj)
2582{
2583 if (recv == obj) {
2584 if (OP_UNREDEFINED_P(EQ, ANY)) {
2585 // Two identical immediate VALUE are always equal
2586 if (SPECIAL_CONST_P(recv)) {
2587 return Qtrue;
2588 }
2589 return Qundef;
2590 }
2591 return Qundef;
2592 }
2593
2594 return opt_equality_specialized_miss(recv, obj);
2595}
2596
2597static VALUE
2598opt_equality(struct rb_control_frame_struct *reg_cfp, VALUE recv, VALUE obj, CALL_DATA cd)
2599{
2600 VM_ASSERT(reg_cfp != NULL);
2601
2602 VALUE val = opt_equality_specialized(recv, obj);
2603 if (!UNDEF_P(val)) return val;
2604
2605 if (!vm_method_cfunc_is(reg_cfp, cd, recv, rb_obj_equal)) {
2606 return Qundef;
2607 }
2608 else {
2609 return RBOOL(recv == obj);
2610 }
2611}
2612
2613static 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
2614
2615static inline VALUE
2616opt_equality_by_mid(VALUE recv, VALUE obj, ID mid)
2617{
2618 const struct rb_callcache *cc = gccct_method_search(GET_EC(), recv, mid, &VM_CI_ON_STACK(mid, 0, 1, NULL));
2619
2620 if (cc && check_cfunc(vm_cc_cme(cc), rb_obj_equal)) {
2621 return RBOOL(recv == obj);
2622 }
2623 else {
2624 return RBOOL(RTEST(rb_funcall(recv, mid, 1, obj)));
2625 }
2626}
2627
2628VALUE
2629rb_equal_opt(VALUE obj1, VALUE obj2)
2630{
2631 return opt_equality_by_mid(obj1, obj2, idEq);
2632}
2633
2634VALUE
2635rb_eql_opt(VALUE obj1, VALUE obj2)
2636{
2637 return opt_equality_by_mid(obj1, obj2, idEqlP);
2638}
2639
2640extern VALUE rb_vm_call0(rb_execution_context_t *ec, VALUE, ID, int, const VALUE*, const rb_callable_method_entry_t *, int kw_splat);
2641extern VALUE rb_vm_call_with_refinements(rb_execution_context_t *, VALUE, ID, int, const VALUE *, int);
2642
2643static VALUE
2644check_match(rb_execution_context_t *ec, VALUE pattern, VALUE target, enum vm_check_match_type type)
2645{
2646 switch (type) {
2647 case VM_CHECKMATCH_TYPE_WHEN:
2648 return pattern;
2649 case VM_CHECKMATCH_TYPE_RESCUE:
2650 if (!rb_obj_is_kind_of(pattern, rb_cModule)) {
2651 rb_raise(rb_eTypeError, "class or module required for rescue clause");
2652 }
2653 /* fall through */
2654 case VM_CHECKMATCH_TYPE_CASE: {
2655 return rb_vm_call_with_refinements(ec, pattern, idEqq, 1, &target, RB_NO_KEYWORDS);
2656 }
2657 default:
2658 rb_bug("check_match: unreachable");
2659 }
2660}
2661
2662
2663static inline VALUE
2664double_cmp_lt(double a, double b)
2665{
2666 return RBOOL(a < b);
2667}
2668
2669static inline VALUE
2670double_cmp_le(double a, double b)
2671{
2672 return RBOOL(a <= b);
2673}
2674
2675static inline VALUE
2676double_cmp_gt(double a, double b)
2677{
2678 return RBOOL(a > b);
2679}
2680
2681static inline VALUE
2682double_cmp_ge(double a, double b)
2683{
2684 return RBOOL(a >= b);
2685}
2686
2687// Copied by vm_dump.c
2688static inline VALUE *
2689vm_base_ptr(const rb_control_frame_t *cfp)
2690{
2691 const rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2692
2693 if (CFP_ISEQ(cfp) && VM_FRAME_RUBYFRAME_P(cfp)) {
2694 VALUE *bp = prev_cfp->sp + ISEQ_BODY(CFP_ISEQ(cfp))->local_table_size + VM_ENV_DATA_SIZE;
2695
2696 if (ISEQ_BODY(CFP_ISEQ(cfp))->param.flags.forwardable && VM_ENV_LOCAL_P(cfp->ep)) {
2697 int lts = ISEQ_BODY(CFP_ISEQ(cfp))->local_table_size;
2698 int params = ISEQ_BODY(CFP_ISEQ(cfp))->param.size;
2699
2700 CALL_INFO ci = (CALL_INFO)cfp->ep[-(VM_ENV_DATA_SIZE + (lts - params))]; // skip EP stuff, CI should be last local
2701 bp += vm_ci_argc(ci);
2702 }
2703
2704 if (ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_METHOD || VM_FRAME_BMETHOD_P(cfp)) {
2705 /* adjust `self' */
2706 bp += 1;
2707 }
2708#if VM_DEBUG_BP_CHECK
2709 if (bp != cfp->bp_check) {
2710 ruby_debug_printf("bp_check: %ld, bp: %ld\n",
2711 (long)(cfp->bp_check - GET_EC()->vm_stack),
2712 (long)(bp - GET_EC()->vm_stack));
2713 rb_bug("vm_base_ptr: unreachable");
2714 }
2715#endif
2716 return bp;
2717 }
2718 else {
2719 return NULL;
2720 }
2721}
2722
2723VALUE *
2724rb_vm_base_ptr(const rb_control_frame_t *cfp)
2725{
2726 return vm_base_ptr(cfp);
2727}
2728
2729/* method call processes with call_info */
2730
2731#include "vm_args.c"
2732
2733static 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);
2734ALWAYS_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));
2735static 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);
2736static VALUE vm_call_super_method(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling);
2737static VALUE vm_call_method_nome(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling);
2738static VALUE vm_call_method_each_type(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling);
2739static inline VALUE vm_call_method(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling);
2740
2741static vm_call_handler vm_call_iseq_setup_func(const struct rb_callinfo *ci, const int param_size, const int local_size);
2742
2743static VALUE
2744vm_call_iseq_setup_tailcall_0start(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
2745{
2746 RB_DEBUG_COUNTER_INC(ccf_iseq_setup_tailcall_0start);
2747
2748 const struct rb_callcache *cc = calling->cc;
2749 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
2750 unsigned int param = ISEQ_BODY(iseq)->param.size;
2751 unsigned int local = ISEQ_BODY(iseq)->local_table_size;
2752 return vm_call_iseq_setup_tailcall(ec, cfp, calling, 0, param, local);
2753}
2754
2755static VALUE
2756vm_call_iseq_setup_normal_0start(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
2757{
2758 RB_DEBUG_COUNTER_INC(ccf_iseq_setup_0start);
2759
2760 const struct rb_callcache *cc = calling->cc;
2761 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
2762 int param = ISEQ_BODY(iseq)->param.size;
2763 int local = ISEQ_BODY(iseq)->local_table_size;
2764 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), 0, param, local);
2765}
2766
2767bool
2768rb_simple_iseq_p(const rb_iseq_t *iseq)
2769{
2770 return ISEQ_BODY(iseq)->param.flags.has_opt == FALSE &&
2771 ISEQ_BODY(iseq)->param.flags.has_rest == FALSE &&
2772 ISEQ_BODY(iseq)->param.flags.has_post == FALSE &&
2773 ISEQ_BODY(iseq)->param.flags.has_kw == FALSE &&
2774 ISEQ_BODY(iseq)->param.flags.has_kwrest == FALSE &&
2775 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg == FALSE &&
2776 ISEQ_BODY(iseq)->param.flags.forwardable == FALSE &&
2777 ISEQ_BODY(iseq)->param.flags.has_block == FALSE &&
2778 ISEQ_BODY(iseq)->param.flags.accepts_no_block == FALSE;
2779}
2780
2781bool
2782rb_iseq_only_optparam_p(const rb_iseq_t *iseq)
2783{
2784 return ISEQ_BODY(iseq)->param.flags.has_opt == TRUE &&
2785 ISEQ_BODY(iseq)->param.flags.has_rest == FALSE &&
2786 ISEQ_BODY(iseq)->param.flags.has_post == FALSE &&
2787 ISEQ_BODY(iseq)->param.flags.has_kw == FALSE &&
2788 ISEQ_BODY(iseq)->param.flags.has_kwrest == FALSE &&
2789 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg == FALSE &&
2790 ISEQ_BODY(iseq)->param.flags.forwardable == FALSE &&
2791 ISEQ_BODY(iseq)->param.flags.has_block == FALSE &&
2792 ISEQ_BODY(iseq)->param.flags.accepts_no_block == FALSE;
2793}
2794
2795bool
2796rb_iseq_only_kwparam_p(const rb_iseq_t *iseq)
2797{
2798 return ISEQ_BODY(iseq)->param.flags.has_opt == FALSE &&
2799 ISEQ_BODY(iseq)->param.flags.has_rest == FALSE &&
2800 ISEQ_BODY(iseq)->param.flags.has_post == FALSE &&
2801 ISEQ_BODY(iseq)->param.flags.has_kw == TRUE &&
2802 ISEQ_BODY(iseq)->param.flags.has_kwrest == FALSE &&
2803 ISEQ_BODY(iseq)->param.flags.forwardable == FALSE &&
2804 ISEQ_BODY(iseq)->param.flags.has_block == FALSE &&
2805 ISEQ_BODY(iseq)->param.flags.accepts_no_block == FALSE;
2806}
2807
2808#define ALLOW_HEAP_ARGV (-2)
2809#define ALLOW_HEAP_ARGV_KEEP_KWSPLAT (-3)
2810
2811static inline bool
2812vm_caller_setup_arg_splat(rb_control_frame_t *cfp, struct rb_calling_info *calling, VALUE ary, int max_args)
2813{
2814 vm_check_canary(GET_EC(), cfp->sp);
2815 bool ret = false;
2816
2817 if (!NIL_P(ary)) {
2818 const VALUE *ptr = RARRAY_CONST_PTR(ary);
2819 rb_len_t len = RARRAY_LEN(ary);
2820 int argc = calling->argc;
2821
2822 if (UNLIKELY(max_args <= ALLOW_HEAP_ARGV && len + argc > VM_ARGC_STACK_MAX)) {
2823 /* Avoid SystemStackError when splatting large arrays by storing arguments in
2824 * a temporary array, instead of trying to keeping arguments on the VM stack.
2825 */
2826 VALUE *argv = cfp->sp - argc;
2827 VALUE argv_ary = rb_ary_hidden_new(len + argc + 1);
2828 rb_ary_cat(argv_ary, argv, argc);
2829 rb_ary_cat(argv_ary, ptr, len);
2830 cfp->sp -= argc - 1;
2831 cfp->sp[-1] = argv_ary;
2832 calling->argc = 1;
2833 calling->heap_argv = argv_ary;
2834 RB_GC_GUARD(ary);
2835 }
2836 else {
2837 long i;
2838
2839 if (max_args >= 0 && len + argc > max_args) {
2840 /* If only a given max_args is allowed, copy up to max args.
2841 * Used by vm_callee_setup_block_arg for non-lambda blocks,
2842 * where additional arguments are ignored.
2843 *
2844 * Also, copy up to one more argument than the maximum,
2845 * in case it is an empty keyword hash that will be removed.
2846 *
2847 * calling->argc still counts the arguments that are not
2848 * copied, so that arity errors report the given number.
2849 */
2850 rb_len_t copy_len = max_args - argc + 1;
2851 if (copy_len < 0) {
2852 /* More than max_args + 1 arguments precede the splat. */
2853 copy_len = 0;
2854 }
2855 calling->argc += len - copy_len;
2856 len = copy_len;
2857 ret = true;
2858 }
2859 else {
2860 /* Unset heap_argv if set originally. Can happen when
2861 * forwarding modified arguments, where heap_argv was used
2862 * originally, but heap_argv not supported by the forwarded
2863 * method in all cases.
2864 */
2865 calling->heap_argv = 0;
2866 }
2867 CHECK_VM_STACK_OVERFLOW(cfp, len);
2868
2869 for (i = 0; i < len; i++) {
2870 *cfp->sp++ = ptr[i];
2871 }
2872 calling->argc += i;
2873 }
2874 }
2875
2876 return ret;
2877}
2878
2879static inline void
2880vm_caller_setup_arg_kw(rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_callinfo *ci)
2881{
2882 const VALUE *const passed_keywords = vm_ci_kwarg(ci)->keywords;
2883 const int kw_len = vm_ci_kwarg(ci)->keyword_len;
2884 const VALUE h = rb_hash_new_capa(kw_len);
2885 VALUE *sp = cfp->sp;
2886 int i;
2887
2888 for (i=0; i<kw_len; i++) {
2889 rb_hash_aset(h, passed_keywords[i], (sp - kw_len)[i]);
2890 }
2891 (sp-kw_len)[0] = h;
2892
2893 cfp->sp -= kw_len - 1;
2894 calling->argc -= kw_len - 1;
2895 calling->kw_splat = 1;
2896}
2897
2898static inline VALUE
2899vm_caller_setup_keyword_hash(const struct rb_callinfo *ci, VALUE keyword_hash)
2900{
2901 if (UNLIKELY(!RB_TYPE_P(keyword_hash, T_HASH))) {
2902 if (keyword_hash != Qnil) {
2903 /* Convert a non-hash keyword splat to a new hash */
2904 keyword_hash = rb_hash_dup(rb_to_hash_type(keyword_hash));
2905 }
2906 }
2907 else if (!IS_ARGS_KW_SPLAT_MUT(ci) && !RHASH_EMPTY_P(keyword_hash)) {
2908 /* Convert a hash keyword splat to a new hash unless
2909 * a mutable keyword splat was passed.
2910 * Skip allocating new hash for empty keyword splat, as empty
2911 * keyword splat will be ignored by both callers.
2912 */
2913 keyword_hash = rb_hash_dup(keyword_hash);
2914 }
2915 return keyword_hash;
2916}
2917
2918static inline void
2919CALLER_SETUP_ARG(struct rb_control_frame_struct *restrict cfp,
2920 struct rb_calling_info *restrict calling,
2921 const struct rb_callinfo *restrict ci, int max_args)
2922{
2923 if (UNLIKELY(IS_ARGS_SPLAT(ci))) {
2924 if (IS_ARGS_KW_SPLAT(ci)) {
2925 // f(*a, **kw)
2926 VM_ASSERT(calling->kw_splat == 1);
2927
2928 cfp->sp -= 2;
2929 calling->argc -= 2;
2930 VALUE ary = cfp->sp[0];
2931 VALUE kwh = vm_caller_setup_keyword_hash(ci, cfp->sp[1]);
2932
2933 // splat a
2934 if (vm_caller_setup_arg_splat(cfp, calling, ary, max_args)) return;
2935
2936 // put kw
2937 if (kwh != Qnil && !RHASH_EMPTY_P(kwh)) {
2938 if (UNLIKELY(calling->heap_argv)) {
2939 rb_ary_push(calling->heap_argv, kwh);
2940 ((struct RHash *)kwh)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
2941 if (max_args != ALLOW_HEAP_ARGV_KEEP_KWSPLAT) {
2942 calling->kw_splat = 0;
2943 }
2944 }
2945 else {
2946 cfp->sp[0] = kwh;
2947 cfp->sp++;
2948 calling->argc++;
2949
2950 VM_ASSERT(calling->kw_splat == 1);
2951 }
2952 }
2953 else {
2954 calling->kw_splat = 0;
2955 }
2956 }
2957 else {
2958 // f(*a)
2959 VM_ASSERT(calling->kw_splat == 0);
2960
2961 cfp->sp -= 1;
2962 calling->argc -= 1;
2963 VALUE ary = cfp->sp[0];
2964
2965 if (vm_caller_setup_arg_splat(cfp, calling, ary, max_args)) {
2966 goto check_keyword;
2967 }
2968
2969 // check the last argument
2970 VALUE last_hash, argv_ary;
2971 if (UNLIKELY(argv_ary = calling->heap_argv)) {
2972 if (!IS_ARGS_KEYWORD(ci) &&
2973 RARRAY_LEN(argv_ary) > 0 &&
2974 RB_TYPE_P((last_hash = rb_ary_last(0, NULL, argv_ary)), T_HASH) &&
2975 (((struct RHash *)last_hash)->basic.flags & RHASH_PASS_AS_KEYWORDS)) {
2976
2977 rb_ary_pop(argv_ary);
2978 if (!RHASH_EMPTY_P(last_hash)) {
2979 rb_ary_push(argv_ary, rb_hash_dup(last_hash));
2980 calling->kw_splat = 1;
2981 }
2982 }
2983 }
2984 else {
2985check_keyword:
2986 if (!IS_ARGS_KEYWORD(ci) &&
2987 calling->argc > 0 &&
2988 RB_TYPE_P((last_hash = cfp->sp[-1]), T_HASH) &&
2989 (((struct RHash *)last_hash)->basic.flags & RHASH_PASS_AS_KEYWORDS)) {
2990
2991 if (RHASH_EMPTY_P(last_hash)) {
2992 calling->argc--;
2993 cfp->sp -= 1;
2994 }
2995 else {
2996 cfp->sp[-1] = rb_hash_dup(last_hash);
2997 calling->kw_splat = 1;
2998 }
2999 }
3000 }
3001 }
3002 }
3003 else if (UNLIKELY(IS_ARGS_KW_SPLAT(ci))) {
3004 // f(**kw)
3005 VM_ASSERT(calling->kw_splat == 1);
3006 VALUE kwh = vm_caller_setup_keyword_hash(ci, cfp->sp[-1]);
3007
3008 if (kwh == Qnil || RHASH_EMPTY_P(kwh)) {
3009 cfp->sp--;
3010 calling->argc--;
3011 calling->kw_splat = 0;
3012 }
3013 else {
3014 cfp->sp[-1] = kwh;
3015 }
3016 }
3017 else if (UNLIKELY(IS_ARGS_KEYWORD(ci))) {
3018 // f(k1:1, k2:2)
3019 VM_ASSERT(calling->kw_splat == 0);
3020
3021 /* This converts VM_CALL_KWARG style to VM_CALL_KW_SPLAT style
3022 * by creating a keyword hash.
3023 * So, vm_ci_flag(ci) & VM_CALL_KWARG is now inconsistent.
3024 */
3025 vm_caller_setup_arg_kw(cfp, calling, ci);
3026 }
3027}
3028
3029#define USE_OPT_HIST 0
3030
3031#if USE_OPT_HIST
3032#define OPT_HIST_MAX 64
3033static int opt_hist[OPT_HIST_MAX+1];
3034
3035__attribute__((destructor))
3036static void
3037opt_hist_show_results_at_exit(void)
3038{
3039 for (int i=0; i<OPT_HIST_MAX; i++) {
3040 ruby_debug_printf("opt_hist\t%d\t%d\n", i, opt_hist[i]);
3041 }
3042}
3043#endif
3044
3045static VALUE
3046vm_call_iseq_setup_normal_opt_start(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3047 struct rb_calling_info *calling)
3048{
3049 const struct rb_callcache *cc = calling->cc;
3050 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3051 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3052 const int opt = calling->argc - lead_num;
3053 const int opt_num = ISEQ_BODY(iseq)->param.opt_num;
3054 const int opt_pc = (int)ISEQ_BODY(iseq)->param.opt_table[opt];
3055 const int param = ISEQ_BODY(iseq)->param.size;
3056 const int local = ISEQ_BODY(iseq)->local_table_size;
3057 const int delta = opt_num - opt;
3058
3059 RB_DEBUG_COUNTER_INC(ccf_iseq_opt);
3060
3061#if USE_OPT_HIST
3062 if (opt_pc < OPT_HIST_MAX) {
3063 opt_hist[opt]++;
3064 }
3065 else {
3066 opt_hist[OPT_HIST_MAX]++;
3067 }
3068#endif
3069
3070 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), opt_pc, param - delta, local);
3071}
3072
3073static VALUE
3074vm_call_iseq_setup_tailcall_opt_start(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3075 struct rb_calling_info *calling)
3076{
3077 const struct rb_callcache *cc = calling->cc;
3078 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3079 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3080 const int opt = calling->argc - lead_num;
3081 const int opt_pc = (int)ISEQ_BODY(iseq)->param.opt_table[opt];
3082 unsigned const int param = ISEQ_BODY(iseq)->param.size;
3083 unsigned const int local = ISEQ_BODY(iseq)->local_table_size;
3084
3085 RB_DEBUG_COUNTER_INC(ccf_iseq_opt);
3086
3087#if USE_OPT_HIST
3088 if (opt_pc < OPT_HIST_MAX) {
3089 opt_hist[opt]++;
3090 }
3091 else {
3092 opt_hist[OPT_HIST_MAX]++;
3093 }
3094#endif
3095
3096 return vm_call_iseq_setup_tailcall(ec, cfp, calling, opt_pc, param, local);
3097}
3098
3099static void
3100args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *const iseq, const rb_callable_method_entry_t *cme,
3101 VALUE *const passed_values, const int passed_keyword_len, const VALUE *const passed_keywords,
3102 VALUE *const locals);
3103
3104static VALUE
3105vm_call_iseq_forwardable(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3106 struct rb_calling_info *calling)
3107{
3108 const struct rb_callcache *cc = calling->cc;
3109 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3110 int param_size = ISEQ_BODY(iseq)->param.size;
3111 int local_size = ISEQ_BODY(iseq)->local_table_size;
3112
3113 // Setting up local size and param size
3114 VM_ASSERT(ISEQ_BODY(iseq)->param.flags.forwardable);
3115
3116 local_size = local_size + vm_ci_argc(calling->cd->ci);
3117 param_size = param_size + vm_ci_argc(calling->cd->ci);
3118
3119 cfp->sp[0] = (VALUE)calling->cd->ci;
3120
3121 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), 0, param_size, local_size);
3122}
3123
3124static VALUE
3125vm_call_iseq_setup_kwparm_kwarg(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3126 struct rb_calling_info *calling)
3127{
3128 const struct rb_callinfo *ci = calling->cd->ci;
3129 const struct rb_callcache *cc = calling->cc;
3130
3131 VM_ASSERT(vm_ci_flag(ci) & VM_CALL_KWARG);
3132 RB_DEBUG_COUNTER_INC(ccf_iseq_kw1);
3133
3134 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3135 const struct rb_iseq_param_keyword *kw_param = ISEQ_BODY(iseq)->param.keyword;
3136 const struct rb_callinfo_kwarg *kw_arg = vm_ci_kwarg(ci);
3137 const int ci_kw_len = kw_arg->keyword_len;
3138 const VALUE * const ci_keywords = kw_arg->keywords;
3139 VALUE *argv = cfp->sp - calling->argc;
3140 VALUE *const klocals = argv + kw_param->bits_start - kw_param->num;
3141 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3142 VALUE * const ci_kws = ALLOCA_N(VALUE, ci_kw_len);
3143 MEMCPY(ci_kws, argv + lead_num, VALUE, ci_kw_len);
3144 args_setup_kw_parameters(ec, iseq, vm_cc_cme(cc), ci_kws, ci_kw_len, ci_keywords, klocals);
3145
3146 int param = ISEQ_BODY(iseq)->param.size;
3147 int local = ISEQ_BODY(iseq)->local_table_size;
3148 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), 0, param, local);
3149}
3150
3151static VALUE
3152vm_call_iseq_setup_kwparm_nokwarg(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3153 struct rb_calling_info *calling)
3154{
3155 const struct rb_callinfo *MAYBE_UNUSED(ci) = calling->cd->ci;
3156 const struct rb_callcache *cc = calling->cc;
3157
3158 VM_ASSERT((vm_ci_flag(ci) & VM_CALL_KWARG) == 0);
3159 RB_DEBUG_COUNTER_INC(ccf_iseq_kw2);
3160
3161 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3162 const struct rb_iseq_param_keyword *kw_param = ISEQ_BODY(iseq)->param.keyword;
3163 VALUE * const argv = cfp->sp - calling->argc;
3164 VALUE * const klocals = argv + kw_param->bits_start - kw_param->num;
3165
3166 int i;
3167 for (i=0; i<kw_param->num; i++) {
3168 klocals[i] = kw_param->default_values[i];
3169 }
3170 klocals[i] = INT2FIX(0); // kw specify flag
3171 // NOTE:
3172 // nobody check this value, but it should be cleared because it can
3173 // points invalid VALUE (T_NONE objects, raw pointer and so on).
3174
3175 int param = ISEQ_BODY(iseq)->param.size;
3176 int local = ISEQ_BODY(iseq)->local_table_size;
3177 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), 0, param, local);
3178}
3179
3180static VALUE builtin_invoker0(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr);
3181
3182static VALUE
3183vm_call_single_noarg_leaf_builtin(rb_execution_context_t *ec, rb_control_frame_t *cfp,
3184 struct rb_calling_info *calling)
3185{
3186 const struct rb_callcache *cc = calling->cc;
3187 // Pairs with the release store in vm_callee_setup_arg so aux_.bf is visible.
3188 vm_call_handler call = (vm_call_handler)(uintptr_t)rbimpl_atomic_ptr_load((void **)&cc->call_, RBIMPL_ATOMIC_ACQUIRE);
3189 if (UNLIKELY(call != vm_call_single_noarg_leaf_builtin)) {
3190 return call(ec, cfp, calling);
3191 }
3192 const struct rb_builtin_function *bf = cc->aux_.bf;
3193 cfp->sp -= (calling->argc + 1);
3194 rb_insn_func_t func_ptr = (rb_insn_func_t)(uintptr_t)bf->func_ptr;
3195 return builtin_invoker0(ec, calling->recv, NULL, func_ptr);
3196}
3197
3198VALUE rb_gen_method_name(VALUE owner, VALUE name); // in vm_backtrace.c
3199
3200static void
3201warn_unused_block(const rb_callable_method_entry_t *cme, const rb_iseq_t *iseq, void *pc)
3202{
3203 rb_vm_t *vm = GET_VM();
3204 set_table *dup_check_table = &vm->unused_block_warning_table;
3205 st_data_t key;
3206 bool strict_unused_block = rb_warning_category_enabled_p(RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK);
3207
3208 union {
3209 VALUE v;
3210 unsigned char b[SIZEOF_VALUE];
3211 } k1 = {
3212 .v = (VALUE)pc,
3213 }, k2 = {
3214 .v = (VALUE)cme->def,
3215 };
3216
3217 // relax check
3218 if (!strict_unused_block) {
3219 key = (st_data_t)cme->def->original_id;
3220
3221 if (set_table_lookup(dup_check_table, key)) {
3222 return;
3223 }
3224 }
3225
3226 // strict check
3227 // make unique key from pc and me->def pointer
3228 key = 0;
3229 for (int i=0; i<SIZEOF_VALUE; i++) {
3230 // fprintf(stderr, "k1:%3d k2:%3d\n", k1.b[i], k2.b[SIZEOF_VALUE-1-i]);
3231 key |= (st_data_t)(k1.b[i] ^ k2.b[SIZEOF_VALUE-1-i]) << (8 * i);
3232 }
3233
3234 if (0) {
3235 fprintf(stderr, "SIZEOF_VALUE:%d\n", SIZEOF_VALUE);
3236 fprintf(stderr, "pc:%p def:%p\n", pc, (void *)cme->def);
3237 fprintf(stderr, "key:%p\n", (void *)key);
3238 }
3239
3240 // duplication check
3241 if (set_insert(dup_check_table, key)) {
3242 // already shown
3243 }
3244 else if (RTEST(ruby_verbose) || strict_unused_block) {
3245 VALUE m_loc = rb_method_entry_location((const rb_method_entry_t *)cme);
3246 VALUE name = rb_gen_method_name(cme->defined_class, rb_iseq_base_label(iseq));
3247
3248 if (!NIL_P(m_loc)) {
3249 rb_warn("the block passed to '%"PRIsVALUE"' defined at %"PRIsVALUE":%"PRIsVALUE" may be ignored",
3250 name, RARRAY_AREF(m_loc, 0), RARRAY_AREF(m_loc, 1));
3251 }
3252 else {
3253 rb_warn("the block may be ignored because '%"PRIsVALUE"' does not use a block", name);
3254 }
3255 }
3256}
3257
3258static inline int
3259vm_callee_setup_arg(rb_execution_context_t *ec, struct rb_calling_info *calling,
3260 const rb_iseq_t *iseq, VALUE *argv, int param_size, int local_size)
3261{
3262 const struct rb_callinfo *ci = calling->cd->ci;
3263 const struct rb_callcache *cc = calling->cc;
3264
3265 VM_ASSERT((vm_ci_argc(ci), 1));
3266 VM_ASSERT(vm_cc_cme(cc) != NULL);
3267
3268 if (UNLIKELY(!ISEQ_BODY(iseq)->param.flags.use_block &&
3269 calling->block_handler != VM_BLOCK_HANDLER_NONE &&
3270 !(vm_ci_flag(calling->cd->ci) & (VM_CALL_OPT_SEND | VM_CALL_SUPER)))) {
3271 warn_unused_block(vm_cc_cme(cc), iseq, (void *)CFP_PC(ec->cfp));
3272 }
3273
3274 if (LIKELY(!(vm_ci_flag(ci) & VM_CALL_KW_SPLAT))) {
3275 if (LIKELY(rb_simple_iseq_p(iseq))) {
3276 rb_control_frame_t *cfp = ec->cfp;
3277 int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3278 CALLER_SETUP_ARG(cfp, calling, ci, lead_num);
3279
3280 if (calling->argc != lead_num) {
3281 argument_arity_error(ec, iseq, vm_cc_cme(cc), calling->argc, lead_num, lead_num);
3282 }
3283
3284 //VM_ASSERT(ci == calling->cd->ci);
3285 VM_ASSERT(cc == calling->cc);
3286
3287 if (vm_call_iseq_optimizable_p(ci, cc)) {
3288 if ((ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_SINGLE_NOARG_LEAF) && ruby_vm_c_events_enabled == 0) {
3289 VM_ASSERT(ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF);
3290 vm_cc_bf_set(cc, (void *)ISEQ_BODY(iseq)->iseq_encoded[1]);
3291 rbimpl_atomic_ptr_store((volatile void **)&cc->call_,
3292 (void *)(uintptr_t)vm_call_single_noarg_leaf_builtin,
3293 RBIMPL_ATOMIC_RELEASE);
3294 }
3295 else {
3296 CC_SET_FASTPATH(cc, vm_call_iseq_setup_func(ci, param_size, local_size), true);
3297 }
3298 }
3299 return 0;
3300 }
3301 else if (rb_iseq_only_optparam_p(iseq)) {
3302 rb_control_frame_t *cfp = ec->cfp;
3303
3304 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3305 const int opt_num = ISEQ_BODY(iseq)->param.opt_num;
3306
3307 CALLER_SETUP_ARG(cfp, calling, ci, lead_num + opt_num);
3308 const int argc = calling->argc;
3309 const int opt = argc - lead_num;
3310
3311 if (opt < 0 || opt > opt_num) {
3312 argument_arity_error(ec, iseq, vm_cc_cme(cc), argc, lead_num, lead_num + opt_num);
3313 }
3314
3315 if (LIKELY(!(vm_ci_flag(ci) & VM_CALL_TAILCALL))) {
3316 CC_SET_FASTPATH(cc, vm_call_iseq_setup_normal_opt_start,
3317 !IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) &&
3318 vm_call_cacheable(ci, cc));
3319 }
3320 else {
3321 CC_SET_FASTPATH(cc, vm_call_iseq_setup_tailcall_opt_start,
3322 !IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) &&
3323 vm_call_cacheable(ci, cc));
3324 }
3325
3326 /* initialize opt vars for self-references */
3327 VM_ASSERT((int)ISEQ_BODY(iseq)->param.size == lead_num + opt_num);
3328 for (int i=argc; i<lead_num + opt_num; i++) {
3329 argv[i] = Qnil;
3330 }
3331 return (int)ISEQ_BODY(iseq)->param.opt_table[opt];
3332 }
3333 else if (rb_iseq_only_kwparam_p(iseq) && !IS_ARGS_SPLAT(ci)) {
3334 const int lead_num = ISEQ_BODY(iseq)->param.lead_num;
3335 const int argc = calling->argc;
3336 const struct rb_iseq_param_keyword *kw_param = ISEQ_BODY(iseq)->param.keyword;
3337
3338 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3339 const struct rb_callinfo_kwarg *kw_arg = vm_ci_kwarg(ci);
3340
3341 if (argc - kw_arg->keyword_len == lead_num) {
3342 const int ci_kw_len = kw_arg->keyword_len;
3343 const VALUE * const ci_keywords = kw_arg->keywords;
3344 VALUE * const ci_kws = ALLOCA_N(VALUE, ci_kw_len);
3345 MEMCPY(ci_kws, argv + lead_num, VALUE, ci_kw_len);
3346
3347 VALUE *const klocals = argv + kw_param->bits_start - kw_param->num;
3348 args_setup_kw_parameters(ec, iseq, vm_cc_cme(cc), ci_kws, ci_kw_len, ci_keywords, klocals);
3349
3350 CC_SET_FASTPATH(cc, vm_call_iseq_setup_kwparm_kwarg,
3351 vm_call_cacheable(ci, cc));
3352
3353 return 0;
3354 }
3355 }
3356 else if (argc == lead_num) {
3357 /* no kwarg */
3358 VALUE *const klocals = argv + kw_param->bits_start - kw_param->num;
3359 args_setup_kw_parameters(ec, iseq, vm_cc_cme(cc), NULL, 0, NULL, klocals);
3360
3361 if (klocals[kw_param->num] == INT2FIX(0)) {
3362 /* copy from default_values */
3363 CC_SET_FASTPATH(cc, vm_call_iseq_setup_kwparm_nokwarg,
3364 vm_call_cacheable(ci, cc));
3365 }
3366
3367 return 0;
3368 }
3369 }
3370 }
3371
3372 // Called iseq is using ... param
3373 // def foo(...) # <- iseq for foo will have "forwardable"
3374 //
3375 // We want to set the `...` local to the caller's CI
3376 // foo(1, 2) # <- the ci for this should end up as `...`
3377 //
3378 // So hopefully the stack looks like:
3379 //
3380 // => 1
3381 // => 2
3382 // => *
3383 // => **
3384 // => &
3385 // => ... # <- points at `foo`s CI
3386 // => cref_or_me
3387 // => specval
3388 // => type
3389 //
3390 if (ISEQ_BODY(iseq)->param.flags.forwardable) {
3391 bool can_fastpath = true;
3392
3393 if ((vm_ci_flag(ci) & VM_CALL_FORWARDING)) {
3394 struct rb_forwarding_call_data * forward_cd = (struct rb_forwarding_call_data *)calling->cd;
3395 if (vm_ci_argc(ci) != vm_ci_argc(forward_cd->caller_ci)) {
3396 ci = vm_ci_new_runtime(
3397 vm_ci_mid(ci),
3398 vm_ci_flag(ci),
3399 vm_ci_argc(ci),
3400 vm_ci_kwarg(ci));
3401 }
3402 else {
3403 ci = forward_cd->caller_ci;
3404 }
3405 can_fastpath = false;
3406 }
3407 // C functions calling iseqs will stack allocate a CI,
3408 // so we need to convert it to heap allocated
3409 if (!vm_ci_markable(ci)) {
3410 ci = vm_ci_new_runtime(
3411 vm_ci_mid(ci),
3412 vm_ci_flag(ci),
3413 vm_ci_argc(ci),
3414 vm_ci_kwarg(ci));
3415 can_fastpath = false;
3416 }
3417 argv[param_size - 1] = (VALUE)ci;
3418 CC_SET_FASTPATH(cc, vm_call_iseq_forwardable, can_fastpath);
3419 return 0;
3420 }
3421
3422 return setup_parameters_complex(ec, iseq, calling, ci, argv, arg_setup_method);
3423}
3424
3425static void
3426vm_adjust_stack_forwarding(const struct rb_execution_context_struct *ec, struct rb_control_frame_struct *cfp, int argc, VALUE splat)
3427{
3428 // This case is when the caller is using a ... parameter.
3429 // For example `bar(...)`. The call info will have VM_CALL_FORWARDING
3430 // In this case the caller's caller's CI will be on the stack.
3431 //
3432 // For example:
3433 //
3434 // def bar(a, b); a + b; end
3435 // def foo(...); bar(...); end
3436 // foo(1, 2) # <- this CI will be on the stack when we call `bar(...)`
3437 //
3438 // Stack layout will be:
3439 //
3440 // > 1
3441 // > 2
3442 // > CI for foo(1, 2)
3443 // > cref_or_me
3444 // > specval
3445 // > type
3446 // > receiver
3447 // > CI for foo(1, 2), via `getlocal ...`
3448 // > ( SP points here )
3449 const VALUE * lep = VM_CF_LEP(cfp);
3450
3451 const rb_iseq_t *iseq;
3452
3453 // If we're in an escaped environment (lambda for example), get the iseq
3454 // from the captured env.
3455 if (VM_ENV_FLAGS(lep, VM_ENV_FLAG_ESCAPED)) {
3456 rb_env_t * env = (rb_env_t *)lep[VM_ENV_DATA_INDEX_ENV];
3457 iseq = env->iseq;
3458 }
3459 else { // Otherwise use the lep to find the caller
3460 iseq = CFP_ISEQ(rb_vm_search_cf_from_ep(ec, cfp, lep));
3461 }
3462
3463 // Our local storage is below the args we need to copy
3464 int local_size = ISEQ_BODY(iseq)->local_table_size + argc;
3465
3466 const VALUE * from = lep - (local_size + VM_ENV_DATA_SIZE - 1); // 2 for EP values
3467 VALUE * to = cfp->sp - 1; // clobber the CI
3468
3469 if (RTEST(splat)) {
3470 to -= 1; // clobber the splat array
3471 CHECK_VM_STACK_OVERFLOW0(cfp, to, RARRAY_LEN(splat));
3472 MEMCPY(to, RARRAY_CONST_PTR(splat), VALUE, RARRAY_LEN(splat));
3473 to += RARRAY_LEN(splat);
3474 }
3475
3476 CHECK_VM_STACK_OVERFLOW0(cfp, to, argc);
3477 MEMCPY(to, from, VALUE, argc);
3478 cfp->sp = to + argc;
3479
3480 // Stack layout should now be:
3481 //
3482 // > 1
3483 // > 2
3484 // > CI for foo(1, 2)
3485 // > cref_or_me
3486 // > specval
3487 // > type
3488 // > receiver
3489 // > 1
3490 // > 2
3491 // > ( SP points here )
3492}
3493
3494static VALUE
3495vm_call_iseq_setup(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
3496{
3497 RB_DEBUG_COUNTER_INC(ccf_iseq_setup);
3498
3499 const struct rb_callcache *cc = calling->cc;
3500 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3501 unsigned int param_size = ISEQ_BODY(iseq)->param.size;
3502 unsigned int local_size = ISEQ_BODY(iseq)->local_table_size;
3503
3504 RUBY_ASSERT(!ISEQ_BODY(iseq)->param.flags.forwardable);
3505
3506 const int opt_pc = vm_callee_setup_arg(ec, calling, iseq, cfp->sp - calling->argc, param_size, local_size);
3507 return vm_call_iseq_setup_2(ec, cfp, calling, opt_pc, param_size, local_size);
3508}
3509
3510static VALUE
3511vm_call_iseq_fwd_setup(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
3512{
3513 RB_DEBUG_COUNTER_INC(ccf_iseq_setup);
3514
3515 const struct rb_callcache *cc = calling->cc;
3516 const rb_iseq_t *iseq = def_iseq_ptr(vm_cc_cme(cc)->def);
3517 unsigned int param_size = ISEQ_BODY(iseq)->param.size;
3518 unsigned int local_size = ISEQ_BODY(iseq)->local_table_size;
3519
3520 RUBY_ASSERT(ISEQ_BODY(iseq)->param.flags.forwardable);
3521
3522 // Setting up local size and param size
3523 local_size = local_size + vm_ci_argc(calling->cd->ci);
3524 param_size = param_size + vm_ci_argc(calling->cd->ci);
3525
3526 const int opt_pc = vm_callee_setup_arg(ec, calling, iseq, cfp->sp - calling->argc, param_size, local_size);
3527 return vm_call_iseq_setup_2(ec, cfp, calling, opt_pc, param_size, local_size);
3528}
3529
3530static inline VALUE
3531vm_call_iseq_setup_2(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling,
3532 int opt_pc, unsigned int param_size, unsigned int local_size)
3533{
3534 const struct rb_callinfo *ci = calling->cd->ci;
3535 const struct rb_callcache *cc = calling->cc;
3536
3537 if (LIKELY(!(vm_ci_flag(ci) & VM_CALL_TAILCALL))) {
3538 return vm_call_iseq_setup_normal(ec, cfp, calling, vm_cc_cme(cc), opt_pc, param_size, local_size);
3539 }
3540 else {
3541 return vm_call_iseq_setup_tailcall(ec, cfp, calling, opt_pc, param_size, local_size);
3542 }
3543}
3544
3545static inline VALUE
3546vm_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,
3547 int opt_pc, int param_size, int local_size)
3548{
3549 const rb_iseq_t *iseq = def_iseq_ptr(me->def);
3550 VALUE *argv = cfp->sp - calling->argc;
3551 VALUE *sp = argv + param_size;
3552 cfp->sp = argv - 1 /* recv */;
3553
3554 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_METHOD | VM_ENV_FLAG_LOCAL, calling->recv,
3555 calling->block_handler, (VALUE)me,
3556 ISEQ_BODY(iseq)->iseq_encoded + opt_pc, sp,
3557 local_size - param_size,
3558 ISEQ_BODY(iseq)->stack_max);
3559 return Qundef;
3560}
3561
3562static inline VALUE
3563vm_call_iseq_setup_tailcall(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling,
3564 int opt_pc, unsigned int param_size, unsigned int local_size)
3565{
3566 const struct rb_callcache *cc = calling->cc;
3567 VALUE *argv = cfp->sp - calling->argc;
3568 const rb_callable_method_entry_t *me = vm_cc_cme(cc);
3569 const rb_iseq_t *iseq = def_iseq_ptr(me->def);
3570 VALUE *src_argv = argv;
3571 VALUE *sp_orig, *sp;
3572 VALUE finish_flag = VM_FRAME_FINISHED_P(cfp) ? VM_FRAME_FLAG_FINISH : 0;
3573
3574 if (VM_BH_FROM_CFP_P(calling->block_handler, cfp)) {
3575 struct rb_captured_block *dst_captured = VM_CFP_TO_CAPTURED_BLOCK(RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
3576 const struct rb_captured_block *src_captured = VM_BH_TO_CAPT_BLOCK(calling->block_handler);
3577 dst_captured->code.val = src_captured->code.val;
3578 if (VM_BH_ISEQ_BLOCK_P(calling->block_handler)) {
3579 calling->block_handler = VM_BH_FROM_ISEQ_BLOCK(dst_captured);
3580 }
3581 else {
3582 calling->block_handler = VM_BH_FROM_IFUNC_BLOCK(dst_captured);
3583 }
3584 }
3585
3586 vm_pop_frame(ec, cfp, cfp->ep);
3587 cfp = ec->cfp;
3588
3589 sp_orig = sp = cfp->sp;
3590
3591 /* push self */
3592 sp[0] = calling->recv;
3593 sp++;
3594
3595 /* copy arguments */
3596 for (unsigned int i = 0; i < param_size; i++) {
3597 *sp++ = src_argv[i];
3598 }
3599
3600 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_METHOD | VM_ENV_FLAG_LOCAL | finish_flag,
3601 calling->recv, calling->block_handler, (VALUE)me,
3602 ISEQ_BODY(iseq)->iseq_encoded + opt_pc, sp,
3603 local_size - param_size,
3604 ISEQ_BODY(iseq)->stack_max);
3605
3606 cfp->sp = sp_orig;
3607
3608 return Qundef;
3609}
3610
3611static void
3612ractor_unsafe_check(void)
3613{
3614 if (!rb_ractor_main_p()) {
3615 rb_raise(rb_eRactorUnsafeError, "ractor unsafe method called from not main ractor");
3616 }
3617}
3618
3619static VALUE
3620call_cfunc_m2(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3621{
3622 ractor_unsafe_check();
3623 VALUE(*f)(VALUE, VALUE) = (VALUE(*)(VALUE, VALUE))func;
3624 return (*f)(recv, rb_ary_new4(argc, argv));
3625}
3626
3627static VALUE
3628call_cfunc_m1(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3629{
3630 ractor_unsafe_check();
3631 VALUE(*f)(int, const VALUE *, VALUE) = (VALUE(*)(int, const VALUE *, VALUE))func;
3632 return (*f)(argc, argv, recv);
3633}
3634
3635static VALUE
3636call_cfunc_0(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3637{
3638 ractor_unsafe_check();
3639 VALUE(*f)(VALUE) = (VALUE(*)(VALUE))func;
3640 return (*f)(recv);
3641}
3642
3643static VALUE
3644call_cfunc_1(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3645{
3646 ractor_unsafe_check();
3647 VALUE(*f)(VALUE, VALUE) = (VALUE(*)(VALUE, VALUE))func;
3648 return (*f)(recv, argv[0]);
3649}
3650
3651static VALUE
3652call_cfunc_2(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3653{
3654 ractor_unsafe_check();
3655 VALUE(*f)(VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE))func;
3656 return (*f)(recv, argv[0], argv[1]);
3657}
3658
3659static VALUE
3660call_cfunc_3(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3661{
3662 ractor_unsafe_check();
3663 VALUE(*f)(VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE))func;
3664 return (*f)(recv, argv[0], argv[1], argv[2]);
3665}
3666
3667static VALUE
3668call_cfunc_4(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3669{
3670 ractor_unsafe_check();
3671 VALUE(*f)(VALUE, VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE, VALUE))func;
3672 return (*f)(recv, argv[0], argv[1], argv[2], argv[3]);
3673}
3674
3675static VALUE
3676call_cfunc_5(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3677{
3678 ractor_unsafe_check();
3679 VALUE(*f)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE))func;
3680 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4]);
3681}
3682
3683static VALUE
3684call_cfunc_6(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3685{
3686 ractor_unsafe_check();
3688 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
3689}
3690
3691static VALUE
3692call_cfunc_7(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3693{
3694 ractor_unsafe_check();
3696 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
3697}
3698
3699static VALUE
3700call_cfunc_8(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3701{
3702 ractor_unsafe_check();
3704 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
3705}
3706
3707static VALUE
3708call_cfunc_9(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3709{
3710 ractor_unsafe_check();
3712 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
3713}
3714
3715static VALUE
3716call_cfunc_10(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3717{
3718 ractor_unsafe_check();
3720 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
3721}
3722
3723static VALUE
3724call_cfunc_11(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3725{
3726 ractor_unsafe_check();
3728 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]);
3729}
3730
3731static VALUE
3732call_cfunc_12(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3733{
3734 ractor_unsafe_check();
3736 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]);
3737}
3738
3739static VALUE
3740call_cfunc_13(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3741{
3742 ractor_unsafe_check();
3744 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]);
3745}
3746
3747static VALUE
3748call_cfunc_14(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3749{
3750 ractor_unsafe_check();
3752 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]);
3753}
3754
3755static VALUE
3756call_cfunc_15(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3757{
3758 ractor_unsafe_check();
3760 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]);
3761}
3762
3763static VALUE
3764ractor_safe_call_cfunc_m2(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3765{
3766 VALUE(*f)(VALUE, VALUE) = (VALUE(*)(VALUE, VALUE))func;
3767 return (*f)(recv, rb_ary_new4(argc, argv));
3768}
3769
3770static VALUE
3771ractor_safe_call_cfunc_m1(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3772{
3773 VALUE(*f)(int, const VALUE *, VALUE) = (VALUE(*)(int, const VALUE *, VALUE))func;
3774 return (*f)(argc, argv, recv);
3775}
3776
3777static VALUE
3778ractor_safe_call_cfunc_0(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3779{
3780 VALUE(*f)(VALUE) = (VALUE(*)(VALUE))func;
3781 return (*f)(recv);
3782}
3783
3784static VALUE
3785ractor_safe_call_cfunc_1(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3786{
3787 VALUE(*f)(VALUE, VALUE) = (VALUE(*)(VALUE, VALUE))func;
3788 return (*f)(recv, argv[0]);
3789}
3790
3791static VALUE
3792ractor_safe_call_cfunc_2(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3793{
3794 VALUE(*f)(VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE))func;
3795 return (*f)(recv, argv[0], argv[1]);
3796}
3797
3798static VALUE
3799ractor_safe_call_cfunc_3(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3800{
3801 VALUE(*f)(VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE))func;
3802 return (*f)(recv, argv[0], argv[1], argv[2]);
3803}
3804
3805static VALUE
3806ractor_safe_call_cfunc_4(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3807{
3808 VALUE(*f)(VALUE, VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE, VALUE))func;
3809 return (*f)(recv, argv[0], argv[1], argv[2], argv[3]);
3810}
3811
3812static VALUE
3813ractor_safe_call_cfunc_5(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3814{
3815 VALUE(*f)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE) = (VALUE(*)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE))func;
3816 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4]);
3817}
3818
3819static VALUE
3820ractor_safe_call_cfunc_6(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3821{
3823 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
3824}
3825
3826static VALUE
3827ractor_safe_call_cfunc_7(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3828{
3830 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
3831}
3832
3833static VALUE
3834ractor_safe_call_cfunc_8(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3835{
3837 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
3838}
3839
3840static VALUE
3841ractor_safe_call_cfunc_9(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3842{
3844 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
3845}
3846
3847static VALUE
3848ractor_safe_call_cfunc_10(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3849{
3851 return (*f)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
3852}
3853
3854static VALUE
3855ractor_safe_call_cfunc_11(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3856{
3858 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]);
3859}
3860
3861static VALUE
3862ractor_safe_call_cfunc_12(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3863{
3865 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]);
3866}
3867
3868static VALUE
3869ractor_safe_call_cfunc_13(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3870{
3872 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]);
3873}
3874
3875static VALUE
3876ractor_safe_call_cfunc_14(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3877{
3879 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]);
3880}
3881
3882static VALUE
3883ractor_safe_call_cfunc_15(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS))
3884{
3886 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]);
3887}
3888
3889static inline int
3890vm_cfp_consistent_p(rb_execution_context_t *ec, const rb_control_frame_t *reg_cfp)
3891{
3892 const int ov_flags = RAISED_STACKOVERFLOW;
3893 if (LIKELY(reg_cfp == ec->cfp + 1)) return TRUE;
3894 if (rb_ec_raised_p(ec, ov_flags)) {
3895 rb_ec_raised_reset(ec, ov_flags);
3896 return TRUE;
3897 }
3898 return FALSE;
3899}
3900
3901#define CHECK_CFP_CONSISTENCY(func) \
3902 (LIKELY(vm_cfp_consistent_p(ec, reg_cfp)) ? (void)0 : \
3903 rb_bug(func ": cfp consistency error (%p, %p)", (void *)reg_cfp, (void *)(ec->cfp+1)))
3904
3905static inline
3906const rb_method_cfunc_t *
3907vm_method_cfunc_entry(const rb_callable_method_entry_t *me)
3908{
3909#if VM_DEBUG_VERIFY_METHOD_CACHE
3910 switch (me->def->type) {
3911 case VM_METHOD_TYPE_CFUNC:
3912 case VM_METHOD_TYPE_NOTIMPLEMENTED:
3913 break;
3914# define METHOD_BUG(t) case VM_METHOD_TYPE_##t: rb_bug("wrong method type: " #t)
3915 METHOD_BUG(ISEQ);
3916 METHOD_BUG(ATTRSET);
3917 METHOD_BUG(IVAR);
3918 METHOD_BUG(BMETHOD);
3919 METHOD_BUG(ZSUPER);
3920 METHOD_BUG(UNDEF);
3921 METHOD_BUG(OPTIMIZED);
3922 METHOD_BUG(MISSING);
3923 METHOD_BUG(REFINED);
3924 METHOD_BUG(ALIAS);
3925# undef METHOD_BUG
3926 default:
3927 rb_bug("wrong method type: %d", me->def->type);
3928 }
3929#endif
3930 return UNALIGNED_MEMBER_PTR(me->def, body.cfunc);
3931}
3932
3933static VALUE
3934vm_call_cfunc_with_frame_(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling,
3935 int argc, VALUE *argv, VALUE *stack_bottom)
3936{
3937 RB_DEBUG_COUNTER_INC(ccf_cfunc_with_frame);
3938 const struct rb_callinfo *ci = calling->cd->ci;
3939 const struct rb_callcache *cc = calling->cc;
3940 VALUE val;
3941 const rb_callable_method_entry_t *me = vm_cc_cme(cc);
3942 const rb_method_cfunc_t *cfunc = vm_method_cfunc_entry(me);
3943
3944 VALUE recv = calling->recv;
3945 VALUE block_handler = calling->block_handler;
3946 VALUE frame_type = VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL;
3947
3948 if (UNLIKELY(calling->kw_splat)) {
3949 frame_type |= VM_FRAME_FLAG_CFRAME_KW;
3950 }
3951
3952 VM_ASSERT(reg_cfp == ec->cfp);
3953
3954 RUBY_DTRACE_CMETHOD_ENTRY_HOOK(ec, me->owner, me->def->original_id);
3955 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, recv, me->def->original_id, vm_ci_mid(ci), me->owner, Qundef);
3956
3957 vm_push_frame(ec, NULL, frame_type, recv,
3958 block_handler, (VALUE)me,
3959 0, ec->cfp->sp, 0, 0);
3960
3961 int len = cfunc->argc;
3962 if (len >= 0) rb_check_arity(argc, len, len);
3963
3964 reg_cfp->sp = stack_bottom;
3965 val = (*cfunc->invoker)(recv, argc, argv, cfunc->func);
3966
3967 CHECK_CFP_CONSISTENCY("vm_call_cfunc");
3968
3969 rb_vm_pop_frame(ec);
3970
3971 VM_ASSERT(ec->cfp->sp == stack_bottom);
3972
3973 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, recv, me->def->original_id, vm_ci_mid(ci), me->owner, val);
3974 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
3975
3976 return val;
3977}
3978
3979// Push a C method frame for a given cme. This is called when JIT code skipped
3980// pushing a frame but the C method reached a point where a frame is needed.
3981void
3982rb_vm_push_cfunc_frame(const rb_callable_method_entry_t *cme, int recv_idx)
3983{
3984 VM_ASSERT(cme->def->type == VM_METHOD_TYPE_CFUNC);
3985 rb_execution_context_t *ec = GET_EC();
3986 VALUE *sp = ec->cfp->sp;
3987 VALUE recv = *(sp - recv_idx - 1);
3988 VALUE frame_type = VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL;
3989 VALUE block_handler = VM_BLOCK_HANDLER_NONE;
3990#if VM_CHECK_MODE > 0
3991 // Clean up the stack canary since we're about to satisfy the "leaf or lazy push" assumption
3992 *(GET_EC()->cfp->sp) = Qfalse;
3993#endif
3994 vm_push_frame(ec, NULL, frame_type, recv, block_handler, (VALUE)cme, 0, ec->cfp->sp, 0, 0);
3995}
3996
3997// If true, cc->call needs to include `CALLER_SETUP_ARG` (i.e. can't be skipped in fastpath)
3998bool
3999rb_splat_or_kwargs_p(const struct rb_callinfo *restrict ci)
4000{
4001 return IS_ARGS_SPLAT(ci) || IS_ARGS_KW_OR_KW_SPLAT(ci);
4002}
4003
4004static VALUE
4005vm_call_get_kwarg(rb_control_frame_t *reg_cfp, CALL_DATA cd, ID kw)
4006{
4007
4008 VALUE sym = ID2SYM(kw);
4009 if (vm_ci_flag(cd->ci) & VM_CALL_KW_SPLAT) {
4010 VALUE keywords = TOPN(0);
4011 if (UNLIKELY(!RB_TYPE_P(keywords, T_HASH))) {
4012 TOPN(0) = keywords = rb_to_hash_type(keywords);
4013 }
4014 return rb_hash_lookup2(keywords, sym, Qundef);
4015 }
4016 else {
4017 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(cd->ci);
4018 if (kwarg) {
4019 for (int index = 0; index < kwarg->keyword_len; index++) {
4020 if (kwarg->keywords[index] == sym) {
4021 return TOPN(kwarg->keyword_len - index - 1);
4022 }
4023 }
4024 }
4025 }
4026 return Qundef;
4027}
4028
4029static VALUE
4030vm_call_cfunc_with_frame(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4031{
4032 int argc = calling->argc;
4033 VALUE *stack_bottom = reg_cfp->sp - argc - 1;
4034 VALUE *argv = &stack_bottom[1];
4035
4036 return vm_call_cfunc_with_frame_(ec, reg_cfp, calling, argc, argv, stack_bottom);
4037}
4038
4039static VALUE
4040vm_call_cfunc_other(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4041{
4042 const struct rb_callinfo *ci = calling->cd->ci;
4043 RB_DEBUG_COUNTER_INC(ccf_cfunc_other);
4044
4045 CALLER_SETUP_ARG(reg_cfp, calling, ci, ALLOW_HEAP_ARGV_KEEP_KWSPLAT);
4046 VALUE argv_ary;
4047 if (UNLIKELY(argv_ary = calling->heap_argv)) {
4048 VM_ASSERT(!IS_ARGS_KEYWORD(ci));
4049 int argc = RARRAY_LENINT(argv_ary);
4050 VALUE *argv = (VALUE *)RARRAY_CONST_PTR(argv_ary);
4051 VALUE *stack_bottom = reg_cfp->sp - 2;
4052
4053 VM_ASSERT(calling->argc == 1);
4054 VM_ASSERT(RB_TYPE_P(argv_ary, T_ARRAY));
4055 VM_ASSERT(RBASIC_CLASS(argv_ary) == 0); // hidden ary
4056
4057 return vm_call_cfunc_with_frame_(ec, reg_cfp, calling, argc, argv, stack_bottom);
4058 }
4059 else {
4060 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));
4061
4062 return vm_call_cfunc_with_frame(ec, reg_cfp, calling);
4063 }
4064}
4065
4066static inline VALUE
4067vm_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)
4068{
4069 VALUE argv_ary = reg_cfp->sp[-1 - stack_offset];
4070 int argc = RARRAY_LENINT(argv_ary) - argc_offset;
4071
4072 if (UNLIKELY(argc > VM_ARGC_STACK_MAX)) {
4073 return vm_call_cfunc_other(ec, reg_cfp, calling);
4074 }
4075
4076 VALUE *argv = (VALUE *)RARRAY_CONST_PTR(argv_ary);
4077 calling->kw_splat = 0;
4078 int i;
4079 VALUE *stack_bottom = reg_cfp->sp - 2 - stack_offset;
4080 VALUE *sp = stack_bottom;
4081 CHECK_VM_STACK_OVERFLOW(reg_cfp, argc);
4082 for(i = 0; i < argc; i++) {
4083 *++sp = argv[i];
4084 }
4085 reg_cfp->sp = sp+1;
4086
4087 return vm_call_cfunc_with_frame_(ec, reg_cfp, calling, argc, stack_bottom+1, stack_bottom);
4088}
4089
4090static inline VALUE
4091vm_call_cfunc_only_splat(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4092{
4093 RB_DEBUG_COUNTER_INC(ccf_cfunc_only_splat);
4094 VALUE argv_ary = reg_cfp->sp[-1];
4095 int argc = RARRAY_LENINT(argv_ary);
4096 VALUE *argv = (VALUE *)RARRAY_CONST_PTR(argv_ary);
4097 VALUE last_hash;
4098 int argc_offset = 0;
4099
4100 if (UNLIKELY(argc > 0 &&
4101 RB_TYPE_P((last_hash = argv[argc-1]), T_HASH) &&
4102 (((struct RHash *)last_hash)->basic.flags & RHASH_PASS_AS_KEYWORDS))) {
4103 if (!RHASH_EMPTY_P(last_hash)) {
4104 return vm_call_cfunc_other(ec, reg_cfp, calling);
4105 }
4106 argc_offset++;
4107 }
4108 return vm_call_cfunc_array_argv(ec, reg_cfp, calling, 0, argc_offset);
4109}
4110
4111static inline VALUE
4112vm_call_cfunc_only_splat_kw(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4113{
4114 RB_DEBUG_COUNTER_INC(ccf_cfunc_only_splat_kw);
4115 VALUE keyword_hash = reg_cfp->sp[-1];
4116
4117 if (keyword_hash == Qnil || (RB_TYPE_P(keyword_hash, T_HASH) && RHASH_EMPTY_P(keyword_hash))) {
4118 return vm_call_cfunc_array_argv(ec, reg_cfp, calling, 1, 0);
4119 }
4120
4121 return vm_call_cfunc_other(ec, reg_cfp, calling);
4122}
4123
4124static VALUE
4125vm_call_cfunc(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4126{
4127 const struct rb_callinfo *ci = calling->cd->ci;
4128 RB_DEBUG_COUNTER_INC(ccf_cfunc);
4129
4130 if (IS_ARGS_SPLAT(ci) && !(vm_ci_flag(ci) & VM_CALL_FORWARDING)) {
4131 if (!IS_ARGS_KW_SPLAT(ci) && vm_ci_argc(ci) == 1) {
4132 // f(*a)
4133 CC_SET_FASTPATH(calling->cc, vm_call_cfunc_only_splat, TRUE);
4134 return vm_call_cfunc_only_splat(ec, reg_cfp, calling);
4135 }
4136 if (IS_ARGS_KW_SPLAT(ci) && vm_ci_argc(ci) == 2) {
4137 // f(*a, **kw)
4138 CC_SET_FASTPATH(calling->cc, vm_call_cfunc_only_splat_kw, TRUE);
4139 return vm_call_cfunc_only_splat_kw(ec, reg_cfp, calling);
4140 }
4141 }
4142
4143 CC_SET_FASTPATH(calling->cc, vm_call_cfunc_other, TRUE);
4144 return vm_call_cfunc_other(ec, reg_cfp, calling);
4145}
4146
4147static VALUE
4148vm_call_ivar(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4149{
4150 const struct rb_callcache *cc = calling->cc;
4151 RB_DEBUG_COUNTER_INC(ccf_ivar);
4152 cfp->sp -= 1;
4153 VALUE ivar = vm_getivar(calling->recv, vm_cc_cme(cc)->def->body.attr.id, NULL, NULL, cc, TRUE, Qnil);
4154 return ivar;
4155}
4156
4157static VALUE
4158vm_call_attrset_direct(rb_execution_context_t *ec, rb_control_frame_t *cfp, const struct rb_callcache *cc, VALUE obj)
4159{
4160 RB_DEBUG_COUNTER_INC(ccf_attrset);
4161 VALUE val = *(cfp->sp - 1);
4162 cfp->sp -= 2;
4163 rb_setivar_cache cache = rb_setivar_cache_unpack(vm_cc_atomic_cache_read(cc));
4164 ID id = vm_cc_cme(cc)->def->body.attr.id;
4165 rb_check_frozen(obj);
4166 VALUE res = vm_setivar(obj, val, cache);
4167 if (UNDEF_P(res)) {
4168 switch (BUILTIN_TYPE(obj)) {
4169 case T_OBJECT:
4170 break;
4171 case T_CLASS:
4172 case T_MODULE:
4173 {
4174 res = vm_setivar_class(obj, val, cache);
4175 if (!UNDEF_P(res)) {
4176 return res;
4177 }
4178 }
4179 break;
4180 default:
4181 {
4182 res = vm_setivar_default(obj, id, val, cache);
4183 if (!UNDEF_P(res)) {
4184 return res;
4185 }
4186 }
4187 }
4188 res = vm_setivar_slowpath_attr(obj, id, val, cc);
4189 }
4190 return res;
4191}
4192
4193static VALUE
4194vm_call_attrset(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4195{
4196 return vm_call_attrset_direct(ec, cfp, calling->cc, calling->recv);
4197}
4198
4199static inline VALUE
4200vm_call_bmethod_body(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv)
4201{
4202 rb_proc_t *proc;
4203 VALUE val;
4204 const struct rb_callcache *cc = calling->cc;
4205 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4206 VALUE procv = cme->def->body.bmethod.proc;
4207
4208 if (!RB_OBJ_SHAREABLE_P(procv) &&
4209 cme->def->body.bmethod.defined_ractor_id != rb_ec_ractor_id(ec)) {
4210 rb_raise(rb_eRuntimeError, "defined with an un-shareable Proc in a different Ractor");
4211 }
4212
4213 /* control block frame */
4214 GetProcPtr(procv, proc);
4215 val = vm_invoke_bmethod(ec, proc, calling->recv, CALLING_ARGC(calling), argv, calling->kw_splat, calling->block_handler, vm_cc_cme(cc));
4216
4217 return val;
4218}
4219
4220static 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);
4221
4222static VALUE
4223vm_call_iseq_bmethod(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4224{
4225 RB_DEBUG_COUNTER_INC(ccf_iseq_bmethod);
4226
4227 const struct rb_callcache *cc = calling->cc;
4228 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4229 VALUE procv = cme->def->body.bmethod.proc;
4230
4231 if (!RB_OBJ_SHAREABLE_P(procv) &&
4232 cme->def->body.bmethod.defined_ractor_id != rb_ec_ractor_id(ec)) {
4233 rb_raise(rb_eRuntimeError, "defined with an un-shareable Proc in a different Ractor");
4234 }
4235
4236 rb_proc_t *proc;
4237 GetProcPtr(procv, proc);
4238 const struct rb_block *block = &proc->block;
4239
4240 while (vm_block_type(block) == block_type_proc) {
4241 block = vm_proc_block(block->as.proc);
4242 }
4243 VM_ASSERT(vm_block_type(block) == block_type_iseq);
4244
4245 const struct rb_captured_block *captured = &block->as.captured;
4246 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
4247 VALUE * const argv = cfp->sp - calling->argc;
4248 const int arg_size = ISEQ_BODY(iseq)->param.size;
4249
4250 int opt_pc;
4251 if (vm_ci_flag(calling->cd->ci) & VM_CALL_ARGS_SIMPLE) {
4252 opt_pc = vm_callee_setup_block_arg(ec, calling, calling->cd->ci, iseq, argv, arg_setup_method);
4253 }
4254 else {
4255 opt_pc = setup_parameters_complex(ec, iseq, calling, calling->cd->ci, argv, arg_setup_method);
4256 }
4257
4258 cfp->sp = argv - 1; // -1 for the receiver
4259
4260 vm_push_frame(ec, iseq,
4261 VM_FRAME_MAGIC_BLOCK | VM_FRAME_FLAG_BMETHOD | VM_FRAME_FLAG_LAMBDA,
4262 calling->recv,
4263 VM_GUARDED_PREV_EP(captured->ep),
4264 (VALUE)cme,
4265 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
4266 argv + arg_size,
4267 ISEQ_BODY(iseq)->local_table_size - arg_size,
4268 ISEQ_BODY(iseq)->stack_max);
4269
4270 return Qundef;
4271}
4272
4273static VALUE
4274vm_call_noniseq_bmethod(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4275{
4276 RB_DEBUG_COUNTER_INC(ccf_noniseq_bmethod);
4277
4278 VALUE *argv;
4279 int argc;
4280 CALLER_SETUP_ARG(cfp, calling, calling->cd->ci, ALLOW_HEAP_ARGV);
4281 if (UNLIKELY(calling->heap_argv)) {
4282 argv = RARRAY_PTR(calling->heap_argv);
4283 cfp->sp -= 2;
4284 }
4285 else {
4286 argc = calling->argc;
4287 argv = ALLOCA_N(VALUE, argc);
4288 MEMCPY(argv, cfp->sp - argc, VALUE, argc);
4289 cfp->sp += - argc - 1;
4290 }
4291
4292 return vm_call_bmethod_body(ec, calling, argv);
4293}
4294
4295static VALUE
4296vm_call_bmethod(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4297{
4298 RB_DEBUG_COUNTER_INC(ccf_bmethod);
4299
4300 const struct rb_callcache *cc = calling->cc;
4301 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4302 VALUE procv = cme->def->body.bmethod.proc;
4303 rb_proc_t *proc;
4304 GetProcPtr(procv, proc);
4305 const struct rb_block *block = &proc->block;
4306
4307 while (vm_block_type(block) == block_type_proc) {
4308 block = vm_proc_block(block->as.proc);
4309 }
4310 if (vm_block_type(block) == block_type_iseq) {
4311 CC_SET_FASTPATH(cc, vm_call_iseq_bmethod, TRUE);
4312 return vm_call_iseq_bmethod(ec, cfp, calling);
4313 }
4314
4315 CC_SET_FASTPATH(cc, vm_call_noniseq_bmethod, TRUE);
4316 return vm_call_noniseq_bmethod(ec, cfp, calling);
4317}
4318
4319VALUE
4320rb_find_defined_class_by_owner(VALUE current_class, VALUE target_owner)
4321{
4322 VALUE klass = current_class;
4323
4324 /* for prepended Module, then start from cover class */
4325 if (RB_TYPE_P(klass, T_ICLASS) && RICLASS_IS_ORIGIN_P(klass) &&
4326 RB_TYPE_P(RBASIC_CLASS(klass), T_CLASS)) {
4327 klass = RBASIC_CLASS(klass);
4328 }
4329
4330 while (RTEST(klass)) {
4331 VALUE owner = RB_TYPE_P(klass, T_ICLASS) ? RBASIC_CLASS(klass) : klass;
4332 if (owner == target_owner) {
4333 return klass;
4334 }
4335 klass = RCLASS_SUPER(klass);
4336 }
4337
4338 return current_class; /* maybe module function */
4339}
4340
4341static const rb_callable_method_entry_t *
4342aliased_callable_method_entry(const rb_callable_method_entry_t *me)
4343{
4344 const rb_method_entry_t *orig_me =
4345 rb_method_alias_original_me(rb_method_entry_def_acquire((const rb_method_entry_t *)me));
4346 const rb_callable_method_entry_t *cme = NULL;
4347
4348 if (orig_me->defined_class == 0) { // only true on first resolution
4349 // Resolving the alias mutates `me` (and may replace me->def, dropping a
4350 // reference to the shared definition), so it has to be serialized. Two
4351 // ractors calling the same aliased method would otherwise both release
4352 // the same definition which could result in a UAF.
4353 RB_VM_LOCKING() {
4354 orig_me = me->def->body.alias.original_me;
4355 // me is the ALIAS me. It has a def that's different from orig_me->def.
4356 if (orig_me->defined_class == 0) {
4357 // orig_me is the unresolved me clone that rb_add_alias stored.
4358 VALUE defined_class = rb_find_defined_class_by_owner(me->defined_class, orig_me->owner);
4359 VM_ASSERT_TYPE(orig_me->owner, T_MODULE);
4360 cme = rb_method_entry_complement_defined_class(orig_me, me->called_id, defined_class);
4361
4362 if (me->def->reference_count == 1) {
4363 rbimpl_atomic_ptr_store((volatile void **)&me->def->body.alias.original_me,
4364 (void *)cme, RBIMPL_ATOMIC_RELEASE);
4365 RB_OBJ_WRITTEN(me, Qundef, cme);
4366 }
4367 else {
4368 rb_method_definition_t *def =
4369 rb_method_definition_create(VM_METHOD_TYPE_ALIAS, me->def->original_id);
4370 def->body.alias.original_me = (rb_method_entry_t *)cme;
4371 def->original_module = cme->def->original_module;
4372 rb_method_definition_set((rb_method_entry_t *)me, def, NULL);
4373 RB_OBJ_WRITTEN(me, Qundef, cme);
4374 RB_OBJ_WRITTEN(me, Qundef, def->original_module);
4375 }
4376 }
4377 else {
4378 cme = (const rb_callable_method_entry_t *)orig_me;
4379 }
4380 }
4381 }
4382 else {
4383 cme = (const rb_callable_method_entry_t *)orig_me;
4384 }
4385
4386 VM_ASSERT(callable_method_entry_p(cme));
4387 return cme;
4388}
4389
4390const rb_callable_method_entry_t *
4391rb_aliased_callable_method_entry(const rb_callable_method_entry_t *me)
4392{
4393 return aliased_callable_method_entry(me);
4394}
4395
4396static VALUE
4397vm_call_alias(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4398{
4399 calling->cc = &VM_CC_ON_STACK(Qundef,
4400 vm_call_general,
4401 {{0}},
4402 aliased_callable_method_entry(vm_cc_cme(calling->cc)));
4403
4404 return vm_call_method_each_type(ec, cfp, calling);
4405}
4406
4407static enum method_missing_reason
4408ci_missing_reason(const struct rb_callinfo *ci)
4409{
4410 enum method_missing_reason stat = MISSING_NOENTRY;
4411 if (vm_ci_flag(ci) & VM_CALL_VCALL && !(vm_ci_flag(ci) & VM_CALL_FORWARDING)) stat |= MISSING_VCALL;
4412 if (vm_ci_flag(ci) & VM_CALL_FCALL) stat |= MISSING_FCALL;
4413 if (vm_ci_flag(ci) & VM_CALL_SUPER) stat |= MISSING_SUPER;
4414 return stat;
4415}
4416
4417static VALUE vm_call_method_missing(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling);
4418
4419static VALUE
4420vm_call_symbol(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
4421 struct rb_calling_info *calling, const struct rb_callinfo *ci, VALUE symbol, int flags)
4422{
4423 ASSUME(calling->argc >= 0);
4424
4425 enum method_missing_reason missing_reason = MISSING_NOENTRY;
4426 int argc = calling->argc;
4427 VALUE recv = calling->recv;
4428 VALUE klass = CLASS_OF(recv);
4429 ID mid = rb_check_id(&symbol);
4430 flags |= VM_CALL_OPT_SEND;
4431
4432 if (UNLIKELY(! mid)) {
4433 mid = idMethodMissing;
4434 missing_reason = ci_missing_reason(ci);
4435 ec->method_missing_reason = missing_reason;
4436
4437 VALUE argv_ary;
4438 if (UNLIKELY(argv_ary = calling->heap_argv)) {
4439 if (rb_method_basic_definition_p(klass, idMethodMissing)) {
4440 rb_ary_unshift(argv_ary, symbol);
4441
4442 /* Inadvertent symbol creation shall be forbidden, see [Feature #5112] */
4443 int priv = vm_ci_flag(ci) & (VM_CALL_FCALL | VM_CALL_VCALL);
4444 VALUE exc = rb_make_no_method_exception(
4445 rb_eNoMethodError, 0, recv, RARRAY_LENINT(argv_ary), RARRAY_CONST_PTR(argv_ary), priv);
4446
4447 rb_exc_raise(exc);
4448 }
4449 rb_ary_unshift(argv_ary, rb_str_intern(symbol));
4450 }
4451 else {
4452 /* E.g. when argc == 2
4453 *
4454 * | | | | TOPN
4455 * | | +------+
4456 * | | +---> | arg1 | 0
4457 * +------+ | +------+
4458 * | arg1 | -+ +-> | arg0 | 1
4459 * +------+ | +------+
4460 * | arg0 | ---+ | sym | 2
4461 * +------+ +------+
4462 * | recv | | recv | 3
4463 * --+------+--------+------+------
4464 */
4465 int i = argc;
4466 CHECK_VM_STACK_OVERFLOW(reg_cfp, 1);
4467 INC_SP(1);
4468 MEMMOVE(&TOPN(i - 1), &TOPN(i), VALUE, i);
4469 argc = ++calling->argc;
4470
4471 if (rb_method_basic_definition_p(klass, idMethodMissing)) {
4472 /* Inadvertent symbol creation shall be forbidden, see [Feature #5112] */
4473 TOPN(i) = symbol;
4474 int priv = vm_ci_flag(ci) & (VM_CALL_FCALL | VM_CALL_VCALL);
4475 const VALUE *argv = STACK_ADDR_FROM_TOP(argc);
4476 VALUE exc = rb_make_no_method_exception(
4477 rb_eNoMethodError, 0, recv, argc, argv, priv);
4478
4479 rb_exc_raise(exc);
4480 }
4481 else {
4482 TOPN(i) = rb_str_intern(symbol);
4483 }
4484 }
4485 }
4486
4487 struct rb_forwarding_call_data new_fcd = {
4488 .cd = {
4489 .ci = &VM_CI_ON_STACK(mid, flags, argc, vm_ci_kwarg(ci)),
4490 .cc = NULL,
4491 },
4492 .caller_ci = NULL,
4493 };
4494
4495 if (!(vm_ci_flag(ci) & VM_CALL_FORWARDING)) {
4496 calling->cd = &new_fcd.cd;
4497 }
4498 else {
4499 const struct rb_callinfo *caller_ci = ((struct rb_forwarding_call_data *)calling->cd)->caller_ci;
4500 VM_ASSERT((vm_ci_argc(caller_ci), 1));
4501 new_fcd.caller_ci = caller_ci;
4502 calling->cd = (struct rb_call_data *)&new_fcd;
4503 }
4504 calling->cc = &VM_CC_ON_STACK(klass,
4505 vm_call_general,
4506 { .method_missing_reason = missing_reason },
4507 rb_callable_method_entry_with_refinements(klass, mid, NULL));
4508
4509 if (flags & VM_CALL_FCALL) {
4510 return vm_call_method(ec, reg_cfp, calling);
4511 }
4512
4513 const struct rb_callcache *cc = calling->cc;
4514 VM_ASSERT(callable_method_entry_p(vm_cc_cme(cc)));
4515
4516 if (vm_cc_cme(cc) != NULL) {
4517 switch (METHOD_ENTRY_VISI(vm_cc_cme(cc))) {
4518 case METHOD_VISI_PUBLIC: /* likely */
4519 return vm_call_method_each_type(ec, reg_cfp, calling);
4520 case METHOD_VISI_PRIVATE:
4521 vm_cc_method_missing_reason_set(cc, MISSING_PRIVATE);
4522 break;
4523 case METHOD_VISI_PROTECTED:
4524 vm_cc_method_missing_reason_set(cc, MISSING_PROTECTED);
4525 break;
4526 default:
4527 VM_UNREACHABLE(vm_call_method);
4528 }
4529 return vm_call_method_missing(ec, reg_cfp, calling);
4530 }
4531
4532 return vm_call_method_nome(ec, reg_cfp, calling);
4533}
4534
4535static VALUE
4536vm_call_opt_send0(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, int flags)
4537{
4538 const struct rb_callinfo *ci = calling->cd->ci;
4539 int i;
4540 VALUE sym;
4541
4542 i = calling->argc - 1;
4543
4544 if (calling->argc == 0) {
4545 rb_raise(rb_eArgError, "no method name given");
4546 }
4547
4548 sym = TOPN(i);
4549 /* E.g. when i == 2
4550 *
4551 * | | | | TOPN
4552 * +------+ | |
4553 * | arg1 | ---+ | | 0
4554 * +------+ | +------+
4555 * | arg0 | -+ +-> | arg1 | 1
4556 * +------+ | +------+
4557 * | sym | +---> | arg0 | 2
4558 * +------+ +------+
4559 * | recv | | recv | 3
4560 * --+------+--------+------+------
4561 */
4562 /* shift arguments */
4563 if (i > 0) {
4564 MEMMOVE(&TOPN(i), &TOPN(i-1), VALUE, i);
4565 }
4566 calling->argc -= 1;
4567 DEC_SP(1);
4568
4569 return vm_call_symbol(ec, reg_cfp, calling, ci, sym, flags);
4570}
4571
4572static VALUE
4573vm_call_opt_send_complex(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4574{
4575 RB_DEBUG_COUNTER_INC(ccf_opt_send_complex);
4576 const struct rb_callinfo *ci = calling->cd->ci;
4577 int flags = VM_CALL_FCALL;
4578 VALUE sym;
4579
4580 VALUE argv_ary;
4581 CALLER_SETUP_ARG(reg_cfp, calling, ci, ALLOW_HEAP_ARGV);
4582 if (UNLIKELY(argv_ary = calling->heap_argv)) {
4583 sym = rb_ary_shift(argv_ary);
4584 flags |= VM_CALL_ARGS_SPLAT;
4585 if (calling->kw_splat) {
4586 VALUE last_hash = rb_ary_last(0, NULL, argv_ary);
4587 ((struct RHash *)last_hash)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
4588 calling->kw_splat = 0;
4589 }
4590 return vm_call_symbol(ec, reg_cfp, calling, ci, sym, flags);
4591 }
4592
4593 if (calling->kw_splat) flags |= VM_CALL_KW_SPLAT;
4594 return vm_call_opt_send0(ec, reg_cfp, calling, flags);
4595}
4596
4597static VALUE
4598vm_call_opt_send_simple(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4599{
4600 RB_DEBUG_COUNTER_INC(ccf_opt_send_simple);
4601 return vm_call_opt_send0(ec, reg_cfp, calling, vm_ci_flag(calling->cd->ci) | VM_CALL_FCALL);
4602}
4603
4604static VALUE
4605vm_call_opt_send(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4606{
4607 RB_DEBUG_COUNTER_INC(ccf_opt_send);
4608
4609 const struct rb_callinfo *ci = calling->cd->ci;
4610 int flags = vm_ci_flag(ci);
4611
4612 if (UNLIKELY((flags & VM_CALL_FORWARDING) || (!(flags & VM_CALL_ARGS_SIMPLE) &&
4613 ((calling->argc == 1 && (flags & (VM_CALL_ARGS_SPLAT | VM_CALL_KW_SPLAT))) ||
4614 (calling->argc == 2 && (flags & VM_CALL_ARGS_SPLAT) && (flags & VM_CALL_KW_SPLAT)) ||
4615 ((flags & VM_CALL_KWARG) && (vm_ci_kwarg(ci)->keyword_len == calling->argc)))))) {
4616 CC_SET_FASTPATH(calling->cc, vm_call_opt_send_complex, TRUE);
4617 return vm_call_opt_send_complex(ec, reg_cfp, calling);
4618 }
4619
4620 CC_SET_FASTPATH(calling->cc, vm_call_opt_send_simple, TRUE);
4621 return vm_call_opt_send_simple(ec, reg_cfp, calling);
4622}
4623
4624static VALUE
4625vm_call_method_missing_body(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling,
4626 const struct rb_callinfo *orig_ci, enum method_missing_reason reason)
4627{
4628 RB_DEBUG_COUNTER_INC(ccf_method_missing);
4629
4630 VALUE *argv = STACK_ADDR_FROM_TOP(calling->argc);
4631 unsigned int argc, flag;
4632
4633 flag = VM_CALL_FCALL | VM_CALL_OPT_SEND | vm_ci_flag(orig_ci);
4634 argc = ++calling->argc;
4635
4636 /* shift arguments: m(a, b, c) #=> method_missing(:m, a, b, c) */
4637 CHECK_VM_STACK_OVERFLOW(reg_cfp, 1);
4638 vm_check_canary(ec, reg_cfp->sp);
4639 if (argc > 1) {
4640 MEMMOVE(argv+1, argv, VALUE, argc-1);
4641 }
4642 argv[0] = ID2SYM(vm_ci_mid(orig_ci));
4643 INC_SP(1);
4644
4645 ec->method_missing_reason = reason;
4646
4647 struct rb_forwarding_call_data new_fcd = {
4648 .cd = {
4649 .ci = &VM_CI_ON_STACK(idMethodMissing, flag, argc, vm_ci_kwarg(orig_ci)),
4650 .cc = NULL,
4651 },
4652 .caller_ci = NULL,
4653 };
4654
4655 if (!(flag & VM_CALL_FORWARDING)) {
4656 calling->cd = &new_fcd.cd;
4657 }
4658 else {
4659 const struct rb_callinfo *caller_ci = ((struct rb_forwarding_call_data *)calling->cd)->caller_ci;
4660 VM_ASSERT((vm_ci_argc(caller_ci), 1));
4661 new_fcd.caller_ci = caller_ci;
4662 calling->cd = (struct rb_call_data *)&new_fcd;
4663 }
4664
4665 calling->cc = &VM_CC_ON_STACK(Qundef, vm_call_general, {{ 0 }},
4666 rb_callable_method_entry_without_refinements(CLASS_OF(calling->recv), idMethodMissing, NULL));
4667 return vm_call_method(ec, reg_cfp, calling);
4668}
4669
4670static VALUE
4671vm_call_method_missing(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4672{
4673 return vm_call_method_missing_body(ec, reg_cfp, calling, calling->cd->ci, vm_cc_cmethod_missing_reason(calling->cc));
4674}
4675
4676static const rb_callable_method_entry_t *refined_method_callable_without_refinement(const rb_callable_method_entry_t *me);
4677static VALUE
4678vm_call_zsuper(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling, VALUE klass)
4679{
4680 klass = RCLASS_SUPER(klass);
4681
4682 const rb_callable_method_entry_t *cme = klass ? rb_callable_method_entry(klass, vm_ci_mid(calling->cd->ci)) : NULL;
4683 if (cme == NULL) {
4684 return vm_call_method_nome(ec, cfp, calling);
4685 }
4686 if (cme->def->type == VM_METHOD_TYPE_REFINED &&
4687 cme->def->body.refined.orig_me) {
4688 cme = refined_method_callable_without_refinement(cme);
4689 }
4690
4691 calling->cc = &VM_CC_ON_STACK(Qundef, vm_call_general, {{ 0 }}, cme);
4692
4693 return vm_call_method_each_type(ec, cfp, calling);
4694}
4695
4696static inline VALUE
4697find_refinement(VALUE refinements, VALUE klass)
4698{
4699 if (NIL_P(refinements)) {
4700 return Qnil;
4701 }
4702 return rb_hash_lookup(refinements, klass);
4703}
4704
4705PUREFUNC(static rb_control_frame_t * current_method_entry(const rb_execution_context_t *ec, rb_control_frame_t *cfp));
4706static rb_control_frame_t *
4707current_method_entry(const rb_execution_context_t *ec, rb_control_frame_t *cfp)
4708{
4709 rb_control_frame_t *top_cfp = cfp;
4710
4711 if (CFP_ISEQ(cfp) && !VM_FRAME_BMETHOD_P(cfp) && ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_BLOCK) {
4712 const rb_iseq_t *local_iseq = ISEQ_BODY(CFP_ISEQ(cfp))->local_iseq;
4713
4714 do {
4715 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
4716 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
4717 /* TODO: orphan block */
4718 return top_cfp;
4719 }
4720 } while (CFP_ISEQ(cfp) != local_iseq && !VM_FRAME_BMETHOD_P(cfp));
4721 }
4722 return cfp;
4723}
4724
4725static const rb_callable_method_entry_t *
4726refined_method_callable_without_refinement(const rb_callable_method_entry_t *me)
4727{
4728 const rb_method_entry_t *orig_me = me->def->body.refined.orig_me;
4729 const rb_callable_method_entry_t *cme;
4730
4731 if (orig_me->defined_class == 0) {
4732 cme = NULL;
4734 }
4735 else {
4736 cme = (const rb_callable_method_entry_t *)orig_me;
4737 }
4738
4739 VM_ASSERT(callable_method_entry_p(cme));
4740
4741 if (UNDEFINED_METHOD_ENTRY_P(cme)) {
4742 cme = NULL;
4743 }
4744
4745 return cme;
4746}
4747
4748// Construct correct super chain for module refinements.
4749// Each class including/prepending a module has a weak map mapping refinement
4750// iclasses to refinement module iclasses with the correct super. Like
4751// singleton classes, these are lazily initialized (both the map and
4752// the refinement module iclasses).
4753static VALUE
4754module_refinement_iclass(VALUE refinement_iclass, VALUE defined_class)
4755{
4756 VALUE module_ref_iclass = Qundef;
4757
4758 RB_VM_LOCKING() {
4759 VALUE cache = RCLASS_MODULE_REFINEMENT_ICLASSES(defined_class);
4760
4761 if (LIKELY(cache)) {
4762 module_ref_iclass = rb_wmap_lookup(cache, refinement_iclass);
4763 }
4764 else {
4765 cache = rb_wmap_new_hidden();
4766 RCLASS_SET_MODULE_REFINEMENT_ICLASSES(defined_class, cache);
4767 }
4768
4769 if (UNDEF_P(module_ref_iclass)) {
4770 VALUE super;
4771 VALUE refinement_iclass_super = RCLASS_SUPER(refinement_iclass);
4772
4773 if (RICLASS_FOR_REFINEMENT_P(refinement_iclass_super)) {
4774 super = module_refinement_iclass(refinement_iclass_super, defined_class);
4775 }
4776 else {
4777 super = defined_class;
4778 }
4779
4780 module_ref_iclass = rb_include_class_new(RBASIC(refinement_iclass)->klass, super);
4781 RCLASS_SET_REFINED_CLASS(module_ref_iclass, RCLASS_REFINED_CLASS(refinement_iclass));
4782 rb_class_subclass_add(RCLASS_REFINED_CLASS(refinement_iclass), module_ref_iclass);
4783 rb_wmap_aset(cache, refinement_iclass, module_ref_iclass);
4784 }
4785 }
4786
4787 return module_ref_iclass;
4788}
4789
4790VALUE
4791rb_vm_module_refinement_iclass(VALUE refinement_iclass, VALUE defined_class)
4792{
4793 return module_refinement_iclass(refinement_iclass, defined_class);
4794}
4795
4796static VALUE
4797refinement_iclass_for_cme(VALUE refinement_iclass, const rb_callable_method_entry_t *cme)
4798{
4799 if (RB_TYPE_P(cme->owner, T_MODULE) && RB_TYPE_P(cme->defined_class, T_ICLASS)) {
4800 refinement_iclass = module_refinement_iclass(refinement_iclass, cme->defined_class);
4801 }
4802 return refinement_iclass;
4803}
4804
4805VALUE
4806rb_vm_refinement_iclass_for_cme(VALUE refinement_iclass, const rb_callable_method_entry_t *cme)
4807{
4808 return refinement_iclass_for_cme(refinement_iclass, cme);
4809}
4810
4811static const rb_callable_method_entry_t *
4812search_refined_method(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4813{
4814 const struct rb_callinfo *ci = calling->cd->ci;
4815 ID mid = vm_ci_mid(ci);
4816 // The initialize call of opt_new must behave like Class#new, which does not see the caller's refinements. [Bug #22501]
4817 const rb_cref_t *top_cref = (vm_ci_flag(ci) & VM_CALL_IGNORE_REFINEMENTS) ? NULL : vm_get_cref(cfp->ep);
4818 const struct rb_callcache * const cc = calling->cc;
4819 const rb_callable_method_entry_t *refined_cme = vm_cc_cme(cc);
4820 const rb_callable_method_entry_t *cme;
4821 bool skip_current_method = vm_cc_call(cc) == vm_call_super_method;
4822
4823 retry:
4824 cme = refined_cme;
4825
4826 for (const rb_cref_t *cref = top_cref; cref; cref = CREF_NEXT(cref)) {
4827 VALUE refinement = find_refinement(CREF_REFINEMENTS(cref), refined_cme->owner);
4828 if (NIL_P(refinement)) continue;
4829
4830 refinement = refinement_iclass_for_cme(refinement, refined_cme);
4831
4832 const rb_callable_method_entry_t *const ref_me =
4833 rb_callable_method_entry(refinement, mid);
4834
4835 if (ref_me) {
4836 if (skip_current_method) {
4837 const rb_control_frame_t *top_cfp = current_method_entry(ec, cfp);
4838 const rb_callable_method_entry_t *top_me = rb_vm_frame_method_entry(top_cfp);
4839 if (top_me && rb_method_definition_eq(ref_me->def, top_me->def)) {
4840 continue;
4841 }
4842 }
4843
4844 if (cme->def->type != VM_METHOD_TYPE_REFINED ||
4845 cme->def != ref_me->def) {
4846 cme = ref_me;
4847 }
4848 if (ref_me->def->type != VM_METHOD_TYPE_REFINED) {
4849 return cme;
4850 }
4851 }
4852 else {
4853 return NULL;
4854 }
4855 }
4856
4857 if (refined_cme->def->body.refined.orig_me) {
4858 return refined_method_callable_without_refinement(refined_cme);
4859 }
4860 else {
4861 VALUE klass = RCLASS_SUPER(refined_cme->defined_class);
4862 cme = klass ? rb_callable_method_entry(klass, mid) : NULL;
4863 if (cme && cme->def->type == VM_METHOD_TYPE_REFINED) {
4864 // Resolve it here; a second pass of vm_call_refined would cache
4865 // the result for this entry's iclass.
4866 refined_cme = cme;
4867 skip_current_method = false;
4868 goto retry;
4869 }
4870 return cme;
4871 }
4872}
4873
4874static VALUE
4875vm_call_refined(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
4876{
4877 const rb_callable_method_entry_t *ref_cme = search_refined_method(ec, cfp, calling);
4878
4879 if (ref_cme) {
4880 if (calling->cd->cc) {
4881 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);
4882 RB_OBJ_WRITE(CFP_ISEQ(cfp), &calling->cd->cc, cc);
4883 return vm_call_method(ec, cfp, calling);
4884 }
4885 else {
4886 struct rb_callcache *ref_cc = &VM_CC_ON_STACK(Qundef, vm_call_general, {{ 0 }}, ref_cme);
4887 calling->cc= ref_cc;
4888 return vm_call_method(ec, cfp, calling);
4889 }
4890 }
4891 else {
4892 return vm_call_method_nome(ec, cfp, calling);
4893 }
4894}
4895
4896static 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);
4897
4898NOINLINE(static VALUE
4899 vm_invoke_block_opt_call(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
4900 struct rb_calling_info *calling, const struct rb_callinfo *ci, VALUE block_handler));
4901
4902static VALUE
4903vm_invoke_block_opt_call(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
4904 struct rb_calling_info *calling, const struct rb_callinfo *ci, VALUE block_handler)
4905{
4906 int argc = calling->argc;
4907
4908 /* remove self */
4909 if (argc > 0) MEMMOVE(&TOPN(argc), &TOPN(argc-1), VALUE, argc);
4910 DEC_SP(1);
4911
4912 return vm_invoke_block(ec, reg_cfp, calling, ci, false, block_handler);
4913}
4914
4915static VALUE
4916vm_call_opt_call(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4917{
4918 RB_DEBUG_COUNTER_INC(ccf_opt_call);
4919
4920 const struct rb_callinfo *ci = calling->cd->ci;
4921 VALUE procval = calling->recv;
4922 return vm_invoke_block_opt_call(ec, reg_cfp, calling, ci, VM_BH_FROM_PROC(procval));
4923}
4924
4925static VALUE
4926vm_call_opt_block_call(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4927{
4928 RB_DEBUG_COUNTER_INC(ccf_opt_block_call);
4929
4930 VALUE block_handler = VM_ENV_BLOCK_HANDLER(VM_CF_LEP(reg_cfp));
4931 const struct rb_callinfo *ci = calling->cd->ci;
4932
4933 enum ruby_basic_operators bop;
4934 switch (vm_ci_mid(ci)) {
4935 case idAREF: bop = BOP_AREF; break;
4936 case idYield: bop = BOP_YIELD; break;
4937 case idEqq: bop = BOP_EQQ; break;
4938 default: bop = BOP_CALL; break;
4939 }
4940
4941 if (BASIC_OP_UNREDEFINED_P(bop, PROC_REDEFINED_OP_FLAG)) {
4942 return vm_invoke_block_opt_call(ec, reg_cfp, calling, ci, block_handler);
4943 }
4944 else {
4945 calling->recv = rb_vm_bh_to_procval(ec, block_handler);
4946 calling->cc = rb_vm_search_method_slowpath(ci, CLASS_OF(calling->recv));
4947 return vm_call_general(ec, reg_cfp, calling);
4948 }
4949}
4950
4951static VALUE
4952vm_call_opt_struct_aref0(rb_execution_context_t *ec, struct rb_calling_info *calling)
4953{
4954 VALUE recv = calling->recv;
4955
4956 VM_ASSERT(RB_TYPE_P(recv, T_STRUCT));
4957 VM_ASSERT(vm_cc_cme(calling->cc)->def->type == VM_METHOD_TYPE_OPTIMIZED);
4958 VM_ASSERT(vm_cc_cme(calling->cc)->def->body.optimized.type == OPTIMIZED_METHOD_TYPE_STRUCT_AREF);
4959
4960 const unsigned int off = vm_cc_cme(calling->cc)->def->body.optimized.index;
4961 return RSTRUCT_GET_RAW(recv, off);
4962}
4963
4964static VALUE
4965vm_call_opt_struct_aref(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4966{
4967 RB_DEBUG_COUNTER_INC(ccf_opt_struct_aref);
4968
4969 VALUE ret = vm_call_opt_struct_aref0(ec, calling);
4970 reg_cfp->sp -= 1;
4971 return ret;
4972}
4973
4974static VALUE
4975vm_call_opt_struct_aset0(rb_execution_context_t *ec, struct rb_calling_info *calling, VALUE val)
4976{
4977 VALUE recv = calling->recv;
4978
4979 VM_ASSERT(RB_TYPE_P(recv, T_STRUCT));
4980 VM_ASSERT(vm_cc_cme(calling->cc)->def->type == VM_METHOD_TYPE_OPTIMIZED);
4981 VM_ASSERT(vm_cc_cme(calling->cc)->def->body.optimized.type == OPTIMIZED_METHOD_TYPE_STRUCT_ASET);
4982
4983 rb_check_frozen(recv);
4984
4985 const unsigned int off = vm_cc_cme(calling->cc)->def->body.optimized.index;
4986 RSTRUCT_SET_RAW(recv, off, val);
4987
4988 return val;
4989}
4990
4991static VALUE
4992vm_call_opt_struct_aset(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
4993{
4994 RB_DEBUG_COUNTER_INC(ccf_opt_struct_aset);
4995
4996 VALUE ret = vm_call_opt_struct_aset0(ec, calling, *(reg_cfp->sp - 1));
4997 reg_cfp->sp -= 2;
4998 return ret;
4999}
5000
5001NOINLINE(static VALUE vm_call_optimized(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling,
5002 const struct rb_callinfo *ci, const struct rb_callcache *cc));
5003
5004#define VM_CALL_METHOD_ATTR(var, func, nohook) \
5005 if (UNLIKELY(ruby_vm_c_events_enabled > 0)) { \
5006 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, calling->recv, vm_cc_cme(cc)->def->original_id, \
5007 vm_ci_mid(ci), vm_cc_cme(cc)->owner, Qundef); \
5008 var = func; \
5009 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, calling->recv, vm_cc_cme(cc)->def->original_id, \
5010 vm_ci_mid(ci), vm_cc_cme(cc)->owner, (var)); \
5011 } \
5012 else { \
5013 nohook; \
5014 var = func; \
5015 }
5016
5017static VALUE
5018vm_call_optimized(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling,
5019 const struct rb_callinfo *ci, const struct rb_callcache *cc)
5020{
5021 switch (vm_cc_cme(cc)->def->body.optimized.type) {
5022 case OPTIMIZED_METHOD_TYPE_SEND:
5023 CC_SET_FASTPATH(cc, vm_call_opt_send, TRUE);
5024 return vm_call_opt_send(ec, cfp, calling);
5025 case OPTIMIZED_METHOD_TYPE_CALL:
5026 CC_SET_FASTPATH(cc, vm_call_opt_call, TRUE);
5027 return vm_call_opt_call(ec, cfp, calling);
5028 case OPTIMIZED_METHOD_TYPE_BLOCK_CALL:
5029 CC_SET_FASTPATH(cc, vm_call_opt_block_call, TRUE);
5030 return vm_call_opt_block_call(ec, cfp, calling);
5031 case OPTIMIZED_METHOD_TYPE_STRUCT_AREF: {
5032 CALLER_SETUP_ARG(cfp, calling, ci, 0);
5033 rb_check_arity(calling->argc, 0, 0);
5034
5035 VALUE v;
5036 VM_CALL_METHOD_ATTR(v,
5037 vm_call_opt_struct_aref(ec, cfp, calling),
5038 set_vm_cc_ivar(cc); \
5039 CC_SET_FASTPATH(cc, vm_call_opt_struct_aref, (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)))
5040 return v;
5041 }
5042 case OPTIMIZED_METHOD_TYPE_STRUCT_ASET: {
5043 CALLER_SETUP_ARG(cfp, calling, ci, 1);
5044 rb_check_arity(calling->argc, 1, 1);
5045
5046 VALUE v;
5047 VM_CALL_METHOD_ATTR(v,
5048 vm_call_opt_struct_aset(ec, cfp, calling),
5049 set_vm_cc_ivar(cc); \
5050 CC_SET_FASTPATH(cc, vm_call_opt_struct_aset, (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)))
5051 return v;
5052 }
5053 default:
5054 rb_bug("vm_call_method: unsupported optimized method type (%d)", vm_cc_cme(cc)->def->body.optimized.type);
5055 }
5056}
5057
5058static VALUE
5059vm_call_method_each_type(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
5060{
5061 const struct rb_callinfo *ci = calling->cd->ci;
5062 const struct rb_callcache *cc = calling->cc;
5063 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
5064 VALUE v;
5065
5066 VM_ASSERT(! METHOD_ENTRY_INVALIDATED(cme));
5067
5068 switch (cme->def->type) {
5069 case VM_METHOD_TYPE_ISEQ:
5070 if (ISEQ_BODY(def_iseq_ptr(cme->def))->param.flags.forwardable) {
5071 CC_SET_FASTPATH(cc, vm_call_iseq_fwd_setup, TRUE);
5072 return vm_call_iseq_fwd_setup(ec, cfp, calling);
5073 }
5074 else {
5075 CC_SET_FASTPATH(cc, vm_call_iseq_setup, TRUE);
5076 return vm_call_iseq_setup(ec, cfp, calling);
5077 }
5078
5079 case VM_METHOD_TYPE_NOTIMPLEMENTED:
5080 case VM_METHOD_TYPE_CFUNC:
5081 CC_SET_FASTPATH(cc, vm_call_cfunc, TRUE);
5082 return vm_call_cfunc(ec, cfp, calling);
5083
5084 case VM_METHOD_TYPE_ATTRSET:
5085 CALLER_SETUP_ARG(cfp, calling, ci, 1);
5086
5087 rb_check_arity(calling->argc, 1, 1);
5088
5089 const unsigned int aset_mask = (VM_CALL_ARGS_SPLAT | VM_CALL_KW_SPLAT | VM_CALL_KWARG | VM_CALL_FORWARDING);
5090
5091 if (vm_cc_markable(cc)) {
5092 vm_cc_attr_index_set(cc, IVAR_CACHE_INIT);
5093 VM_CALL_METHOD_ATTR(v,
5094 vm_call_attrset_direct(ec, cfp, cc, calling->recv),
5095 CC_SET_FASTPATH(cc, vm_call_attrset, !(vm_ci_flag(ci) & aset_mask)));
5096 }
5097 else {
5098 cc = &((struct rb_callcache) {
5099 .flags = T_IMEMO |
5100 (imemo_callcache << FL_USHIFT) |
5101 VM_CALLCACHE_UNMARKABLE |
5102 VM_CALLCACHE_ON_STACK,
5103 .klass = cc->klass,
5104 .cme_ = cc->cme_,
5105 .call_ = cc->call_,
5106 .aux_ = {
5107 .attr = {
5108 .value = IVAR_CACHE_INIT,
5109 }
5110 },
5111 });
5112
5113 VM_CALL_METHOD_ATTR(v,
5114 vm_call_attrset_direct(ec, cfp, cc, calling->recv),
5115 CC_SET_FASTPATH(cc, vm_call_attrset, !(vm_ci_flag(ci) & aset_mask)));
5116 }
5117 return v;
5118
5119 case VM_METHOD_TYPE_IVAR:
5120 CALLER_SETUP_ARG(cfp, calling, ci, 0);
5121 rb_check_arity(calling->argc, 0, 0);
5122 vm_cc_attr_index_set(cc, rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET));
5123 const unsigned int ivar_mask = (VM_CALL_ARGS_SPLAT | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING);
5124 VM_CALL_METHOD_ATTR(v,
5125 vm_call_ivar(ec, cfp, calling),
5126 CC_SET_FASTPATH(cc, vm_call_ivar, !(vm_ci_flag(ci) & ivar_mask)));
5127 return v;
5128
5129 case VM_METHOD_TYPE_MISSING:
5130 vm_cc_method_missing_reason_set(cc, 0);
5131 CC_SET_FASTPATH(cc, vm_call_method_missing, TRUE);
5132 return vm_call_method_missing(ec, cfp, calling);
5133
5134 case VM_METHOD_TYPE_BMETHOD:
5135 CC_SET_FASTPATH(cc, vm_call_bmethod, TRUE);
5136 return vm_call_bmethod(ec, cfp, calling);
5137
5138 case VM_METHOD_TYPE_ALIAS:
5139 CC_SET_FASTPATH(cc, vm_call_alias, TRUE);
5140 return vm_call_alias(ec, cfp, calling);
5141
5142 case VM_METHOD_TYPE_OPTIMIZED:
5143 return vm_call_optimized(ec, cfp, calling, ci, cc);
5144
5145 case VM_METHOD_TYPE_UNDEF:
5146 break;
5147
5148 case VM_METHOD_TYPE_ZSUPER:
5149 return vm_call_zsuper(ec, cfp, calling, RCLASS_ORIGIN(vm_cc_cme(cc)->defined_class));
5150
5151 case VM_METHOD_TYPE_REFINED:
5152 // CC_SET_FASTPATH(cc, vm_call_refined, TRUE);
5153 // should not set FASTPATH since vm_call_refined assumes cc->call is vm_call_super_method on invokesuper.
5154 return vm_call_refined(ec, cfp, calling);
5155 }
5156
5157 rb_bug("vm_call_method: unsupported method type (%d)", vm_cc_cme(cc)->def->type);
5158}
5159
5160NORETURN(static void vm_raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE obj, int call_status));
5161
5162static VALUE
5163vm_call_method_nome(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
5164{
5165 /* method missing */
5166 const struct rb_callinfo *ci = calling->cd->ci;
5167 const int stat = ci_missing_reason(ci);
5168
5169 if (vm_ci_mid(ci) == idMethodMissing) {
5170 if (UNLIKELY(calling->heap_argv)) {
5171 vm_raise_method_missing(ec, RARRAY_LENINT(calling->heap_argv), RARRAY_CONST_PTR(calling->heap_argv), calling->recv, stat);
5172 }
5173 else {
5174 rb_control_frame_t *reg_cfp = cfp;
5175 VALUE *argv = STACK_ADDR_FROM_TOP(calling->argc);
5176 vm_raise_method_missing(ec, calling->argc, argv, calling->recv, stat);
5177 }
5178 }
5179 else {
5180 return vm_call_method_missing_body(ec, cfp, calling, ci, stat);
5181 }
5182}
5183
5184/* Protected method calls and super invocations need to check that the receiver
5185 * (self for super) inherits the module on which the method is defined.
5186 * In the case of refinements, it should consider the original class not the
5187 * refinement.
5188 */
5189static VALUE
5190vm_defined_class_for_protected_call(const rb_callable_method_entry_t *me)
5191{
5192 VALUE defined_class = me->defined_class;
5193 VALUE refined_class = RCLASS_REFINED_CLASS(defined_class);
5194 return NIL_P(refined_class) ? defined_class : refined_class;
5195}
5196
5197static inline VALUE
5198vm_call_method(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
5199{
5200 const struct rb_callinfo *ci = calling->cd->ci;
5201 const struct rb_callcache *cc = calling->cc;
5202
5203 VM_ASSERT(callable_method_entry_p(vm_cc_cme(cc)));
5204
5205 if (vm_cc_cme(cc) != NULL) {
5206 switch (METHOD_ENTRY_VISI(vm_cc_cme(cc))) {
5207 case METHOD_VISI_PUBLIC: /* likely */
5208 return vm_call_method_each_type(ec, cfp, calling);
5209
5210 case METHOD_VISI_PRIVATE:
5211 if (!(vm_ci_flag(ci) & VM_CALL_FCALL)) {
5212 enum method_missing_reason stat = MISSING_PRIVATE;
5213 if (vm_ci_flag(ci) & VM_CALL_VCALL) stat |= MISSING_VCALL;
5214
5215 vm_cc_method_missing_reason_set(cc, stat);
5216 CC_SET_FASTPATH(cc, vm_call_method_missing, TRUE);
5217 return vm_call_method_missing(ec, cfp, calling);
5218 }
5219 return vm_call_method_each_type(ec, cfp, calling);
5220
5221 case METHOD_VISI_PROTECTED:
5222 if (!(vm_ci_flag(ci) & (VM_CALL_OPT_SEND | VM_CALL_FCALL))) {
5223 VALUE defined_class = vm_defined_class_for_protected_call(vm_cc_cme(cc));
5224 if (!rb_obj_is_kind_of(cfp->self, defined_class)) {
5225 vm_cc_method_missing_reason_set(cc, MISSING_PROTECTED);
5226 return vm_call_method_missing(ec, cfp, calling);
5227 }
5228 else {
5229 /* caching method info to dummy cc */
5230 VM_ASSERT(vm_cc_cme(cc) != NULL);
5231 struct rb_callcache cc_on_stack = *cc;
5232 FL_SET_RAW((VALUE)&cc_on_stack, VM_CALLCACHE_UNMARKABLE);
5233 calling->cc = &cc_on_stack;
5234 return vm_call_method_each_type(ec, cfp, calling);
5235 }
5236 }
5237 return vm_call_method_each_type(ec, cfp, calling);
5238
5239 default:
5240 rb_bug("unreachable");
5241 }
5242 }
5243 else {
5244 return vm_call_method_nome(ec, cfp, calling);
5245 }
5246}
5247
5248static VALUE
5249vm_call_general(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
5250{
5251 RB_DEBUG_COUNTER_INC(ccf_general);
5252 return vm_call_method(ec, reg_cfp, calling);
5253}
5254
5255void
5256rb_vm_cc_general(const struct rb_callcache *cc)
5257{
5258 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
5259 VM_ASSERT(cc != vm_cc_empty());
5260
5261 *(vm_call_handler *)&cc->call_ = vm_call_general;
5262}
5263
5264static VALUE
5265vm_call_super_method(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
5266{
5267 RB_DEBUG_COUNTER_INC(ccf_super_method);
5268
5269 // This line is introduced to make different from `vm_call_general` because some compilers (VC we found)
5270 // can merge the function and the address of the function becomes same.
5271 // The address of `vm_call_super_method` is used in `search_refined_method`, so it should be different.
5272 if (ec == NULL) rb_bug("unreachable");
5273
5274 /* this check is required to distinguish with other functions. */
5275 VM_ASSERT(vm_cc_call(calling->cc) == vm_call_super_method);
5276 return vm_call_method(ec, reg_cfp, calling);
5277}
5278
5279/* super */
5280
5281static inline VALUE
5282vm_search_normal_superclass(VALUE klass)
5283{
5284 if (RICLASS_FOR_REFINEMENT_P(klass)) {
5285 if (RB_TYPE_P(RCLASS_REFINED_CLASS(klass), T_MODULE)) {
5286 do {
5287 klass = RCLASS_SUPER(klass);
5288 } while (RICLASS_FOR_REFINEMENT_P(klass));
5289
5290 return klass;
5291 }
5292
5293 klass = RBASIC(klass)->klass;
5294 }
5295 klass = RCLASS_ORIGIN(klass);
5296 return RCLASS_SUPER(klass);
5297}
5298
5299NORETURN(static void vm_super_outside(void));
5300
5301static void
5302vm_super_outside(void)
5303{
5304 rb_raise(rb_eNoMethodError, "super called outside of method");
5305}
5306
5307static const struct rb_callcache *
5308empty_cc_for_super(void)
5309{
5310 return &rb_vm_empty_cc_for_super;
5311}
5312
5313static const struct rb_callcache *
5314vm_search_super_method(const rb_control_frame_t *reg_cfp, struct rb_call_data *cd, VALUE recv)
5315{
5316 VALUE current_defined_class;
5317 const rb_iseq_t *iseq = CFP_ISEQ(reg_cfp);
5318 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(reg_cfp);
5319
5320 if (!me) {
5321 vm_super_outside();
5322 }
5323
5324 current_defined_class = vm_defined_class_for_protected_call(me);
5325
5326 if (BUILTIN_TYPE(current_defined_class) != T_MODULE &&
5327 iseq != method_entry_iseqptr(me) &&
5328 !rb_obj_is_kind_of(recv, current_defined_class)) {
5329 VALUE m = RB_TYPE_P(current_defined_class, T_ICLASS) ?
5330 RCLASS_INCLUDER(current_defined_class) : current_defined_class;
5331
5332 if (m) { /* not bound UnboundMethod */
5333 rb_raise(rb_eTypeError,
5334 "self has wrong type to call super in this context: "
5335 "%"PRIsVALUE" (expected %"PRIsVALUE")",
5336 rb_obj_class(recv), m);
5337 }
5338 }
5339
5340 if (me->def->type == VM_METHOD_TYPE_BMETHOD && (vm_ci_flag(cd->ci) & VM_CALL_ZSUPER)) {
5341 rb_raise(rb_eRuntimeError,
5342 "implicit argument passing of super from method defined"
5343 " by define_method() is not supported."
5344 " Specify all arguments explicitly.");
5345 }
5346
5347 ID mid = me->def->original_id;
5348
5349 if (!vm_ci_markable(cd->ci)) {
5350 VM_FORCE_WRITE((const VALUE *)&cd->ci->mid, (VALUE)mid);
5351 }
5352 else {
5353 // update iseq. really? (TODO)
5354 cd->ci = vm_ci_new_runtime(mid,
5355 vm_ci_flag(cd->ci),
5356 vm_ci_argc(cd->ci),
5357 vm_ci_kwarg(cd->ci));
5358
5359 RB_OBJ_WRITTEN(iseq, Qundef, cd->ci);
5360 }
5361
5362 const struct rb_callcache *cc;
5363
5364 VALUE klass = vm_search_normal_superclass(me->defined_class);
5365
5366 if (!klass) {
5367 /* bound instance method of module */
5368 cc = vm_cc_new(Qundef, NULL, vm_call_method_missing, cc_type_super);
5369 vm_cc_method_missing_reason_set(cc, ci_missing_reason(cd->ci));
5370 RB_OBJ_WRITE(iseq, &cd->cc, cc);
5371 }
5372 else {
5373 cc = vm_search_method_fastpath(reg_cfp, cd, klass);
5374 VM_ASSERT(cc != &rb_vm_uncached_cc);
5375 const rb_callable_method_entry_t *cached_cme = vm_cc_cme(cc);
5376
5377 // define_method can cache for different method id
5378 if (cached_cme == NULL) {
5379 // empty_cc_for_super is not markable object
5380 cd->cc = empty_cc_for_super();
5381 }
5382 else if (cached_cme->called_id != mid) {
5383 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, mid);
5384 if (cme) {
5385 cc = vm_cc_new(klass, cme, vm_call_super_method, cc_type_super);
5386 RB_OBJ_WRITE(iseq, &cd->cc, cc);
5387 }
5388 else {
5389 cd->cc = cc = empty_cc_for_super();
5390 }
5391 }
5392 else {
5393 switch (cached_cme->def->type) {
5394 // vm_call_refined (search_refined_method) assumes cc->call is vm_call_super_method on invokesuper
5395 case VM_METHOD_TYPE_REFINED:
5396 // cc->klass is superclass of receiver class. Checking cc->klass is not enough to invalidate IVC for the receiver class.
5397 case VM_METHOD_TYPE_ATTRSET:
5398 case VM_METHOD_TYPE_IVAR:
5399 vm_cc_call_set(cc, vm_call_super_method); // invalidate fastpath
5400 break;
5401 default:
5402 break; // use fastpath
5403 }
5404 }
5405 }
5406
5407 VM_ASSERT((vm_cc_cme(cc), true));
5408
5409 return cc;
5410}
5411
5412/* yield */
5413
5414static inline int
5415block_proc_is_lambda(const VALUE procval)
5416{
5417 rb_proc_t *proc;
5418
5419 if (procval) {
5420 GetProcPtr(procval, proc);
5421 return proc->header.is_lambda;
5422 }
5423 else {
5424 return 0;
5425 }
5426}
5427
5428static VALUE
5429vm_yield_with_cfunc(rb_execution_context_t *ec,
5430 const struct rb_captured_block *captured,
5431 VALUE self, int argc, const VALUE *argv, int kw_splat, VALUE block_handler,
5432 const rb_callable_method_entry_t *me, int stack_argc)
5433{
5434 int is_lambda = FALSE; /* TODO */
5435 VALUE val, arg, blockarg;
5436 int frame_flag;
5437 const struct vm_ifunc *ifunc = captured->code.ifunc;
5438
5439 if (is_lambda) {
5440 arg = rb_ary_new4(argc, argv);
5441 }
5442 else if (argc == 0) {
5443 arg = Qnil;
5444 }
5445 else {
5446 arg = argv[0];
5447 }
5448
5449 blockarg = rb_vm_bh_to_procval(ec, block_handler);
5450
5451 frame_flag = VM_FRAME_MAGIC_IFUNC | VM_FRAME_FLAG_CFRAME | (me ? VM_FRAME_FLAG_BMETHOD : 0);
5452 if (kw_splat) {
5453 frame_flag |= VM_FRAME_FLAG_CFRAME_KW;
5454 }
5455
5456 rb_control_frame_t *cfp = ec->cfp;
5457 vm_push_frame(ec, (const rb_iseq_t *)captured->code.ifunc,
5458 frame_flag,
5459 self,
5460 VM_GUARDED_PREV_EP(captured->ep),
5461 (VALUE)me,
5462 0, cfp->sp, 0, 0);
5463 /* The new frame keeps the arguments live. Pop them from the caller
5464 * before the callback can exit via break. */
5465 cfp->sp -= stack_argc;
5466 val = (*ifunc->func)(arg, (VALUE)ifunc->data, argc, argv, blockarg);
5467 rb_vm_pop_frame(ec);
5468
5469 return val;
5470}
5471
5472VALUE
5473rb_vm_yield_with_cfunc(rb_execution_context_t *ec, const struct rb_captured_block *captured, int argc, const VALUE *argv)
5474{
5475 return vm_yield_with_cfunc(ec, captured, captured->self, argc, argv, 0, VM_BLOCK_HANDLER_NONE, NULL, 0);
5476}
5477
5478static VALUE
5479vm_yield_with_symbol(rb_execution_context_t *ec, VALUE symbol, int argc, const VALUE *argv, int kw_splat, VALUE block_handler)
5480{
5481 VALUE passed_proc = rb_vm_bh_to_procval(ec, block_handler);
5482
5483 if (!rb_box_available()) {
5484 return rb_sym_proc_call(SYM2ID(symbol), argc, argv, kw_splat, passed_proc);
5485 }
5486
5487 const rb_control_frame_t *reg_cfp = ec->cfp;
5488 const rb_control_frame_t *ruby_cfp = rb_vm_get_ruby_level_next_cfp(ec, reg_cfp);
5489 const rb_box_t *box = NULL;
5490
5491 /*
5492 * Traverse the frames until a user box (Main or Optional Box) is found.
5493 * Frames for built-in methods like <internal:xxx> run in the Root or Master Box,
5494 */
5495 while (ruby_cfp) {
5496 box = current_box_on_cfp(ec, ruby_cfp);
5497 if (BOX_USER_P(box)) {
5498 break;
5499 }
5500 ruby_cfp = rb_vm_get_ruby_level_next_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(ruby_cfp));
5501 }
5502
5503 // Fallback to the normal call if no user box is found
5504 if (!ruby_cfp || !box) {
5505 return rb_sym_proc_call(SYM2ID(symbol), argc, argv, kw_splat, passed_proc);
5506 }
5507
5508 /*
5509 * Push a dummy block frame whose outer env is `ruby_cfp` so that the
5510 * method resolves in the caller's box (via the ep chain) and the
5511 * backtrace reads like a usual block invocation at the caller's site.
5512 */
5513 const rb_iseq_t *caller_iseq = CFP_ISEQ(ruby_cfp);
5514 VALUE name = rb_sprintf("block in %"PRIsVALUE, rb_iseq_label(caller_iseq));
5515 const rb_iseq_t *iseq = rb_iseq_new_with_opt(Qnil, name,
5516 rb_iseq_path(caller_iseq), rb_iseq_realpath(caller_iseq),
5517 rb_vm_get_sourceline(ruby_cfp), caller_iseq, 0,
5518 ISEQ_TYPE_BLOCK, NULL, Qnil);
5519 volatile VALUE val = Qnil;
5520 enum ruby_tag_type state;
5521
5522 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_BLOCK | VM_FRAME_FLAG_FINISH,
5523 ruby_cfp->self, VM_GUARDED_PREV_EP(ruby_cfp->ep),
5524 Qfalse, /* cref or me */
5525 ISEQ_BODY(iseq)->iseq_encoded, reg_cfp->sp,
5526 ISEQ_BODY(iseq)->local_table_size, ISEQ_BODY(iseq)->stack_max);
5527
5528 /*
5529 * The pushed frame is finished (owned by no vm_exec loop), so catch the
5530 * non-local exit here, pop the frame, and let the caller's handlers see
5531 * the exception.
5532 */
5533 EC_PUSH_TAG(ec);
5534 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
5535 val = rb_sym_proc_call(SYM2ID(symbol), argc, argv, kw_splat, passed_proc);
5536 }
5537 EC_POP_TAG();
5538
5539 if (state != TAG_NONE) {
5540 rb_vm_rewind_cfp(ec, (rb_control_frame_t *)reg_cfp);
5541 EC_JUMP_TAG(ec, state);
5542 }
5543
5544 rb_vm_pop_frame(ec);
5545
5546 return val;
5547}
5548
5549static inline int
5550vm_callee_setup_block_arg_arg0_splat(rb_control_frame_t *cfp, const rb_iseq_t *iseq, VALUE *argv, VALUE ary)
5551{
5552 int i;
5553 rb_len_t len = RARRAY_LEN(ary);
5554
5555 CHECK_VM_STACK_OVERFLOW(cfp, ISEQ_BODY(iseq)->param.lead_num);
5556
5557 for (i=0; i<len && i<ISEQ_BODY(iseq)->param.lead_num; i++) {
5558 argv[i] = RARRAY_AREF(ary, i);
5559 }
5560
5561 return i;
5562}
5563
5564static inline VALUE
5565vm_callee_setup_block_arg_arg0_check(VALUE *argv)
5566{
5567 VALUE ary, arg0 = argv[0];
5568 ary = rb_check_array_type(arg0);
5569#if 0
5570 argv[0] = arg0;
5571#else
5572 VM_ASSERT(argv[0] == arg0);
5573#endif
5574 return ary;
5575}
5576
5577static int
5578vm_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)
5579{
5580 if (rb_simple_iseq_p(iseq)) {
5581 rb_control_frame_t *cfp = ec->cfp;
5582 VALUE arg0;
5583
5584 CALLER_SETUP_ARG(cfp, calling, ci, ISEQ_BODY(iseq)->param.lead_num);
5585
5586 if (arg_setup_type == arg_setup_block &&
5587 calling->argc == 1 &&
5588 ISEQ_BODY(iseq)->param.flags.has_lead &&
5589 !ISEQ_BODY(iseq)->param.flags.ambiguous_param0 &&
5590 !NIL_P(arg0 = vm_callee_setup_block_arg_arg0_check(argv))) {
5591 calling->argc = vm_callee_setup_block_arg_arg0_splat(cfp, iseq, argv, arg0);
5592 }
5593
5594 if (calling->argc != ISEQ_BODY(iseq)->param.lead_num) {
5595 if (arg_setup_type == arg_setup_block) {
5596 if (calling->argc < ISEQ_BODY(iseq)->param.lead_num) {
5597 int i;
5598 CHECK_VM_STACK_OVERFLOW(cfp, ISEQ_BODY(iseq)->param.lead_num);
5599 for (i=calling->argc; i<ISEQ_BODY(iseq)->param.lead_num; i++) argv[i] = Qnil;
5600 calling->argc = ISEQ_BODY(iseq)->param.lead_num; /* fill rest parameters */
5601 }
5602 else if (calling->argc > ISEQ_BODY(iseq)->param.lead_num) {
5603 calling->argc = ISEQ_BODY(iseq)->param.lead_num; /* simply truncate arguments */
5604 }
5605 }
5606 else {
5607 argument_arity_error(ec, iseq, NULL, calling->argc, ISEQ_BODY(iseq)->param.lead_num, ISEQ_BODY(iseq)->param.lead_num);
5608 }
5609 }
5610
5611 return 0;
5612 }
5613 else {
5614 return setup_parameters_complex(ec, iseq, calling, ci, argv, arg_setup_type);
5615 }
5616}
5617
5618static int
5619vm_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)
5620{
5621 struct rb_calling_info calling_entry, *calling;
5622
5623 calling = &calling_entry;
5624 calling->argc = argc;
5625 calling->block_handler = block_handler;
5626 calling->kw_splat = (flags & VM_CALL_KW_SPLAT) ? 1 : 0;
5627 calling->recv = Qundef;
5628 calling->heap_argv = 0;
5629 calling->cc = NULL;
5630 struct rb_callinfo dummy_ci = VM_CI_ON_STACK(0, flags, 0, 0);
5631
5632 return vm_callee_setup_block_arg(ec, calling, &dummy_ci, iseq, argv, arg_setup_type);
5633}
5634
5635/* ruby iseq -> ruby block */
5636
5637static VALUE
5638vm_invoke_iseq_block_with_cref(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5639 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5640 bool is_lambda, VALUE block_handler, const rb_cref_t *cref)
5641{
5642 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
5643 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
5644 const int arg_size = ISEQ_BODY(iseq)->param.size;
5645 VALUE * const rsp = GET_SP() - calling->argc;
5646 VALUE * const argv = rsp;
5647 int opt_pc = vm_callee_setup_block_arg(ec, calling, ci, iseq, argv, is_lambda ? arg_setup_method : arg_setup_block);
5648 int frame_flag = VM_FRAME_MAGIC_BLOCK | (is_lambda ? VM_FRAME_FLAG_LAMBDA : 0);
5649
5650 SET_SP(rsp);
5651
5652 /* cref is normally 0; Proc#refined supplies a refinement cref */
5653 vm_push_frame(ec, iseq,
5654 frame_flag,
5655 captured->self,
5656 VM_GUARDED_PREV_EP(captured->ep), (VALUE)cref,
5657 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
5658 rsp + arg_size,
5659 ISEQ_BODY(iseq)->local_table_size - arg_size, ISEQ_BODY(iseq)->stack_max);
5660
5661 return Qundef;
5662}
5663
5664static VALUE
5665vm_invoke_iseq_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5666 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5667 bool is_lambda, VALUE block_handler)
5668{
5669 return vm_invoke_iseq_block_with_cref(ec, reg_cfp, calling, ci, is_lambda, block_handler, NULL);
5670}
5671
5672static VALUE
5673vm_invoke_symbol_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5674 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5675 MAYBE_UNUSED(bool is_lambda), VALUE block_handler)
5676{
5677 VALUE symbol = VM_BH_TO_SYMBOL(block_handler);
5678 int flags = vm_ci_flag(ci);
5679
5680 if (UNLIKELY(!(flags & VM_CALL_ARGS_SIMPLE) &&
5681 ((calling->argc == 0) ||
5682 (calling->argc == 1 && (flags & (VM_CALL_ARGS_SPLAT | VM_CALL_KW_SPLAT))) ||
5683 (calling->argc == 2 && (flags & VM_CALL_ARGS_SPLAT) && (flags & VM_CALL_KW_SPLAT)) ||
5684 ((flags & VM_CALL_KWARG) && (vm_ci_kwarg(ci)->keyword_len == calling->argc))))) {
5685 CALLER_SETUP_ARG(reg_cfp, calling, ci, ALLOW_HEAP_ARGV);
5686 flags = 0;
5687 if (UNLIKELY(calling->heap_argv)) {
5688#if VM_ARGC_STACK_MAX < 0
5689 if (RARRAY_LEN(calling->heap_argv) < 1) {
5690 rb_raise(rb_eArgError, "no receiver given");
5691 }
5692#endif
5693 calling->recv = rb_ary_shift(calling->heap_argv);
5694 // Modify stack to avoid cfp consistency error
5695 reg_cfp->sp++;
5696 reg_cfp->sp[-1] = reg_cfp->sp[-2];
5697 reg_cfp->sp[-2] = calling->recv;
5698 flags |= VM_CALL_ARGS_SPLAT;
5699 }
5700 else {
5701 if (calling->argc < 1) {
5702 rb_raise(rb_eArgError, "no receiver given");
5703 }
5704 calling->recv = TOPN(--calling->argc);
5705 }
5706 if (calling->kw_splat) {
5707 flags |= VM_CALL_KW_SPLAT;
5708 }
5709 }
5710 else {
5711 if (calling->argc < 1) {
5712 rb_raise(rb_eArgError, "no receiver given");
5713 }
5714 calling->recv = TOPN(--calling->argc);
5715 }
5716
5717 return vm_call_symbol(ec, reg_cfp, calling, ci, symbol, flags);
5718}
5719
5720static VALUE
5721vm_invoke_ifunc_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5722 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5723 MAYBE_UNUSED(bool is_lambda), VALUE block_handler)
5724{
5725 const struct rb_captured_block *captured = VM_BH_TO_IFUNC_BLOCK(block_handler);
5726 CALLER_SETUP_ARG(reg_cfp, calling, ci, ALLOW_HEAP_ARGV_KEEP_KWSPLAT);
5727 const VALUE *argv = calling->heap_argv ? RARRAY_CONST_PTR(calling->heap_argv) : STACK_ADDR_FROM_TOP(calling->argc);
5728 return vm_yield_with_cfunc(ec, captured, captured->self, CALLING_ARGC(calling), argv,
5729 calling->kw_splat, calling->block_handler, NULL, calling->argc);
5730}
5731
5732static inline VALUE
5733vm_block_to_block_handler(const struct rb_block *block)
5734{
5735 switch (vm_block_type(block)) {
5736 case block_type_iseq:
5737 return VM_BH_FROM_ISEQ_BLOCK(&block->as.captured);
5738 case block_type_ifunc:
5739 return VM_BH_FROM_IFUNC_BLOCK(&block->as.captured);
5740 case block_type_symbol:
5741 return VM_BH_FROM_SYMBOL(block->as.symbol);
5742 case block_type_proc:
5743 return VM_BH_FROM_PROC(block->as.proc);
5744 }
5745 VM_UNREACHABLE(vm_yield_with_proc);
5746 return Qundef;
5747}
5748
5749static VALUE
5750vm_proc_to_block_handler(VALUE procval)
5751{
5752 return vm_block_to_block_handler(vm_proc_block(procval));
5753}
5754
5755NOINLINE(static VALUE
5756vm_invoke_proc_block_with_cref(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5757 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5758 bool is_lambda, VALUE block_handler, VALUE refined_procval));
5759static VALUE
5760vm_invoke_proc_block_with_cref(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5761 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5762 bool is_lambda, VALUE block_handler, VALUE refined_procval)
5763{
5764 const rb_cref_t *cref = rb_proc_refinements_cref_for_call(refined_procval);
5765 return vm_invoke_iseq_block_with_cref(ec, reg_cfp, calling, ci, is_lambda, block_handler, cref);
5766}
5767
5768static VALUE
5769vm_invoke_proc_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5770 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5771 bool is_lambda, VALUE block_handler)
5772{
5773 VALUE refined_procval = 0;
5774
5775 while (vm_block_handler_type(block_handler) == block_handler_type_proc) {
5776 VALUE procval = VM_BH_TO_PROC(block_handler);
5777 rb_proc_t *po;
5778 GetProcPtr(procval, po);
5779 if (po->header.is_refined) refined_procval = procval;
5780 is_lambda = po->header.is_lambda;
5781 block_handler = vm_block_to_block_handler(&po->block);
5782 }
5783
5784 if (UNLIKELY(refined_procval) && vm_block_handler_type(block_handler) == block_handler_type_iseq) {
5785 /* should not be inlined so that vm_invoke_proc_block stays leaf/frameless. */
5786 return vm_invoke_proc_block_with_cref(ec, reg_cfp, calling, ci, is_lambda, block_handler,
5787 refined_procval);
5788 }
5789
5790 return vm_invoke_block(ec, reg_cfp, calling, ci, is_lambda, block_handler);
5791}
5792
5793static inline VALUE
5794vm_invoke_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5795 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5796 bool is_lambda, VALUE block_handler)
5797{
5798 VALUE (*func)(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
5799 struct rb_calling_info *calling, const struct rb_callinfo *ci,
5800 bool is_lambda, VALUE block_handler);
5801
5802 switch (vm_block_handler_type(block_handler)) {
5803 case block_handler_type_iseq: func = vm_invoke_iseq_block; break;
5804 case block_handler_type_ifunc: func = vm_invoke_ifunc_block; break;
5805 case block_handler_type_proc: func = vm_invoke_proc_block; break;
5806 case block_handler_type_symbol: func = vm_invoke_symbol_block; break;
5807 default: rb_bug("vm_invoke_block: unreachable");
5808 }
5809
5810 return func(ec, reg_cfp, calling, ci, is_lambda, block_handler);
5811}
5812
5813static VALUE
5814vm_make_proc_with_iseq(const rb_iseq_t *blockiseq)
5815{
5816 const rb_execution_context_t *ec = GET_EC();
5817 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
5818 struct rb_captured_block *captured;
5819
5820 if (cfp == 0) {
5821 rb_bug("vm_make_proc_with_iseq: unreachable");
5822 }
5823
5824 captured = VM_CFP_TO_CAPTURED_BLOCK(cfp);
5825 captured->code.iseq = blockiseq;
5826
5827 return rb_vm_make_proc(ec, captured, rb_cProc);
5828}
5829
5830static VALUE
5831vm_once_exec(VALUE iseq)
5832{
5833 VALUE proc = vm_make_proc_with_iseq((rb_iseq_t *)iseq);
5834 return rb_proc_call_with_block(proc, 0, 0, Qnil);
5835}
5836
5837#define RUNNING_THREAD_ONCE_DONE ((rb_thread_t *)0x1)
5838
5840 rb_vm_t *vm;
5841 struct rb_imemo_once_storage *storage;
5842};
5843
5844static void
5845vm_once_broadcast(rb_vm_t *vm)
5846{
5847 rb_native_mutex_lock(&vm->once_lock);
5848 rb_native_cond_broadcast(&vm->once_cond);
5849 rb_native_mutex_unlock(&vm->once_lock);
5850}
5851
5852static void *
5853vm_once_wait_no_gvl(void *ptr)
5854{
5855 struct vm_once_wait_arg *arg = (struct vm_once_wait_arg *)ptr;
5856 rb_vm_t *vm = arg->vm;
5857 rb_thread_t *running_th;
5858
5859 rb_native_mutex_lock(&vm->once_lock);
5860 running_th = rbimpl_atomic_ptr_load((void **)&arg->storage->running_thread, RBIMPL_ATOMIC_ACQUIRE);
5861 if (running_th != NULL && running_th != RUNNING_THREAD_ONCE_DONE) {
5862 rb_native_cond_wait(&vm->once_cond, &vm->once_lock);
5863 }
5864 rb_native_mutex_unlock(&vm->once_lock);
5865 return NULL;
5866}
5867
5868static void
5869vm_once_wait_ubf(void *ptr)
5870{
5871 vm_once_broadcast((rb_vm_t *)ptr);
5872}
5873
5874static VALUE
5875vm_once_clear(VALUE data)
5876{
5877 struct rb_imemo_once_storage *storage = (struct rb_imemo_once_storage *)data;
5878 rbimpl_atomic_ptr_store((volatile void **)&storage->running_thread, NULL, RBIMPL_ATOMIC_RELEASE);
5879 vm_once_broadcast(GET_VM());
5880 return Qnil;
5881}
5882
5883/* defined insn */
5884
5885static bool
5886check_respond_to_missing(VALUE obj, VALUE v)
5887{
5888 VALUE args[2];
5889 VALUE r;
5890
5891 args[0] = obj; args[1] = Qfalse;
5892 r = rb_check_funcall(v, idRespond_to_missing, 2, args);
5893 if (!UNDEF_P(r) && RTEST(r)) {
5894 return true;
5895 }
5896 else {
5897 return false;
5898 }
5899}
5900
5901static bool
5902vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE obj, VALUE v)
5903{
5904 VALUE klass;
5905 enum defined_type type = (enum defined_type)op_type;
5906
5907 switch (type) {
5908 case DEFINED_IVAR:
5909 return rb_ivar_defined(GET_SELF(), SYM2ID(obj));
5910 break;
5911 case DEFINED_GVAR:
5912 return rb_gvar_defined(SYM2ID(obj));
5913 break;
5914 case DEFINED_CVAR: {
5915 const rb_cref_t *cref = vm_get_cref(GET_EP());
5916 klass = vm_get_cvar_base(cref, GET_CFP(), 0);
5917 return rb_cvar_defined(klass, SYM2ID(obj));
5918 break;
5919 }
5920 case DEFINED_CONST:
5921 case DEFINED_CONST_FROM: {
5922 bool allow_nil = type == DEFINED_CONST;
5923 klass = v;
5924 return vm_get_ev_const(ec, klass, SYM2ID(obj), allow_nil, true);
5925 break;
5926 }
5927 case DEFINED_FUNC:
5928 klass = CLASS_OF(v);
5929 return rb_ec_obj_respond_to(ec, v, SYM2ID(obj), TRUE);
5930 break;
5931 case DEFINED_METHOD:{
5932 VALUE klass = CLASS_OF(v);
5933 const rb_callable_method_entry_t *cme = rb_callable_method_entry_with_refinements(klass, SYM2ID(obj), NULL);
5934
5935 if (cme) {
5936 switch (METHOD_ENTRY_VISI(cme)) {
5937 case METHOD_VISI_PRIVATE:
5938 break;
5939 case METHOD_VISI_PROTECTED:
5940 if (!rb_obj_is_kind_of(GET_SELF(), vm_defined_class_for_protected_call(cme))) {
5941 break;
5942 }
5943 case METHOD_VISI_PUBLIC:
5944 return true;
5945 break;
5946 default:
5947 rb_bug("vm_defined: unreachable: %u", (unsigned int)METHOD_ENTRY_VISI(cme));
5948 }
5949 }
5950 else {
5951 return check_respond_to_missing(obj, v);
5952 }
5953 break;
5954 }
5955 case DEFINED_YIELD:
5956 if (GET_BLOCK_HANDLER() != VM_BLOCK_HANDLER_NONE) {
5957 return true;
5958 }
5959 break;
5960 case DEFINED_ZSUPER:
5961 {
5962 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(GET_CFP());
5963
5964 if (me) {
5965 VALUE klass = vm_search_normal_superclass(me->defined_class);
5966 if (!klass) return false;
5967
5968 ID id = me->def->original_id;
5969
5970 return rb_method_boundp(klass, id, 0);
5971 }
5972 }
5973 break;
5974 case DEFINED_REF:
5975 return RTEST(vm_backref_defined(ec, GET_LEP(), FIX2INT(obj)));
5976 default:
5977 rb_bug("unimplemented defined? type (VM)");
5978 break;
5979 }
5980
5981 return false;
5982}
5983
5984bool
5985rb_vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE obj, VALUE v)
5986{
5987 return vm_defined(ec, reg_cfp, op_type, obj, v);
5988}
5989
5990static const VALUE *
5991vm_get_ep(const VALUE *const reg_ep, rb_num_t lv)
5992{
5993 rb_num_t i;
5994 const VALUE *ep = reg_ep;
5995 for (i = 0; i < lv; i++) {
5996 ep = GET_PREV_EP(ep);
5997 }
5998 return ep;
5999}
6000
6001static VALUE
6002vm_get_special_object(const VALUE *const reg_ep,
6003 enum vm_special_object_type type)
6004{
6005 switch (type) {
6006 case VM_SPECIAL_OBJECT_VMCORE:
6007 return rb_mRubyVMFrozenCore;
6008 case VM_SPECIAL_OBJECT_CBASE:
6009 return vm_get_cbase(reg_ep);
6010 case VM_SPECIAL_OBJECT_CONST_BASE:
6011 return vm_get_const_base(reg_ep);
6012 default:
6013 rb_bug("putspecialobject insn: unknown value_type %d", type);
6014 }
6015}
6016
6017// ZJIT implementation is using the C function
6018// and needs to call a non-static function
6019VALUE
6020rb_vm_get_special_object(const VALUE *reg_ep, enum vm_special_object_type type)
6021{
6022 return vm_get_special_object(reg_ep, type);
6023}
6024
6025static VALUE
6026vm_concat_array(VALUE ary1, VALUE ary2st)
6027{
6028 const VALUE ary2 = ary2st;
6029 VALUE tmp1 = rb_check_to_array(ary1);
6030 VALUE tmp2 = rb_check_to_array(ary2);
6031
6032 if (NIL_P(tmp1)) {
6033 tmp1 = rb_ary_new3(1, ary1);
6034 }
6035 if (tmp1 == ary1) {
6036 tmp1 = rb_ary_dup(ary1);
6037 }
6038
6039 if (NIL_P(tmp2)) {
6040 return rb_ary_push(tmp1, ary2);
6041 }
6042 else {
6043 return rb_ary_concat(tmp1, tmp2);
6044 }
6045}
6046
6047static VALUE
6048vm_concat_to_array(VALUE ary1, VALUE ary2st)
6049{
6050 /* ary1 must be a newly created array */
6051 const VALUE ary2 = ary2st;
6052
6053 if (NIL_P(ary2)) return ary1;
6054
6055 VALUE tmp2 = rb_check_to_array(ary2);
6056
6057 if (NIL_P(tmp2)) {
6058 return rb_ary_push(ary1, ary2);
6059 }
6060 else {
6061 return rb_ary_concat(ary1, tmp2);
6062 }
6063}
6064
6065// YJIT implementation is using the C function
6066// and needs to call a non-static function
6067VALUE
6068rb_vm_concat_array(VALUE ary1, VALUE ary2st)
6069{
6070 return vm_concat_array(ary1, ary2st);
6071}
6072
6073VALUE
6074rb_vm_concat_to_array(VALUE ary1, VALUE ary2st)
6075{
6076 return vm_concat_to_array(ary1, ary2st);
6077}
6078
6079static VALUE
6080vm_splat_array(VALUE flag, VALUE ary)
6081{
6082 if (NIL_P(ary)) {
6083 return RTEST(flag) ? rb_ary_new() : rb_cArray_empty_frozen;
6084 }
6085 VALUE tmp = rb_check_to_array(ary);
6086 if (NIL_P(tmp)) {
6087 return rb_ary_new3(1, ary);
6088 }
6089 else if (RTEST(flag)) {
6090 return rb_ary_dup(tmp);
6091 }
6092 else {
6093 return tmp;
6094 }
6095}
6096
6097// YJIT implementation is using the C function
6098// and needs to call a non-static function
6099VALUE
6100rb_vm_splat_array(VALUE flag, VALUE ary)
6101{
6102 return vm_splat_array(flag, ary);
6103}
6104
6105static VALUE
6106vm_check_match(rb_execution_context_t *ec, VALUE target, VALUE pattern, rb_num_t flag)
6107{
6108 enum vm_check_match_type type = ((int)flag) & VM_CHECKMATCH_TYPE_MASK;
6109
6110 if (flag & VM_CHECKMATCH_ARRAY) {
6111 rb_len_t i;
6112 const rb_len_t n = RARRAY_LEN(pattern);
6113
6114 for (i = 0; i < n; i++) {
6115 VALUE v = RARRAY_AREF(pattern, i);
6116 VALUE c = check_match(ec, v, target, type);
6117
6118 if (RTEST(c)) {
6119 return c;
6120 }
6121 }
6122 return Qfalse;
6123 }
6124 else {
6125 return check_match(ec, pattern, target, type);
6126 }
6127}
6128
6129VALUE
6130rb_vm_check_match(rb_execution_context_t *ec, VALUE target, VALUE pattern, rb_num_t flag)
6131{
6132 return vm_check_match(ec, target, pattern, flag);
6133}
6134
6135static VALUE
6136vm_check_keyword(lindex_t bits, lindex_t idx, const VALUE *ep)
6137{
6138 const VALUE kw_bits = *(ep - bits);
6139
6140 if (FIXNUM_P(kw_bits)) {
6141 unsigned int b = (unsigned int)FIX2ULONG(kw_bits);
6142 if ((idx < VM_KW_SPECIFIED_BITS_MAX) && (b & (0x01 << idx)))
6143 return Qfalse;
6144 }
6145 else {
6146 VM_ASSERT(rb_set_p(kw_bits), "%s", rb_obj_info(kw_bits));
6147 if (rb_set_lookup(kw_bits, INT2FIX(idx))) return Qfalse;
6148 }
6149 return Qtrue;
6150}
6151
6152static void
6153vm_dtrace(rb_event_flag_t flag, rb_execution_context_t *ec)
6154{
6155 if (RUBY_DTRACE_METHOD_ENTRY_ENABLED() ||
6156 RUBY_DTRACE_METHOD_RETURN_ENABLED() ||
6157 RUBY_DTRACE_CMETHOD_ENTRY_ENABLED() ||
6158 RUBY_DTRACE_CMETHOD_RETURN_ENABLED()) {
6159
6160 switch (flag) {
6161 case RUBY_EVENT_CALL:
6162 RUBY_DTRACE_METHOD_ENTRY_HOOK(ec, 0, 0);
6163 return;
6164 case RUBY_EVENT_C_CALL:
6165 RUBY_DTRACE_CMETHOD_ENTRY_HOOK(ec, 0, 0);
6166 return;
6167 case RUBY_EVENT_RETURN:
6168 RUBY_DTRACE_METHOD_RETURN_HOOK(ec, 0, 0);
6169 return;
6171 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, 0, 0);
6172 return;
6173 }
6174 }
6175}
6176
6177static VALUE
6178vm_const_get_under(ID id, rb_num_t flags, VALUE cbase)
6179{
6180 if (!rb_const_defined_at(cbase, id)) {
6181 return 0;
6182 }
6183 else if (VM_DEFINECLASS_SCOPED_P(flags)) {
6184 return rb_public_const_get_at(cbase, id);
6185 }
6186 else {
6187 return rb_const_get_at(cbase, id);
6188 }
6189}
6190
6191static VALUE
6192vm_check_if_class(ID id, rb_num_t flags, VALUE super, VALUE klass)
6193{
6194 if (!RB_TYPE_P(klass, T_CLASS)) {
6195 return 0;
6196 }
6197 else if (VM_DEFINECLASS_HAS_SUPERCLASS_P(flags)) {
6198 VALUE tmp = rb_class_real(RCLASS_SUPER(klass));
6199
6200 if (tmp != super) {
6201 rb_raise(rb_eTypeError,
6202 "superclass mismatch for class %"PRIsVALUE"",
6203 rb_id2str(id));
6204 }
6205 else {
6206 return klass;
6207 }
6208 }
6209 else {
6210 return klass;
6211 }
6212}
6213
6214static VALUE
6215vm_check_if_module(ID id, VALUE mod)
6216{
6217 if (!RB_TYPE_P(mod, T_MODULE)) {
6218 return 0;
6219 }
6220 else {
6221 return mod;
6222 }
6223}
6224
6225static VALUE
6226declare_under(ID id, VALUE cbase, VALUE c)
6227{
6228 rb_set_class_path_string(c, cbase, rb_id2str(id));
6229 rb_const_set(cbase, id, c);
6230 return c;
6231}
6232
6233static VALUE
6234vm_declare_class(ID id, rb_num_t flags, VALUE cbase, VALUE super)
6235{
6236 /* new class declaration */
6237 VALUE s = VM_DEFINECLASS_HAS_SUPERCLASS_P(flags) ? super : rb_cObject;
6238 VALUE c = declare_under(id, cbase, rb_define_class_id(id, s));
6239 rb_class_inherited(s, c);
6240 return c;
6241}
6242
6243static VALUE
6244vm_declare_module(ID id, VALUE cbase)
6245{
6246 /* new module declaration */
6247 return declare_under(id, cbase, rb_module_new());
6248}
6249
6250NORETURN(static void unmatched_redefinition(const char *type, VALUE cbase, ID id, VALUE old));
6251static void
6252unmatched_redefinition(const char *type, VALUE cbase, ID id, VALUE old)
6253{
6254 VALUE name = rb_id2str(id);
6255 VALUE message = rb_sprintf("%"PRIsVALUE" is not a %s",
6256 name, type);
6257 VALUE location = rb_const_source_location_at(cbase, id);
6258 if (!NIL_P(location)) {
6259 rb_str_catf(message, "\n%"PRIsVALUE":%"PRIsVALUE":"
6260 " previous definition of %"PRIsVALUE" was here",
6261 rb_ary_entry(location, 0), rb_ary_entry(location, 1), name);
6262 }
6264}
6265
6266static VALUE
6267vm_define_class(ID id, rb_num_t flags, VALUE cbase, VALUE super)
6268{
6269 VALUE klass;
6270
6271 if (VM_DEFINECLASS_HAS_SUPERCLASS_P(flags) && !RB_TYPE_P(super, T_CLASS)) {
6272 rb_raise(rb_eTypeError,
6273 "superclass must be an instance of Class (given an instance of %"PRIsVALUE")",
6274 rb_obj_class(super));
6275 }
6276
6277 vm_check_if_namespace(cbase);
6278
6279 /* find klass */
6280 rb_autoload_load(cbase, id);
6281
6282 if ((klass = vm_const_get_under(id, flags, cbase)) != 0) {
6283 if (!vm_check_if_class(id, flags, super, klass))
6284 unmatched_redefinition("class", cbase, id, klass);
6285 return klass;
6286 }
6287 else {
6288 return vm_declare_class(id, flags, cbase, super);
6289 }
6290}
6291
6292static VALUE
6293vm_define_module(ID id, rb_num_t flags, VALUE cbase)
6294{
6295 VALUE mod;
6296
6297 vm_check_if_namespace(cbase);
6298 if ((mod = vm_const_get_under(id, flags, cbase)) != 0) {
6299 if (!vm_check_if_module(id, mod))
6300 unmatched_redefinition("module", cbase, id, mod);
6301 return mod;
6302 }
6303 else {
6304 return vm_declare_module(id, cbase);
6305 }
6306}
6307
6308static VALUE
6309vm_find_or_create_class_by_id(ID id,
6310 rb_num_t flags,
6311 VALUE cbase,
6312 VALUE super)
6313{
6314 rb_vm_defineclass_type_t type = VM_DEFINECLASS_TYPE(flags);
6315
6316 switch (type) {
6317 case VM_DEFINECLASS_TYPE_CLASS:
6318 /* classdef returns class scope value */
6319 return vm_define_class(id, flags, cbase, super);
6320
6321 case VM_DEFINECLASS_TYPE_SINGLETON_CLASS:
6322 /* classdef returns class scope value */
6323 return rb_singleton_class(cbase);
6324
6325 case VM_DEFINECLASS_TYPE_MODULE:
6326 /* classdef returns class scope value */
6327 return vm_define_module(id, flags, cbase);
6328
6329 default:
6330 rb_bug("unknown defineclass type: %d", (int)type);
6331 }
6332}
6333
6334static rb_method_visibility_t
6335vm_scope_visibility_get(const rb_execution_context_t *ec)
6336{
6337 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
6338
6339 if (!vm_env_cref_by_cref(cfp->ep)) {
6340 return METHOD_VISI_PUBLIC;
6341 }
6342 else {
6343 return CREF_SCOPE_VISI(vm_ec_cref(ec))->method_visi;
6344 }
6345}
6346
6347static int
6348vm_scope_module_func_check(const rb_execution_context_t *ec)
6349{
6350 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
6351
6352 if (!vm_env_cref_by_cref(cfp->ep)) {
6353 return FALSE;
6354 }
6355 else {
6356 return CREF_SCOPE_VISI(vm_ec_cref(ec))->module_func;
6357 }
6358}
6359
6360static void
6361vm_define_method(const rb_execution_context_t *ec, VALUE obj, ID id, VALUE iseqval, int is_singleton)
6362{
6363 VALUE klass;
6364 rb_method_visibility_t visi;
6365 rb_cref_t *cref = vm_ec_cref(ec);
6366
6367 if (is_singleton) {
6368 klass = rb_singleton_class(obj); /* class and frozen checked in this API */
6369 visi = METHOD_VISI_PUBLIC;
6370 }
6371 else {
6372 klass = CREF_CLASS_FOR_DEFINITION(cref);
6373 visi = vm_scope_visibility_get(ec);
6374 }
6375
6376 if (NIL_P(klass)) {
6377 rb_raise(rb_eTypeError, "no class/module to add method");
6378 }
6379
6380 rb_add_method_iseq(klass, id, (const rb_iseq_t *)iseqval, cref, visi);
6381 // Set max_iv_count on klasses based on number of ivar sets that are in the initialize method
6382 if (id == idInitialize && RB_TYPE_P(klass, T_CLASS) &&
6383 !RCLASS_SINGLETON_P(klass) && !RCLASS_EXPECT_NO_IVAR(klass) &&
6384 (rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
6385 RCLASS_SET_MAX_IV_COUNT(klass, rb_estimate_iv_count(klass, (const rb_iseq_t *)iseqval));
6386 }
6387
6388 if (!is_singleton && vm_scope_module_func_check(ec)) {
6389 klass = rb_singleton_class(klass);
6390 rb_add_method_iseq(klass, id, (const rb_iseq_t *)iseqval, cref, METHOD_VISI_PUBLIC);
6391 }
6392}
6393
6394// Return the untagged block handler:
6395// * If it's VM_BLOCK_HANDLER_NONE, return nil
6396// * If it's an ISEQ or an IFUNC, fetch it from its rb_captured_block
6397// * If it's a PROC or SYMBOL, return it as is
6398VALUE
6399rb_vm_untag_block_handler(VALUE block_handler)
6400{
6401 if (VM_BLOCK_HANDLER_NONE == block_handler) return Qnil;
6402
6403 switch (vm_block_handler_type(block_handler)) {
6404 case block_handler_type_iseq:
6405 case block_handler_type_ifunc: {
6406 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
6407 return captured->code.val;
6408 }
6409 case block_handler_type_proc:
6410 case block_handler_type_symbol:
6411 return block_handler;
6412 default:
6413 rb_bug("rb_vm_untag_block_handler: unreachable");
6414 }
6415}
6416
6417VALUE
6418rb_vm_get_untagged_block_handler(rb_control_frame_t *reg_cfp)
6419{
6420 return rb_vm_untag_block_handler(VM_CF_BLOCK_HANDLER(reg_cfp));
6421}
6422
6423static VALUE
6424vm_invokeblock_i(struct rb_execution_context_struct *ec,
6425 struct rb_control_frame_struct *reg_cfp,
6426 struct rb_calling_info *calling)
6427{
6428 const struct rb_callinfo *ci = calling->cd->ci;
6429 VALUE block_handler = VM_CF_BLOCK_HANDLER(GET_CFP());
6430
6431 if (block_handler == VM_BLOCK_HANDLER_NONE) {
6432 rb_vm_localjump_error("no block given (yield)", Qnil, 0);
6433 }
6434 else {
6435 return vm_invoke_block(ec, GET_CFP(), calling, ci, false, block_handler);
6436 }
6437}
6438
6439enum method_explorer_type {
6440 mexp_search_method,
6441 mexp_search_invokeblock,
6442 mexp_search_super,
6443};
6444
6445ALWAYS_INLINE(static VALUE vm_sendish(struct rb_execution_context_struct *ec,
6446 struct rb_control_frame_struct *reg_cfp,
6447 struct rb_call_data *cd, VALUE block_handler,
6448 enum method_explorer_type method_explorer));
6449static inline VALUE
6450vm_sendish(
6451 struct rb_execution_context_struct *ec,
6452 struct rb_control_frame_struct *reg_cfp,
6453 struct rb_call_data *cd,
6454 VALUE block_handler,
6455 enum method_explorer_type method_explorer
6456) {
6457 VALUE val = Qundef;
6458 const struct rb_callinfo *ci = cd->ci;
6459 const struct rb_callcache *cc;
6460 int argc = vm_ci_argc(ci);
6461 VALUE recv = TOPN(argc);
6462 struct rb_calling_info calling = {
6463 .block_handler = block_handler,
6464 .kw_splat = IS_ARGS_KW_SPLAT(ci) > 0,
6465 .recv = recv,
6466 .argc = argc,
6467 .cd = cd,
6468 };
6469
6470 switch (method_explorer) {
6471 case mexp_search_method:
6472 calling.cc = cc = vm_search_method_fastpath(reg_cfp, cd, CLASS_OF(recv));
6473 val = vm_cc_call(cc)(ec, GET_CFP(), &calling);
6474 break;
6475 case mexp_search_super:
6476 calling.cc = cc = vm_search_super_method(reg_cfp, cd, recv);
6477 val = vm_cc_call(cc)(ec, GET_CFP(), &calling);
6478 break;
6479 case mexp_search_invokeblock:
6480 val = vm_invokeblock_i(ec, GET_CFP(), &calling);
6481 break;
6482 }
6483 return val;
6484}
6485
6486VALUE
6487rb_vm_send(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd, ISEQ blockiseq)
6488{
6489 stack_check(ec);
6490 VALUE bh = vm_caller_setup_arg_block(ec, GET_CFP(), cd->ci, blockiseq, false);
6491 VALUE val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_method);
6492 VM_EXEC(ec, val);
6493 return val;
6494}
6495
6496// Fallback for YJIT/ZJIT, not used by the interpreter
6497VALUE
6498rb_vm_sendforward(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd, ISEQ blockiseq)
6499{
6500 stack_check(ec);
6501
6502 struct rb_forwarding_call_data adjusted_cd;
6503 struct rb_callinfo adjusted_ci;
6504
6505 VALUE bh = vm_caller_setup_fwd_args(GET_EC(), GET_CFP(), cd, blockiseq, false, &adjusted_cd, &adjusted_ci);
6506
6507 VALUE val = vm_sendish(ec, GET_CFP(), &adjusted_cd.cd, bh, mexp_search_method);
6508
6509 if (cd->cc != adjusted_cd.cd.cc && vm_cc_markable(adjusted_cd.cd.cc)) {
6510 RB_OBJ_WRITE(CFP_ISEQ(GET_CFP()), &cd->cc, adjusted_cd.cd.cc);
6511 }
6512
6513 VM_EXEC(ec, val);
6514 return val;
6515}
6516
6517VALUE
6518rb_vm_opt_send_without_block(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd)
6519{
6520 stack_check(ec);
6521 VALUE bh = VM_BLOCK_HANDLER_NONE;
6522 VALUE val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_method);
6523 VM_EXEC(ec, val);
6524 return val;
6525}
6526
6527VALUE
6528rb_vm_invokesuper(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd, ISEQ blockiseq)
6529{
6530 stack_check(ec);
6531
6532 struct rb_callinfo adjusted_ci = VM_CI_ON_STACK(vm_ci_mid(cd->ci),
6533 vm_ci_flag(cd->ci),
6534 vm_ci_argc(cd->ci),
6535 vm_ci_kwarg(cd->ci));
6536 const struct rb_callcache *original_cc = rbimpl_atomic_ptr_load((void **)&cd->cc, RBIMPL_ATOMIC_ACQUIRE);
6537 struct rb_call_data adjusted_cd = {
6538 .ci = &adjusted_ci,
6539 .cc = original_cc,
6540 };
6541
6542 VALUE bh = vm_caller_setup_arg_block(ec, GET_CFP(), adjusted_cd.ci, blockiseq, true);
6543 VALUE val = vm_sendish(ec, GET_CFP(), &adjusted_cd, bh, mexp_search_super);
6544
6545 if (original_cc != adjusted_cd.cc && vm_cc_markable(adjusted_cd.cc)) {
6546 rbimpl_atomic_ptr_store((volatile void **)&cd->cc, (void *)adjusted_cd.cc, RBIMPL_ATOMIC_RELEASE);
6547 RB_OBJ_WRITTEN(CFP_ISEQ(GET_CFP()), Qundef, adjusted_cd.cc);
6548 }
6549
6550 VM_EXEC(ec, val);
6551 return val;
6552}
6553
6554// Fallback for YJIT/ZJIT, not used by the interpreter
6555VALUE
6556rb_vm_invokesuperforward(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd, ISEQ blockiseq)
6557{
6558 stack_check(ec);
6559 struct rb_forwarding_call_data adjusted_cd;
6560 struct rb_callinfo adjusted_ci;
6561
6562 VALUE bh = vm_caller_setup_fwd_args(GET_EC(), GET_CFP(), cd, blockiseq, true, &adjusted_cd, &adjusted_ci);
6563
6564 VALUE val = vm_sendish(ec, GET_CFP(), &adjusted_cd.cd, bh, mexp_search_super);
6565
6566 if (cd->cc != adjusted_cd.cd.cc && vm_cc_markable(adjusted_cd.cd.cc)) {
6567 RB_OBJ_WRITE(CFP_ISEQ(GET_CFP()), &cd->cc, adjusted_cd.cd.cc);
6568 }
6569
6570 VM_EXEC(ec, val);
6571 return val;
6572}
6573
6574VALUE
6575rb_vm_invokeblock(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, CALL_DATA cd)
6576{
6577 stack_check(ec);
6578 VALUE bh = VM_BLOCK_HANDLER_NONE;
6579 VALUE val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_invokeblock);
6580 VM_EXEC(ec, val);
6581 return val;
6582}
6583
6584/* object.c */
6585VALUE rb_nil_to_s(VALUE);
6586VALUE rb_true_to_s(VALUE);
6587VALUE rb_false_to_s(VALUE);
6588/* numeric.c */
6589VALUE rb_int_to_s(int argc, VALUE *argv, VALUE x);
6590VALUE rb_fix_to_s(VALUE);
6591/* variable.c */
6592VALUE rb_mod_to_s(VALUE);
6594
6595static VALUE
6596vm_objtostring(struct rb_control_frame_struct *reg_cfp, VALUE recv, CALL_DATA cd)
6597{
6598 int type = TYPE(recv);
6599 if (type == T_STRING) {
6600 return recv;
6601 }
6602
6603 const struct rb_callable_method_entry_struct *cme = vm_search_method(reg_cfp, cd, recv);
6604
6605 switch (type) {
6606 case T_SYMBOL:
6607 if (check_method_basic_definition(cme)) {
6608 return rb_sym2str(recv);
6609 }
6610 break;
6611 case T_MODULE:
6612 case T_CLASS:
6613 if (check_cfunc(cme, rb_mod_to_s)) {
6614 // rb_mod_to_s() allocates a mutable string, but since we are only
6615 // going to use this string for interpolation, it's fine to use the
6616 // frozen string.
6617 VALUE val = rb_mod_name(recv);
6618 if (NIL_P(val)) {
6619 val = rb_mod_to_s(recv);
6620 }
6621 return val;
6622 }
6623 break;
6624 case T_NIL:
6625 if (check_cfunc(cme, rb_nil_to_s)) {
6626 return rb_nil_to_s(recv);
6627 }
6628 break;
6629 case T_TRUE:
6630 if (check_cfunc(cme, rb_true_to_s)) {
6631 return rb_true_to_s(recv);
6632 }
6633 break;
6634 case T_FALSE:
6635 if (check_cfunc(cme, rb_false_to_s)) {
6636 return rb_false_to_s(recv);
6637 }
6638 break;
6639 case T_FIXNUM:
6640 if (check_cfunc(cme, rb_int_to_s)) {
6641 return rb_fix_to_s(recv);
6642 }
6643 break;
6644 }
6645 return Qundef;
6646}
6647
6648// ZJIT implementation is using the C function
6649// and needs to call a non-static function
6650VALUE
6651rb_vm_objtostring(struct rb_control_frame_struct *reg_cfp, VALUE recv, CALL_DATA cd)
6652{
6653 return vm_objtostring(reg_cfp, recv, cd);
6654}
6655
6656static VALUE
6657vm_opt_ary_freeze(VALUE ary, int bop, ID id)
6658{
6659 if (BASIC_OP_UNREDEFINED_P(bop, ARRAY_REDEFINED_OP_FLAG)) {
6660 return ary;
6661 }
6662 else {
6663 return Qundef;
6664 }
6665}
6666
6667static VALUE
6668vm_opt_hash_freeze(VALUE hash, int bop, ID id)
6669{
6670 if (BASIC_OP_UNREDEFINED_P(bop, HASH_REDEFINED_OP_FLAG)) {
6671 return hash;
6672 }
6673 else {
6674 return Qundef;
6675 }
6676}
6677
6678static VALUE
6679vm_opt_str_freeze(VALUE str, int bop, ID id)
6680{
6681 if (BASIC_OP_UNREDEFINED_P(bop, STRING_REDEFINED_OP_FLAG)) {
6682 return str;
6683 }
6684 else {
6685 return Qundef;
6686 }
6687}
6688
6689/* this macro is mandatory to use OPTIMIZED_CMP. What a design! */
6690#define id_cmp idCmp
6691
6692static VALUE
6693vm_opt_duparray_include_p(rb_execution_context_t *ec, const VALUE ary, VALUE target)
6694{
6695 if (OP_UNREDEFINED_P(INCLUDE_P, ARRAY)) {
6696 return rb_ary_includes(ary, target);
6697 }
6698 else {
6699 VALUE args[1] = {target};
6700
6701 // duparray
6702 RUBY_DTRACE_CREATE_HOOK(ARRAY, RARRAY_LEN(ary));
6703 VALUE dupary = rb_ary_resurrect(ary);
6704
6705 return rb_vm_call_with_refinements(ec, dupary, idIncludeP, 1, args, RB_NO_KEYWORDS);
6706 }
6707}
6708
6709VALUE
6710rb_vm_opt_duparray_include_p(rb_execution_context_t *ec, const VALUE ary, VALUE target)
6711{
6712 return vm_opt_duparray_include_p(ec, ary, target);
6713}
6714
6715static VALUE
6716vm_opt_newarray_max(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6717{
6718 if (OP_UNREDEFINED_P(MAX, ARRAY)) {
6719 if (array_len == 0) {
6720 return Qnil;
6721 }
6722 else {
6723 VALUE result = *ptr;
6724 rb_snum_t i = array_len - 1;
6725 while (i-- > 0) {
6726 const VALUE v = *++ptr;
6727 if (OPTIMIZED_CMP(v, result) > 0) {
6728 result = v;
6729 }
6730 }
6731 return result;
6732 }
6733 }
6734 else {
6735 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idMax, 0, NULL, RB_NO_KEYWORDS);
6736 }
6737}
6738
6739VALUE
6740rb_vm_opt_newarray_max(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6741{
6742 return vm_opt_newarray_max(ec, array_len, ptr);
6743}
6744
6745static VALUE
6746vm_opt_newarray_min(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6747{
6748 if (OP_UNREDEFINED_P(MIN, ARRAY)) {
6749 if (array_len == 0) {
6750 return Qnil;
6751 }
6752 else {
6753 VALUE result = *ptr;
6754 rb_snum_t i = array_len - 1;
6755 while (i-- > 0) {
6756 const VALUE v = *++ptr;
6757 if (OPTIMIZED_CMP(v, result) < 0) {
6758 result = v;
6759 }
6760 }
6761 return result;
6762 }
6763 }
6764 else {
6765 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idMin, 0, NULL, RB_NO_KEYWORDS);
6766 }
6767}
6768
6769VALUE
6770rb_vm_opt_newarray_min(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6771{
6772 return vm_opt_newarray_min(ec, array_len, ptr);
6773}
6774
6775static VALUE
6776vm_opt_newarray_hash(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6777{
6778 // If Array#hash is _not_ monkeypatched, use the optimized call
6779 if (OP_UNREDEFINED_P(HASH, ARRAY)) {
6780 return rb_ary_hash_values(array_len, ptr);
6781 }
6782 else {
6783 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idHash, 0, NULL, RB_NO_KEYWORDS);
6784 }
6785}
6786
6787VALUE
6788rb_vm_opt_newarray_hash(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr)
6789{
6790 return vm_opt_newarray_hash(ec, array_len, ptr);
6791}
6792
6793VALUE rb_setup_fake_ary(struct RArray *fake_ary, const VALUE *list, rb_len_t len);
6794VALUE rb_ec_pack_ary(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer);
6795
6796static VALUE
6797vm_opt_newarray_include_p(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr, VALUE target)
6798{
6799 if (OP_UNREDEFINED_P(INCLUDE_P, ARRAY)) {
6800 struct RArray fake_ary = {RBASIC_INIT};
6801 VALUE ary = rb_setup_fake_ary(&fake_ary, ptr, array_len);
6802 return rb_ary_includes(ary, target);
6803 }
6804 else {
6805 VALUE args[1] = {target};
6806 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idIncludeP, 1, args, RB_NO_KEYWORDS);
6807 }
6808}
6809
6810VALUE
6811rb_vm_opt_newarray_include_p(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr, VALUE target)
6812{
6813 return vm_opt_newarray_include_p(ec, array_len, ptr, target);
6814}
6815
6816static VALUE
6817vm_opt_newarray_pack_buffer(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr, VALUE fmt, VALUE buffer)
6818{
6819 if (OP_UNREDEFINED_P(PACK, ARRAY)) {
6820 struct RArray fake_ary = {RBASIC_INIT};
6821 VALUE ary = rb_setup_fake_ary(&fake_ary, ptr, array_len);
6822 return rb_ec_pack_ary(ec, ary, fmt, (UNDEF_P(buffer) ? Qnil : buffer));
6823 }
6824 else {
6825 // The opt_newarray_send insn drops the keyword args so we need to rebuild them.
6826 // Setup an array with room for keyword hash.
6827 VALUE args[2];
6828 args[0] = fmt;
6829 int kw_splat = RB_NO_KEYWORDS;
6830 int argc = 1;
6831
6832 if (!UNDEF_P(buffer)) {
6833 args[1] = rb_hash_new_capa(1);
6834 rb_hash_aset(args[1], ID2SYM(idBuffer), buffer);
6835 kw_splat = RB_PASS_KEYWORDS;
6836 argc++;
6837 }
6838
6839 return rb_vm_call_with_refinements(ec, rb_ary_new4(array_len, ptr), idPack, argc, args, kw_splat);
6840 }
6841}
6842
6843VALUE
6844rb_vm_opt_newarray_pack_buffer(rb_execution_context_t *ec, rb_num_t array_len, const VALUE *ptr, VALUE fmt, VALUE buffer)
6845{
6846 return vm_opt_newarray_pack_buffer(ec, array_len, ptr, fmt, buffer);
6847}
6848
6849
6850#undef id_cmp
6851
6852static void
6853vm_track_constant_cache(ID id, void *ic)
6854{
6855 rb_vm_t *vm = GET_VM();
6856 struct rb_id_table *const_cache = &vm->constant_cache;
6857 VALUE lookup_result;
6858 set_table *ics;
6859
6860 if (rb_id_table_lookup(const_cache, id, &lookup_result)) {
6861 ics = (set_table *)lookup_result;
6862 }
6863 else {
6864 ics = set_init_numtable();
6865 rb_id_table_insert(const_cache, id, (VALUE)ics);
6866 }
6867
6868 /* The call below to st_insert could allocate which could trigger a GC.
6869 * If it triggers a GC, it may free an iseq that also holds a cache to this
6870 * constant. If that iseq is the last iseq with a cache to this constant, then
6871 * it will free this ST table, which would cause an use-after-free during this
6872 * st_insert.
6873 *
6874 * So to fix this issue, we store the ID that is currently being inserted
6875 * and, in remove_from_constant_cache, we don't free the ST table for ID
6876 * equal to this one.
6877 *
6878 * See [Bug #20921].
6879 */
6880 vm->inserting_constant_cache_id = id;
6881
6882 set_insert(ics, (st_data_t)ic);
6883
6884 vm->inserting_constant_cache_id = (ID)0;
6885}
6886
6887static void
6888vm_ic_track_const_chain(rb_control_frame_t *cfp, IC ic, const rb_const_path_t const_path)
6889{
6890 RB_VM_LOCKING() {
6891 size_t i = 0;
6892 ID segment;
6893 while (((segment = const_path_aref(const_path, i)))) {
6894 vm_track_constant_cache(segment, ic);
6895 i++;
6896 }
6897 }
6898}
6899
6900// For JIT inlining
6901static inline bool
6902vm_inlined_ic_hit_p(VALUE flags, VALUE value, const rb_cref_t *ic_cref, rb_serial_t ractor_id, const VALUE *reg_ep)
6903{
6904 // Not rb_ractor_main_p(): an owner Ractor may now cache an unshareable constant
6905 // of its own class, and only it may be handed that value back.
6906 if ((flags & IMEMO_CONST_CACHE_SHAREABLE) || ractor_id == rb_ractor_id(GET_RACTOR())) {
6907 VM_ASSERT(ractor_incidental_shareable_p(flags & IMEMO_CONST_CACHE_SHAREABLE, value));
6908
6909 return (ic_cref == NULL || // no need to check CREF
6910 ic_cref == vm_get_cref(reg_ep));
6911 }
6912 return false;
6913}
6914
6915static bool
6916vm_ic_hit_p(const struct iseq_inline_constant_cache_entry *ice, const VALUE *reg_ep)
6917{
6918 VM_ASSERT(IMEMO_TYPE_P(ice, imemo_constcache));
6919 return vm_inlined_ic_hit_p(ice->flags, ice->value, ice->ic_cref, ice->ractor_id, reg_ep);
6920}
6921
6922// YJIT needs this function to never allocate and never raise
6923bool
6924rb_vm_ic_hit_p(IC ic, const VALUE *reg_ep)
6925{
6926 return ic->entry && vm_ic_hit_p(ic->entry, reg_ep);
6927}
6928
6929static void
6930vm_ic_update(const rb_execution_context_t *ec, const rb_iseq_t *iseq, IC ic, VALUE val, const VALUE *reg_ep)
6931{
6932 if (ruby_vm_const_missing_count > 0) {
6933 ruby_vm_const_missing_count = 0;
6934 ic->entry = NULL;
6935 return;
6936 }
6937
6938 struct iseq_inline_constant_cache_entry *ice = SHAREABLE_IMEMO_NEW(struct iseq_inline_constant_cache_entry, imemo_constcache, 0);
6939 RB_OBJ_WRITE(ice, &ice->value, val);
6940 ice->ic_cref = vm_get_const_key_cref(reg_ep);
6941 ice->ractor_id = rb_ractor_id(rb_ec_ractor_ptr(ec));
6942
6943 if (rb_ractor_shareable_p(val)) {
6944 RUBY_ASSERT((rb_gc_verify_shareable(val), 1));
6945 ice->flags |= IMEMO_CONST_CACHE_SHAREABLE;
6946 }
6947 RB_OBJ_WRITE(iseq, &ic->entry, ice);
6948
6949#if USE_YJIT
6950 if (rb_yjit_enabled_p && ISEQ_BODY(iseq)->jit_payload) {
6951 // Multiple `opt_getconstant_path` instructions may share the same IC,
6952 // we must call `rb_yjit_constant_ic_update` for each of them.
6953 const VALUE *encoded = ISEQ_BODY(iseq)->iseq_encoded;
6954 unsigned int size = ISEQ_BODY(iseq)->iseq_size;
6955 for (unsigned int pos = 0; pos < size;) {
6956 int insn = rb_vm_insn_decode(encoded[pos]);
6957 if (insn == BIN(opt_getconstant_path) && (IC)encoded[pos + 1] == ic) {
6958 rb_yjit_constant_ic_update(iseq, ic, pos);
6959 }
6960 pos += insn_len(insn);
6961 }
6962 }
6963#endif
6964}
6965
6966VALUE
6967rb_vm_opt_getconstant_path(rb_execution_context_t *ec, rb_control_frame_t *const reg_cfp, IC ic)
6968{
6969 VALUE val;
6970 const rb_const_path_t const_path = ic->const_path;
6971 struct iseq_inline_constant_cache_entry *ice = ic->entry;
6972
6973 if (ice && vm_ic_hit_p(ice, GET_EP())) {
6974 val = ice->value;
6975
6976 // On a non-main ractor another ractor can concurrently reassign a
6977 // shareable constant (which invalidates ICs asynchronously), so the
6978 // cached-but-still-shareable value may legitimately differ from a
6979 // freshly recomputed chain. Only assert equality when single-ractor.
6980 VM_ASSERT(val == vm_get_ev_const_chain(ec, const_path) || rb_multi_ractor_p());
6981 }
6982 else {
6983 ruby_vm_constant_cache_misses++;
6984 val = vm_get_ev_const_chain(ec, const_path);
6985 vm_ic_track_const_chain(GET_CFP(), ic, const_path);
6986 vm_ic_update(ec, CFP_ISEQ(GET_CFP()), ic, val, GET_EP());
6987 }
6988 return val;
6989}
6990
6991// Return true if the once value is already computed and set *result to the value.
6992// Otherwise, return false.
6993// Used for ZJIT. Keep in sync with `vm_once_dispatch` below.
6994bool
6995rb_vm_once_done_value(VALUE once_storage, VALUE *result)
6996{
6997 struct rb_imemo_once_storage *storage = (struct rb_imemo_once_storage *)once_storage;
6998 rb_thread_t *running_th = rbimpl_atomic_ptr_load((void **)&storage->running_thread, RBIMPL_ATOMIC_ACQUIRE);
6999 if (running_th == RUNNING_THREAD_ONCE_DONE) {
7000 *result = storage->value;
7001 return true;
7002 }
7003 return false;
7004}
7005
7006static VALUE
7007vm_once_dispatch(rb_execution_context_t *ec, ISEQ iseq, VALUE once_storage)
7008{
7009 struct rb_imemo_once_storage *storage = (struct rb_imemo_once_storage *)once_storage;
7010 rb_thread_t *th = rb_ec_thread_ptr(ec);
7011 rb_thread_t *running_th;
7012
7013 again:
7014 running_th = rbimpl_atomic_ptr_load((void **)&storage->running_thread, RBIMPL_ATOMIC_ACQUIRE);
7015 if (running_th == RUNNING_THREAD_ONCE_DONE) {
7016 return storage->value;
7017 }
7018 else if (running_th == NULL) {
7019 VALUE val = Qundef;
7020 enum ruby_tag_type state;
7021 if (rbimpl_atomic_ptr_cas((void **)&storage->running_thread, running_th, th, RBIMPL_ATOMIC_RELEASE, RBIMPL_ATOMIC_RELAXED) != running_th) {
7022 goto again;
7023 }
7024 EC_PUSH_TAG(ec);
7025 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
7026 val = vm_once_exec((VALUE)iseq);
7027 }
7028 EC_POP_TAG();
7029 if (state != TAG_NONE) {
7030 vm_once_clear((VALUE)storage);
7031 EC_JUMP_TAG(ec, state);
7032 }
7033
7034 // TODO: confirm that it is shareable
7035 if (RB_FL_ABLE(val)) {
7037 }
7038
7039 RB_OBJ_WRITE((VALUE)storage, &storage->value, val);
7040
7041 rbimpl_atomic_ptr_store((volatile void**)&storage->running_thread, RUNNING_THREAD_ONCE_DONE, RBIMPL_ATOMIC_RELEASE); /* success */
7042 vm_once_broadcast(rb_ec_vm_ptr(ec));
7043 return val;
7044 }
7045 else if (running_th == th) {
7046 /* recursive once */
7047 return vm_once_exec((VALUE)iseq);
7048 }
7049 else {
7050 /* wait for the winner to finish */
7051 struct vm_once_wait_arg arg = { .vm = rb_ec_vm_ptr(ec), .storage = storage };
7052 rb_nogvl(vm_once_wait_no_gvl, &arg, vm_once_wait_ubf, arg.vm,
7054 RUBY_VM_CHECK_INTS(ec);
7055 goto again;
7056 }
7057}
7058
7059static OFFSET
7060vm_case_dispatch(CDHASH hash, OFFSET else_offset, VALUE key)
7061{
7062 switch (OBJ_BUILTIN_TYPE(key)) {
7063 case -1:
7064 case T_FLOAT:
7065 case T_SYMBOL:
7066 case T_BIGNUM:
7067 case T_STRING:
7068 if (BASIC_OP_UNREDEFINED_P(BOP_EQQ,
7069 SYMBOL_REDEFINED_OP_FLAG |
7070 INTEGER_REDEFINED_OP_FLAG |
7071 FLOAT_REDEFINED_OP_FLAG |
7072 NIL_REDEFINED_OP_FLAG |
7073 TRUE_REDEFINED_OP_FLAG |
7074 FALSE_REDEFINED_OP_FLAG |
7075 STRING_REDEFINED_OP_FLAG)) {
7076 st_data_t val;
7077 if (RB_FLOAT_TYPE_P(key)) {
7078 double kval = RFLOAT_VALUE(key);
7079 if (!isinf(kval) && modf(kval, &kval) == 0.0) {
7080 key = FIXABLE(kval) ? LONG2FIX((long)kval) : rb_dbl2big(kval);
7081 }
7082 }
7083 if (st_lookup(rb_imemo_cdhash_tbl(hash), key, &val)) {
7084 return (VALUE)val;
7085 }
7086 else {
7087 return else_offset;
7088 }
7089 }
7090 }
7091 return 0;
7092}
7093
7094NORETURN(static void
7095 vm_stack_consistency_error(const rb_execution_context_t *ec,
7096 const rb_control_frame_t *,
7097 const VALUE *));
7098static void
7099vm_stack_consistency_error(const rb_execution_context_t *ec,
7100 const rb_control_frame_t *cfp,
7101 const VALUE *bp)
7102{
7103 const ptrdiff_t nsp = VM_SP_CNT(ec, cfp->sp);
7104 const ptrdiff_t nbp = VM_SP_CNT(ec, bp);
7105 static const char stack_consistency_error[] =
7106 "Stack consistency error (sp: %"PRIdPTRDIFF", bp: %"PRIdPTRDIFF")";
7107#if defined RUBY_DEVEL
7108 VALUE mesg = rb_sprintf(stack_consistency_error, nsp, nbp);
7109 rb_str_cat_cstr(mesg, "\n");
7110 rb_str_append(mesg, rb_iseq_disasm(CFP_ISEQ(cfp)));
7112#else
7113 rb_bug(stack_consistency_error, nsp, nbp);
7114#endif
7115}
7116
7117static VALUE
7118vm_opt_plus(VALUE recv, VALUE obj)
7119{
7120 if (FIXNUM_2_P(recv, obj) &&
7121 OP_UNREDEFINED_P(PLUS, INTEGER)) {
7122 return rb_fix_plus_fix(recv, obj);
7123 }
7124 else if (FLONUM_2_P(recv, obj) &&
7125 OP_UNREDEFINED_P(PLUS, FLOAT)) {
7126 return DBL2NUM(RFLOAT_VALUE(recv) + RFLOAT_VALUE(obj));
7127 }
7128 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7129 return Qundef;
7130 }
7131 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7132 RBASIC_CLASS(obj) == rb_cFloat &&
7133 OP_UNREDEFINED_P(PLUS, FLOAT)) {
7134 return DBL2NUM(RFLOAT_VALUE(recv) + RFLOAT_VALUE(obj));
7135 }
7136 else if (RBASIC_CLASS(recv) == rb_cString &&
7137 RBASIC_CLASS(obj) == rb_cString &&
7138 OP_UNREDEFINED_P(PLUS, STRING)) {
7139 return rb_str_opt_plus(recv, obj);
7140 }
7141 else if (RBASIC_CLASS(recv) == rb_cArray &&
7142 RBASIC_CLASS(obj) == rb_cArray &&
7143 OP_UNREDEFINED_P(PLUS, ARRAY)) {
7144 return rb_ary_plus(recv, obj);
7145 }
7146 else {
7147 return Qundef;
7148 }
7149}
7150
7151static VALUE
7152vm_opt_minus(VALUE recv, VALUE obj)
7153{
7154 if (FIXNUM_2_P(recv, obj) &&
7155 OP_UNREDEFINED_P(MINUS, INTEGER)) {
7156 return rb_fix_minus_fix(recv, obj);
7157 }
7158 else if (FLONUM_2_P(recv, obj) &&
7159 OP_UNREDEFINED_P(MINUS, FLOAT)) {
7160 return DBL2NUM(RFLOAT_VALUE(recv) - RFLOAT_VALUE(obj));
7161 }
7162 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7163 return Qundef;
7164 }
7165 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7166 RBASIC_CLASS(obj) == rb_cFloat &&
7167 OP_UNREDEFINED_P(MINUS, FLOAT)) {
7168 return DBL2NUM(RFLOAT_VALUE(recv) - RFLOAT_VALUE(obj));
7169 }
7170 else {
7171 return Qundef;
7172 }
7173}
7174
7175static VALUE
7176vm_opt_mult(VALUE recv, VALUE obj)
7177{
7178 if (FIXNUM_2_P(recv, obj) &&
7179 OP_UNREDEFINED_P(MULT, INTEGER)) {
7180 return rb_fix_mul_fix(recv, obj);
7181 }
7182 else if (FLONUM_2_P(recv, obj) &&
7183 OP_UNREDEFINED_P(MULT, FLOAT)) {
7184 return DBL2NUM(RFLOAT_VALUE(recv) * RFLOAT_VALUE(obj));
7185 }
7186 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7187 return Qundef;
7188 }
7189 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7190 RBASIC_CLASS(obj) == rb_cFloat &&
7191 OP_UNREDEFINED_P(MULT, FLOAT)) {
7192 return DBL2NUM(RFLOAT_VALUE(recv) * RFLOAT_VALUE(obj));
7193 }
7194 else {
7195 return Qundef;
7196 }
7197}
7198
7199static VALUE
7200vm_opt_div(VALUE recv, VALUE obj)
7201{
7202 if (FIXNUM_2_P(recv, obj) &&
7203 OP_UNREDEFINED_P(DIV, INTEGER)) {
7204 return (FIX2LONG(obj) == 0) ? Qundef : rb_fix_div_fix(recv, obj);
7205 }
7206 else if (FLONUM_2_P(recv, obj) &&
7207 OP_UNREDEFINED_P(DIV, FLOAT)) {
7208 return rb_flo_div_flo(recv, obj);
7209 }
7210 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7211 return Qundef;
7212 }
7213 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7214 RBASIC_CLASS(obj) == rb_cFloat &&
7215 OP_UNREDEFINED_P(DIV, FLOAT)) {
7216 return rb_flo_div_flo(recv, obj);
7217 }
7218 else {
7219 return Qundef;
7220 }
7221}
7222
7223static VALUE
7224vm_opt_mod(VALUE recv, VALUE obj)
7225{
7226 if (FIXNUM_2_P(recv, obj) &&
7227 OP_UNREDEFINED_P(MOD, INTEGER)) {
7228 return (FIX2LONG(obj) == 0) ? Qundef : rb_fix_mod_fix(recv, obj);
7229 }
7230 else if (FLONUM_2_P(recv, obj) &&
7231 OP_UNREDEFINED_P(MOD, FLOAT)) {
7232 return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(recv), RFLOAT_VALUE(obj)));
7233 }
7234 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7235 return Qundef;
7236 }
7237 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7238 RBASIC_CLASS(obj) == rb_cFloat &&
7239 OP_UNREDEFINED_P(MOD, FLOAT)) {
7240 return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(recv), RFLOAT_VALUE(obj)));
7241 }
7242 else {
7243 return Qundef;
7244 }
7245}
7246
7247static VALUE
7248vm_opt_neq(struct rb_control_frame_struct *reg_cfp, CALL_DATA cd, CALL_DATA cd_eq, VALUE recv, VALUE obj)
7249{
7250 if (vm_method_cfunc_is(reg_cfp, cd, recv, rb_obj_not_equal)) {
7251 VALUE val = opt_equality(reg_cfp, recv, obj, cd_eq);
7252
7253 if (!UNDEF_P(val)) {
7254 return RBOOL(!RTEST(val));
7255 }
7256 }
7257
7258 return Qundef;
7259}
7260
7261static VALUE
7262vm_opt_lt(VALUE recv, VALUE obj)
7263{
7264 if (FIXNUM_2_P(recv, obj) &&
7265 OP_UNREDEFINED_P(LT, INTEGER)) {
7266 return RBOOL((SIGNED_VALUE)recv < (SIGNED_VALUE)obj);
7267 }
7268 else if (FLONUM_2_P(recv, obj) &&
7269 OP_UNREDEFINED_P(LT, FLOAT)) {
7270 return RBOOL(RFLOAT_VALUE(recv) < RFLOAT_VALUE(obj));
7271 }
7272 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7273 return Qundef;
7274 }
7275 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7276 RBASIC_CLASS(obj) == rb_cFloat &&
7277 OP_UNREDEFINED_P(LT, FLOAT)) {
7278 return RBOOL(RFLOAT_VALUE(recv) < RFLOAT_VALUE(obj));
7279 }
7280 else {
7281 return Qundef;
7282 }
7283}
7284
7285static VALUE
7286vm_opt_le(VALUE recv, VALUE obj)
7287{
7288 if (FIXNUM_2_P(recv, obj) &&
7289 OP_UNREDEFINED_P(LE, INTEGER)) {
7290 return RBOOL((SIGNED_VALUE)recv <= (SIGNED_VALUE)obj);
7291 }
7292 else if (FLONUM_2_P(recv, obj) &&
7293 OP_UNREDEFINED_P(LE, FLOAT)) {
7294 return RBOOL(RFLOAT_VALUE(recv) <= RFLOAT_VALUE(obj));
7295 }
7296 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7297 return Qundef;
7298 }
7299 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7300 RBASIC_CLASS(obj) == rb_cFloat &&
7301 OP_UNREDEFINED_P(LE, FLOAT)) {
7302 return RBOOL(RFLOAT_VALUE(recv) <= RFLOAT_VALUE(obj));
7303 }
7304 else {
7305 return Qundef;
7306 }
7307}
7308
7309static VALUE
7310vm_opt_gt(VALUE recv, VALUE obj)
7311{
7312 if (FIXNUM_2_P(recv, obj) &&
7313 OP_UNREDEFINED_P(GT, INTEGER)) {
7314 return RBOOL((SIGNED_VALUE)recv > (SIGNED_VALUE)obj);
7315 }
7316 else if (FLONUM_2_P(recv, obj) &&
7317 OP_UNREDEFINED_P(GT, FLOAT)) {
7318 return RBOOL(RFLOAT_VALUE(recv) > RFLOAT_VALUE(obj));
7319 }
7320 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7321 return Qundef;
7322 }
7323 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7324 RBASIC_CLASS(obj) == rb_cFloat &&
7325 OP_UNREDEFINED_P(GT, FLOAT)) {
7326 return RBOOL(RFLOAT_VALUE(recv) > RFLOAT_VALUE(obj));
7327 }
7328 else {
7329 return Qundef;
7330 }
7331}
7332
7333static VALUE
7334vm_opt_ge(VALUE recv, VALUE obj)
7335{
7336 if (FIXNUM_2_P(recv, obj) &&
7337 OP_UNREDEFINED_P(GE, INTEGER)) {
7338 return RBOOL((SIGNED_VALUE)recv >= (SIGNED_VALUE)obj);
7339 }
7340 else if (FLONUM_2_P(recv, obj) &&
7341 OP_UNREDEFINED_P(GE, FLOAT)) {
7342 return RBOOL(RFLOAT_VALUE(recv) >= RFLOAT_VALUE(obj));
7343 }
7344 else if (SPECIAL_CONST_P(recv) || SPECIAL_CONST_P(obj)) {
7345 return Qundef;
7346 }
7347 else if (RBASIC_CLASS(recv) == rb_cFloat &&
7348 RBASIC_CLASS(obj) == rb_cFloat &&
7349 OP_UNREDEFINED_P(GE, FLOAT)) {
7350 return RBOOL(RFLOAT_VALUE(recv) >= RFLOAT_VALUE(obj));
7351 }
7352 else {
7353 return Qundef;
7354 }
7355}
7356
7357
7358static VALUE
7359vm_opt_ltlt(VALUE recv, VALUE obj)
7360{
7361 if (SPECIAL_CONST_P(recv)) {
7362 return Qundef;
7363 }
7364 else if (RBASIC_CLASS(recv) == rb_cString &&
7365 OP_UNREDEFINED_P(LTLT, STRING)) {
7366 if (LIKELY(RB_TYPE_P(obj, T_STRING))) {
7367 return rb_str_buf_append(recv, obj);
7368 }
7369 else {
7370 return rb_str_concat(recv, obj);
7371 }
7372 }
7373 else if (RBASIC_CLASS(recv) == rb_cArray &&
7374 OP_UNREDEFINED_P(LTLT, ARRAY)) {
7375 return rb_ary_push(recv, obj);
7376 }
7377 else {
7378 return Qundef;
7379 }
7380}
7381
7382static VALUE
7383vm_opt_and(VALUE recv, VALUE obj)
7384{
7385 // If recv and obj are both fixnums, then the bottom tag bit
7386 // will be 1 on both. 1 & 1 == 1, so the result value will also
7387 // be a fixnum. If either side is *not* a fixnum, then the tag bit
7388 // will be 0, and we return Qundef.
7389 VALUE ret = ((SIGNED_VALUE) recv) & ((SIGNED_VALUE) obj);
7390
7391 if (FIXNUM_P(ret) &&
7392 OP_UNREDEFINED_P(AND, INTEGER)) {
7393 return ret;
7394 }
7395 else {
7396 return Qundef;
7397 }
7398}
7399
7400static VALUE
7401vm_opt_or(VALUE recv, VALUE obj)
7402{
7403 if (FIXNUM_2_P(recv, obj) &&
7404 OP_UNREDEFINED_P(OR, INTEGER)) {
7405 return recv | obj;
7406 }
7407 else {
7408 return Qundef;
7409 }
7410}
7411
7412static VALUE
7413vm_opt_aref(VALUE recv, VALUE obj)
7414{
7415 if (SPECIAL_CONST_P(recv)) {
7416 if (FIXNUM_2_P(recv, obj) &&
7417 OP_UNREDEFINED_P(AREF, INTEGER)) {
7418 return rb_fix_aref(recv, obj);
7419 }
7420 return Qundef;
7421 }
7422 else if (RBASIC_CLASS(recv) == rb_cArray &&
7423 OP_UNREDEFINED_P(AREF, ARRAY)) {
7424 if (FIXNUM_P(obj)) {
7425 return rb_ary_entry_internal(recv, FIX2LONG(obj));
7426 }
7427 else {
7428 return rb_ary_aref1(recv, obj);
7429 }
7430 }
7431 else if (RBASIC_CLASS(recv) == rb_cHash &&
7432 OP_UNREDEFINED_P(AREF, HASH)) {
7433 return rb_hash_aref(recv, obj);
7434 }
7435 else {
7436 return Qundef;
7437 }
7438}
7439
7440static VALUE
7441vm_opt_aset(VALUE recv, VALUE obj, VALUE set)
7442{
7443 if (SPECIAL_CONST_P(recv)) {
7444 return Qundef;
7445 }
7446 else if (RBASIC_CLASS(recv) == rb_cArray &&
7447 OP_UNREDEFINED_P(ASET, ARRAY) &&
7448 FIXNUM_P(obj)) {
7449 rb_ary_store(recv, FIX2LONG(obj), set);
7450 return set;
7451 }
7452 else if (RBASIC_CLASS(recv) == rb_cHash &&
7453 OP_UNREDEFINED_P(ASET, HASH)) {
7454 rb_hash_aset(recv, obj, set);
7455 return set;
7456 }
7457 else {
7458 return Qundef;
7459 }
7460}
7461
7462static VALUE
7463vm_opt_length(VALUE recv, int bop)
7464{
7465 if (SPECIAL_CONST_P(recv)) {
7466 return Qundef;
7467 }
7468 else if (RBASIC_CLASS(recv) == rb_cString &&
7469 BASIC_OP_UNREDEFINED_P(bop, STRING_REDEFINED_OP_FLAG)) {
7470 if (bop == BOP_EMPTY_P) {
7471 return LEN2NUM(RSTRING_LEN(recv));
7472 }
7473 else {
7474 return rb_str_length(recv);
7475 }
7476 }
7477 else if (RBASIC_CLASS(recv) == rb_cArray &&
7478 BASIC_OP_UNREDEFINED_P(bop, ARRAY_REDEFINED_OP_FLAG)) {
7479 return LEN2NUM(RARRAY_LEN(recv));
7480 }
7481 else if (RBASIC_CLASS(recv) == rb_cHash &&
7482 BASIC_OP_UNREDEFINED_P(bop, HASH_REDEFINED_OP_FLAG)) {
7483 return INT2FIX(RHASH_SIZE(recv));
7484 }
7485 else {
7486 return Qundef;
7487 }
7488}
7489
7490static VALUE
7491vm_opt_empty_p(VALUE recv)
7492{
7493 switch (vm_opt_length(recv, BOP_EMPTY_P)) {
7494 case Qundef: return Qundef;
7495 case INT2FIX(0): return Qtrue;
7496 default: return Qfalse;
7497 }
7498}
7499
7500VALUE rb_false(VALUE obj);
7501
7502static VALUE
7503vm_opt_nil_p(struct rb_control_frame_struct *reg_cfp, CALL_DATA cd, VALUE recv)
7504{
7505 if (NIL_P(recv) &&
7506 OP_UNREDEFINED_P(NIL_P, NIL)) {
7507 return Qtrue;
7508 }
7509 else if (vm_method_cfunc_is(reg_cfp, cd, recv, rb_false)) {
7510 return Qfalse;
7511 }
7512 else {
7513 return Qundef;
7514 }
7515}
7516
7517static VALUE
7518fix_succ(VALUE x)
7519{
7520 switch (x) {
7521 case ~0UL:
7522 /* 0xFFFF_FFFF == INT2FIX(-1)
7523 * `-1.succ` is of course 0. */
7524 return INT2FIX(0);
7525 case RSHIFT(~0UL, 1):
7526 /* 0x7FFF_FFFF == LONG2FIX(0x3FFF_FFFF)
7527 * 0x3FFF_FFFF + 1 == 0x4000_0000, which is a Bignum. */
7528 return rb_uint2big(1UL << (SIZEOF_LONG * CHAR_BIT - 2));
7529 default:
7530 /* LONG2FIX(FIX2LONG(x)+FIX2LONG(y))
7531 * == ((lx*2+1)/2 + (ly*2+1)/2)*2+1
7532 * == lx*2 + ly*2 + 1
7533 * == (lx*2+1) + (ly*2+1) - 1
7534 * == x + y - 1
7535 *
7536 * Here, if we put y := INT2FIX(1):
7537 *
7538 * == x + INT2FIX(1) - 1
7539 * == x + 2 .
7540 */
7541 return x + 2;
7542 }
7543}
7544
7545static VALUE
7546vm_opt_succ(VALUE recv)
7547{
7548 if (FIXNUM_P(recv) &&
7549 OP_UNREDEFINED_P(SUCC, INTEGER)) {
7550 return fix_succ(recv);
7551 }
7552 else if (SPECIAL_CONST_P(recv)) {
7553 return Qundef;
7554 }
7555 else if (RBASIC_CLASS(recv) == rb_cString &&
7556 OP_UNREDEFINED_P(SUCC, STRING)) {
7557 return rb_str_succ(recv);
7558 }
7559 else {
7560 return Qundef;
7561 }
7562}
7563
7564static VALUE
7565vm_opt_not(struct rb_control_frame_struct *reg_cfp, CALL_DATA cd, VALUE recv)
7566{
7567 if (vm_method_cfunc_is(reg_cfp, cd, recv, rb_obj_not)) {
7568 return RBOOL(!RTEST(recv));
7569 }
7570 else {
7571 return Qundef;
7572 }
7573}
7574
7575static VALUE
7576vm_opt_regexpmatch2(VALUE recv, VALUE obj)
7577{
7578 if (SPECIAL_CONST_P(recv)) {
7579 return Qundef;
7580 }
7581 else if (RBASIC_CLASS(recv) == rb_cString &&
7582 CLASS_OF(obj) == rb_cRegexp &&
7583 OP_UNREDEFINED_P(MATCH, STRING)) {
7584 return rb_reg_match(obj, recv);
7585 }
7586 else if (RBASIC_CLASS(recv) == rb_cRegexp &&
7587 OP_UNREDEFINED_P(MATCH, REGEXP)) {
7588 return rb_reg_match(recv, obj);
7589 }
7590 else {
7591 return Qundef;
7592 }
7593}
7594
7595#undef EQ_UNREDEFINED_P
7596#undef OP_UNREDEFINED_P
7597
7598NOINLINE(static void vm_trace(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp));
7599
7600static inline void
7601vm_trace_hook(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const VALUE *pc, size_t pc_len,
7602 rb_event_flag_t pc_events, rb_event_flag_t target_event,
7603 rb_hook_list_t *global_hooks, st_data_t local_hooks_key, VALUE val)
7604{
7605 rb_event_flag_t event = pc_events & target_event;
7606 VALUE self = GET_SELF();
7607
7608 VM_ASSERT(rb_popcount64((uint64_t)event) == 1);
7609
7610 if (event & global_hooks->events) {
7611 /* point PC to the next instruction, like during execution, for the source line */
7612 reg_cfp->pc += pc_len;
7613 vm_dtrace(event, ec);
7614 rb_exec_event_hook_orig(ec, global_hooks, event, self, 0, 0, 0 , val, 0);
7615 reg_cfp->pc -= pc_len;
7616 }
7617
7618 st_data_t local_hooks_data;
7619 if (local_hooks_key && st_lookup(rb_ractor_targeted_hooks(rb_ec_ractor_ptr(ec)), local_hooks_key, &local_hooks_data)) {
7620 rb_hook_list_t *local_hooks = (rb_hook_list_t *)local_hooks_data;
7621 if (event & local_hooks->events) {
7622 /* point PC to the next instruction, like during execution, for the source line */
7623 reg_cfp->pc += pc_len;
7624 rb_exec_event_hook_orig(ec, local_hooks, event, self, 0, 0, 0 , val, 0);
7625 reg_cfp->pc -= pc_len;
7626 }
7627 }
7628}
7629
7630#define VM_TRACE_HOOK(target_event, val) do { \
7631 if ((pc_events & (target_event)) & enabled_flags) { \
7632 vm_trace_hook(ec, reg_cfp, pc, pc_len, pc_events, (target_event), global_hooks, iseq_local_hooks_key, (val)); \
7633 } \
7634} while (0)
7635
7636static VALUE
7637rescue_errinfo(rb_execution_context_t *ec, rb_control_frame_t *cfp)
7638{
7639 VM_ASSERT(VM_FRAME_RUBYFRAME_P(cfp));
7640 VM_ASSERT(ISEQ_BODY(CFP_ISEQ(cfp))->type == ISEQ_TYPE_RESCUE);
7641 return cfp->ep[VM_ENV_INDEX_LAST_LVAR];
7642}
7643
7644static void
7645vm_trace(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp)
7646{
7647 const VALUE *pc = reg_cfp->pc;
7648 rb_ractor_t *r = rb_ec_ractor_ptr(ec);
7649 rb_event_flag_t enabled_flags = r->pub.hooks.events & ISEQ_TRACE_EVENTS;
7650 rb_event_flag_t ractor_events = enabled_flags;
7651
7652 if (enabled_flags == 0 && rb_ractor_targeted_hooks_cnt(r) == 0) {
7653 return;
7654 }
7655 else {
7656 const rb_iseq_t *iseq = CFP_ISEQ(reg_cfp);
7657 size_t pos = pc - ISEQ_BODY(iseq)->iseq_encoded;
7658 rb_iseq_events_t pc_events = rb_iseq_events(iseq, pos);
7659 st_data_t iseq_local_hooks_key = 0;
7660 rb_event_flag_t iseq_local_events = 0;
7661 if (RB_UNLIKELY(iseq->aux.exec.local_hooks_cnt > 0)) {
7662 st_data_t val;
7663 iseq_local_hooks_key = (st_data_t)iseq;
7664 if (st_lookup(rb_ractor_targeted_hooks(r), iseq_local_hooks_key, &val)) {
7665 iseq_local_events = ((rb_hook_list_t *)val)->events;
7666 }
7667 }
7668
7669 st_data_t bmethod_local_hooks_key = 0;
7670 rb_event_flag_t bmethod_local_events = 0;
7671 const bool bmethod_frame = VM_FRAME_BMETHOD_P(reg_cfp);
7672 enabled_flags |= iseq_local_events;
7673
7674 VM_ASSERT((iseq_local_events & ~ISEQ_TRACE_EVENTS) == 0);
7675
7676 if (bmethod_frame) {
7677 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(reg_cfp);
7678 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
7679 if (RB_UNLIKELY(me->def->body.bmethod.local_hooks_cnt > 0)) {
7680 st_data_t val;
7681 bmethod_local_hooks_key = (st_data_t)me->def;
7682 if (st_lookup(rb_ractor_targeted_hooks(r), bmethod_local_hooks_key, &val)) {
7683 bmethod_local_events = ((rb_hook_list_t *)val)->events;
7684 }
7685 }
7686 }
7687
7688 if ((pc_events & enabled_flags) == 0 && !bmethod_frame) {
7689#if 0
7690 /* disable trace */
7691 /* TODO: incomplete */
7692 rb_iseq_trace_set(iseq, vm_event_flags & ISEQ_TRACE_EVENTS);
7693#else
7694 /* do not disable trace because of performance problem
7695 * (re-enable overhead)
7696 */
7697#endif
7698 return;
7699 }
7700 else if (ec->trace_arg != NULL) {
7701 /* already tracing */
7702 return;
7703 }
7704 else {
7705 rb_hook_list_t *global_hooks = rb_ec_ractor_hooks(ec);
7706 /* Note, not considering iseq local events here since the same
7707 * iseq could be used in multiple bmethods. */
7708 rb_event_flag_t bmethod_events = ractor_events | bmethod_local_events;
7709 const size_t pc_len = (size_t)insn_len(rb_vm_insn_decode(*pc));
7710
7711 if (0) {
7712 VALUE path = rb_iseq_path(iseq);
7713 VALUE label = rb_iseq_label(iseq);
7714 ruby_debug_printf("vm_trace>>%4d (%4x) - %.*s:%d %.*s\n",
7715 (int)pos,
7716 (int)pc_events,
7717 RSTRING_LENINT(path), RSTRING_PTR(path),
7718 rb_iseq_line_no(iseq, pos),
7719 RSTRING_LENINT(label), RSTRING_PTR(label));
7720 }
7721 VM_ASSERT(reg_cfp->pc == pc);
7722 VM_ASSERT(pc_events != 0);
7723
7724 /* check traces */
7725 if ((pc_events & RUBY_EVENT_B_CALL) && bmethod_frame && (bmethod_events & RUBY_EVENT_CALL)) {
7726 /* b_call instruction running as a method. Fire call event. */
7727 vm_trace_hook(ec, reg_cfp, pc, pc_len, RUBY_EVENT_CALL, RUBY_EVENT_CALL, global_hooks, bmethod_local_hooks_key, Qundef);
7728 }
7730 VM_TRACE_HOOK(RUBY_EVENT_RESCUE, rescue_errinfo(ec, reg_cfp));
7731 VM_TRACE_HOOK(RUBY_EVENT_LINE, Qundef);
7732 VM_TRACE_HOOK(RUBY_EVENT_COVERAGE_LINE, Qundef);
7733 VM_TRACE_HOOK(RUBY_EVENT_COVERAGE_BRANCH, Qundef);
7734 VM_TRACE_HOOK(RUBY_EVENT_END | RUBY_EVENT_RETURN | RUBY_EVENT_B_RETURN, TOPN(0));
7735 if ((pc_events & RUBY_EVENT_B_RETURN) && bmethod_frame && (bmethod_events & RUBY_EVENT_RETURN)) {
7736 /* b_return instruction running as a method. Fire return event. */
7737 vm_trace_hook(ec, reg_cfp, pc, pc_len, RUBY_EVENT_RETURN, RUBY_EVENT_RETURN, global_hooks, bmethod_local_hooks_key, TOPN(0));
7738 }
7739 }
7740 }
7741}
7742#undef VM_TRACE_HOOK
7743
7744#if VM_CHECK_MODE > 0
7745NORETURN( NOINLINE( COLDFUNC
7746void rb_vm_canary_is_found_dead(enum ruby_vminsn_type i, VALUE c)));
7747
7748void
7749Init_vm_stack_canary(void)
7750{
7751 /* This has to be called _after_ our PRNG is properly set up. */
7752 int n = ruby_fill_random_bytes(&vm_stack_canary, sizeof vm_stack_canary, false);
7753 /* Make it a large positive Fixnum. Arguments a block does not take
7754 * stay at the stack top, where vm_push_frame checks the canary. */
7755 vm_stack_canary >>= 2;
7756 vm_stack_canary |= (VALUE)1 << (SIZEOF_VALUE * CHAR_BIT - 2);
7757 vm_stack_canary |= 0x01; // valid VALUE (Fixnum)
7758
7759 vm_stack_canary_was_born = true;
7760 VM_ASSERT(n == 0);
7761}
7762
7763void
7764rb_vm_canary_is_found_dead(enum ruby_vminsn_type i, VALUE c)
7765{
7766 /* Because a method has already been called, why not call
7767 * another one. */
7768 const char *insn = rb_insns_name(i);
7769 VALUE inspection = rb_inspect(c);
7770 const char *str = StringValueCStr(inspection);
7771
7772 rb_bug("dead canary found at %s: %s", insn, str);
7773}
7774
7775#else
7776void Init_vm_stack_canary(void) { /* nothing to do */ }
7777#endif
7778
7779
7780/* a part of the following code is generated by this ruby script:
7781
778216.times{|i|
7783 typedef_args = (0...i).map{|j| "VALUE v#{j+1}"}.join(", ")
7784 typedef_args.prepend(", ") if i != 0
7785 call_args = (0...i).map{|j| "argv[#{j}]"}.join(", ")
7786 call_args.prepend(", ") if i != 0
7787 puts %Q{
7788static VALUE
7789builtin_invoker#{i}(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7790{
7791 typedef VALUE (*rb_invoke_funcptr#{i}_t)(rb_execution_context_t *ec, VALUE self#{typedef_args});
7792 return (*(rb_invoke_funcptr#{i}_t)funcptr)(ec, self#{call_args});
7793}}
7794}
7795
7796puts
7797puts "static VALUE (* const cfunc_invokers[])(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr) = {"
779816.times{|i|
7799 puts " builtin_invoker#{i},"
7800}
7801puts "};"
7802*/
7803
7804static VALUE
7805builtin_invoker0(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7806{
7807 typedef VALUE (*rb_invoke_funcptr0_t)(rb_execution_context_t *ec, VALUE self);
7808 return (*(rb_invoke_funcptr0_t)funcptr)(ec, self);
7809}
7810
7811static VALUE
7812builtin_invoker1(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7813{
7814 typedef VALUE (*rb_invoke_funcptr1_t)(rb_execution_context_t *ec, VALUE self, VALUE v1);
7815 return (*(rb_invoke_funcptr1_t)funcptr)(ec, self, argv[0]);
7816}
7817
7818static VALUE
7819builtin_invoker2(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7820{
7821 typedef VALUE (*rb_invoke_funcptr2_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2);
7822 return (*(rb_invoke_funcptr2_t)funcptr)(ec, self, argv[0], argv[1]);
7823}
7824
7825static VALUE
7826builtin_invoker3(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7827{
7828 typedef VALUE (*rb_invoke_funcptr3_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3);
7829 return (*(rb_invoke_funcptr3_t)funcptr)(ec, self, argv[0], argv[1], argv[2]);
7830}
7831
7832static VALUE
7833builtin_invoker4(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7834{
7835 typedef VALUE (*rb_invoke_funcptr4_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4);
7836 return (*(rb_invoke_funcptr4_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3]);
7837}
7838
7839static VALUE
7840builtin_invoker5(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7841{
7842 typedef VALUE (*rb_invoke_funcptr5_t)(rb_execution_context_t *ec, VALUE self, VALUE v1, VALUE v2, VALUE v3, VALUE v4, VALUE v5);
7843 return (*(rb_invoke_funcptr5_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4]);
7844}
7845
7846static VALUE
7847builtin_invoker6(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7848{
7849 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);
7850 return (*(rb_invoke_funcptr6_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
7851}
7852
7853static VALUE
7854builtin_invoker7(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7855{
7856 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);
7857 return (*(rb_invoke_funcptr7_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
7858}
7859
7860static VALUE
7861builtin_invoker8(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7862{
7863 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);
7864 return (*(rb_invoke_funcptr8_t)funcptr)(ec, self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
7865}
7866
7867static VALUE
7868builtin_invoker9(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7869{
7870 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);
7871 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]);
7872}
7873
7874static VALUE
7875builtin_invoker10(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7876{
7877 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);
7878 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]);
7879}
7880
7881static VALUE
7882builtin_invoker11(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7883{
7884 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);
7885 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]);
7886}
7887
7888static VALUE
7889builtin_invoker12(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7890{
7891 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);
7892 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]);
7893}
7894
7895static VALUE
7896builtin_invoker13(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7897{
7898 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);
7899 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]);
7900}
7901
7902static VALUE
7903builtin_invoker14(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7904{
7905 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);
7906 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]);
7907}
7908
7909static VALUE
7910builtin_invoker15(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr)
7911{
7912 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);
7913 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]);
7914}
7915
7916typedef VALUE (*builtin_invoker)(rb_execution_context_t *ec, VALUE self, const VALUE *argv, rb_insn_func_t funcptr);
7917
7918static builtin_invoker
7919lookup_builtin_invoker(int argc)
7920{
7921 static const builtin_invoker invokers[] = {
7922 builtin_invoker0,
7923 builtin_invoker1,
7924 builtin_invoker2,
7925 builtin_invoker3,
7926 builtin_invoker4,
7927 builtin_invoker5,
7928 builtin_invoker6,
7929 builtin_invoker7,
7930 builtin_invoker8,
7931 builtin_invoker9,
7932 builtin_invoker10,
7933 builtin_invoker11,
7934 builtin_invoker12,
7935 builtin_invoker13,
7936 builtin_invoker14,
7937 builtin_invoker15,
7938 };
7939
7940 return invokers[argc];
7941}
7942
7943static inline VALUE
7944invoke_bf(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const struct rb_builtin_function* bf, const VALUE *argv)
7945{
7946 const bool canary_p = ISEQ_BODY(CFP_ISEQ(reg_cfp))->builtin_attrs & BUILTIN_ATTR_LEAF; // Verify an assumption of `Primitive.attr! :leaf`
7947 SETUP_CANARY(canary_p);
7948 rb_insn_func_t func_ptr = (rb_insn_func_t)(uintptr_t)bf->func_ptr;
7949 VALUE ret = (*lookup_builtin_invoker(bf->argc))(ec, reg_cfp->self, argv, func_ptr);
7950 CHECK_CANARY(canary_p, BIN(invokebuiltin));
7951 return ret;
7952}
7953
7954static VALUE
7955vm_invoke_builtin(rb_execution_context_t *ec, rb_control_frame_t *cfp, const struct rb_builtin_function* bf, const VALUE *argv)
7956{
7957 return invoke_bf(ec, cfp, bf, argv);
7958}
7959
7960static VALUE
7961vm_invoke_builtin_delegate(rb_execution_context_t *ec, rb_control_frame_t *cfp, const struct rb_builtin_function *bf, unsigned int start_index)
7962{
7963 if (0) { // debug print
7964 fputs("vm_invoke_builtin_delegate: passing -> ", stderr);
7965 for (int i=0; i<bf->argc; i++) {
7966 ruby_debug_printf(":%s ", rb_id2name(ISEQ_BODY(CFP_ISEQ(cfp))->local_table[i+start_index]));
7967 }
7968 ruby_debug_printf("\n" "%s %s(%d):%p\n", RUBY_FUNCTION_NAME_STRING, bf->name, bf->argc,
7969 (void *)(uintptr_t)bf->func_ptr);
7970 }
7971
7972 if (bf->argc == 0) {
7973 return invoke_bf(ec, cfp, bf, NULL);
7974 }
7975 else {
7976 const VALUE *argv = cfp->ep - ISEQ_BODY(CFP_ISEQ(cfp))->local_table_size - VM_ENV_DATA_SIZE + 1 + start_index;
7977 return invoke_bf(ec, cfp, bf, argv);
7978 }
7979}
7980
7981// for __builtin_inline!()
7982
7983VALUE
7984rb_vm_lvar_exposed(rb_execution_context_t *ec, int index)
7985{
7986 const rb_control_frame_t *cfp = ec->cfp;
7987 return cfp->ep[index];
7988}
#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:74
#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:73
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:115
#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:75
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:3074
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1678
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition class.c:1578
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
Definition class.c:1557
#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:679
#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:692
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:3635
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:5714
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:2740
VALUE rb_reg_match(VALUE re, VALUE str)
This is the match operator.
Definition re.c:4829
VALUE rb_reg_nth_match(int n, VALUE md)
Queries the nth captured substring.
Definition re.c:2716
VALUE rb_reg_match_post(VALUE md)
The portion of the original string after the given match.
Definition re.c:2798
VALUE rb_reg_nth_defined(int n, VALUE md)
Identical to rb_reg_nth_match(), except it just returns Boolean.
Definition re.c:2700
VALUE rb_reg_match_pre(VALUE md)
The portion of the original string before the given match.
Definition re.c:2766
VALUE rb_reg_match_last(VALUE md)
The portion of the original string that captured at the very last.
Definition re.c:2827
bool rb_set_lookup(VALUE set, VALUE element)
Whether the set contains the given element.
Definition set.c:2389
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:4001
VALUE rb_str_succ(VALUE orig)
Searches for the "successor" of a string.
Definition string.c:5741
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:3965
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:4250
#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:2532
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:1098
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:1303
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:1162
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:369
Definition vm_core.h:250
Definition method.h:63
Internal header for Class.
Definition class.h:31
Storage for the once instruction (see vm_once_dispatch).
Definition imemo.h:112
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:90
SVAR (Special VARiable).
Definition imemo.h:55
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition imemo.h:57
THROW_DATA.
Definition imemo.h:64
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.
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