Ruby 4.1.0dev (2026-11-18 revision e5e6cd45db456b2a4d368f2a34598641319df630)
vm_callinfo.h (e5e6cd45db456b2a4d368f2a34598641319df630)
1#ifndef RUBY_VM_CALLINFO_H /*-*-C-*-vi:se ft=c:*/
2#define RUBY_VM_CALLINFO_H
10
11#include "debug_counter.h"
12#include "internal/class.h"
13#include "shape.h"
14
15enum vm_call_flag_bits {
16 VM_CALL_ARGS_SPLAT_bit, // m(*args)
17 VM_CALL_ARGS_BLOCKARG_bit, // m(&block)
18 VM_CALL_FCALL_bit, // m(args) # receiver is self
19 VM_CALL_VCALL_bit, // m # method call that looks like a local variable
20 VM_CALL_ARGS_SIMPLE_bit, // !(ci->flag & (SPLAT|BLOCKARG|KWARG|KW_SPLAT|FORWARDING)) && !has_block_iseq
21 VM_CALL_KWARG_bit, // has kwarg
22 VM_CALL_KW_SPLAT_bit, // m(**opts)
23 VM_CALL_TAILCALL_bit, // located at tail position
24 VM_CALL_SUPER_bit, // super
25 VM_CALL_ZSUPER_bit, // zsuper
26 VM_CALL_OPT_SEND_bit, // internal flag
27 VM_CALL_KW_SPLAT_MUT_bit, // kw splat hash can be modified (to avoid allocating a new one)
28 VM_CALL_ARGS_SPLAT_MUT_bit, // args splat can be modified (to avoid allocating a new one)
29 VM_CALL_FORWARDING_bit, // m(...)
30 VM_CALL_IGNORE_REFINEMENTS_bit, // internal flag: ignore the caller's refinements (initialize call of opt_new)
31 VM_CALL__END
32};
33
34#define VM_CALL_ARGS_SPLAT (0x01 << VM_CALL_ARGS_SPLAT_bit)
35#define VM_CALL_ARGS_BLOCKARG (0x01 << VM_CALL_ARGS_BLOCKARG_bit)
36#define VM_CALL_FCALL (0x01 << VM_CALL_FCALL_bit)
37#define VM_CALL_VCALL (0x01 << VM_CALL_VCALL_bit)
38#define VM_CALL_ARGS_SIMPLE (0x01 << VM_CALL_ARGS_SIMPLE_bit)
39#define VM_CALL_KWARG (0x01 << VM_CALL_KWARG_bit)
40#define VM_CALL_KW_SPLAT (0x01 << VM_CALL_KW_SPLAT_bit)
41#define VM_CALL_TAILCALL (0x01 << VM_CALL_TAILCALL_bit)
42#define VM_CALL_SUPER (0x01 << VM_CALL_SUPER_bit)
43#define VM_CALL_ZSUPER (0x01 << VM_CALL_ZSUPER_bit)
44#define VM_CALL_OPT_SEND (0x01 << VM_CALL_OPT_SEND_bit)
45#define VM_CALL_KW_SPLAT_MUT (0x01 << VM_CALL_KW_SPLAT_MUT_bit)
46#define VM_CALL_ARGS_SPLAT_MUT (0x01 << VM_CALL_ARGS_SPLAT_MUT_bit)
47#define VM_CALL_FORWARDING (0x01 << VM_CALL_FORWARDING_bit)
48#define VM_CALL_IGNORE_REFINEMENTS (0x01 << VM_CALL_IGNORE_REFINEMENTS_bit)
49
51 int keyword_len;
52 rb_atomic_t references;
53 VALUE keywords[];
54};
55
56static inline size_t
57rb_callinfo_kwarg_bytes(int keyword_len)
58{
59 return rb_size_mul_add_or_raise(
60 keyword_len,
61 sizeof(VALUE),
62 sizeof(struct rb_callinfo_kwarg),
64}
65
66static inline void
67rb_callinfo_kwarg_retain(struct rb_callinfo_kwarg *kwarg)
68{
69 if (kwarg) RUBY_ATOMIC_INC(kwarg->references);
70}
71
72static inline void
73rb_callinfo_kwarg_release(struct rb_callinfo_kwarg *kwarg)
74{
75 if (kwarg && RUBY_ATOMIC_FETCH_SUB(kwarg->references, 1) == 1) {
76 ruby_xfree_sized(kwarg, rb_callinfo_kwarg_bytes(kwarg->keyword_len));
77 }
78}
79
80// imemo_callinfo
82 VALUE flags;
83 const struct rb_callinfo_kwarg *kwarg;
84 VALUE mid;
85 unsigned int flag;
86 unsigned int argc;
87};
88
89#if !defined(USE_EMBED_CI) || (USE_EMBED_CI+0)
90#undef USE_EMBED_CI
91#define USE_EMBED_CI 1
92#else
93#undef USE_EMBED_CI
94#define USE_EMBED_CI 0
95#endif
96
97#if SIZEOF_VALUE == 8
98#define CI_EMBED_TAG_bits 1
99#define CI_EMBED_ARGC_bits 15
100#define CI_EMBED_FLAG_bits 16
101#define CI_EMBED_ID_bits 32
102#elif SIZEOF_VALUE == 4
103#define CI_EMBED_TAG_bits 1
104#define CI_EMBED_ARGC_bits 3
105#define CI_EMBED_FLAG_bits 13
106#define CI_EMBED_ID_bits 15
107#endif
108
109#if (CI_EMBED_TAG_bits + CI_EMBED_ARGC_bits + CI_EMBED_FLAG_bits + CI_EMBED_ID_bits) != (SIZEOF_VALUE * 8)
110#error
111#endif
112
113#define CI_EMBED_FLAG 0x01
114#define CI_EMBED_ARGC_SHFT (CI_EMBED_TAG_bits)
115#define CI_EMBED_ARGC_MASK ((((VALUE)1)<<CI_EMBED_ARGC_bits) - 1)
116#define CI_EMBED_FLAG_SHFT (CI_EMBED_TAG_bits + CI_EMBED_ARGC_bits)
117#define CI_EMBED_FLAG_MASK ((((VALUE)1)<<CI_EMBED_FLAG_bits) - 1)
118#define CI_EMBED_ID_SHFT (CI_EMBED_TAG_bits + CI_EMBED_ARGC_bits + CI_EMBED_FLAG_bits)
119#define CI_EMBED_ID_MASK ((((VALUE)1)<<CI_EMBED_ID_bits) - 1)
120
121static inline bool
122vm_ci_packed_p(const struct rb_callinfo *ci)
123{
124 if (!USE_EMBED_CI) {
125 return 0;
126 }
127 if (LIKELY(((VALUE)ci) & 0x01)) {
128 return 1;
129 }
130 else {
131 VM_ASSERT(IMEMO_TYPE_P(ci, imemo_callinfo));
132 return 0;
133 }
134}
135
136static inline bool
137vm_ci_p(const struct rb_callinfo *ci)
138{
139 if (vm_ci_packed_p(ci) || IMEMO_TYPE_P(ci, imemo_callinfo)) {
140 return 1;
141 }
142 else {
143 return 0;
144 }
145}
146
147static inline ID
148vm_ci_mid(const struct rb_callinfo *ci)
149{
150 if (vm_ci_packed_p(ci)) {
151 return (((VALUE)ci) >> CI_EMBED_ID_SHFT) & CI_EMBED_ID_MASK;
152 }
153 else {
154 return (ID)ci->mid;
155 }
156}
157
158static inline unsigned int
159vm_ci_flag(const struct rb_callinfo *ci)
160{
161 if (vm_ci_packed_p(ci)) {
162 return (unsigned int)((((VALUE)ci) >> CI_EMBED_FLAG_SHFT) & CI_EMBED_FLAG_MASK);
163 }
164 else {
165 return ci->flag;
166 }
167}
168
169static inline unsigned int
170vm_ci_argc(const struct rb_callinfo *ci)
171{
172 if (vm_ci_packed_p(ci)) {
173 return (unsigned int)((((VALUE)ci) >> CI_EMBED_ARGC_SHFT) & CI_EMBED_ARGC_MASK);
174 }
175 else {
176 return ci->argc;
177 }
178}
179
180static inline const struct rb_callinfo_kwarg *
181vm_ci_kwarg(const struct rb_callinfo *ci)
182{
183 if (vm_ci_packed_p(ci)) {
184 return NULL;
185 }
186 else {
187 return ci->kwarg;
188 }
189}
190
191static inline void
192vm_ci_dump(const struct rb_callinfo *ci)
193{
194 if (vm_ci_packed_p(ci)) {
195 ruby_debug_printf("packed_ci ID:%s flag:%x argc:%u\n",
196 rb_id2name(vm_ci_mid(ci)), vm_ci_flag(ci), vm_ci_argc(ci));
197 }
198 else {
199 rp(ci);
200 }
201}
202
203#define vm_ci_new(mid, flag, argc, kwarg) vm_ci_new_(mid, flag, argc, kwarg, __FILE__, __LINE__)
204#define vm_ci_new_runtime(mid, flag, argc, kwarg) vm_ci_new_runtime_(mid, flag, argc, kwarg, __FILE__, __LINE__)
205
206/* This is passed to STATIC_ASSERT. Cannot be an inline function. */
207#define VM_CI_EMBEDDABLE_P(mid, flag, argc, kwarg) \
208 (((mid ) & ~CI_EMBED_ID_MASK) ? false : \
209 ((flag) & ~CI_EMBED_FLAG_MASK) ? false : \
210 ((argc) & ~CI_EMBED_ARGC_MASK) ? false : \
211 (kwarg) ? false : true)
212
213#define vm_ci_new_id(mid, flag, argc, must_zero) \
214 ((const struct rb_callinfo *) \
215 ((((VALUE)(mid )) << CI_EMBED_ID_SHFT) | \
216 (((VALUE)(flag)) << CI_EMBED_FLAG_SHFT) | \
217 (((VALUE)(argc)) << CI_EMBED_ARGC_SHFT) | \
218 RUBY_FIXNUM_FLAG))
219
220// vm_method.c
221const struct rb_callinfo *rb_vm_ci_lookup(ID mid, unsigned int flag, unsigned int argc, const struct rb_callinfo_kwarg *kwarg);
222void rb_vm_ci_free(const struct rb_callinfo *);
223
224static inline const struct rb_callinfo *
225vm_ci_new_(ID mid, unsigned int flag, unsigned int argc, const struct rb_callinfo_kwarg *kwarg, const char *file, int line)
226{
227 if (USE_EMBED_CI && VM_CI_EMBEDDABLE_P(mid, flag, argc, kwarg)) {
228 RB_DEBUG_COUNTER_INC(ci_packed);
229 return vm_ci_new_id(mid, flag, argc, kwarg);
230 }
231
232 const bool debug = 0;
233 if (debug) ruby_debug_printf("%s:%d ", file, line);
234
235 const struct rb_callinfo *ci = rb_vm_ci_lookup(mid, flag, argc, kwarg);
236
237 if (debug) rp(ci);
238 if (kwarg) {
239 RB_DEBUG_COUNTER_INC(ci_kw);
240 }
241 else {
242 RB_DEBUG_COUNTER_INC(ci_nokw);
243 }
244
245 VM_ASSERT(vm_ci_flag(ci) == flag);
246 VM_ASSERT(vm_ci_argc(ci) == argc);
247
248 return ci;
249}
250
251
252static inline const struct rb_callinfo *
253vm_ci_new_runtime_(ID mid, unsigned int flag, unsigned int argc, const struct rb_callinfo_kwarg *kwarg, const char *file, int line)
254{
255 RB_DEBUG_COUNTER_INC(ci_runtime);
256 return vm_ci_new_(mid, flag, argc, kwarg, file, line);
257}
258
259#define VM_CALLINFO_NOT_UNDER_GC IMEMO_FL_USER0
260
261static inline bool
262vm_ci_markable(const struct rb_callinfo *ci)
263{
264 if (! ci) {
265 return false; /* or true? This is Qfalse... */
266 }
267 else if (vm_ci_packed_p(ci)) {
268 return true;
269 }
270 else {
271 VM_ASSERT(IMEMO_TYPE_P(ci, imemo_callinfo));
272 return ! FL_ANY_RAW((VALUE)ci, VM_CALLINFO_NOT_UNDER_GC);
273 }
274}
275
276#define VM_CI_ON_STACK(mid_, flags_, argc_, kwarg_) \
277 (struct rb_callinfo) { \
278 .flags = T_IMEMO | \
279 (imemo_callinfo << FL_USHIFT) | \
280 VM_CALLINFO_NOT_UNDER_GC, \
281 .mid = mid_, \
282 .flag = flags_, \
283 .argc = argc_, \
284 .kwarg = kwarg_, \
285 }
286
287typedef VALUE (*vm_call_handler)(
289 struct rb_control_frame_struct *cfp,
290 struct rb_calling_info *calling);
291
292// imemo_callcache
293
295 const VALUE flags;
296
297 /* inline cache: key */
298 const VALUE klass; // Weak reference. When klass is collected, `cc->klass = Qundef`.
299
300 /* inline cache: values */
301 const struct rb_callable_method_entry_struct * const cme_;
302 const vm_call_handler call_;
303
304 union {
305 struct {
306 uint64_t value; // Shape ID in former half, index in latter half
307 } attr;
308 const enum method_missing_reason method_missing_reason; /* used by method_missing */
309 VALUE v;
310 const struct rb_builtin_function *bf;
311 } aux_;
312};
313
314/* VM_CALLCACHE_IVAR used for IVAR/ATTRSET/STRUCT_AREF/STRUCT_ASET methods */
315#define VM_CALLCACHE_IVAR IMEMO_FL_USER0
316#define VM_CALLCACHE_BF IMEMO_FL_USER1
317#define VM_CALLCACHE_SUPER IMEMO_FL_USER2
318#define VM_CALLCACHE_REFINEMENT IMEMO_FL_USER3
319#define VM_CALLCACHE_UNMARKABLE IMEMO_FL_USER4
320#define VM_CALLCACHE_ON_STACK IMEMO_FL_USER5
321#define VM_CALLCACHE_INVALID_SUPER IMEMO_FL_USER6
322
323enum vm_cc_type {
324 cc_type_normal, // chained from ccs
325 cc_type_super,
326 cc_type_refinement,
327};
328
329extern const struct rb_callcache rb_vm_empty_cc;
330extern const struct rb_callcache rb_vm_empty_cc_for_super;
331extern const struct rb_callcache rb_vm_uncached_cc;
332
333#define vm_cc_empty() (&rb_vm_empty_cc)
334
335static inline VALUE
336cc_check_class(VALUE klass)
337{
338 VM_ASSERT(klass == Qundef || RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_ICLASS));
339 return klass;
340}
341
342VALUE rb_vm_cc_table_create(size_t capa);
343VALUE rb_vm_cc_table_dup(VALUE old_table);
344void rb_vm_cc_table_delete(VALUE table, ID mid);
345
346static inline void vm_cc_attr_index_set(const struct rb_callcache *cc, uint64_t packed_cache);
347
348static inline const struct rb_callcache *
349vm_cc_new(VALUE klass,
350 const struct rb_callable_method_entry_struct *cme,
351 vm_call_handler call,
352 enum vm_cc_type type)
353{
354 cc_check_class(klass);
355 struct rb_callcache *cc = SHAREABLE_IMEMO_NEW(struct rb_callcache, imemo_callcache, klass);
356 rb_gc_declare_weak_references((VALUE)cc);
357
358 *((struct rb_callable_method_entry_struct **)&cc->cme_) = (struct rb_callable_method_entry_struct *)cme;
359 *((vm_call_handler *)&cc->call_) = call;
360
361 switch (type) {
362 case cc_type_normal:
363 break;
364 case cc_type_super:
365 *(VALUE *)&cc->flags |= VM_CALLCACHE_SUPER;
366 break;
367 case cc_type_refinement:
368 *(VALUE *)&cc->flags |= VM_CALLCACHE_REFINEMENT;
369 rb_vm_insert_cc_refinement(cc);
370 break;
371 }
372
373 if (cme) {
374 if (cme->def->type == VM_METHOD_TYPE_ATTRSET) {
375 vm_cc_attr_index_set(cc, IVAR_CACHE_INIT);
376 }
377 else if (cme->def->type == VM_METHOD_TYPE_IVAR) {
378 vm_cc_attr_index_set(cc, rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET));
379 }
380 }
381 else {
382 *(VALUE *)&cc->flags |= VM_CALLCACHE_INVALID_SUPER;
383 }
384
385 RB_DEBUG_COUNTER_INC(cc_new);
386 return cc;
387}
388
389static inline bool
390vm_cc_super_p(const struct rb_callcache *cc)
391{
392 return (cc->flags & VM_CALLCACHE_SUPER) != 0;
393}
394
395static inline bool
396vm_cc_refinement_p(const struct rb_callcache *cc)
397{
398 return (cc->flags & VM_CALLCACHE_REFINEMENT) != 0;
399}
400
401#define VM_CC_ON_STACK(clazz, call, aux, cme) \
402 (struct rb_callcache) { \
403 .flags = T_IMEMO | \
404 (imemo_callcache << FL_USHIFT) | \
405 VM_CALLCACHE_UNMARKABLE | \
406 VM_CALLCACHE_ON_STACK, \
407 .klass = cc_check_class(clazz), \
408 .cme_ = cme, \
409 .call_ = call, \
410 .aux_ = aux, \
411 }
412
413static inline bool
414vm_cc_class_check(const struct rb_callcache *cc, VALUE klass)
415{
416 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
417 VM_ASSERT(cc_check_class(cc->klass));
418 return cc->klass == klass;
419}
420
421static inline int
422vm_cc_markable(const struct rb_callcache *cc)
423{
424 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
425 return FL_TEST_RAW((VALUE)cc, VM_CALLCACHE_UNMARKABLE) == 0;
426}
427
428static inline bool
429vm_cc_invalid_super(const struct rb_callcache *cc)
430{
431 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
432 // Set when calling super and there is no superclass.
433 return FL_TEST_RAW((VALUE)cc, VM_CALLCACHE_INVALID_SUPER);
434}
435
436static inline bool
437vm_cc_valid(const struct rb_callcache *cc)
438{
439 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
440 VM_ASSERT(cc_check_class(cc->klass));
441
442 return !UNDEF_P(cc->klass);
443}
444
445/* Whether another ractor may have invalidated this cc mid-use (see
446 * vm_cc_invalidate()). Extensions cannot use rb_multi_ractor_p() here: with
447 * assertions enabled at -O0 (e.g. --enable-yjit=dev on macOS) the inline
448 * materializes references to core-internal globals (ruby_current_vm_ptr,
449 * ruby_single_main_ractor) that are not exported to them, and objspace.bundle
450 * fails to load. RUBY_EXPORT marks a core TU. */
451#ifdef RUBY_EXPORT
452# define VM_CC_RACED_P() rb_multi_ractor_p()
453#else
454# define VM_CC_RACED_P() true
455#endif
456
457static inline const struct rb_callable_method_entry_struct *
458vm_cc_cme(const struct rb_callcache *cc)
459{
460 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
461 // VM_CC_RACED_P(): another ractor can invalidate this cc (klass = Qundef)
462 // while a call using it is in flight; cme_/call_ stay intact, so the call
463 // proceeds with the method resolved before the redefinition.
464 VM_ASSERT(cc->klass != Qundef || !vm_cc_markable(cc) || vm_cc_invalid_super(cc) || VM_CC_RACED_P());
465 VM_ASSERT(cc_check_class(cc->klass));
466 VM_ASSERT(cc->call_ == NULL || // not initialized yet
467 !vm_cc_markable(cc) ||
468 vm_cc_invalid_super(cc) ||
469 cc->cme_ != NULL);
470
471 return cc->cme_;
472}
473
474static inline vm_call_handler
475vm_cc_call(const struct rb_callcache *cc)
476{
477 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
478 VM_ASSERT(cc->call_ != NULL);
479 VM_ASSERT(cc->klass != Qundef || !vm_cc_markable(cc) || vm_cc_invalid_super(cc) || VM_CC_RACED_P());
480 VM_ASSERT(cc_check_class(cc->klass));
481 return cc->call_;
482}
483
484static inline uint64_t
485vm_cc_atomic_cache_read(const struct rb_callcache *cc)
486{
487 return ATOMIC_U64_LOAD_RELAXED(cc->aux_.attr.value);
488}
489
490static inline uint64_t
491vm_ic_atomic_cache_read(const struct iseq_inline_iv_cache_entry *ic)
492{
493 return ATOMIC_U64_LOAD_RELAXED(ic->value);
494}
495
496static inline uint64_t
497vm_cache_attr_index_atomic_read(bool is_attr, const struct iseq_inline_iv_cache_entry *ic, const struct rb_callcache *cc)
498{
499 if (is_attr) {
500 return vm_cc_atomic_cache_read(cc);
501 }
502 else {
503 return vm_ic_atomic_cache_read(ic);
504 }
505}
506
507static inline unsigned int
508vm_cc_cmethod_missing_reason(const struct rb_callcache *cc)
509{
510 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
511 return cc->aux_.method_missing_reason;
512}
513
514static inline bool
515vm_cc_invalidated_p(const struct rb_callcache *cc)
516{
517 if (vm_cc_valid(cc) && !METHOD_ENTRY_INVALIDATED(vm_cc_cme(cc))) {
518 return false;
519 }
520 else {
521 return true;
522 }
523}
524
525/* callcache: mutate */
526
527static inline void
528vm_cc_call_set(const struct rb_callcache *cc, vm_call_handler call)
529{
530 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
531 VM_ASSERT(cc != vm_cc_empty());
532 *(vm_call_handler *)&cc->call_ = call;
533}
534
535static inline void
536set_vm_cc_ivar(const struct rb_callcache *cc)
537{
538 *(VALUE *)&cc->flags |= VM_CALLCACHE_IVAR;
539}
540
541static inline bool
542vm_cc_ivar_p(const struct rb_callcache *cc)
543{
544 return (cc->flags & VM_CALLCACHE_IVAR) != 0;
545}
546
547static inline void
548vm_cc_attr_index_set(const struct rb_callcache *cc, uint64_t packed_cache)
549{
550 uint64_t *attr_value = (uint64_t *)&cc->aux_.attr.value;
551 if (!vm_cc_markable(cc)) {
552 *attr_value = IVAR_CACHE_INIT;
553 return;
554 }
555 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
556 VM_ASSERT(cc != vm_cc_empty());
557 *attr_value = packed_cache;
558 set_vm_cc_ivar(cc);
559}
560
561static inline void
562vm_cache_attr_index_set(bool is_attr, struct iseq_inline_iv_cache_entry *ic, const struct rb_callcache *cc, uint64_t packed_cache)
563{
564 if (is_attr) {
565 vm_cc_attr_index_set(cc, packed_cache);
566 }
567 else {
568 ATOMIC_U64_SET_RELAXED(ic->value, packed_cache);
569 }
570}
571
572static inline void
573vm_cc_method_missing_reason_set(const struct rb_callcache *cc, enum method_missing_reason reason)
574{
575 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
576 VM_ASSERT(cc != vm_cc_empty());
577 *(enum method_missing_reason *)&cc->aux_.method_missing_reason = reason;
578}
579
580static inline void
581vm_cc_bf_set(const struct rb_callcache *cc, const struct rb_builtin_function *bf)
582{
583 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
584 VM_ASSERT(cc != vm_cc_empty());
585 *(const struct rb_builtin_function **)&cc->aux_.bf = bf;
586 *(VALUE *)&cc->flags |= VM_CALLCACHE_BF;
587}
588
589static inline bool
590vm_cc_bf_p(const struct rb_callcache *cc)
591{
592 return (cc->flags & VM_CALLCACHE_BF) != 0;
593}
594
595static inline void
596vm_cc_invalidate(const struct rb_callcache *cc)
597{
598 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
599 VM_ASSERT(cc != vm_cc_empty());
600 // TODO: rb_multi_ractor_p() is a workaround to stabilize CI
601 VM_ASSERT(cc->klass != Qundef || rb_multi_ractor_p()); // should be enable
602
603 *(VALUE *)&cc->klass = Qundef;
604 RB_DEBUG_COUNTER_INC(cc_ent_invalidate);
605}
606
607/* calldata */
608
610 const struct rb_callinfo *ci;
611 const struct rb_callcache *cc;
612};
613
615#if VM_CHECK_MODE > 0
616 VALUE debug_sig;
617#endif
618 int capa;
619 int len;
620 const struct rb_callable_method_entry_struct *cme;
622 const struct rb_callcache *cc;
623 unsigned int argc;
624 unsigned short flag;
625 unsigned short kw_len;
626 } entries[FLEX_ARY_LEN];
627};
628
629/* entries[].flag is an unsigned short, so every VM_CALL flag bit must fit in 16 bits. */
630STATIC_ASSERT(cc_entries_flag_fits_in_short, VM_CALL__END <= 16);
631
632/* entries[].kw_len is an unsigned short, so a call site cannot carry, nor a method
633 declare, more keyword arguments than this. Enforced at compile time. */
634#define VM_CALL_KW_LEN_MAX UINT16_MAX
635
636static inline size_t
637vm_ccs_alloc_size(size_t capa)
638{
639 return offsetof(struct rb_class_cc_entries, entries) + (sizeof(struct rb_class_cc_entries_entry) * capa);
640}
641
642#if VM_CHECK_MODE > 0
643
644const rb_callable_method_entry_t *rb_vm_lookup_overloaded_cme(const rb_callable_method_entry_t *cme);
645void rb_vm_dump_overloaded_cme_table(void);
646
647static inline bool
648vm_ccs_p(const struct rb_class_cc_entries *ccs)
649{
650 return ccs->debug_sig == ~(VALUE)ccs;
651}
652
653static inline bool
654vm_cc_check_cme_unlocked(const struct rb_callcache *cc, const rb_callable_method_entry_t *cme)
655{
656 return vm_cc_cme(cc) == cme ||
657 (cme->def->iseq_overload && vm_cc_cme(cc) == rb_vm_lookup_overloaded_cme(cme));
658}
659
660static inline bool
661vm_cc_check_cme(const struct rb_callcache *cc, const rb_callable_method_entry_t *cme)
662{
663 bool valid;
664 if (rb_current_execution_context(false) == NULL) {
665 valid = vm_cc_check_cme_unlocked(cc, cme);
666 }
667 else {
668 RB_VM_LOCKING_NO_BARRIER() {
669 valid = vm_cc_check_cme_unlocked(cc, cme);
670 }
671 }
672 if (valid) {
673 return true;
674 }
675#if 1
676 // debug print
677
678 fprintf(stderr, "iseq_overload:%d, cme:%p (def:%p), cm_cc_cme(cc):%p (def:%p)\n",
679 (int)cme->def->iseq_overload,
680 (void *)cme, (void *)cme->def,
681 (void *)vm_cc_cme(cc), (void *)vm_cc_cme(cc)->def);
682 rp(cme);
683 rp(vm_cc_cme(cc));
684 rp(rb_vm_lookup_overloaded_cme(cme));
685#endif
686 return false;
687}
688
689#endif
690
691#endif /* RUBY_VM_CALLINFO_H */
#define RUBY_ATOMIC_INC(var)
Atomically increments the value pointed by var.
Definition atomic.h:214
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define RUBY_ATOMIC_FETCH_SUB(var, val)
Atomically replaces the value pointed by var with the result of subtraction of val to the old value o...
Definition atomic.h:129
#define Qundef
Old name of RUBY_Qundef.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define FL_ANY_RAW
Old name of RB_FL_ANY_RAW.
Definition fl_type.h:122
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1455
int capa
Designed capacity of the buffer.
Definition io.h:11
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition vm_core.h:292
Definition method.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
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