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