1 #ifndef RUBY_VM_CALLINFO_H
2 #define RUBY_VM_CALLINFO_H
11 #include "debug_counter.h"
12 #include "internal/class.h"
15 enum vm_call_flag_bits {
16 VM_CALL_ARGS_SPLAT_bit,
17 VM_CALL_ARGS_BLOCKARG_bit,
20 VM_CALL_ARGS_SIMPLE_bit,
27 VM_CALL_KW_SPLAT_MUT_bit,
28 VM_CALL_ARGS_SPLAT_MUT_bit,
29 VM_CALL_FORWARDING_bit,
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)
55 rb_callinfo_kwarg_bytes(
int keyword_len)
57 return rb_size_mul_add_or_raise(
73 #if !defined(USE_EMBED_CI) || (USE_EMBED_CI+0)
75 #define USE_EMBED_CI 1
78 #define USE_EMBED_CI 0
82 #define CI_EMBED_TAG_bits 1
83 #define CI_EMBED_ARGC_bits 15
84 #define CI_EMBED_FLAG_bits 16
85 #define CI_EMBED_ID_bits 32
86 #elif SIZEOF_VALUE == 4
87 #define CI_EMBED_TAG_bits 1
88 #define CI_EMBED_ARGC_bits 3
89 #define CI_EMBED_FLAG_bits 13
90 #define CI_EMBED_ID_bits 15
93 #if (CI_EMBED_TAG_bits + CI_EMBED_ARGC_bits + CI_EMBED_FLAG_bits + CI_EMBED_ID_bits) != (SIZEOF_VALUE * 8)
97 #define CI_EMBED_FLAG 0x01
98 #define CI_EMBED_ARGC_SHFT (CI_EMBED_TAG_bits)
99 #define CI_EMBED_ARGC_MASK ((((VALUE)1)<<CI_EMBED_ARGC_bits) - 1)
100 #define CI_EMBED_FLAG_SHFT (CI_EMBED_TAG_bits + CI_EMBED_ARGC_bits)
101 #define CI_EMBED_FLAG_MASK ((((VALUE)1)<<CI_EMBED_FLAG_bits) - 1)
102 #define CI_EMBED_ID_SHFT (CI_EMBED_TAG_bits + CI_EMBED_ARGC_bits + CI_EMBED_FLAG_bits)
103 #define CI_EMBED_ID_MASK ((((VALUE)1)<<CI_EMBED_ID_bits) - 1)
111 if (LIKELY(((
VALUE)ci) & 0x01)) {
115 VM_ASSERT(IMEMO_TYPE_P(ci, imemo_callinfo));
123 if (vm_ci_packed_p(ci) || IMEMO_TYPE_P(ci, imemo_callinfo)) {
134 if (vm_ci_packed_p(ci)) {
135 return (((
VALUE)ci) >> CI_EMBED_ID_SHFT) & CI_EMBED_ID_MASK;
142 static inline unsigned int
145 if (vm_ci_packed_p(ci)) {
146 return (
unsigned int)((((
VALUE)ci) >> CI_EMBED_FLAG_SHFT) & CI_EMBED_FLAG_MASK);
149 return (
unsigned int)ci->flag;
153 static inline unsigned int
156 if (vm_ci_packed_p(ci)) {
157 return (
unsigned int)((((
VALUE)ci) >> CI_EMBED_ARGC_SHFT) & CI_EMBED_ARGC_MASK);
160 return (
unsigned int)ci->argc;
167 if (vm_ci_packed_p(ci)) {
178 if (vm_ci_packed_p(ci)) {
179 ruby_debug_printf(
"packed_ci ID:%s flag:%x argc:%u\n",
180 rb_id2name(vm_ci_mid(ci)), vm_ci_flag(ci), vm_ci_argc(ci));
187 #define vm_ci_new(mid, flag, argc, kwarg) vm_ci_new_(mid, flag, argc, kwarg, __FILE__, __LINE__)
188 #define vm_ci_new_runtime(mid, flag, argc, kwarg) vm_ci_new_runtime_(mid, flag, argc, kwarg, __FILE__, __LINE__)
191 #define VM_CI_EMBEDDABLE_P(mid, flag, argc, kwarg) \
192 (((mid ) & ~CI_EMBED_ID_MASK) ? false : \
193 ((flag) & ~CI_EMBED_FLAG_MASK) ? false : \
194 ((argc) & ~CI_EMBED_ARGC_MASK) ? false : \
195 (kwarg) ? false : true)
197 #define vm_ci_new_id(mid, flag, argc, must_zero) \
198 ((const struct rb_callinfo *) \
199 ((((VALUE)(mid )) << CI_EMBED_ID_SHFT) | \
200 (((VALUE)(flag)) << CI_EMBED_FLAG_SHFT) | \
201 (((VALUE)(argc)) << CI_EMBED_ARGC_SHFT) | \
209 vm_ci_new_(
ID mid,
unsigned int flag,
unsigned int argc,
const struct rb_callinfo_kwarg *kwarg,
const char *file,
int line)
211 if (USE_EMBED_CI && VM_CI_EMBEDDABLE_P(mid, flag, argc, kwarg)) {
212 RB_DEBUG_COUNTER_INC(ci_packed);
213 return vm_ci_new_id(mid, flag, argc, kwarg);
216 const bool debug = 0;
217 if (debug) ruby_debug_printf(
"%s:%d ", file, line);
219 const struct rb_callinfo *ci = rb_vm_ci_lookup(mid, flag, argc, kwarg);
223 RB_DEBUG_COUNTER_INC(ci_kw);
226 RB_DEBUG_COUNTER_INC(ci_nokw);
229 VM_ASSERT(vm_ci_flag(ci) == flag);
230 VM_ASSERT(vm_ci_argc(ci) == argc);
237 vm_ci_new_runtime_(
ID mid,
unsigned int flag,
unsigned int argc,
const struct rb_callinfo_kwarg *kwarg,
const char *file,
int line)
239 RB_DEBUG_COUNTER_INC(ci_runtime);
240 return vm_ci_new_(mid, flag, argc, kwarg, file, line);
243 #define VM_CALLINFO_NOT_UNDER_GC IMEMO_FL_USER0
251 else if (vm_ci_packed_p(ci)) {
255 VM_ASSERT(IMEMO_TYPE_P(ci, imemo_callinfo));
260 #define VM_CI_ON_STACK(mid_, flags_, argc_, kwarg_) \
261 (struct rb_callinfo) { \
263 (imemo_callinfo << FL_USHIFT) | \
264 VM_CALLINFO_NOT_UNDER_GC, \
271 typedef VALUE (*vm_call_handler)(
288 const vm_call_handler call_;
294 const enum method_missing_reason method_missing_reason;
300 #define VM_CALLCACHE_UNMARKABLE FL_FREEZE
301 #define VM_CALLCACHE_ON_STACK FL_EXIVAR
304 #define VM_CALLCACHE_IVAR IMEMO_FL_USER0
305 #define VM_CALLCACHE_BF IMEMO_FL_USER1
306 #define VM_CALLCACHE_SUPER IMEMO_FL_USER2
307 #define VM_CALLCACHE_REFINEMENT IMEMO_FL_USER3
316 extern const struct rb_callcache *rb_vm_empty_cc_for_super(
void);
318 #define vm_cc_empty() rb_vm_empty_cc()
320 static inline void vm_cc_attr_index_set(
const struct rb_callcache *cc, attr_index_t index, shape_id_t dest_shape_id);
323 vm_cc_attr_index_initialize(
const struct rb_callcache *cc, shape_id_t shape_id)
325 vm_cc_attr_index_set(cc, (attr_index_t)-1, shape_id);
329 vm_cc_new(
VALUE klass,
331 vm_call_handler call,
332 enum vm_cc_type
type)
336 *((vm_call_handler *)&cc->call_) = call;
344 *(
VALUE *)&cc->flags |= VM_CALLCACHE_SUPER;
346 case cc_type_refinement:
347 *(
VALUE *)&cc->flags |= VM_CALLCACHE_REFINEMENT;
351 if (cme->def->type == VM_METHOD_TYPE_ATTRSET || cme->def->type == VM_METHOD_TYPE_IVAR) {
352 vm_cc_attr_index_initialize(cc, INVALID_SHAPE_ID);
355 RB_DEBUG_COUNTER_INC(cc_new);
362 return (cc->flags & VM_CALLCACHE_SUPER) != 0;
368 return (cc->flags & VM_CALLCACHE_REFINEMENT) != 0;
371 #define VM_CC_ON_STACK(clazz, call, aux, cme) \
372 (struct rb_callcache) { \
374 (imemo_callcache << FL_USHIFT) | \
375 VM_CALLCACHE_UNMARKABLE | \
376 VM_CALLCACHE_ON_STACK, \
386 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
387 VM_ASSERT(cc->klass == 0 ||
389 return cc->klass == klass;
395 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
402 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
403 VM_ASSERT(cc->call_ == NULL ||
404 !vm_cc_markable(cc) ||
410 static inline vm_call_handler
413 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
414 VM_ASSERT(cc->call_ != NULL);
418 static inline attr_index_t
421 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
422 return (attr_index_t)((cc->aux_.attr.value & SHAPE_FLAG_MASK) - 1);
425 static inline shape_id_t
426 vm_cc_attr_index_dest_shape_id(
const struct rb_callcache *cc)
428 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
430 return cc->aux_.attr.value >> SHAPE_FLAG_SHIFT;
434 vm_cc_atomic_shape_and_index(
const struct rb_callcache *cc, shape_id_t * shape_id, attr_index_t * index)
436 uintptr_t cache_value = cc->aux_.attr.value;
437 *shape_id = (shape_id_t)(cache_value >> SHAPE_FLAG_SHIFT);
438 *index = (attr_index_t)(cache_value & SHAPE_FLAG_MASK) - 1;
445 uintptr_t cache_value = ic->value;
446 *shape_id = (shape_id_t)(cache_value >> SHAPE_FLAG_SHIFT);
447 *index = (attr_index_t)(cache_value & SHAPE_FLAG_MASK) - 1;
451 static inline shape_id_t
454 return (shape_id_t)(ic->value >> SHAPE_FLAG_SHIFT);
457 static inline unsigned int
458 vm_cc_cmethod_missing_reason(
const struct rb_callcache *cc)
460 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
461 return cc->aux_.method_missing_reason;
467 if (cc->klass && !METHOD_ENTRY_INVALIDATED(vm_cc_cme(cc))) {
479 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
480 if (cc->klass == klass && !METHOD_ENTRY_INVALIDATED(cc_cme)) {
491 vm_cc_call_set(
const struct rb_callcache *cc, vm_call_handler call)
493 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
494 VM_ASSERT(cc != vm_cc_empty());
495 *(vm_call_handler *)&cc->call_ = call;
501 *(
VALUE *)&cc->flags |= VM_CALLCACHE_IVAR;
505 vm_cc_attr_index_set(
const struct rb_callcache *cc, attr_index_t index, shape_id_t dest_shape_id)
507 uintptr_t *attr_value = (uintptr_t *)&cc->aux_.attr.value;
508 if (!vm_cc_markable(cc)) {
509 *attr_value = (uintptr_t)INVALID_SHAPE_ID << SHAPE_FLAG_SHIFT;
512 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
513 VM_ASSERT(cc != vm_cc_empty());
514 *attr_value = (attr_index_t)(index + 1) | ((uintptr_t)(dest_shape_id) << SHAPE_FLAG_SHIFT);
521 return (cc->flags & VM_CALLCACHE_IVAR) != 0;
527 *(uintptr_t *)&ic->value = ((uintptr_t)dest_shape_id << SHAPE_FLAG_SHIFT) | (attr_index_t)(index + 1);
533 *(uintptr_t *)&ic->value = (uintptr_t)shape_id << SHAPE_FLAG_SHIFT;
537 vm_cc_method_missing_reason_set(
const struct rb_callcache *cc,
enum method_missing_reason reason)
539 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
540 VM_ASSERT(cc != vm_cc_empty());
541 *(
enum method_missing_reason *)&cc->aux_.method_missing_reason = reason;
547 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
548 VM_ASSERT(cc != vm_cc_empty());
550 *(
VALUE *)&cc->flags |= VM_CALLCACHE_BF;
556 return (cc->flags & VM_CALLCACHE_BF) != 0;
562 VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
563 VM_ASSERT(cc != vm_cc_empty());
564 VM_ASSERT(cc->klass != 0);
566 *(
VALUE *)&cc->klass = 0;
567 RB_DEBUG_COUNTER_INC(cc_ent_invalidate);
578 #if VM_CHECK_MODE > 0
591 #if VM_CHECK_MODE > 0
594 void rb_vm_dump_overloaded_cme_table(
void);
599 return ccs->debug_sig == ~(
VALUE)ccs;
605 if (vm_cc_cme(cc) == cme ||
606 (cme->def->iseq_overload && vm_cc_cme(cc) == rb_vm_lookup_overloaded_cme(cme))) {
613 fprintf(stderr,
"iseq_overload:%d\n", (
int)cme->def->iseq_overload);
616 rb_vm_lookup_overloaded_cme(cme);
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define FL_ANY_RAW
Old name of RB_FL_ANY_RAW.
#define T_CLASS
Old name of RUBY_T_CLASS.
VALUE rb_eRuntimeError
RuntimeError exception.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
VALUE type(ANYARGS)
ANYARGS-ed function type.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.