11 #define vm_exec rb_vm_exec
13 #include "eval_intern.h"
15 #include "internal/class.h"
16 #include "internal/compile.h"
17 #include "internal/cont.h"
18 #include "internal/error.h"
19 #include "internal/encoding.h"
20 #include "internal/eval.h"
21 #include "internal/gc.h"
22 #include "internal/inits.h"
23 #include "internal/missing.h"
24 #include "internal/object.h"
25 #include "internal/proc.h"
26 #include "internal/re.h"
27 #include "internal/ruby_parser.h"
28 #include "internal/symbol.h"
29 #include "internal/thread.h"
30 #include "internal/transcode.h"
31 #include "internal/vm.h"
32 #include "internal/sanitizers.h"
33 #include "internal/variable.h"
40 #include "vm_callinfo.h"
43 #include "vm_insnhelper.h"
44 #include "ractor_core.h"
51 #include "probes_helper.h"
53 #ifdef RUBY_ASSERT_CRITICAL_SECTION
54 int ruby_assert_critical_section_entered = 0;
57 static void *native_main_thread_stack_top;
59 VALUE rb_str_concat_literals(
size_t,
const VALUE*);
63 extern const char *
const rb_debug_counter_names[];
65 PUREFUNC(
static inline const VALUE *VM_EP_LEP(
const VALUE *));
66 static inline const VALUE *
67 VM_EP_LEP(
const VALUE *ep)
69 while (!VM_ENV_LOCAL_P(ep)) {
70 ep = VM_ENV_PREV_EP(ep);
88 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
96 rb_vm_ep_local_ep(
const VALUE *ep)
102 static inline const VALUE *
105 return VM_EP_LEP(cfp->ep);
108 static inline const VALUE *
111 return VM_ENV_PREV_EP(cfp->ep);
118 const VALUE *ep = VM_CF_LEP(cfp);
119 return VM_ENV_BLOCK_HANDLER(ep);
125 return VM_FRAME_CFRAME_KW_P(cfp);
131 return VM_CF_BLOCK_HANDLER(cfp);
134 #if VM_CHECK_MODE > 0
138 const VALUE *start = ec->vm_stack;
139 const VALUE *end = (
VALUE *)ec->vm_stack + ec->vm_stack_size;
140 VM_ASSERT(start != NULL);
142 if (start <= (
VALUE *)cfp && (
VALUE *)cfp < end) {
153 const VALUE *start = ec->vm_stack;
155 VM_ASSERT(start != NULL);
157 if (start <= ep && ep < end) {
168 if (VM_EP_IN_HEAP_P(ec, ep)) {
169 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
171 if (!UNDEF_P(envval)) {
174 VM_ASSERT(imemo_type_p(envval, imemo_env));
175 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
176 VM_ASSERT(env->ep == ep);
186 rb_vm_ep_in_heap_p(
const VALUE *ep)
189 if (ec->vm_stack == NULL)
return TRUE;
190 return vm_ep_in_heap_p_(ec, ep);
197 VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), cfp));
205 VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), cfp));
214 return VM_TAGGED_PTR_REF(block_handler, 0x03) == captured;
220 VALUE block_handler = ec->passed_block_handler;
221 ec->passed_block_handler = VM_BLOCK_HANDLER_NONE;
222 vm_block_handler_verify(block_handler);
223 return block_handler;
227 vm_cref_new0(
VALUE klass, rb_method_visibility_t visi,
int module_func,
rb_cref_t *prev_cref,
int pushed_by_eval,
int use_prev_prev,
int singleton)
230 int omod_shared = FALSE;
238 scope_visi.visi.method_visi = visi;
239 scope_visi.visi.module_func = module_func;
242 if (prev_cref != NULL && prev_cref != (
void *)1 ) {
243 refinements = CREF_REFINEMENTS(prev_cref);
245 if (!
NIL_P(refinements)) {
247 CREF_OMOD_SHARED_SET(prev_cref);
251 VM_ASSERT(singleton || klass);
254 cref->klass_or_self = klass;
255 cref->next = use_prev_prev ? CREF_NEXT(prev_cref) : prev_cref;
258 if (pushed_by_eval) CREF_PUSHED_BY_EVAL_SET(cref);
259 if (omod_shared) CREF_OMOD_SHARED_SET(cref);
260 if (singleton) CREF_SINGLETON_SET(cref);
266 vm_cref_new(
VALUE klass, rb_method_visibility_t visi,
int module_func,
rb_cref_t *prev_cref,
int pushed_by_eval,
int singleton)
268 return vm_cref_new0(klass, visi, module_func, prev_cref, pushed_by_eval, FALSE, singleton);
272 vm_cref_new_use_prev(
VALUE klass, rb_method_visibility_t visi,
int module_func,
rb_cref_t *prev_cref,
int pushed_by_eval)
274 return vm_cref_new0(klass, visi, module_func, prev_cref, pushed_by_eval, TRUE, FALSE);
280 return SYMBOL_P(key) ? ST_DELETE : ST_CONTINUE;
287 rb_cref_t *next_cref = CREF_NEXT(cref), *new_cref;
288 int pushed_by_eval = CREF_PUSHED_BY_EVAL(cref);
289 int singleton = CREF_SINGLETON(cref);
291 new_cref = vm_cref_new(cref->klass_or_self, visi->method_visi, visi->module_func, next_cref, pushed_by_eval, singleton);
293 if (!
NIL_P(CREF_REFINEMENTS(cref))) {
296 CREF_REFINEMENTS_SET(new_cref, ref);
297 CREF_OMOD_SHARED_UNSET(new_cref);
305 rb_vm_cref_dup_without_refinements(
const rb_cref_t *cref)
308 rb_cref_t *next_cref = CREF_NEXT(cref), *new_cref;
309 int pushed_by_eval = CREF_PUSHED_BY_EVAL(cref);
310 int singleton = CREF_SINGLETON(cref);
312 new_cref = vm_cref_new(cref->klass_or_self, visi->method_visi, visi->module_func, next_cref, pushed_by_eval, singleton);
314 if (!
NIL_P(CREF_REFINEMENTS(cref))) {
315 CREF_REFINEMENTS_SET(new_cref,
Qnil);
316 CREF_OMOD_SHARED_UNSET(new_cref);
325 rb_cref_t *cref = vm_cref_new(rb_cObject, METHOD_VISI_PRIVATE , FALSE, NULL, FALSE, FALSE);
326 VALUE top_wrapper = rb_ec_thread_ptr(ec)->top_wrapper;
329 cref = vm_cref_new(top_wrapper, METHOD_VISI_PRIVATE, FALSE, cref, FALSE, FALSE);
336 rb_vm_cref_new_toplevel(
void)
338 return vm_cref_new_toplevel(GET_EC());
342 vm_cref_dump(
const char *mesg,
const rb_cref_t *cref)
344 ruby_debug_printf(
"vm_cref_dump: %s (%p)\n", mesg, (
void *)cref);
348 cref = CREF_NEXT(cref);
355 *((
const VALUE **)&dst->as.captured.ep) = ep;
363 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, env->iseq);
364 rb_vm_block_ep_update(bindval, &bind->block, env->ep);
367 #if VM_COLLECT_USAGE_DETAILS
368 static void vm_collect_usage_operand(
int insn,
int n,
VALUE op);
369 static void vm_collect_usage_insn(
int insn);
370 static void vm_collect_usage_register(
int reg,
int isset);
375 int argc,
const VALUE *argv,
int kw_splat,
VALUE block_handler,
381 static uint64_t yjit_total_entry_hits = 0;
384 #define YJIT_CALL_COUNT_INTERV 20u
388 rb_yjit_threshold_hit(
const rb_iseq_t *iseq, uint64_t entry_calls)
390 yjit_total_entry_hits += 1;
393 if (entry_calls + YJIT_CALL_COUNT_INTERV == rb_yjit_call_threshold) {
394 iseq->body->yjit_calls_at_interv = yjit_total_entry_hits;
399 if (entry_calls == rb_yjit_call_threshold) {
401 if (rb_yjit_call_threshold < YJIT_CALL_COUNT_INTERV) {
405 uint64_t num_calls = yjit_total_entry_hits - iseq->body->yjit_calls_at_interv;
408 if (num_calls > rb_yjit_cold_threshold) {
409 rb_yjit_incr_counter(
"cold_iseq_entry");
419 #define rb_yjit_threshold_hit(iseq, entry_calls) false
422 #if USE_RJIT || USE_YJIT
431 static inline rb_jit_func_t
436 bool yjit_enabled = rb_yjit_enabled_p;
437 if (!(yjit_enabled || rb_rjit_call_p)) {
442 if (body->jit_entry == NULL) {
443 body->jit_entry_calls++;
445 if (rb_yjit_threshold_hit(iseq, body->jit_entry_calls)) {
446 rb_yjit_compile_iseq(iseq, ec,
false);
449 else if (body->jit_entry_calls == rb_rjit_call_threshold()) {
450 rb_rjit_compile(iseq);
453 return body->jit_entry;
460 rb_jit_func_t func = jit_compile(ec);
463 return func(ec, ec->cfp);
470 # define jit_compile(ec) ((rb_jit_func_t)0)
471 # define jit_exec(ec) Qundef
481 static inline rb_jit_func_t
486 if (!rb_yjit_enabled_p) {
491 if (body->jit_exception == NULL) {
492 body->jit_exception_calls++;
493 if (body->jit_exception_calls == rb_yjit_call_threshold) {
494 rb_yjit_compile_iseq(iseq, ec,
true);
498 return body->jit_exception;
505 rb_jit_func_t func = jit_compile_exception(ec);
508 return func(ec, ec->cfp);
515 # define jit_compile_exception(ec) ((rb_jit_func_t)0)
516 # define jit_exec_exception(ec) Qundef
521 #define RB_TYPE_2_P(obj, type1, type2) \
522 (RB_TYPE_P(obj, type1) || RB_TYPE_P(obj, type2))
523 #define RB_TYPE_3_P(obj, type1, type2, type3) \
524 (RB_TYPE_P(obj, type1) || RB_TYPE_P(obj, type2) || RB_TYPE_P(obj, type3))
526 #define VM_ASSERT_TYPE(obj, type) \
527 VM_ASSERT(RB_TYPE_P(obj, type), #obj ": %s", rb_obj_info(obj))
528 #define VM_ASSERT_TYPE2(obj, type1, type2) \
529 VM_ASSERT(RB_TYPE_2_P(obj, type1, type2), #obj ": %s", rb_obj_info(obj))
530 #define VM_ASSERT_TYPE3(obj, type1, type2, type3) \
531 VM_ASSERT(RB_TYPE_3_P(obj, type1, type2, type3), #obj ": %s", rb_obj_info(obj))
533 #include "vm_insnhelper.c"
537 #include "vm_method.c"
544 VALUE rb_mRubyVMFrozenCore;
545 VALUE rb_block_param_proxy;
547 VALUE ruby_vm_const_missing_count = 0;
548 rb_vm_t *ruby_current_vm_ptr = NULL;
550 bool ruby_vm_keep_script_lines;
552 #ifdef RB_THREAD_LOCAL_SPECIFIER
555 #ifdef RUBY_NT_SERIAL
556 RB_THREAD_LOCAL_SPECIFIER
rb_atomic_t ruby_nt_serial;
561 rb_current_ec_noinline(
void)
563 return ruby_current_ec;
569 ruby_current_ec = ec;
577 return ruby_current_ec;
582 native_tls_key_t ruby_current_ec_key;
586 rb_current_ec_noinline(
void)
588 return native_tls_get(ruby_current_ec_key);
595 unsigned int ruby_vm_event_local_num;
597 rb_serial_t ruby_vm_constant_cache_invalidations = 0;
598 rb_serial_t ruby_vm_constant_cache_misses = 0;
599 rb_serial_t ruby_vm_global_cvar_state = 1;
602 .flags =
T_IMEMO | (imemo_callcache <<
FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
605 .call_ = vm_call_general,
611 static const struct rb_callcache vm_empty_cc_for_super = {
612 .flags =
T_IMEMO | (imemo_callcache <<
FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
615 .call_ = vm_call_super_method,
621 static void thread_free(
void *
ptr);
624 rb_vm_inc_const_missing_count(
void)
626 ruby_vm_const_missing_count +=1;
635 if (!ec) ec = GET_EC();
636 if (!rb_ec_frame_method_id_and_class(ec, &
id, 0, &klass) || !klass)
640 klass =
RBASIC(klass)->klass;
642 else if (RCLASS_SINGLETON_P(klass)) {
643 klass = RCLASS_ATTACHED_OBJECT(klass);
644 if (
NIL_P(klass))
return FALSE;
649 const char *classname, *filename;
651 if (methodname && (filename = rb_source_location_cstr(&args->line_no)) != 0) {
653 classname =
"<unknown>";
654 args->classname = classname;
655 args->methodname = methodname;
656 args->filename = filename;
665 extern unsigned int redblack_buffer_size;
693 static VALUE sym_constant_cache_invalidations, sym_constant_cache_misses, sym_global_cvar_state, sym_next_shape_id;
694 static VALUE sym_shape_cache_size;
711 #define S(s) sym_##s = ID2SYM(rb_intern_const(#s))
712 S(constant_cache_invalidations);
713 S(constant_cache_misses);
714 S(global_cvar_state);
719 #define SET(name, attr) \
720 if (key == sym_##name) \
721 return SERIALT2NUM(attr); \
722 else if (hash != Qnil) \
723 rb_hash_aset(hash, sym_##name, SERIALT2NUM(attr));
725 SET(constant_cache_invalidations, ruby_vm_constant_cache_invalidations);
726 SET(constant_cache_misses, ruby_vm_constant_cache_misses);
727 SET(global_cvar_state, ruby_vm_global_cvar_state);
728 SET(next_shape_id, (rb_serial_t)GET_SHAPE_TREE()->next_shape_id);
729 SET(shape_cache_size, (rb_serial_t)GET_SHAPE_TREE()->cache_size);
732 #if USE_DEBUG_COUNTER
733 ruby_debug_counter_show_at_exit(FALSE);
734 for (
size_t i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
735 const VALUE name = rb_sym_intern_ascii_cstr(rb_debug_counter_names[i]);
741 else if (hash !=
Qnil) {
759 if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_TOP) {
764 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH, rb_ec_thread_ptr(ec)->top_self,
765 VM_BLOCK_HANDLER_NONE,
766 (
VALUE)vm_cref_new_toplevel(ec),
767 ISEQ_BODY(iseq)->iseq_encoded, ec->cfp->sp,
768 ISEQ_BODY(iseq)->local_table_size, ISEQ_BODY(iseq)->stack_max);
774 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_EVAL | VM_FRAME_FLAG_FINISH,
775 vm_block_self(base_block), VM_GUARDED_PREV_EP(vm_block_ep(base_block)),
777 ISEQ_BODY(iseq)->iseq_encoded,
778 ec->cfp->sp, ISEQ_BODY(iseq)->local_table_size,
779 ISEQ_BODY(iseq)->stack_max);
788 GetBindingPtr(toplevel_binding, bind);
791 vm_set_eval_stack(ec, iseq, 0, &bind->block);
794 if (ISEQ_BODY(iseq)->local_table_size > 0) {
795 vm_bind_update_env(toplevel_binding, bind, vm_make_env_object(ec, ec->cfp));
802 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
806 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
814 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
815 if (VM_FRAME_RUBYFRAME_P(cfp)) {
818 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
826 if (VM_FRAME_RUBYFRAME_P(cfp)) {
830 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
832 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
833 if (VM_FRAME_RUBYFRAME_P(cfp)) {
837 if (VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_PASSED) == FALSE) {
840 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
846 rb_vm_pop_cfunc_frame(
void)
853 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
854 vm_pop_frame(ec, cfp, cfp->ep);
861 while (ec->cfp != cfp) {
863 printf(
"skipped frame: %s\n", vm_frametype_name(ec->cfp));
865 if (VM_FRAME_TYPE(ec->cfp) != VM_FRAME_MAGIC_CFUNC) {
869 rb_vm_pop_cfunc_frame();
882 nl->next = vm->at_exit;
887 ruby_vm_run_at_exit_hooks(
rb_vm_t *vm)
893 rb_vm_at_exit_func *func = l->func;
907 fputs(
"---\n", stderr);
908 ruby_debug_printf(
"envptr: %p\n", (
void *)&env->ep[0]);
909 ruby_debug_printf(
"envval: %10p ", (
void *)env->ep[1]);
911 ruby_debug_printf(
"ep: %10p\n", (
void *)env->ep);
912 if (rb_vm_env_prev_env(env)) {
913 fputs(
">>\n", stderr);
914 check_env_value(rb_vm_env_prev_env(env));
915 fputs(
"<<\n", stderr);
921 check_env_value(
const rb_env_t *env)
923 if (check_env(env)) {
933 switch (vm_block_handler_type(block_handler)) {
934 case block_handler_type_ifunc:
935 case block_handler_type_iseq:
936 return rb_vm_make_proc(ec, VM_BH_TO_CAPT_BLOCK(block_handler),
rb_cProc);
938 case block_handler_type_symbol:
939 case block_handler_type_proc:
940 return block_handler;
942 VM_UNREACHABLE(vm_block_handler_escape);
949 const VALUE *
const ep = cfp->ep;
950 VALUE *env_body, *env_ep;
951 int local_size, env_size;
953 if (VM_ENV_ESCAPED_P(ep)) {
954 return VM_ENV_ENVVAL(ep);
957 if (!VM_ENV_LOCAL_P(ep)) {
958 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
959 if (!VM_ENV_ESCAPED_P(prev_ep)) {
962 while (prev_cfp->ep != prev_ep) {
963 prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(prev_cfp);
964 VM_ASSERT(prev_cfp->ep != NULL);
967 vm_make_env_each(ec, prev_cfp);
968 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_SPECVAL], VM_GUARDED_PREV_EP(prev_cfp->ep));
972 VALUE block_handler = VM_ENV_BLOCK_HANDLER(ep);
974 if (block_handler != VM_BLOCK_HANDLER_NONE) {
975 VALUE blockprocval = vm_block_handler_escape(ec, block_handler);
976 VM_STACK_ENV_WRITE(ep, VM_ENV_DATA_INDEX_SPECVAL, blockprocval);
980 if (!VM_FRAME_RUBYFRAME_P(cfp)) {
981 local_size = VM_ENV_DATA_SIZE;
984 local_size = ISEQ_BODY(cfp->iseq)->local_table_size;
985 if (ISEQ_BODY(cfp->iseq)->param.flags.forwardable && VM_ENV_LOCAL_P(cfp->ep)) {
986 int ci_offset = local_size - ISEQ_BODY(cfp->iseq)->param.size + VM_ENV_DATA_SIZE;
989 local_size += vm_ci_argc(ci);
991 local_size += VM_ENV_DATA_SIZE;
1006 env_size = local_size +
1014 MEMCPY(env_body, ep - (local_size - 1 ),
VALUE, local_size);
1016 env_ep = &env_body[local_size - 1 ];
1017 env_ep[VM_ENV_DATA_INDEX_ENV] = (
VALUE)env;
1019 env->iseq = (
rb_iseq_t *)(VM_FRAME_RUBYFRAME_P(cfp) ? cfp->iseq : NULL);
1021 env->env = env_body;
1022 env->env_size = env_size;
1025 VM_ENV_FLAGS_SET(env_ep, VM_ENV_FLAG_ESCAPED | VM_ENV_FLAG_WB_REQUIRED);
1026 VM_STACK_ENV_WRITE(ep, 0, (
VALUE)env);
1029 for (i = 0; i < local_size; i++) {
1030 if (VM_FRAME_RUBYFRAME_P(cfp)) {
1032 ep[-local_size + i] = 0;
1039 rb_yjit_invalidate_ep_is_bp(env->iseq);
1048 VALUE envval = vm_make_env_each(ec, cfp);
1051 check_env_value((
const rb_env_t *)envval);
1061 while ((cfp = rb_vm_get_binding_creatable_next_cfp(ec, cfp)) != 0) {
1062 vm_make_env_object(ec, cfp);
1063 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1068 rb_vm_env_prev_env(
const rb_env_t *env)
1070 const VALUE *ep = env->ep;
1072 if (VM_ENV_LOCAL_P(ep)) {
1076 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
1077 return VM_ENV_ENVVAL_PTR(prev_ep);
1085 if (!iseq)
return 0;
1086 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1087 local_var_list_add(vars, ISEQ_BODY(iseq)->local_table[i]);
1096 if (VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_ISOLATED))
break;
1097 collect_local_variables_in_iseq(env->iseq, vars);
1098 }
while ((env = rb_vm_env_prev_env(env)) != NULL);
1104 if (VM_ENV_ESCAPED_P(ep)) {
1105 collect_local_variables_in_env(VM_ENV_ENVVAL_PTR(ep), vars);
1114 rb_vm_env_local_variables(
const rb_env_t *env)
1117 local_var_list_init(&vars);
1118 collect_local_variables_in_env(env, &vars);
1119 return local_var_list_finish(&vars);
1123 rb_iseq_local_variables(
const rb_iseq_t *iseq)
1126 local_var_list_init(&vars);
1127 while (collect_local_variables_in_iseq(iseq, &vars)) {
1128 iseq = ISEQ_BODY(iseq)->parent_iseq;
1130 return local_var_list_finish(&vars);
1136 vm_proc_create_from_captured(
VALUE klass,
1138 enum rb_block_type block_type,
1139 int8_t is_from_method, int8_t is_lambda)
1141 VALUE procval = rb_proc_alloc(klass);
1144 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), captured->ep));
1147 RB_OBJ_WRITE(procval, &proc->block.as.captured.code.val, captured->code.val);
1148 RB_OBJ_WRITE(procval, &proc->block.as.captured.self, captured->self);
1149 rb_vm_block_ep_update(procval, &proc->block, captured->ep);
1151 vm_block_type_set(&proc->block, block_type);
1152 proc->is_from_method = is_from_method;
1153 proc->is_lambda = is_lambda;
1162 switch (vm_block_type(src)) {
1163 case block_type_iseq:
1164 case block_type_ifunc:
1165 RB_OBJ_WRITE(obj, &dst->as.captured.self, src->as.captured.self);
1166 RB_OBJ_WRITE(obj, &dst->as.captured.code.val, src->as.captured.code.val);
1167 rb_vm_block_ep_update(obj, dst, src->as.captured.ep);
1169 case block_type_symbol:
1172 case block_type_proc:
1179 proc_create(
VALUE klass,
const struct rb_block *block, int8_t is_from_method, int8_t is_lambda)
1181 VALUE procval = rb_proc_alloc(klass);
1184 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), vm_block_ep(block)));
1185 rb_vm_block_copy(procval, &proc->block, block);
1186 vm_block_type_set(&proc->block, block->type);
1187 proc->is_from_method = is_from_method;
1188 proc->is_lambda = is_lambda;
1194 rb_proc_dup(
VALUE self)
1199 GetProcPtr(
self, src);
1200 procval = proc_create(
rb_obj_class(
self), &src->block, src->is_from_method, src->is_lambda);
1216 if (SIZEOF_VOIDP > SIZEOF_LONG)
1225 if (SIZEOF_VOIDP > SIZEOF_LONG)
1231 static enum rb_id_table_iterator_result
1232 collect_outer_variable_names(
ID id,
VALUE val,
void *
ptr)
1241 if (data->isolate ||
1246 store = &data->read_only;
1251 return ID_TABLE_CONTINUE;
1255 env_copy(
const VALUE *src_ep,
VALUE read_only_variables)
1258 VM_ASSERT(src_env->ep == src_ep);
1261 VALUE *ep = &env_body[src_env->env_size - 2];
1262 const rb_env_t *copied_env = vm_env_new(ep, env_body, src_env->env_size, src_env->iseq);
1265 RB_OBJ_WRITE(copied_env, &ep[VM_ENV_DATA_INDEX_ME_CREF], src_ep[VM_ENV_DATA_INDEX_ME_CREF]);
1266 ep[VM_ENV_DATA_INDEX_FLAGS] = src_ep[VM_ENV_DATA_INDEX_FLAGS] | VM_ENV_FLAG_ISOLATED;
1267 if (!VM_ENV_LOCAL_P(src_ep)) {
1268 VM_ENV_FLAGS_SET(ep, VM_ENV_FLAG_LOCAL);
1271 if (read_only_variables) {
1272 for (
int i=
RARRAY_LENINT(read_only_variables)-1; i>=0; i--) {
1275 for (
unsigned int j=0; j<ISEQ_BODY(src_env->iseq)->local_table_size; j++) {
1276 if (
id == ISEQ_BODY(src_env->iseq)->local_table[j]) {
1277 VALUE v = src_env->env[j];
1280 VALUE msg =
rb_sprintf(
"can not make shareable Proc because it can refer"
1281 " unshareable object %+" PRIsVALUE
" from ", v);
1283 rb_str_catf(msg,
"variable '%" PRIsVALUE
"'", name);
1296 if (!VM_ENV_LOCAL_P(src_ep)) {
1297 const VALUE *prev_ep = VM_ENV_PREV_EP(src_env->ep);
1298 const rb_env_t *new_prev_env = env_copy(prev_ep, read_only_variables);
1299 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_GUARDED_PREV_EP(new_prev_env->ep);
1301 VM_ENV_FLAGS_UNSET(ep, VM_ENV_FLAG_LOCAL);
1304 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
1314 const rb_env_t *env = env_copy(captured->ep, read_only_variables);
1315 *((
const VALUE **)&proc->block.as.captured.ep) = env->ep;
1320 proc_shared_outer_variables(
struct rb_id_table *outer_variables,
bool isolate,
const char *message)
1328 rb_id_table_foreach(outer_variables, collect_outer_variable_names, (
void *)&data);
1330 if (data.ary !=
Qfalse) {
1331 VALUE str =
rb_sprintf(
"can not %s because it accesses outer variables", message);
1332 VALUE ary = data.ary;
1333 const char *sep =
" (";
1336 if (!name)
continue;
1345 else if (data.yield) {
1349 return data.read_only;
1353 rb_proc_isolate_bang(
VALUE self)
1355 const rb_iseq_t *iseq = vm_proc_iseq(
self);
1361 if (ISEQ_BODY(iseq)->outer_variables) {
1362 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables,
true,
"isolate a Proc");
1365 proc_isolate_env(
self, proc,
Qfalse);
1366 proc->is_isolated = TRUE;
1374 rb_proc_isolate(
VALUE self)
1376 VALUE dst = rb_proc_dup(
self);
1377 rb_proc_isolate_bang(dst);
1382 rb_proc_ractor_make_shareable(
VALUE self)
1384 const rb_iseq_t *iseq = vm_proc_iseq(
self);
1392 "Proc's self is not shareable: %" PRIsVALUE,
1398 if (ISEQ_BODY(iseq)->outer_variables) {
1399 read_only_variables =
1400 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables,
false,
"make a Proc shareable");
1403 proc_isolate_env(
self, proc, read_only_variables);
1404 proc->is_isolated = TRUE;
1415 enum imemo_type code_type = imemo_type(captured->code.val);
1417 if (!VM_ENV_ESCAPED_P(captured->ep)) {
1419 vm_make_env_object(ec, cfp);
1422 VM_ASSERT(VM_EP_IN_HEAP_P(ec, captured->ep));
1423 VM_ASSERT(code_type == imemo_iseq || code_type == imemo_ifunc);
1425 procval = vm_proc_create_from_captured(klass, captured,
1426 code_type == imemo_iseq ? block_type_iseq : block_type_ifunc,
1429 if (code_type == imemo_ifunc) {
1431 if (ifunc->svar_lep) {
1432 VALUE ep0 = ifunc->svar_lep[0];
1436 ifunc->svar_lep = (
VALUE *)env->ep;
1440 if (ep0 & VM_ENV_FLAG_ESCAPED) {
1444 ifunc->svar_lep = NULL;
1460 VALUE bindval, envval;
1463 if (cfp == 0 || ruby_level_cfp == 0) {
1466 if (!VM_FRAME_RUBYFRAME_P(src_cfp) &&
1467 !VM_FRAME_RUBYFRAME_P(RUBY_VM_PREVIOUS_CONTROL_FRAME(src_cfp))) {
1471 envval = vm_make_env_object(ec, cfp);
1473 GetBindingPtr(bindval, bind);
1474 vm_bind_update_env(bindval, bind, envval);
1475 RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, cfp->self);
1476 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, cfp->iseq);
1477 RB_OBJ_WRITE(bindval, &bind->pathobj, ISEQ_BODY(ruby_level_cfp->iseq)->location.pathobj);
1478 bind->first_lineno = rb_vm_get_sourceline(ruby_level_cfp);
1484 rb_binding_add_dynavars(
VALUE bindval,
rb_binding_t *bind,
int dyncount,
const ID *dynvars)
1486 VALUE envval, pathobj = bind->pathobj;
1487 VALUE path = pathobj_path(pathobj);
1488 VALUE realpath = pathobj_realpath(pathobj);
1495 if (dyncount < 0)
return 0;
1497 base_block = &bind->block;
1498 base_iseq = vm_block_iseq(base_block);
1502 dyns->size = dyncount;
1503 MEMCPY(dyns->ids, dynvars,
ID, dyncount);
1505 rb_node_init(RNODE(&tmp_node), NODE_SCOPE);
1506 tmp_node.nd_tbl = dyns;
1507 tmp_node.nd_body = 0;
1508 tmp_node.nd_args = 0;
1510 VALUE ast_value = rb_ruby_ast_new(RNODE(&tmp_node));
1513 iseq = rb_iseq_new(ast_value, ISEQ_BODY(base_iseq)->location.label, path, realpath, base_iseq, ISEQ_TYPE_EVAL);
1516 VALUE tempstr = rb_fstring_lit(
"<temp>");
1517 iseq = rb_iseq_new_top(ast_value, tempstr, tempstr, tempstr, NULL);
1519 tmp_node.nd_tbl = 0;
1522 vm_set_eval_stack(ec, iseq, 0, base_block);
1523 vm_bind_update_env(bindval, bind, envval = vm_make_env_object(ec, ec->cfp));
1524 rb_vm_pop_frame(ec);
1535 int arg_size = ISEQ_BODY(iseq)->param.size;
1537 vm_push_frame(ec, iseq,
type | VM_FRAME_FLAG_FINISH,
self,
1538 VM_GUARDED_PREV_EP(captured->ep),
1540 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1541 ec->cfp->sp + arg_size,
1542 ISEQ_BODY(iseq)->local_table_size - arg_size,
1543 ISEQ_BODY(iseq)->stack_max);
1550 int arg_size = ISEQ_BODY(iseq)->param.size;
1552 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
1554 vm_push_frame(ec, iseq,
type | VM_FRAME_FLAG_BMETHOD,
self,
1555 VM_GUARDED_PREV_EP(captured->ep),
1557 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1558 ec->cfp->sp + 1 + arg_size,
1559 ISEQ_BODY(iseq)->local_table_size - arg_size,
1560 ISEQ_BODY(iseq)->stack_max);
1562 VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);
1565 ALWAYS_INLINE(
static VALUE
1567 VALUE self,
int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler,
1572 VALUE self,
int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler,
1575 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
1577 VALUE type = VM_FRAME_MAGIC_BLOCK | (is_lambda ? VM_FRAME_FLAG_LAMBDA : 0);
1579 VALUE *sp = cfp->sp;
1580 int flags = (kw_splat ? VM_CALL_KW_SPLAT : 0);
1586 if (UNLIKELY(argc > VM_ARGC_STACK_MAX) &&
1587 (VM_ARGC_STACK_MAX >= 1 ||
1589 (argc != 1 || is_lambda))) {
1590 use_argv = vm_argv_ruby_array(av, argv, &flags, &argc, kw_splat);
1593 CHECK_VM_STACK_OVERFLOW(cfp, argc + 1);
1594 vm_check_canary(ec, sp);
1596 VALUE *stack_argv = sp;
1601 cfp->sp = stack_argv + argc;
1604 opt_pc = vm_yield_setup_args(ec, iseq, argc, stack_argv, flags, passed_block_handler,
1605 (is_lambda ? arg_setup_method : arg_setup_block));
1609 invoke_block(ec, iseq,
self, captured, cref,
type, opt_pc);
1612 invoke_bmethod(ec, iseq,
self, captured, me,
type, opt_pc);
1620 int argc,
const VALUE *argv,
1622 int is_lambda,
int force_blockarg)
1625 switch (vm_block_handler_type(block_handler)) {
1626 case block_handler_type_iseq:
1629 return invoke_iseq_block_from_c(ec, captured, captured->self,
1630 argc, argv, kw_splat, passed_block_handler,
1631 cref, is_lambda, NULL);
1633 case block_handler_type_ifunc:
1634 return vm_yield_with_cfunc(ec, VM_BH_TO_IFUNC_BLOCK(block_handler),
1635 VM_BH_TO_IFUNC_BLOCK(block_handler)->
self,
1636 argc, argv, kw_splat, passed_block_handler, NULL);
1637 case block_handler_type_symbol:
1638 return vm_yield_with_symbol(ec, VM_BH_TO_SYMBOL(block_handler),
1639 argc, argv, kw_splat, passed_block_handler);
1640 case block_handler_type_proc:
1641 if (force_blockarg == FALSE) {
1642 is_lambda = block_proc_is_lambda(VM_BH_TO_PROC(block_handler));
1644 block_handler = vm_proc_to_block_handler(VM_BH_TO_PROC(block_handler));
1647 VM_UNREACHABLE(invoke_block_from_c_splattable);
1654 VALUE block_handler = VM_CF_BLOCK_HANDLER(ec->cfp);
1655 vm_block_handler_verify(block_handler);
1656 if (UNLIKELY(block_handler == VM_BLOCK_HANDLER_NONE)) {
1657 rb_vm_localjump_error(
"no block given",
Qnil, 0);
1660 return block_handler;
1666 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1667 argc, argv, kw_splat, VM_BLOCK_HANDLER_NONE,
1668 cref, is_lambda, FALSE);
1674 return vm_yield_with_cref(ec, argc, argv, kw_splat, NULL, FALSE);
1680 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1681 argc, argv, kw_splat, block_handler,
1682 NULL, FALSE, FALSE);
1688 return invoke_block_from_c_bh(ec, check_block_handler(ec), 1, &args,
1692 ALWAYS_INLINE(
static VALUE
1695 int kw_splat,
VALUE passed_block_handler,
int is_lambda,
1701 int kw_splat,
VALUE passed_block_handler,
int is_lambda,
1704 const struct rb_block *block = &proc->block;
1707 switch (vm_block_type(block)) {
1708 case block_type_iseq:
1709 return invoke_iseq_block_from_c(ec, &block->as.captured,
self, argc, argv, kw_splat, passed_block_handler, NULL, is_lambda, me);
1710 case block_type_ifunc:
1711 if (kw_splat == 1) {
1712 VALUE keyword_hash = argv[argc-1];
1714 keyword_hash = rb_to_hash_type(keyword_hash);
1723 return vm_yield_with_cfunc(ec, &block->as.captured,
self, argc, argv, kw_splat, passed_block_handler, me);
1724 case block_type_symbol:
1725 return vm_yield_with_symbol(ec, block->as.symbol, argc, argv, kw_splat, passed_block_handler);
1726 case block_type_proc:
1727 is_lambda = block_proc_is_lambda(block->as.proc);
1728 block = vm_proc_block(block->as.proc);
1731 VM_UNREACHABLE(invoke_block_from_c_proc);
1737 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler)
1739 return invoke_block_from_c_proc(ec, proc,
self, argc, argv, kw_splat, passed_block_handler, proc->is_lambda, NULL);
1746 return invoke_block_from_c_proc(ec, proc,
self, argc, argv, kw_splat, block_handler, TRUE, me);
1751 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler)
1753 VALUE self = vm_block_self(&proc->block);
1754 vm_block_handler_verify(passed_block_handler);
1756 if (proc->is_from_method) {
1757 return vm_invoke_bmethod(ec, proc,
self, argc, argv, kw_splat, passed_block_handler, NULL);
1760 return vm_invoke_proc(ec, proc,
self, argc, argv, kw_splat, passed_block_handler);
1766 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler)
1768 vm_block_handler_verify(passed_block_handler);
1770 if (proc->is_from_method) {
1771 return vm_invoke_bmethod(ec, proc,
self, argc, argv, kw_splat, passed_block_handler, NULL);
1774 return vm_invoke_proc(ec, proc,
self, argc, argv, kw_splat, passed_block_handler);
1783 while (cfp->pc == 0 || cfp->iseq == 0) {
1784 if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_IFUNC) {
1786 return ifunc->svar_lep;
1789 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1792 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
1797 return (
VALUE *)VM_CF_LEP(cfp);
1803 return lep_svar_get(ec, rb_vm_svar_lep(ec, cfp), key);
1809 lep_svar_set(ec, rb_vm_svar_lep(ec, cfp), key, val);
1815 return vm_cfp_svar_get(ec, ec->cfp, key);
1821 vm_cfp_svar_set(ec, ec->cfp, key, val);
1827 return vm_svar_get(GET_EC(), VM_SVAR_BACKREF);
1833 vm_svar_set(GET_EC(), VM_SVAR_BACKREF, val);
1839 return vm_svar_get(GET_EC(), VM_SVAR_LASTLINE);
1845 vm_svar_set(GET_EC(), VM_SVAR_LASTLINE, val);
1849 rb_lastline_set_up(
VALUE val,
unsigned int up)
1853 for(
unsigned int i = 0; i < up; i++) {
1854 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1856 vm_cfp_svar_set(GET_EC(), cfp, VM_SVAR_LASTLINE, val);
1882 return rb_vm_get_sourceline(cfp);
1890 rb_source_location(
int *pline)
1895 if (cfp && VM_FRAME_RUBYFRAME_P(cfp)) {
1896 if (pline) *pline = rb_vm_get_sourceline(cfp);
1897 return rb_iseq_path(cfp->iseq);
1900 if (pline) *pline = 0;
1906 rb_source_location_cstr(
int *pline)
1908 VALUE path = rb_source_location(pline);
1909 if (
NIL_P(path))
return NULL;
1917 return vm_ec_cref(ec);
1921 rb_vm_cref_replace_with_duplicated_cref(
void)
1925 rb_cref_t *cref = vm_cref_replace_with_duplicated_cref(cfp->ep);
1931 rb_vm_cref_in_context(
VALUE self,
VALUE cbase)
1936 if (!cfp || cfp->self !=
self)
return NULL;
1937 if (!vm_env_cref_by_cref(cfp->ep))
return NULL;
1938 cref = vm_get_cref(cfp->ep);
1939 if (CREF_CLASS(cref) != cbase)
return NULL;
1948 dp(CREF_CLASS(cref));
1949 printf(
"%ld\n", CREF_VISI(cref));
1950 cref = CREF_NEXT(cref);
1964 return vm_get_cbase(cfp->ep);
1970 make_localjump_error(
const char *mesg,
VALUE value,
int reason)
2002 rb_vm_localjump_error(
const char *mesg,
VALUE value,
int reason)
2004 VALUE exc = make_localjump_error(mesg, value, reason);
2009 rb_vm_make_jump_tag_but_local_jump(
enum ruby_tag_type state,
VALUE val)
2015 mesg =
"unexpected return";
2018 mesg =
"unexpected break";
2021 mesg =
"unexpected next";
2024 mesg =
"unexpected redo";
2028 mesg =
"retry outside of rescue clause";
2035 val = GET_EC()->tag->retval;
2037 return make_localjump_error(mesg, val, state);
2041 rb_vm_jump_tag_but_local_jump(
enum ruby_tag_type state)
2043 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
2045 EC_JUMP_TAG(GET_EC(), state);
2051 while (VM_ENV_LOCAL_P(cfp->ep)) {
2052 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2063 const VALUE *ep = VM_CF_PREV_EP(cfp);
2067 rb_vm_localjump_error(
"unexpected break", val, TAG_BREAK);
2070 ec->errinfo = (
VALUE)THROW_DATA_NEW(val, target_cfp, TAG_BREAK);
2071 EC_JUMP_TAG(ec, TAG_BREAK);
2077 vm_iter_break(GET_EC(),
Qnil);
2083 vm_iter_break(GET_EC(), val);
2088 short ruby_vm_redefined_flag[BOP_LAST_];
2089 static st_table *vm_opt_method_def_table = 0;
2090 static st_table *vm_opt_mid_table = 0;
2093 rb_free_vm_opt_tables(
void)
2095 st_free_table(vm_opt_method_def_table);
2096 st_free_table(vm_opt_mid_table);
2100 vm_redefinition_check_flag(
VALUE klass)
2102 if (klass ==
rb_cInteger)
return INTEGER_REDEFINED_OP_FLAG;
2103 if (klass ==
rb_cFloat)
return FLOAT_REDEFINED_OP_FLAG;
2104 if (klass ==
rb_cString)
return STRING_REDEFINED_OP_FLAG;
2105 if (klass ==
rb_cArray)
return ARRAY_REDEFINED_OP_FLAG;
2106 if (klass ==
rb_cHash)
return HASH_REDEFINED_OP_FLAG;
2107 if (klass ==
rb_cSymbol)
return SYMBOL_REDEFINED_OP_FLAG;
2109 if (klass ==
rb_cTime)
return TIME_REDEFINED_OP_FLAG;
2111 if (klass ==
rb_cRegexp)
return REGEXP_REDEFINED_OP_FLAG;
2112 if (klass ==
rb_cNilClass)
return NIL_REDEFINED_OP_FLAG;
2115 if (klass ==
rb_cProc)
return PROC_REDEFINED_OP_FLAG;
2120 rb_vm_check_optimizable_mid(
VALUE mid)
2122 if (!vm_opt_mid_table) {
2126 return st_lookup(vm_opt_mid_table, mid, NULL);
2132 if (me->called_id != me->def->original_id) {
2136 if (METHOD_ENTRY_BASIC(me))
return TRUE;
2139 switch (def->type) {
2140 case VM_METHOD_TYPE_CFUNC:
2141 case VM_METHOD_TYPE_OPTIMIZED:
2156 if (vm_redefinition_check_method_type(me)) {
2157 if (st_lookup(vm_opt_method_def_table, (st_data_t)me->def, &bop)) {
2158 int flag = vm_redefinition_check_flag(klass);
2162 "Redefining '%s#%s' disables interpreter and JIT optimizations",
2166 rb_yjit_bop_redefined(flag, (
enum ruby_basic_operators)bop);
2167 rb_rjit_bop_redefined(flag, (
enum ruby_basic_operators)bop);
2168 ruby_vm_redefined_flag[bop] |= flag;
2174 static enum rb_id_table_iterator_result
2175 check_redefined_method(
ID mid,
VALUE value,
void *data)
2181 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
2183 return ID_TABLE_CONTINUE;
2187 rb_vm_check_redefinition_by_prepend(
VALUE klass)
2189 if (!vm_redefinition_check_flag(klass))
return;
2190 rb_id_table_foreach(RCLASS_M_TBL(RCLASS_ORIGIN(klass)), check_redefined_method, (
void *)klass);
2194 add_opt_method_entry_bop(
const rb_method_entry_t *me,
ID mid,
enum ruby_basic_operators bop)
2196 st_insert(vm_opt_method_def_table, (st_data_t)me->def, (st_data_t)bop);
2197 st_insert(vm_opt_mid_table, (st_data_t)mid, (st_data_t)
Qtrue);
2201 add_opt_method(
VALUE klass,
ID mid,
enum ruby_basic_operators bop)
2205 if (me && vm_redefinition_check_method_type(me)) {
2206 add_opt_method_entry_bop(me, mid, bop);
2213 static enum ruby_basic_operators vm_redefinition_bop_for_id(
ID mid);
2218 if (me && vm_redefinition_check_method_type(me)) {
2219 ID mid = me->called_id;
2220 enum ruby_basic_operators bop = vm_redefinition_bop_for_id(mid);
2221 if ((
int)bop >= 0) {
2222 add_opt_method_entry_bop(me, mid, bop);
2228 vm_init_redefined_flag(
void)
2231 enum ruby_basic_operators bop;
2233 #define OP(mid_, bop_) (mid = id##mid_, bop = BOP_##bop_, ruby_vm_redefined_flag[bop] = 0)
2234 #define C(k) add_opt_method(rb_c##k, mid, bop)
2235 OP(PLUS, PLUS), (C(Integer), C(Float), C(String), C(Array));
2236 OP(MINUS, MINUS), (C(Integer), C(Float));
2237 OP(MULT, MULT), (C(Integer), C(Float));
2238 OP(DIV, DIV), (C(Integer), C(Float));
2239 OP(MOD, MOD), (C(Integer), C(Float));
2240 OP(Eq, EQ), (C(Integer), C(Float), C(String), C(Symbol));
2241 OP(Eqq, EQQ), (C(Integer), C(Float), C(Symbol), C(String),
2242 C(NilClass), C(TrueClass), C(FalseClass));
2243 OP(LT, LT), (C(Integer), C(Float));
2244 OP(LE, LE), (C(Integer), C(Float));
2245 OP(GT, GT), (C(Integer), C(Float));
2246 OP(GE, GE), (C(Integer), C(Float));
2247 OP(LTLT, LTLT), (C(String), C(Array));
2248 OP(AREF, AREF), (C(Array), C(Hash), C(Integer));
2249 OP(ASET, ASET), (C(Array), C(Hash));
2250 OP(Length, LENGTH), (C(Array), C(String), C(Hash));
2251 OP(Size, SIZE), (C(Array), C(String), C(Hash));
2252 OP(EmptyP, EMPTY_P), (C(Array), C(String), C(Hash));
2253 OP(Succ, SUCC), (C(Integer), C(String));
2254 OP(EqTilde, MATCH), (C(
Regexp), C(String));
2255 OP(Freeze, FREEZE), (C(String), C(Array), C(Hash));
2256 OP(UMinus, UMINUS), (C(String));
2257 OP(Max, MAX), (C(Array));
2258 OP(Min, MIN), (C(Array));
2259 OP(Hash, HASH), (C(Array));
2260 OP(Call, CALL), (C(Proc));
2261 OP(And, AND), (C(Integer));
2262 OP(Or, OR), (C(Integer));
2263 OP(NilP,
NIL_P), (C(NilClass));
2264 OP(Cmp, CMP), (C(Integer), C(Float), C(String));
2265 OP(Default, DEFAULT), (C(Hash));
2270 static enum ruby_basic_operators
2271 vm_redefinition_bop_for_id(
ID mid)
2274 #define OP(mid_, bop_) case id##mid_: return BOP_##bop_
2291 OP(EmptyP, EMPTY_P);
2304 OP(Default, DEFAULT);
2317 switch (VM_FRAME_TYPE(cfp)) {
2318 case VM_FRAME_MAGIC_METHOD:
return "method";
2319 case VM_FRAME_MAGIC_BLOCK:
return "block";
2320 case VM_FRAME_MAGIC_CLASS:
return "class";
2321 case VM_FRAME_MAGIC_TOP:
return "top";
2322 case VM_FRAME_MAGIC_CFUNC:
return "cfunc";
2323 case VM_FRAME_MAGIC_IFUNC:
return "ifunc";
2324 case VM_FRAME_MAGIC_EVAL:
return "eval";
2325 case VM_FRAME_MAGIC_RESCUE:
return "rescue";
2335 if (THROW_DATA_P(err) &&
2336 THROW_DATA_STATE(err) == TAG_BREAK &&
2337 THROW_DATA_CONSUMED_P(err) == FALSE) {
2338 return THROW_DATA_VAL(err);
2350 unsigned long type = VM_FRAME_TYPE(cfp);
2351 #define C(t) if (type == VM_FRAME_MAGIC_##t) return #t
2380 switch (VM_FRAME_TYPE(ec->cfp)) {
2381 case VM_FRAME_MAGIC_METHOD:
2382 RUBY_DTRACE_METHOD_RETURN_HOOK(ec, 0, 0);
2383 EXEC_EVENT_HOOK_AND_POP_FRAME(ec,
RUBY_EVENT_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2387 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2390 THROW_DATA_CONSUMED_SET(err);
2392 case VM_FRAME_MAGIC_BLOCK:
2393 if (VM_FRAME_BMETHOD_P(ec->cfp)) {
2394 VALUE bmethod_return_value = frame_return_value(err);
2395 if (cfp_returning_with_value) {
2397 bmethod_return_value = THROW_DATA_VAL(err);
2401 EXEC_EVENT_HOOK_AND_POP_FRAME(ec,
RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, bmethod_return_value);
2404 ec->cfp->self, 0, 0, 0, bmethod_return_value, TRUE);
2410 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2411 rb_vm_frame_method_entry(ec->cfp)->called_id,
2412 rb_vm_frame_method_entry(ec->cfp)->owner,
2413 bmethod_return_value);
2415 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
2416 local_hooks = me->def->body.bmethod.hooks;
2420 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2421 rb_vm_frame_method_entry(ec->cfp)->called_id,
2422 rb_vm_frame_method_entry(ec->cfp)->owner,
2423 bmethod_return_value, TRUE);
2425 THROW_DATA_CONSUMED_SET(err);
2428 EXEC_EVENT_HOOK_AND_POP_FRAME(ec,
RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2431 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2433 THROW_DATA_CONSUMED_SET(err);
2436 case VM_FRAME_MAGIC_CLASS:
2529 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2531 struct rb_vm_exec_context {
2539 vm_exec_bottom_main(
void *context)
2541 struct rb_vm_exec_context *ctx = context;
2544 ctx->result = vm_exec_loop(ec, TAG_NONE, ctx->tag, vm_exec_core(ec));
2548 vm_exec_bottom_rescue(
void *context)
2550 struct rb_vm_exec_context *ctx = context;
2553 ctx->result = vm_exec_loop(ec, rb_ec_tag_state(ec), ctx->tag, ec->errinfo);
2566 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2567 struct rb_vm_exec_context ctx = {
2575 rb_wasm_try_catch_init(&try_catch, vm_exec_bottom_main, vm_exec_bottom_rescue, &ctx);
2577 rb_wasm_try_catch_loop_run(&try_catch, &RB_VM_TAG_JMPBUF_GET(_tag.buf));
2579 result = ctx.result;
2581 enum ruby_tag_type state;
2582 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2583 if (UNDEF_P(result = jit_exec(ec))) {
2584 result = vm_exec_core(ec);
2587 result = vm_exec_loop(ec, TAG_NONE, &_tag, result);
2590 result = vm_exec_loop(ec, state, &_tag, ec->errinfo);
2602 if (state == TAG_NONE) {
2606 rb_ec_raised_reset(ec, RAISED_STACKOVERFLOW | RAISED_NOMEMORY);
2607 while (UNDEF_P(result = vm_exec_handle_exception(ec, state, result))) {
2610 if (UNDEF_P(result = jit_exec_exception(ec))) {
2611 result = vm_exec_core(ec);
2614 VM_ASSERT(ec->tag == tag);
2616 if ((state = tag->state) == TAG_NONE)
break;
2617 tag->state = TAG_NONE;
2632 unsigned long epc, cont_pc, cont_sp;
2637 cont_pc = cont_sp = 0;
2640 while (ec->cfp->pc == 0 || ec->cfp->iseq == 0) {
2641 if (UNLIKELY(VM_FRAME_TYPE(ec->cfp) == VM_FRAME_MAGIC_CFUNC)) {
2643 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2644 rb_vm_frame_method_entry(ec->cfp)->called_id,
2645 rb_vm_frame_method_entry(ec->cfp)->owner,
Qnil);
2646 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec,
2647 rb_vm_frame_method_entry(ec->cfp)->owner,
2648 rb_vm_frame_method_entry(ec->cfp)->def->original_id);
2650 rb_vm_pop_frame(ec);
2654 epc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
2657 if (state == TAG_BREAK || state == TAG_RETURN) {
2658 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2660 if (cfp == escape_cfp) {
2661 if (state == TAG_RETURN) {
2662 if (!VM_FRAME_FINISHED_P(cfp)) {
2663 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2664 THROW_DATA_STATE_SET(err, state = TAG_BREAK);
2667 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2668 if (ct)
for (i = 0; i < ct->size; i++) {
2669 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2670 if (entry->start < epc && entry->end >= epc) {
2671 if (entry->type == CATCH_TYPE_ENSURE) {
2672 catch_iseq = entry->iseq;
2673 cont_pc = entry->cont;
2674 cont_sp = entry->sp;
2679 if (catch_iseq == NULL) {
2681 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2683 hook_before_rewind(ec,
true, state, err);
2684 rb_vm_pop_frame(ec);
2685 return THROW_DATA_VAL(err);
2692 *cfp->sp++ = THROW_DATA_VAL(err);
2699 if (state == TAG_RAISE) {
2700 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2701 if (ct)
for (i = 0; i < ct->size; i++) {
2702 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2703 if (entry->start < epc && entry->end >= epc) {
2705 if (entry->type == CATCH_TYPE_RESCUE ||
2706 entry->type == CATCH_TYPE_ENSURE) {
2707 catch_iseq = entry->iseq;
2708 cont_pc = entry->cont;
2709 cont_sp = entry->sp;
2715 else if (state == TAG_RETRY) {
2716 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2717 if (ct)
for (i = 0; i < ct->size; i++) {
2718 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2719 if (entry->start < epc && entry->end >= epc) {
2721 if (entry->type == CATCH_TYPE_ENSURE) {
2722 catch_iseq = entry->iseq;
2723 cont_pc = entry->cont;
2724 cont_sp = entry->sp;
2727 else if (entry->type == CATCH_TYPE_RETRY) {
2729 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2730 if (cfp == escape_cfp) {
2731 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2739 else if ((state == TAG_BREAK && !escape_cfp) ||
2740 (state == TAG_REDO) ||
2741 (state == TAG_NEXT)) {
2742 type = (
const enum rb_catch_type[TAG_MASK]) {
2743 [TAG_BREAK] = CATCH_TYPE_BREAK,
2744 [TAG_NEXT] = CATCH_TYPE_NEXT,
2745 [TAG_REDO] = CATCH_TYPE_REDO,
2749 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2750 if (ct)
for (i = 0; i < ct->size; i++) {
2751 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2753 if (entry->start < epc && entry->end >= epc) {
2754 if (entry->type == CATCH_TYPE_ENSURE) {
2755 catch_iseq = entry->iseq;
2756 cont_pc = entry->cont;
2757 cont_sp = entry->sp;
2760 else if (entry->type ==
type) {
2761 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2762 cfp->sp = vm_base_ptr(cfp) + entry->sp;
2764 if (state != TAG_REDO) {
2765 *cfp->sp++ = THROW_DATA_VAL(err);
2768 VM_ASSERT(ec->tag->state == TAG_NONE);
2775 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2776 if (ct)
for (i = 0; i < ct->size; i++) {
2777 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2778 if (entry->start < epc && entry->end >= epc) {
2780 if (entry->type == CATCH_TYPE_ENSURE) {
2781 catch_iseq = entry->iseq;
2782 cont_pc = entry->cont;
2783 cont_sp = entry->sp;
2790 if (catch_iseq != NULL) {
2792 const int arg_size = 1;
2794 rb_iseq_check(catch_iseq);
2795 cfp->sp = vm_base_ptr(cfp) + cont_sp;
2796 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + cont_pc;
2799 cfp->sp[0] = (
VALUE)err;
2800 vm_push_frame(ec, catch_iseq, VM_FRAME_MAGIC_RESCUE,
2802 VM_GUARDED_PREV_EP(cfp->ep),
2804 ISEQ_BODY(catch_iseq)->iseq_encoded,
2805 cfp->sp + arg_size ,
2806 ISEQ_BODY(catch_iseq)->local_table_size - arg_size,
2807 ISEQ_BODY(catch_iseq)->stack_max);
2810 ec->tag->state = TAG_NONE;
2816 hook_before_rewind(ec, (cfp == escape_cfp), state, err);
2818 if (VM_FRAME_FINISHED_P(ec->cfp)) {
2819 rb_vm_pop_frame(ec);
2820 ec->errinfo = (
VALUE)err;
2821 ec->tag = ec->tag->prev;
2822 EC_JUMP_TAG(ec, state);
2825 rb_vm_pop_frame(ec);
2838 vm_set_top_stack(ec, iseq);
2844 rb_iseq_eval_main(
const rb_iseq_t *iseq)
2849 vm_set_main_stack(ec, iseq);
2860 if (idp) *idp = me->def->original_id;
2861 if (called_idp) *called_idp = me->called_id;
2862 if (klassp) *klassp = me->owner;
2873 return rb_vm_control_frame_id_and_class(ec->cfp, idp, called_idp, klassp);
2879 return rb_ec_frame_method_id_and_class(GET_EC(), idp, 0, klassp);
2888 const rb_iseq_t *iseq = rb_iseq_new(
Qnil, filename, filename,
Qnil, 0, ISEQ_TYPE_TOP);
2891 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH,
2892 recv, block_handler,
2893 (
VALUE)vm_cref_new_toplevel(ec),
2894 0, reg_cfp->sp, 0, 0);
2898 rb_vm_pop_frame(ec);
2905 rb_vm_update_references(
void *
ptr)
2916 if (vm->load_path_check_cache) {
2917 vm->load_path_check_cache =
rb_gc_location(vm->load_path_check_cache);
2922 vm->loaded_features_snapshot =
rb_gc_location(vm->loaded_features_snapshot);
2923 vm->loaded_features_realpaths =
rb_gc_location(vm->loaded_features_realpaths);
2924 vm->loaded_features_realpath_map =
rb_gc_location(vm->loaded_features_realpath_map);
2930 rb_gc_update_values(RUBY_NSIG, vm->trap_list.cmd);
2932 if (vm->coverages) {
2940 rb_vm_each_stack_value(
void *
ptr,
void (*cb)(
VALUE,
void*),
void *ctx)
2945 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2946 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
2947 rb_ractor_status_p(r, ractor_running));
2948 if (r->threads.cnt > 0) {
2950 ccan_list_for_each(&r->threads.set, th, lt_node) {
2951 VM_ASSERT(th != NULL);
2954 VALUE *p = ec->vm_stack;
2955 VALUE *sp = ec->cfp->sp;
2969 static enum rb_id_table_iterator_result
2970 vm_mark_negative_cme(
VALUE val,
void *dmy)
2973 return ID_TABLE_CONTINUE;
2976 void rb_thread_sched_mark_zombies(
rb_vm_t *vm);
2979 rb_vm_mark(
void *
ptr)
2981 RUBY_MARK_ENTER(
"vm");
2982 RUBY_GC_INFO(
"-------------------------------------------------\n");
2988 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2990 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
2991 rb_ractor_status_p(r, ractor_running));
2995 for (
struct global_object_list *list = vm->global_object_list; list; list = list->next) {
3013 if (vm->loading_table) {
3017 rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);
3019 rb_id_table_foreach_values(vm->negative_cme_table, vm_mark_negative_cme, NULL);
3021 for (i=0; i<VM_GLOBAL_CC_CACHE_TABLE_SIZE; i++) {
3022 const struct rb_callcache *cc = vm->global_cc_cache_table[i];
3025 if (!vm_cc_invalidated_p(cc)) {
3029 vm->global_cc_cache_table[i] = NULL;
3034 rb_thread_sched_mark_zombies(vm);
3038 RUBY_MARK_LEAVE(
"vm");
3041 #undef rb_vm_register_special_exception
3043 rb_vm_register_special_exception_str(
enum ruby_special_exceptions sp,
VALUE cls,
VALUE mesg)
3048 ((
VALUE *)vm->special_exceptions)[sp] = exc;
3049 rb_vm_register_global_object(exc);
3053 free_loading_table_entry(st_data_t key, st_data_t value, st_data_t arg)
3059 void rb_free_loaded_features_index(
rb_vm_t *vm);
3060 void rb_objspace_free_objects(
void *objspace);
3065 RUBY_FREE_ENTER(
"vm");
3069 VALUE *stack = th->ec->vm_stack;
3070 if (rb_free_at_exit) {
3071 rb_free_encoded_insn_data();
3072 rb_free_global_enc_table();
3073 rb_free_loaded_builtin_table();
3075 rb_free_shared_fiber_pool();
3076 rb_free_static_symid_str();
3077 rb_free_transcoder_table();
3078 rb_free_vm_opt_tables();
3080 rb_free_rb_global_tbl();
3081 rb_free_loaded_features_index(vm);
3083 rb_id_table_free(vm->negative_cme_table);
3084 st_free_table(vm->overloaded_cme_table);
3086 rb_id_table_free(
RCLASS(rb_mRubyVMFrozenCore)->m_tbl);
3088 rb_shape_t *cursor = rb_shape_get_root_shape();
3089 rb_shape_t *end = rb_shape_get_shape_by_id(GET_SHAPE_TREE()->next_shape_id);
3090 while (cursor < end) {
3092 if (cursor->edges && !(((uintptr_t)cursor->edges) & 0x1))
3093 rb_id_table_free(cursor->edges);
3097 xfree(GET_SHAPE_TREE());
3099 st_free_table(vm->static_ext_inits);
3101 rb_vm_postponed_job_free();
3103 rb_id_table_free(vm->constant_cache);
3104 st_free_table(vm->unused_block_warning_table);
3111 #ifndef HAVE_SETPROCTITLE
3112 ruby_free_proctitle();
3117 rb_fiber_reset_root_local_storage(th);
3124 rb_vm_living_threads_init(vm);
3125 ruby_vm_run_at_exit_hooks(vm);
3126 if (vm->loading_table) {
3127 st_foreach(vm->loading_table, free_loading_table_entry, 0);
3128 st_free_table(vm->loading_table);
3129 vm->loading_table = 0;
3132 st_free_table(vm->ci_table);
3133 vm->ci_table = NULL;
3135 if (vm->frozen_strings) {
3136 st_free_table(vm->frozen_strings);
3137 vm->frozen_strings = 0;
3139 RB_ALTSTACK_FREE(vm->main_altstack);
3148 if (rb_free_at_exit) {
3149 rb_objspace_free_objects(objspace);
3150 rb_free_generic_iv_tbl_();
3151 rb_free_default_rand_key();
3152 if (th && vm->fork_gen == 0) {
3158 rb_objspace_free(objspace);
3163 ruby_current_vm_ptr = NULL;
3166 if (rb_free_at_exit) {
3167 rb_yjit_free_at_exit();
3171 RUBY_FREE_LEAVE(
"vm");
3175 size_t rb_vm_memsize_waiting_fds(
struct ccan_list_head *waiting_fds);
3176 size_t rb_vm_memsize_workqueue(
struct ccan_list_head *workqueue);
3180 static enum rb_id_table_iterator_result
3181 vm_memsize_constant_cache_i(
ID id,
VALUE ics,
void *size)
3183 *((
size_t *) size) += rb_st_memsize((
st_table *) ics);
3184 return ID_TABLE_CONTINUE;
3191 vm_memsize_constant_cache(
void)
3194 size_t size = rb_id_table_memsize(vm->constant_cache);
3196 rb_id_table_foreach(vm->constant_cache, vm_memsize_constant_cache_i, &size);
3207 at_exit = at_exit->next;
3216 vm_memsize_builtin_function_table(
const struct rb_builtin_function *builtin_function_table)
3224 vm_memsize(
const void *
ptr)
3230 rb_vm_memsize_waiting_fds(&vm->waiting_fds) +
3231 rb_st_memsize(vm->loaded_features_index) +
3232 rb_st_memsize(vm->loading_table) +
3233 rb_vm_memsize_postponed_job_queue() +
3234 rb_vm_memsize_workqueue(&vm->workqueue) +
3235 vm_memsize_at_exit_list(vm->at_exit) +
3236 rb_st_memsize(vm->ci_table) +
3237 rb_st_memsize(vm->frozen_strings) +
3238 vm_memsize_builtin_function_table(vm->builtin_function_table) +
3239 rb_id_table_memsize(vm->negative_cme_table) +
3240 rb_st_memsize(vm->overloaded_cme_table) +
3241 vm_memsize_constant_cache() +
3253 {0, 0, vm_memsize,},
3254 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3259 vm_default_params(
void)
3262 VALUE result = rb_hash_new_with_size(4);
3263 #define SET(name) rb_hash_aset(result, ID2SYM(rb_intern(#name)), SIZET2NUM(vm->default_params.name));
3264 SET(thread_vm_stack_size);
3265 SET(thread_machine_stack_size);
3266 SET(fiber_vm_stack_size);
3267 SET(fiber_machine_stack_size);
3274 get_param(
const char *name,
size_t default_value,
size_t min_value)
3277 size_t result = default_value;
3278 if ((envval = getenv(name)) != 0) {
3279 long val = atol(envval);
3280 if (val < (
long)min_value) {
3281 val = (long)min_value;
3283 result = (size_t)(((val -1 + RUBY_VM_SIZE_ALIGN) / RUBY_VM_SIZE_ALIGN) * RUBY_VM_SIZE_ALIGN);
3285 if (0) ruby_debug_printf(
"%s: %"PRIuSIZE
"\n", name, result);
3291 check_machine_stack_size(
size_t *sizep)
3293 #ifdef PTHREAD_STACK_MIN
3294 size_t size = *sizep;
3297 #ifdef PTHREAD_STACK_MIN
3298 if (size < (
size_t)PTHREAD_STACK_MIN) {
3299 *sizep = (size_t)PTHREAD_STACK_MIN * 2;
3305 vm_default_params_setup(
rb_vm_t *vm)
3307 vm->default_params.thread_vm_stack_size =
3308 get_param(
"RUBY_THREAD_VM_STACK_SIZE",
3309 RUBY_VM_THREAD_VM_STACK_SIZE,
3310 RUBY_VM_THREAD_VM_STACK_SIZE_MIN);
3312 vm->default_params.thread_machine_stack_size =
3313 get_param(
"RUBY_THREAD_MACHINE_STACK_SIZE",
3314 RUBY_VM_THREAD_MACHINE_STACK_SIZE,
3315 RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN);
3317 vm->default_params.fiber_vm_stack_size =
3318 get_param(
"RUBY_FIBER_VM_STACK_SIZE",
3319 RUBY_VM_FIBER_VM_STACK_SIZE,
3320 RUBY_VM_FIBER_VM_STACK_SIZE_MIN);
3322 vm->default_params.fiber_machine_stack_size =
3323 get_param(
"RUBY_FIBER_MACHINE_STACK_SIZE",
3324 RUBY_VM_FIBER_MACHINE_STACK_SIZE,
3325 RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN);
3328 check_machine_stack_size(&vm->default_params.thread_machine_stack_size);
3329 check_machine_stack_size(&vm->default_params.fiber_machine_stack_size);
3335 rb_vm_living_threads_init(vm);
3336 vm->thread_report_on_exception = 1;
3337 vm->src_encoding_index = -1;
3339 vm_default_params_setup(vm);
3349 VALUE *p = ec->vm_stack;
3350 VALUE *sp = ec->cfp->sp;
3354 for (i = 0; i < (long)(sp - p); i++) {
3357 if (ref != update) {
3362 while (cfp != limit_cfp) {
3363 const VALUE *ep = cfp->ep;
3368 if (!VM_ENV_LOCAL_P(ep)) {
3369 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3370 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3371 VM_FORCE_WRITE(&prev_ep[VM_ENV_DATA_INDEX_ENV],
rb_gc_location(prev_ep[VM_ENV_DATA_INDEX_ENV]));
3374 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3375 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ENV],
rb_gc_location(ep[VM_ENV_DATA_INDEX_ENV]));
3376 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ME_CREF],
rb_gc_location(ep[VM_ENV_DATA_INDEX_ME_CREF]));
3380 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3387 static enum rb_id_table_iterator_result
3388 mark_local_storage_i(
VALUE local,
void *data)
3391 return ID_TABLE_CONTINUE;
3400 VALUE *p = ec->vm_stack;
3401 VALUE *sp = ec->cfp->sp;
3405 VM_ASSERT(sp == ec->cfp->sp);
3406 rb_gc_mark_vm_stack_values((
long)(sp - p), p);
3408 while (cfp != limit_cfp) {
3409 const VALUE *ep = cfp->ep;
3410 VM_ASSERT(!!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) == vm_ep_in_heap_p_(ec, ep));
3412 if (VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
3417 if (!VM_ENV_LOCAL_P(ep)) {
3418 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3419 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3423 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3430 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3435 if (ec->machine.stack_start && ec->machine.stack_end &&
3438 rb_gc_mark_machine_context(ec);
3443 if (ec->local_storage) {
3444 rb_id_table_foreach_values(ec->local_storage, mark_local_storage_i, NULL);
3446 rb_gc_mark(ec->local_storage_recursive_hash);
3447 rb_gc_mark(ec->local_storage_recursive_hash_for_trace);
3455 void rb_threadptr_root_fiber_setup(
rb_thread_t *th);
3456 void rb_threadptr_root_fiber_release(
rb_thread_t *th);
3459 thread_compact(
void *
ptr)
3465 if (!th->root_fiber) {
3466 rb_execution_context_update(th->ec);
3471 thread_mark(
void *
ptr)
3474 RUBY_MARK_ENTER(
"thread");
3475 rb_fiber_mark_self(th->ec->fiber_ptr);
3478 switch (th->invoke_type) {
3479 case thread_invoke_type_proc:
3480 case thread_invoke_type_ractor_proc:
3484 case thread_invoke_type_func:
3495 rb_gc_mark(th->pending_interrupt_mask_stack);
3498 if (th->root_fiber) rb_fiber_mark_self(th->root_fiber);
3500 RUBY_ASSERT(th->ec == rb_fiberptr_get_ec(th->ec->fiber_ptr));
3508 rb_threadptr_interrupt_exec_task_mark(th);
3510 RUBY_MARK_LEAVE(
"thread");
3516 thread_free(
void *
ptr)
3519 RUBY_FREE_ENTER(
"thread");
3521 rb_threadptr_sched_free(th);
3523 if (th->locking_mutex !=
Qfalse) {
3524 rb_bug(
"thread_free: locking_mutex must be NULL (%p:%p)", (
void *)th, (
void *)th->locking_mutex);
3526 if (th->keeping_mutexes != NULL) {
3527 rb_bug(
"thread_free: keeping_mutexes must be NULL (%p:%p)", (
void *)th, (
void *)th->keeping_mutexes);
3532 rb_threadptr_root_fiber_release(th);
3534 if (th->vm && th->vm->ractor.main_thread == th) {
3535 RUBY_GC_INFO(
"MRI main thread\n");
3543 RUBY_FREE_LEAVE(
"thread");
3547 thread_memsize(
const void *
ptr)
3552 if (!th->root_fiber) {
3553 size += th->ec->vm_stack_size *
sizeof(
VALUE);
3555 if (th->ec->local_storage) {
3556 size += rb_id_table_memsize(th->ec->local_storage);
3561 #define thread_data_type ruby_threadptr_data_type
3570 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3574 rb_obj_is_thread(
VALUE obj)
3580 thread_alloc(
VALUE klass)
3589 ec->vm_stack = stack;
3590 ec->vm_stack_size = size;
3596 rb_ec_set_vm_stack(ec, stack, size);
3598 #if VM_CHECK_MODE > 0
3602 ec->cfp = (
void *)(ec->vm_stack + ec->vm_stack_size);
3606 VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME ,
3607 Qnil , VM_BLOCK_HANDLER_NONE ,
3609 0 , ec->vm_stack, 0, 0
3616 rb_ec_set_vm_stack(ec, NULL, 0);
3627 rb_threadptr_root_fiber_setup(th);
3631 th->scheduler =
Qnil;
3634 size_t size = vm->default_params.thread_vm_stack_size /
sizeof(
VALUE);
3635 rb_ec_initialize_vm_stack(th->ec,
ALLOC_N(
VALUE, size), size);
3638 VM_ASSERT(th->ec->cfp == NULL);
3639 VM_ASSERT(th->ec->vm_stack == NULL);
3640 VM_ASSERT(th->ec->vm_stack_size == 0);
3643 th->status = THREAD_RUNNABLE;
3644 th->last_status =
Qnil;
3645 th->top_wrapper = 0;
3646 th->top_self = vm->top_self;
3649 th->ec->errinfo =
Qnil;
3650 th->ec->root_svar =
Qfalse;
3651 th->ec->local_storage_recursive_hash =
Qnil;
3652 th->ec->local_storage_recursive_hash_for_trace =
Qnil;
3654 th->ec->storage =
Qnil;
3656 #if OPT_CALL_THREADED_CODE
3660 th->report_on_exception = vm->thread_report_on_exception;
3661 th->ext_config.ractor_safe =
true;
3663 ccan_list_head_init(&th->interrupt_exec_tasks);
3665 #if USE_RUBY_DEBUG_LOG
3669 RUBY_DEBUG_LOG(
"th:%u", th->serial);
3674 rb_thread_alloc(
VALUE klass)
3676 VALUE self = thread_alloc(klass);
3678 target_th->ractor = GET_RACTOR();
3679 th_init(target_th,
self, target_th->vm = GET_VM());
3683 #define REWIND_CFP(expr) do { \
3684 rb_execution_context_t *ec__ = GET_EC(); \
3685 VALUE *const curr_sp = (ec__->cfp++)->sp; \
3686 VALUE *const saved_sp = ec__->cfp->sp; \
3687 ec__->cfp->sp = curr_sp; \
3689 (ec__->cfp--)->sp = saved_sp; \
3716 rb_clear_method_cache(
self, mid);
3722 m_core_set_postexe(
VALUE self)
3731 core_hash_merge(
VALUE hash,
long argc,
const VALUE *argv)
3734 VM_ASSERT(argc % 2 == 0);
3740 m_core_hash_merge_ptr(
int argc,
VALUE *argv,
VALUE recv)
3742 VALUE hash = argv[0];
3744 REWIND_CFP(hash = core_hash_merge(hash, argc-1, argv+1));
3760 REWIND_CFP(hash = core_hash_merge_kwd(hash, kw));
3772 m_core_make_shareable_copy(
VALUE recv,
VALUE obj)
3780 return rb_ractor_ensure_shareable(obj, name);
3790 extern VALUE *rb_gc_stack_start;
3791 extern size_t rb_gc_stack_maxsize;
3799 rb_vm_bugreport(NULL, stderr);
3808 #ifdef HAVE_BACKTRACE
3809 #include <execinfo.h>
3810 #define MAX_NATIVE_TRACE 1024
3811 static void *trace[MAX_NATIVE_TRACE];
3812 int n = (int)backtrace(trace, MAX_NATIVE_TRACE);
3813 char **syms = backtrace_symbols(trace, n);
3820 for (i=0; i<n; i++) {
3828 #if VM_COLLECT_USAGE_DETAILS
3829 static VALUE usage_analysis_insn_start(
VALUE self);
3830 static VALUE usage_analysis_operand_start(
VALUE self);
3831 static VALUE usage_analysis_register_start(
VALUE self);
3832 static VALUE usage_analysis_insn_stop(
VALUE self);
3833 static VALUE usage_analysis_operand_stop(
VALUE self);
3834 static VALUE usage_analysis_register_stop(
VALUE self);
3835 static VALUE usage_analysis_insn_running(
VALUE self);
3836 static VALUE usage_analysis_operand_running(
VALUE self);
3837 static VALUE usage_analysis_register_running(
VALUE self);
3838 static VALUE usage_analysis_insn_clear(
VALUE self);
3839 static VALUE usage_analysis_operand_clear(
VALUE self);
3840 static VALUE usage_analysis_register_clear(
VALUE self);
3846 return rb_f_raise(c, v);
3895 vm_keep_script_lines(
VALUE self)
3897 return RBOOL(ruby_vm_keep_script_lines);
3912 vm_keep_script_lines_set(
VALUE self,
VALUE flags)
3914 ruby_vm_keep_script_lines =
RTEST(flags);
3943 #if USE_DEBUG_COUNTER
3965 rb_define_method(klass,
"make_shareable_copy", m_core_make_shareable_copy, 1);
3968 RBASIC_CLEAR_CLASS(klass);
3970 rb_vm_register_global_object(fcore);
3971 rb_mRubyVMFrozenCore = fcore;
4134 #if VM_COLLECT_USAGE_DETAILS
4136 #define define_usage_analysis_hash(name) \
4137 rb_define_const(rb_cRubyVM, "USAGE_ANALYSIS_" #name, rb_hash_new())
4138 define_usage_analysis_hash(
INSN);
4139 define_usage_analysis_hash(REGS);
4140 define_usage_analysis_hash(INSN_BIGRAM);
4162 #if OPT_DIRECT_THREADED_CODE
4164 #elif OPT_TOKEN_THREADED_CODE
4166 #elif OPT_CALL_THREADED_CODE
4170 #if OPT_OPERANDS_UNIFICATION
4173 #if OPT_INSTRUCTIONS_UNIFICATION
4176 #if OPT_INLINE_METHOD_CACHE
4184 rb_define_const(rb_cRubyVM,
"INSTRUCTION_NAMES", rb_insns_name_array());
4209 rb_vm_t *vm = ruby_current_vm_ptr;
4211 VALUE filename = rb_fstring_lit(
"<main>");
4212 const rb_iseq_t *iseq = rb_iseq_new(
Qnil, filename, filename,
Qnil, 0, ISEQ_TYPE_TOP);
4215 rb_ractor_main_setup(vm, th->ractor, th);
4222 vm->ractor.main_thread = th;
4223 vm->ractor.main_ractor = th->ractor;
4225 th->top_wrapper = 0;
4226 th->top_self = rb_vm_top_self();
4228 rb_vm_register_global_object((
VALUE)iseq);
4229 th->ec->cfp->iseq = iseq;
4230 th->ec->cfp->pc = ISEQ_BODY(iseq)->iseq_encoded;
4231 th->ec->cfp->self = th->top_self;
4233 VM_ENV_FLAGS_UNSET(th->ec->cfp->ep, VM_FRAME_FLAG_CFRAME);
4234 VM_STACK_ENV_WRITE(th->ec->cfp->ep, VM_ENV_DATA_INDEX_ME_CREF, (
VALUE)vm_cref_new(rb_cObject, METHOD_VISI_PRIVATE, FALSE, NULL, FALSE, FALSE));
4242 rb_objspace_gc_enable(vm->gc.objspace);
4245 vm_init_redefined_flag();
4249 OPTIMIZED_METHOD_TYPE_BLOCK_CALL, 0, METHOD_VISI_PUBLIC);
4251 rb_vm_register_global_object(rb_block_param_proxy);
4254 Init_vm_backtrace();
4258 rb_vm_set_progname(
VALUE filename)
4265 rb_iseq_pathobj_set(cfp->iseq, filename, rb_iseq_realpath(cfp->iseq));
4274 rb_vm_t *vm = ruby_mimcalloc(1,
sizeof(*vm));
4277 fputs(
"[FATAL] failed to allocate memory\n", stderr);
4284 rb_vm_postponed_job_queue_init(vm);
4285 ruby_current_vm_ptr = vm;
4286 rb_objspace_alloc();
4287 vm->negative_cme_table = rb_id_table_create(16);
4288 vm->overloaded_cme_table = st_init_numtable();
4289 vm->constant_cache = rb_id_table_create(0);
4290 vm->unused_block_warning_table = st_init_numtable();
4295 th->ractor = vm->ractor.main_ractor = rb_ractor_main_alloc();
4296 Init_native_thread(th);
4300 rb_ractor_set_current_ec(th->ractor, th->ec);
4302 ruby_thread_init_stack(th, native_main_thread_stack_top);
4308 vm_opt_method_def_table = st_init_numtable();
4309 vm_opt_mid_table = st_init_numtable();
4311 #ifdef RUBY_THREAD_WIN32_H
4319 native_main_thread_stack_top = addr;
4324 #include <sys/mman.h>
4328 #ifndef MARK_OBJECT_ARY_BUCKET_SIZE
4329 #define MARK_OBJECT_ARY_BUCKET_SIZE 1024
4339 pin_array_list_mark(
void *data)
4344 rb_gc_mark_vm_stack_values(array->len, array->array);
4348 pin_array_list_free(
void *data)
4351 xfree(array->array);
4355 pin_array_list_memsize(
const void *data)
4361 pin_array_list_update_references(
void *data)
4370 .dmark = pin_array_list_mark,
4371 .dfree = pin_array_list_free,
4372 .dsize = pin_array_list_memsize,
4373 .dcompact = pin_array_list_update_references,
4375 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
4379 pin_array_list_new(
VALUE next)
4384 array_list->array =
ALLOC_N(
VALUE, MARK_OBJECT_ARY_BUCKET_SIZE);
4394 if (array_list->len >= MARK_OBJECT_ARY_BUCKET_SIZE) {
4395 obj = pin_array_list_new(obj);
4399 RB_OBJ_WRITE(obj, &array_list->array[array_list->len], item);
4405 rb_vm_register_global_object(
VALUE obj)
4415 if (
FL_TEST(obj, RCLASS_IS_ROOT)) {
4418 FL_SET(obj, RCLASS_IS_ROOT);
4425 VALUE list = GET_VM()->mark_object_ary;
4426 VALUE head = pin_array_list_append(list, obj);
4428 GET_VM()->mark_object_ary = head;
4436 Init_vm_objects(
void)
4441 vm->mark_object_ary = pin_array_list_new(
Qnil);
4442 vm->loading_table = st_init_strtable();
4443 vm->ci_table = st_init_table(&vm_ci_hashtype);
4444 vm->frozen_strings = st_init_table_with_size(&rb_fstring_hash_type, 10000);
4449 void Init_builtin_yjit(
void) {}
4453 #include "yjit_hook.rbinc"
4457 void Init_builtin_rjit(
void) {}
4458 void Init_builtin_rjit_c(
void) {}
4464 main_to_s(
VALUE obj)
4470 rb_vm_top_self(
void)
4472 return GET_VM()->top_self;
4489 return &cr->verbose;
4499 bool rb_free_at_exit =
false;
4504 return rb_free_at_exit;
4513 rb_vm_fstring_table(
void)
4515 return GET_VM()->frozen_strings;
4518 #if VM_COLLECT_USAGE_DETAILS
4520 #define HASH_ASET(h, k, v) rb_hash_aset((h), (st_data_t)(k), (st_data_t)(v))
4534 vm_analysis_insn(
int insn)
4538 static int prev_insn = -1;
4544 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4545 CONST_ID(bigram_hash,
"USAGE_ANALYSIS_INSN_BIGRAM");
4549 HASH_ASET(uh,
INT2FIX(insn), ihash);
4557 if (prev_insn != -1) {
4576 vm_analysis_operand(
int insn,
int n,
VALUE op)
4586 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4591 HASH_ASET(uh,
INT2FIX(insn), ihash);
4595 HASH_ASET(ihash,
INT2FIX(n), ophash);
4598 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4608 vm_analysis_register(
int reg,
int isset)
4613 static const char regstrs[][5] = {
4621 static const char getsetstr[][4] = {
4625 static VALUE syms[
sizeof(regstrs) /
sizeof(regstrs[0])][2];
4629 CONST_ID(usage_hash,
"USAGE_ANALYSIS_REGS");
4634 for (i = 0; i < (int)(
sizeof(regstrs) /
sizeof(regstrs[0])); i++) {
4636 for (j = 0; j < 2; j++) {
4637 snprintf(buff, 0x10,
"%d %s %-4s", i, getsetstr[j], regstrs[i]);
4642 valstr = syms[reg][isset];
4653 static void (*ruby_vm_collect_usage_func_insn)(
int insn) = NULL;
4654 static void (*ruby_vm_collect_usage_func_operand)(
int insn,
int n,
VALUE op) = NULL;
4655 static void (*ruby_vm_collect_usage_func_register)(
int reg,
int isset) = NULL;
4659 usage_analysis_insn_start(
VALUE self)
4661 ruby_vm_collect_usage_func_insn = vm_analysis_insn;
4667 usage_analysis_operand_start(
VALUE self)
4669 ruby_vm_collect_usage_func_operand = vm_analysis_operand;
4675 usage_analysis_register_start(
VALUE self)
4677 ruby_vm_collect_usage_func_register = vm_analysis_register;
4683 usage_analysis_insn_stop(
VALUE self)
4685 ruby_vm_collect_usage_func_insn = 0;
4691 usage_analysis_operand_stop(
VALUE self)
4693 ruby_vm_collect_usage_func_operand = 0;
4699 usage_analysis_register_stop(
VALUE self)
4701 ruby_vm_collect_usage_func_register = 0;
4707 usage_analysis_insn_running(
VALUE self)
4709 return RBOOL(ruby_vm_collect_usage_func_insn != 0);
4714 usage_analysis_operand_running(
VALUE self)
4716 return RBOOL(ruby_vm_collect_usage_func_operand != 0);
4721 usage_analysis_register_running(
VALUE self)
4723 return RBOOL(ruby_vm_collect_usage_func_register != 0);
4727 usage_analysis_clear(
VALUE self,
ID usage_hash)
4739 usage_analysis_insn_clear(
VALUE self)
4744 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4745 CONST_ID(bigram_hash,
"USAGE_ANALYSIS_INSN_BIGRAM");
4746 usage_analysis_clear(rb_cRubyVM, usage_hash);
4747 return usage_analysis_clear(rb_cRubyVM, bigram_hash);
4752 usage_analysis_operand_clear(
VALUE self)
4756 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4757 return usage_analysis_clear(
self, usage_hash);
4762 usage_analysis_register_clear(
VALUE self)
4766 CONST_ID(usage_hash,
"USAGE_ANALYSIS_REGS");
4767 return usage_analysis_clear(
self, usage_hash);
4772 MAYBE_UNUSED(
static void (*ruby_vm_collect_usage_func_insn)(
int insn)) = 0;
4773 MAYBE_UNUSED(
static void (*ruby_vm_collect_usage_func_operand)(
int insn,
int n,
VALUE op)) = 0;
4774 MAYBE_UNUSED(
static void (*ruby_vm_collect_usage_func_register)(
int reg,
int isset)) = 0;
4778 #if VM_COLLECT_USAGE_DETAILS
4781 vm_collect_usage_insn(
int insn)
4783 if (RUBY_DTRACE_INSN_ENABLED()) {
4784 RUBY_DTRACE_INSN(rb_insns_name(insn));
4786 if (ruby_vm_collect_usage_func_insn)
4787 (*ruby_vm_collect_usage_func_insn)(insn);
4795 vm_collect_usage_operand(
int insn,
int n,
VALUE op)
4797 if (RUBY_DTRACE_INSN_OPERAND_ENABLED()) {
4800 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4802 RUBY_DTRACE_INSN_OPERAND(
RSTRING_PTR(valstr), rb_insns_name(insn));
4805 if (ruby_vm_collect_usage_func_operand)
4806 (*ruby_vm_collect_usage_func_operand)(insn, n, op);
4812 vm_collect_usage_register(
int reg,
int isset)
4814 if (ruby_vm_collect_usage_func_register)
4815 (*ruby_vm_collect_usage_func_register)(reg, isset);
4820 rb_vm_empty_cc(
void)
4822 return &vm_empty_cc;
4826 rb_vm_empty_cc_for_super(
void)
4828 return &vm_empty_cc_for_super;
4831 #include "vm_call_iseq_optimized.inc"
#define RUBY_ASSERT_MESG(expr,...)
Asserts that the expression is truthy.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define rb_define_method_id(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
uint32_t rb_event_flag_t
Represents event(s).
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define NUM2ULONG
Old name of RB_NUM2ULONG.
#define ALLOCV
Old name of RB_ALLOCV.
#define ALLOC
Old name of RB_ALLOC.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define ID2SYM
Old name of RB_ID2SYM.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define SYM2ID
Old name of RB_SYM2ID.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define rb_ary_new4
Old name of rb_ary_new_from_values.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define rb_exc_new2
Old name of rb_exc_new_cstr.
#define FIX2INT
Old name of RB_FIX2INT.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_SET
Old name of RB_FL_SET.
#define rb_exc_new3
Old name of rb_exc_new_str.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define NUM2ULL
Old name of RB_NUM2ULL.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void ruby_init_stack(void *addr)
Set stack bottom of Ruby implementation.
VALUE rb_eLocalJumpError
LocalJumpError exception.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
void rb_iter_break(void)
Breaks from a block.
VALUE rb_eTypeError
TypeError exception.
void rb_iter_break_value(VALUE val)
Identical to rb_iter_break(), except it additionally takes the "value" of this breakage.
VALUE rb_eRuntimeError
RuntimeError exception.
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.
VALUE rb_eArgError
ArgumentError exception.
VALUE * rb_ruby_debug_ptr(void)
This is an implementation detail of ruby_debug.
VALUE rb_eSysStackError
SystemStackError exception.
VALUE * rb_ruby_verbose_ptr(void)
This is an implementation detail of ruby_verbose.
@ RB_WARN_CATEGORY_PERFORMANCE
Warning is for performance issues (not enabled by -w).
VALUE rb_cTime
Time class.
VALUE rb_cArray
Array class.
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
VALUE rb_cInteger
Module class.
VALUE rb_cNilClass
NilClass class.
VALUE rb_cBinding
Binding class.
VALUE rb_cRegexp
Regexp class.
VALUE rb_cHash
Hash class.
VALUE rb_cFalseClass
FalseClass class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cSymbol
Symbol class.
VALUE rb_cBasicObject
BasicObject class.
VALUE rb_cThread
Thread class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
VALUE rb_cFloat
Float class.
VALUE rb_cProc
Proc class.
VALUE rb_cTrueClass
TrueClass class.
VALUE rb_cString
String class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
void rb_gc_mark(VALUE obj)
Marks an object.
void rb_mark_tbl_no_pin(struct st_table *tbl)
Identical to rb_mark_tbl(), except it marks objects using rb_gc_mark_movable().
void rb_memerror(void)
Triggers out-of-memory error.
void rb_gc_mark_movable(VALUE obj)
Maybe this is the only function provided for C extensions to control the pinning of objects,...
void rb_mark_tbl(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only values of the table and leave their associated keys...
void rb_gc_mark_maybe(VALUE obj)
Identical to rb_gc_mark(), except it allows the passed value be a non-object.
VALUE rb_gc_location(VALUE obj)
Finds a new "location" of an object.
void rb_gc_update_tbl_refs(st_table *ptr)
Updates references inside of tables.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_delete_at(VALUE ary, long pos)
Destructively removes an element which resides at the specific index of the passed array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
void rb_undef(VALUE mod, ID mid)
Inserts a method entry that hides previous method definition of the given name.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_set_end_proc(void(*func)(VALUE arg), VALUE arg)
Registers a function that shall run on process exit.
void rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash)
Inserts a list of key-value pairs into a hash table at once.
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_hash_dup(VALUE hash)
Duplicates a hash.
VALUE rb_hash_clear(VALUE hash)
Swipes everything out of the passed hash table.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
void rb_lastline_set(VALUE str)
Updates $_.
VALUE rb_lastline_get(void)
Queries the last line, or the $_.
void rb_backref_set(VALUE md)
Updates $~.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_block_lambda(void)
Identical to rb_proc_new(), except it returns a lambda.
VALUE rb_binding_new(void)
Snapshots the current execution context and turn it into an instance of rb_cBinding.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_str_cat_cstr(VALUE dst, const char *src)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
void rb_set_class_path(VALUE klass, VALUE space, const char *name)
Names a class.
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
void rb_alias_variable(ID dst, ID src)
Aliases a global variable.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
const char * rb_sourcefile(void)
Resembles __FILE__.
int rb_frame_method_id_and_class(ID *idp, VALUE *klassp)
Resembles __method__.
int rb_sourceline(void)
Resembles __LINE__.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
Assigns to an instance variable.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
VALUE rb_ractor_make_shareable_copy(VALUE obj)
Identical to rb_ractor_make_shareable(), except it returns a (deep) copy of the passed one instead of...
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
void ruby_vm_at_exit(void(*func)(ruby_vm_t *))
ruby_vm_at_exit registers a function func to be invoked when a VM passed away.
bool ruby_free_at_exit_p(void)
Returns whether the Ruby VM will free all memory at shutdown.
int ruby_vm_destruct(ruby_vm_t *vm)
Destructs the passed VM.
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Identical to rb_str_format(), except how the arguments are arranged.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS(obj)
Convenient casting macro.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
#define RB_NO_KEYWORDS
Do not pass keywords.
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 _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
IFUNC (Internal FUNCtion)
void rb_native_cond_initialize(rb_nativethread_cond_t *cond)
Fills the passed condition variable with an initial value.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_destroy.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
ruby_value_type
C-level type of an object.
void ruby_xfree(void *ptr)
Deallocates a storage instance.