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