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"
41 #include "vm_callinfo.h"
44 #include "vm_insnhelper.h"
45 #include "ractor_core.h"
52 #include "probes_helper.h"
54 #ifdef RUBY_ASSERT_CRITICAL_SECTION
55 int ruby_assert_critical_section_entered = 0;
58 static void *native_main_thread_stack_top;
60 VALUE rb_str_concat_literals(
size_t,
const VALUE*);
64 extern const char *
const rb_debug_counter_names[];
66 PUREFUNC(
static inline const VALUE *VM_EP_LEP(
const VALUE *));
67 static inline const VALUE *
68 VM_EP_LEP(
const VALUE *ep)
70 while (!VM_ENV_LOCAL_P(ep)) {
71 ep = VM_ENV_PREV_EP(ep);
89 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
97 rb_vm_ep_local_ep(
const VALUE *ep)
103 static inline const VALUE *
106 return VM_EP_LEP(cfp->ep);
109 static inline const VALUE *
112 return VM_ENV_PREV_EP(cfp->ep);
119 const VALUE *ep = VM_CF_LEP(cfp);
120 return VM_ENV_BLOCK_HANDLER(ep);
126 return VM_FRAME_CFRAME_KW_P(cfp);
132 return VM_CF_BLOCK_HANDLER(cfp);
135 #if VM_CHECK_MODE > 0
139 const VALUE *start = ec->vm_stack;
140 const VALUE *end = (
VALUE *)ec->vm_stack + ec->vm_stack_size;
141 VM_ASSERT(start != NULL);
143 if (start <= (
VALUE *)cfp && (
VALUE *)cfp < end) {
154 const VALUE *start = ec->vm_stack;
156 VM_ASSERT(start != NULL);
158 if (start <= ep && ep < end) {
169 if (VM_EP_IN_HEAP_P(ec, ep)) {
170 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
172 if (!UNDEF_P(envval)) {
175 VM_ASSERT(imemo_type_p(envval, imemo_env));
176 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
177 VM_ASSERT(env->ep == ep);
187 rb_vm_ep_in_heap_p(
const VALUE *ep)
190 if (ec->vm_stack == NULL)
return TRUE;
191 return vm_ep_in_heap_p_(ec, ep);
198 VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), cfp));
206 VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), cfp));
215 return VM_TAGGED_PTR_REF(block_handler, 0x03) == captured;
221 VALUE block_handler = ec->passed_block_handler;
222 ec->passed_block_handler = VM_BLOCK_HANDLER_NONE;
223 vm_block_handler_verify(block_handler);
224 return block_handler;
228 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)
231 int omod_shared = FALSE;
239 scope_visi.visi.method_visi = visi;
240 scope_visi.visi.module_func = module_func;
243 if (prev_cref != NULL && prev_cref != (
void *)1 ) {
244 refinements = CREF_REFINEMENTS(prev_cref);
246 if (!
NIL_P(refinements)) {
248 CREF_OMOD_SHARED_SET(prev_cref);
252 VM_ASSERT(singleton || klass);
255 cref->klass_or_self = klass;
256 cref->next = use_prev_prev ? CREF_NEXT(prev_cref) : prev_cref;
259 if (pushed_by_eval) CREF_PUSHED_BY_EVAL_SET(cref);
260 if (omod_shared) CREF_OMOD_SHARED_SET(cref);
261 if (singleton) CREF_SINGLETON_SET(cref);
267 vm_cref_new(
VALUE klass, rb_method_visibility_t visi,
int module_func,
rb_cref_t *prev_cref,
int pushed_by_eval,
int singleton)
269 return vm_cref_new0(klass, visi, module_func, prev_cref, pushed_by_eval, FALSE, singleton);
273 vm_cref_new_use_prev(
VALUE klass, rb_method_visibility_t visi,
int module_func,
rb_cref_t *prev_cref,
int pushed_by_eval)
275 return vm_cref_new0(klass, visi, module_func, prev_cref, pushed_by_eval, TRUE, FALSE);
281 return SYMBOL_P(key) ? ST_DELETE : ST_CONTINUE;
288 rb_cref_t *next_cref = CREF_NEXT(cref), *new_cref;
289 int pushed_by_eval = CREF_PUSHED_BY_EVAL(cref);
290 int singleton = CREF_SINGLETON(cref);
292 new_cref = vm_cref_new(cref->klass_or_self, visi->method_visi, visi->module_func, next_cref, pushed_by_eval, singleton);
294 if (!
NIL_P(CREF_REFINEMENTS(cref))) {
297 CREF_REFINEMENTS_SET(new_cref, ref);
298 CREF_OMOD_SHARED_UNSET(new_cref);
306 rb_vm_cref_dup_without_refinements(
const rb_cref_t *cref)
309 rb_cref_t *next_cref = CREF_NEXT(cref), *new_cref;
310 int pushed_by_eval = CREF_PUSHED_BY_EVAL(cref);
311 int singleton = CREF_SINGLETON(cref);
313 new_cref = vm_cref_new(cref->klass_or_self, visi->method_visi, visi->module_func, next_cref, pushed_by_eval, singleton);
315 if (!
NIL_P(CREF_REFINEMENTS(cref))) {
316 CREF_REFINEMENTS_SET(new_cref,
Qnil);
317 CREF_OMOD_SHARED_UNSET(new_cref);
326 rb_cref_t *cref = vm_cref_new(rb_cObject, METHOD_VISI_PRIVATE , FALSE, NULL, FALSE, FALSE);
327 VALUE top_wrapper = rb_ec_thread_ptr(ec)->top_wrapper;
330 cref = vm_cref_new(top_wrapper, METHOD_VISI_PRIVATE, FALSE, cref, FALSE, FALSE);
337 rb_vm_cref_new_toplevel(
void)
339 return vm_cref_new_toplevel(GET_EC());
343 vm_cref_dump(
const char *mesg,
const rb_cref_t *cref)
345 ruby_debug_printf(
"vm_cref_dump: %s (%p)\n", mesg, (
void *)cref);
349 cref = CREF_NEXT(cref);
356 *((
const VALUE **)&dst->as.captured.ep) = ep;
364 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, env->iseq);
365 rb_vm_block_ep_update(bindval, &bind->block, env->ep);
368 #if VM_COLLECT_USAGE_DETAILS
369 static void vm_collect_usage_operand(
int insn,
int n,
VALUE op);
370 static void vm_collect_usage_insn(
int insn);
371 static void vm_collect_usage_register(
int reg,
int isset);
376 int argc,
const VALUE *argv,
int kw_splat,
VALUE block_handler,
382 static uint64_t yjit_total_entry_hits = 0;
385 #define YJIT_CALL_COUNT_INTERV 20u
389 rb_yjit_threshold_hit(
const rb_iseq_t *iseq, uint64_t entry_calls)
391 yjit_total_entry_hits += 1;
394 if (entry_calls + YJIT_CALL_COUNT_INTERV == rb_yjit_call_threshold) {
395 iseq->body->yjit_calls_at_interv = yjit_total_entry_hits;
400 if (entry_calls == rb_yjit_call_threshold) {
402 if (rb_yjit_call_threshold < YJIT_CALL_COUNT_INTERV) {
406 uint64_t num_calls = yjit_total_entry_hits - iseq->body->yjit_calls_at_interv;
409 if (num_calls > rb_yjit_cold_threshold) {
410 rb_yjit_incr_counter(
"cold_iseq_entry");
420 #define rb_yjit_threshold_hit(iseq, entry_calls) false
423 #if USE_RJIT || USE_YJIT
432 static inline rb_jit_func_t
437 bool yjit_enabled = rb_yjit_enabled_p;
438 if (!(yjit_enabled || rb_rjit_call_p)) {
443 if (body->jit_entry == NULL) {
444 body->jit_entry_calls++;
446 if (rb_yjit_threshold_hit(iseq, body->jit_entry_calls)) {
447 rb_yjit_compile_iseq(iseq, ec,
false);
450 else if (body->jit_entry_calls == rb_rjit_call_threshold()) {
451 rb_rjit_compile(iseq);
454 return body->jit_entry;
461 rb_jit_func_t func = jit_compile(ec);
464 return func(ec, ec->cfp);
471 # define jit_compile(ec) ((rb_jit_func_t)0)
472 # define jit_exec(ec) Qundef
482 static inline rb_jit_func_t
487 if (!rb_yjit_enabled_p) {
492 if (body->jit_exception == NULL) {
493 body->jit_exception_calls++;
494 if (body->jit_exception_calls == rb_yjit_call_threshold) {
495 rb_yjit_compile_iseq(iseq, ec,
true);
499 return body->jit_exception;
506 rb_jit_func_t func = jit_compile_exception(ec);
509 return func(ec, ec->cfp);
516 # define jit_compile_exception(ec) ((rb_jit_func_t)0)
517 # define jit_exec_exception(ec) Qundef
522 #define RB_TYPE_2_P(obj, type1, type2) \
523 (RB_TYPE_P(obj, type1) || RB_TYPE_P(obj, type2))
524 #define RB_TYPE_3_P(obj, type1, type2, type3) \
525 (RB_TYPE_P(obj, type1) || RB_TYPE_P(obj, type2) || RB_TYPE_P(obj, type3))
527 #define VM_ASSERT_TYPE(obj, type) \
528 VM_ASSERT(RB_TYPE_P(obj, type), #obj ": %s", rb_obj_info(obj))
529 #define VM_ASSERT_TYPE2(obj, type1, type2) \
530 VM_ASSERT(RB_TYPE_2_P(obj, type1, type2), #obj ": %s", rb_obj_info(obj))
531 #define VM_ASSERT_TYPE3(obj, type1, type2, type3) \
532 VM_ASSERT(RB_TYPE_3_P(obj, type1, type2, type3), #obj ": %s", rb_obj_info(obj))
534 #include "vm_insnhelper.c"
538 #include "vm_method.c"
545 VALUE rb_mRubyVMFrozenCore;
546 VALUE rb_block_param_proxy;
548 VALUE ruby_vm_const_missing_count = 0;
549 rb_vm_t *ruby_current_vm_ptr = NULL;
551 bool ruby_vm_keep_script_lines;
553 #ifdef RB_THREAD_LOCAL_SPECIFIER
556 #ifdef RUBY_NT_SERIAL
557 RB_THREAD_LOCAL_SPECIFIER
rb_atomic_t ruby_nt_serial;
562 rb_current_ec_noinline(
void)
564 return ruby_current_ec;
570 ruby_current_ec = ec;
578 return ruby_current_ec;
583 native_tls_key_t ruby_current_ec_key;
587 rb_current_ec_noinline(
void)
589 return native_tls_get(ruby_current_ec_key);
596 unsigned int ruby_vm_event_local_num;
598 rb_serial_t ruby_vm_constant_cache_invalidations = 0;
599 rb_serial_t ruby_vm_constant_cache_misses = 0;
600 rb_serial_t ruby_vm_global_cvar_state = 1;
603 .flags =
T_IMEMO | (imemo_callcache <<
FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
606 .call_ = vm_call_general,
612 static const struct rb_callcache vm_empty_cc_for_super = {
613 .flags =
T_IMEMO | (imemo_callcache <<
FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
616 .call_ = vm_call_super_method,
622 static void thread_free(
void *
ptr);
625 rb_vm_inc_const_missing_count(
void)
627 ruby_vm_const_missing_count +=1;
636 if (!ec) ec = GET_EC();
637 if (!rb_ec_frame_method_id_and_class(ec, &
id, 0, &klass) || !klass)
641 klass =
RBASIC(klass)->klass;
643 else if (RCLASS_SINGLETON_P(klass)) {
644 klass = RCLASS_ATTACHED_OBJECT(klass);
645 if (
NIL_P(klass))
return FALSE;
650 const char *classname, *filename;
652 if (methodname && (filename = rb_source_location_cstr(&args->line_no)) != 0) {
654 classname =
"<unknown>";
655 args->classname = classname;
656 args->methodname = methodname;
657 args->filename = filename;
666 extern unsigned int redblack_buffer_size;
694 static VALUE sym_constant_cache_invalidations, sym_constant_cache_misses, sym_global_cvar_state, sym_next_shape_id;
695 static VALUE sym_shape_cache_size;
712 #define S(s) sym_##s = ID2SYM(rb_intern_const(#s))
713 S(constant_cache_invalidations);
714 S(constant_cache_misses);
715 S(global_cvar_state);
720 #define SET(name, attr) \
721 if (key == sym_##name) \
722 return SERIALT2NUM(attr); \
723 else if (hash != Qnil) \
724 rb_hash_aset(hash, sym_##name, SERIALT2NUM(attr));
726 SET(constant_cache_invalidations, ruby_vm_constant_cache_invalidations);
727 SET(constant_cache_misses, ruby_vm_constant_cache_misses);
728 SET(global_cvar_state, ruby_vm_global_cvar_state);
729 SET(next_shape_id, (rb_serial_t)GET_SHAPE_TREE()->next_shape_id);
730 SET(shape_cache_size, (rb_serial_t)GET_SHAPE_TREE()->cache_size);
733 #if USE_DEBUG_COUNTER
734 ruby_debug_counter_show_at_exit(FALSE);
735 for (
size_t i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
736 const VALUE name = rb_sym_intern_ascii_cstr(rb_debug_counter_names[i]);
742 else if (hash !=
Qnil) {
760 if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_TOP) {
765 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH, rb_ec_thread_ptr(ec)->top_self,
766 VM_BLOCK_HANDLER_NONE,
767 (
VALUE)vm_cref_new_toplevel(ec),
768 ISEQ_BODY(iseq)->iseq_encoded, ec->cfp->sp,
769 ISEQ_BODY(iseq)->local_table_size, ISEQ_BODY(iseq)->stack_max);
775 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_EVAL | VM_FRAME_FLAG_FINISH,
776 vm_block_self(base_block), VM_GUARDED_PREV_EP(vm_block_ep(base_block)),
778 ISEQ_BODY(iseq)->iseq_encoded,
779 ec->cfp->sp, ISEQ_BODY(iseq)->local_table_size,
780 ISEQ_BODY(iseq)->stack_max);
789 GetBindingPtr(toplevel_binding, bind);
792 vm_set_eval_stack(ec, iseq, 0, &bind->block);
795 if (ISEQ_BODY(iseq)->local_table_size > 0) {
796 vm_bind_update_env(toplevel_binding, bind, vm_make_env_object(ec, ec->cfp));
803 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
807 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
815 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
816 if (VM_FRAME_RUBYFRAME_P(cfp)) {
819 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
827 if (VM_FRAME_RUBYFRAME_P(cfp)) {
831 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
833 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
834 if (VM_FRAME_RUBYFRAME_P(cfp)) {
838 if (VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_PASSED) == FALSE) {
841 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
847 rb_vm_pop_cfunc_frame(
void)
854 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
855 vm_pop_frame(ec, cfp, cfp->ep);
862 while (ec->cfp != cfp) {
864 printf(
"skipped frame: %s\n", vm_frametype_name(ec->cfp));
866 if (VM_FRAME_TYPE(ec->cfp) != VM_FRAME_MAGIC_CFUNC) {
870 rb_vm_pop_cfunc_frame();
883 nl->next = vm->at_exit;
888 ruby_vm_run_at_exit_hooks(
rb_vm_t *vm)
894 rb_vm_at_exit_func *func = l->func;
908 fputs(
"---\n", stderr);
909 ruby_debug_printf(
"envptr: %p\n", (
void *)&env->ep[0]);
910 ruby_debug_printf(
"envval: %10p ", (
void *)env->ep[1]);
912 ruby_debug_printf(
"ep: %10p\n", (
void *)env->ep);
913 if (rb_vm_env_prev_env(env)) {
914 fputs(
">>\n", stderr);
915 check_env_value(rb_vm_env_prev_env(env));
916 fputs(
"<<\n", stderr);
922 check_env_value(
const rb_env_t *env)
924 if (check_env(env)) {
934 switch (vm_block_handler_type(block_handler)) {
935 case block_handler_type_ifunc:
936 case block_handler_type_iseq:
937 return rb_vm_make_proc(ec, VM_BH_TO_CAPT_BLOCK(block_handler),
rb_cProc);
939 case block_handler_type_symbol:
940 case block_handler_type_proc:
941 return block_handler;
943 VM_UNREACHABLE(vm_block_handler_escape);
950 const VALUE *
const ep = cfp->ep;
951 VALUE *env_body, *env_ep;
952 int local_size, env_size;
954 if (VM_ENV_ESCAPED_P(ep)) {
955 return VM_ENV_ENVVAL(ep);
958 if (!VM_ENV_LOCAL_P(ep)) {
959 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
960 if (!VM_ENV_ESCAPED_P(prev_ep)) {
963 while (prev_cfp->ep != prev_ep) {
964 prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(prev_cfp);
965 VM_ASSERT(prev_cfp->ep != NULL);
968 vm_make_env_each(ec, prev_cfp);
969 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_SPECVAL], VM_GUARDED_PREV_EP(prev_cfp->ep));
973 VALUE block_handler = VM_ENV_BLOCK_HANDLER(ep);
975 if (block_handler != VM_BLOCK_HANDLER_NONE) {
976 VALUE blockprocval = vm_block_handler_escape(ec, block_handler);
977 VM_STACK_ENV_WRITE(ep, VM_ENV_DATA_INDEX_SPECVAL, blockprocval);
981 if (!VM_FRAME_RUBYFRAME_P(cfp)) {
982 local_size = VM_ENV_DATA_SIZE;
985 local_size = ISEQ_BODY(cfp->iseq)->local_table_size;
986 if (ISEQ_BODY(cfp->iseq)->param.flags.forwardable && VM_ENV_LOCAL_P(cfp->ep)) {
987 int ci_offset = local_size - ISEQ_BODY(cfp->iseq)->param.size + VM_ENV_DATA_SIZE;
990 local_size += vm_ci_argc(ci);
992 local_size += VM_ENV_DATA_SIZE;
1007 env_size = local_size +
1015 MEMCPY(env_body, ep - (local_size - 1 ),
VALUE, local_size);
1017 env_ep = &env_body[local_size - 1 ];
1018 env_ep[VM_ENV_DATA_INDEX_ENV] = (
VALUE)env;
1020 env->iseq = (
rb_iseq_t *)(VM_FRAME_RUBYFRAME_P(cfp) ? cfp->iseq : NULL);
1022 env->env = env_body;
1023 env->env_size = env_size;
1026 VM_ENV_FLAGS_SET(env_ep, VM_ENV_FLAG_ESCAPED | VM_ENV_FLAG_WB_REQUIRED);
1027 VM_STACK_ENV_WRITE(ep, 0, (
VALUE)env);
1030 for (i = 0; i < local_size; i++) {
1031 if (VM_FRAME_RUBYFRAME_P(cfp)) {
1033 ep[-local_size + i] = 0;
1040 rb_yjit_invalidate_ep_is_bp(env->iseq);
1049 VALUE envval = vm_make_env_each(ec, cfp);
1052 check_env_value((
const rb_env_t *)envval);
1062 while ((cfp = rb_vm_get_binding_creatable_next_cfp(ec, cfp)) != 0) {
1063 vm_make_env_object(ec, cfp);
1064 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1069 rb_vm_env_prev_env(
const rb_env_t *env)
1071 const VALUE *ep = env->ep;
1073 if (VM_ENV_LOCAL_P(ep)) {
1077 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
1078 return VM_ENV_ENVVAL_PTR(prev_ep);
1086 if (!iseq)
return 0;
1087 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1088 local_var_list_add(vars, ISEQ_BODY(iseq)->local_table[i]);
1097 if (VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_ISOLATED))
break;
1098 collect_local_variables_in_iseq(env->iseq, vars);
1099 }
while ((env = rb_vm_env_prev_env(env)) != NULL);
1105 if (VM_ENV_ESCAPED_P(ep)) {
1106 collect_local_variables_in_env(VM_ENV_ENVVAL_PTR(ep), vars);
1115 rb_vm_env_local_variables(
const rb_env_t *env)
1118 local_var_list_init(&vars);
1119 collect_local_variables_in_env(env, &vars);
1120 return local_var_list_finish(&vars);
1124 rb_iseq_local_variables(
const rb_iseq_t *iseq)
1127 local_var_list_init(&vars);
1128 while (collect_local_variables_in_iseq(iseq, &vars)) {
1129 iseq = ISEQ_BODY(iseq)->parent_iseq;
1131 return local_var_list_finish(&vars);
1137 vm_proc_create_from_captured(
VALUE klass,
1139 enum rb_block_type block_type,
1140 int8_t is_from_method, int8_t is_lambda)
1142 VALUE procval = rb_proc_alloc(klass);
1145 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), captured->ep));
1148 RB_OBJ_WRITE(procval, &proc->block.as.captured.code.val, captured->code.val);
1149 RB_OBJ_WRITE(procval, &proc->block.as.captured.self, captured->self);
1150 rb_vm_block_ep_update(procval, &proc->block, captured->ep);
1152 vm_block_type_set(&proc->block, block_type);
1153 proc->is_from_method = is_from_method;
1154 proc->is_lambda = is_lambda;
1163 switch (vm_block_type(src)) {
1164 case block_type_iseq:
1165 case block_type_ifunc:
1166 RB_OBJ_WRITE(obj, &dst->as.captured.self, src->as.captured.self);
1167 RB_OBJ_WRITE(obj, &dst->as.captured.code.val, src->as.captured.code.val);
1168 rb_vm_block_ep_update(obj, dst, src->as.captured.ep);
1170 case block_type_symbol:
1173 case block_type_proc:
1180 proc_create(
VALUE klass,
const struct rb_block *block, int8_t is_from_method, int8_t is_lambda)
1182 VALUE procval = rb_proc_alloc(klass);
1185 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), vm_block_ep(block)));
1186 rb_vm_block_copy(procval, &proc->block, block);
1187 vm_block_type_set(&proc->block, block->type);
1188 proc->is_from_method = is_from_method;
1189 proc->is_lambda = is_lambda;
1195 rb_proc_dup(
VALUE self)
1200 GetProcPtr(
self, src);
1201 procval = proc_create(
rb_obj_class(
self), &src->block, src->is_from_method, src->is_lambda);
1217 if (SIZEOF_VOIDP > SIZEOF_LONG)
1226 if (SIZEOF_VOIDP > SIZEOF_LONG)
1232 static enum rb_id_table_iterator_result
1233 collect_outer_variable_names(
ID id,
VALUE val,
void *
ptr)
1242 if (data->isolate ||
1247 store = &data->read_only;
1252 return ID_TABLE_CONTINUE;
1256 env_copy(
const VALUE *src_ep,
VALUE read_only_variables)
1259 VM_ASSERT(src_env->ep == src_ep);
1262 VALUE *ep = &env_body[src_env->env_size - 2];
1263 const rb_env_t *copied_env = vm_env_new(ep, env_body, src_env->env_size, src_env->iseq);
1266 RB_OBJ_WRITE(copied_env, &ep[VM_ENV_DATA_INDEX_ME_CREF], src_ep[VM_ENV_DATA_INDEX_ME_CREF]);
1267 ep[VM_ENV_DATA_INDEX_FLAGS] = src_ep[VM_ENV_DATA_INDEX_FLAGS] | VM_ENV_FLAG_ISOLATED;
1268 if (!VM_ENV_LOCAL_P(src_ep)) {
1269 VM_ENV_FLAGS_SET(ep, VM_ENV_FLAG_LOCAL);
1272 if (read_only_variables) {
1273 for (
int i=
RARRAY_LENINT(read_only_variables)-1; i>=0; i--) {
1276 for (
unsigned int j=0; j<ISEQ_BODY(src_env->iseq)->local_table_size; j++) {
1277 if (
id == ISEQ_BODY(src_env->iseq)->local_table[j]) {
1278 VALUE v = src_env->env[j];
1281 VALUE msg =
rb_sprintf(
"can not make shareable Proc because it can refer"
1282 " unshareable object %+" PRIsVALUE
" from ", v);
1284 rb_str_catf(msg,
"variable '%" PRIsVALUE
"'", name);
1297 if (!VM_ENV_LOCAL_P(src_ep)) {
1298 const VALUE *prev_ep = VM_ENV_PREV_EP(src_env->ep);
1299 const rb_env_t *new_prev_env = env_copy(prev_ep, read_only_variables);
1300 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_GUARDED_PREV_EP(new_prev_env->ep);
1302 VM_ENV_FLAGS_UNSET(ep, VM_ENV_FLAG_LOCAL);
1305 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
1315 const rb_env_t *env = env_copy(captured->ep, read_only_variables);
1316 *((
const VALUE **)&proc->block.as.captured.ep) = env->ep;
1321 proc_shared_outer_variables(
struct rb_id_table *outer_variables,
bool isolate,
const char *message)
1329 rb_id_table_foreach(outer_variables, collect_outer_variable_names, (
void *)&data);
1331 if (data.ary !=
Qfalse) {
1332 VALUE str =
rb_sprintf(
"can not %s because it accesses outer variables", message);
1333 VALUE ary = data.ary;
1334 const char *sep =
" (";
1337 if (!name)
continue;
1346 else if (data.yield) {
1350 return data.read_only;
1354 rb_proc_isolate_bang(
VALUE self)
1356 const rb_iseq_t *iseq = vm_proc_iseq(
self);
1362 if (ISEQ_BODY(iseq)->outer_variables) {
1363 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables,
true,
"isolate a Proc");
1366 proc_isolate_env(
self, proc,
Qfalse);
1367 proc->is_isolated = TRUE;
1375 rb_proc_isolate(
VALUE self)
1377 VALUE dst = rb_proc_dup(
self);
1378 rb_proc_isolate_bang(dst);
1383 rb_proc_ractor_make_shareable(
VALUE self)
1385 const rb_iseq_t *iseq = vm_proc_iseq(
self);
1393 "Proc's self is not shareable: %" PRIsVALUE,
1399 if (ISEQ_BODY(iseq)->outer_variables) {
1400 read_only_variables =
1401 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables,
false,
"make a Proc shareable");
1404 proc_isolate_env(
self, proc, read_only_variables);
1405 proc->is_isolated = TRUE;
1416 enum imemo_type code_type = imemo_type(captured->code.val);
1418 if (!VM_ENV_ESCAPED_P(captured->ep)) {
1420 vm_make_env_object(ec, cfp);
1423 VM_ASSERT(VM_EP_IN_HEAP_P(ec, captured->ep));
1424 VM_ASSERT(code_type == imemo_iseq || code_type == imemo_ifunc);
1426 procval = vm_proc_create_from_captured(klass, captured,
1427 code_type == imemo_iseq ? block_type_iseq : block_type_ifunc,
1430 if (code_type == imemo_ifunc) {
1432 if (ifunc->svar_lep) {
1433 VALUE ep0 = ifunc->svar_lep[0];
1437 ifunc->svar_lep = (
VALUE *)env->ep;
1441 if (ep0 & VM_ENV_FLAG_ESCAPED) {
1445 ifunc->svar_lep = NULL;
1461 VALUE bindval, envval;
1464 if (cfp == 0 || ruby_level_cfp == 0) {
1467 if (!VM_FRAME_RUBYFRAME_P(src_cfp) &&
1468 !VM_FRAME_RUBYFRAME_P(RUBY_VM_PREVIOUS_CONTROL_FRAME(src_cfp))) {
1472 envval = vm_make_env_object(ec, cfp);
1474 GetBindingPtr(bindval, bind);
1475 vm_bind_update_env(bindval, bind, envval);
1476 RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, cfp->self);
1477 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, cfp->iseq);
1478 RB_OBJ_WRITE(bindval, &bind->pathobj, ISEQ_BODY(ruby_level_cfp->iseq)->location.pathobj);
1479 bind->first_lineno = rb_vm_get_sourceline(ruby_level_cfp);
1485 rb_binding_add_dynavars(
VALUE bindval,
rb_binding_t *bind,
int dyncount,
const ID *dynvars)
1487 VALUE envval, pathobj = bind->pathobj;
1488 VALUE path = pathobj_path(pathobj);
1489 VALUE realpath = pathobj_realpath(pathobj);
1496 if (dyncount < 0)
return 0;
1498 base_block = &bind->block;
1499 base_iseq = vm_block_iseq(base_block);
1503 dyns->size = dyncount;
1504 MEMCPY(dyns->ids, dynvars,
ID, dyncount);
1506 rb_node_init(RNODE(&tmp_node), NODE_SCOPE);
1507 tmp_node.nd_tbl = dyns;
1508 tmp_node.nd_body = 0;
1509 tmp_node.nd_args = 0;
1511 VALUE ast_value = rb_ruby_ast_new(RNODE(&tmp_node));
1514 iseq = rb_iseq_new(ast_value, ISEQ_BODY(base_iseq)->location.label, path, realpath, base_iseq, ISEQ_TYPE_EVAL);
1517 VALUE tempstr = rb_fstring_lit(
"<temp>");
1518 iseq = rb_iseq_new_top(ast_value, tempstr, tempstr, tempstr, NULL);
1520 tmp_node.nd_tbl = 0;
1523 vm_set_eval_stack(ec, iseq, 0, base_block);
1524 vm_bind_update_env(bindval, bind, envval = vm_make_env_object(ec, ec->cfp));
1525 rb_vm_pop_frame(ec);
1536 int arg_size = ISEQ_BODY(iseq)->param.size;
1538 vm_push_frame(ec, iseq,
type | VM_FRAME_FLAG_FINISH,
self,
1539 VM_GUARDED_PREV_EP(captured->ep),
1541 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1542 ec->cfp->sp + arg_size,
1543 ISEQ_BODY(iseq)->local_table_size - arg_size,
1544 ISEQ_BODY(iseq)->stack_max);
1551 int arg_size = ISEQ_BODY(iseq)->param.size;
1553 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
1555 vm_push_frame(ec, iseq,
type | VM_FRAME_FLAG_BMETHOD,
self,
1556 VM_GUARDED_PREV_EP(captured->ep),
1558 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1559 ec->cfp->sp + 1 + arg_size,
1560 ISEQ_BODY(iseq)->local_table_size - arg_size,
1561 ISEQ_BODY(iseq)->stack_max);
1563 VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);
1566 ALWAYS_INLINE(
static VALUE
1568 VALUE self,
int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler,
1573 VALUE self,
int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler,
1576 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
1578 VALUE type = VM_FRAME_MAGIC_BLOCK | (is_lambda ? VM_FRAME_FLAG_LAMBDA : 0);
1580 VALUE *sp = cfp->sp;
1581 int flags = (kw_splat ? VM_CALL_KW_SPLAT : 0);
1587 if (UNLIKELY(argc > VM_ARGC_STACK_MAX) &&
1588 (VM_ARGC_STACK_MAX >= 1 ||
1590 (argc != 1 || is_lambda))) {
1591 use_argv = vm_argv_ruby_array(av, argv, &flags, &argc, kw_splat);
1594 CHECK_VM_STACK_OVERFLOW(cfp, argc + 1);
1595 vm_check_canary(ec, sp);
1597 VALUE *stack_argv = sp;
1602 cfp->sp = stack_argv + argc;
1605 opt_pc = vm_yield_setup_args(ec, iseq, argc, stack_argv, flags, passed_block_handler,
1606 (is_lambda ? arg_setup_method : arg_setup_block));
1610 invoke_block(ec, iseq,
self, captured, cref,
type, opt_pc);
1613 invoke_bmethod(ec, iseq,
self, captured, me,
type, opt_pc);
1621 int argc,
const VALUE *argv,
1623 int is_lambda,
int force_blockarg)
1626 switch (vm_block_handler_type(block_handler)) {
1627 case block_handler_type_iseq:
1630 return invoke_iseq_block_from_c(ec, captured, captured->self,
1631 argc, argv, kw_splat, passed_block_handler,
1632 cref, is_lambda, NULL);
1634 case block_handler_type_ifunc:
1635 return vm_yield_with_cfunc(ec, VM_BH_TO_IFUNC_BLOCK(block_handler),
1636 VM_BH_TO_IFUNC_BLOCK(block_handler)->
self,
1637 argc, argv, kw_splat, passed_block_handler, NULL);
1638 case block_handler_type_symbol:
1639 return vm_yield_with_symbol(ec, VM_BH_TO_SYMBOL(block_handler),
1640 argc, argv, kw_splat, passed_block_handler);
1641 case block_handler_type_proc:
1642 if (force_blockarg == FALSE) {
1643 is_lambda = block_proc_is_lambda(VM_BH_TO_PROC(block_handler));
1645 block_handler = vm_proc_to_block_handler(VM_BH_TO_PROC(block_handler));
1648 VM_UNREACHABLE(invoke_block_from_c_splattable);
1655 VALUE block_handler = VM_CF_BLOCK_HANDLER(ec->cfp);
1656 vm_block_handler_verify(block_handler);
1657 if (UNLIKELY(block_handler == VM_BLOCK_HANDLER_NONE)) {
1658 rb_vm_localjump_error(
"no block given",
Qnil, 0);
1661 return block_handler;
1667 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1668 argc, argv, kw_splat, VM_BLOCK_HANDLER_NONE,
1669 cref, is_lambda, FALSE);
1675 return vm_yield_with_cref(ec, argc, argv, kw_splat, NULL, FALSE);
1681 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1682 argc, argv, kw_splat, block_handler,
1683 NULL, FALSE, FALSE);
1689 return invoke_block_from_c_bh(ec, check_block_handler(ec), 1, &args,
1693 ALWAYS_INLINE(
static VALUE
1696 int kw_splat,
VALUE passed_block_handler,
int is_lambda,
1702 int kw_splat,
VALUE passed_block_handler,
int is_lambda,
1705 const struct rb_block *block = &proc->block;
1708 switch (vm_block_type(block)) {
1709 case block_type_iseq:
1710 return invoke_iseq_block_from_c(ec, &block->as.captured,
self, argc, argv, kw_splat, passed_block_handler, NULL, is_lambda, me);
1711 case block_type_ifunc:
1712 if (kw_splat == 1) {
1713 VALUE keyword_hash = argv[argc-1];
1715 keyword_hash = rb_to_hash_type(keyword_hash);
1724 return vm_yield_with_cfunc(ec, &block->as.captured,
self, argc, argv, kw_splat, passed_block_handler, me);
1725 case block_type_symbol:
1726 return vm_yield_with_symbol(ec, block->as.symbol, argc, argv, kw_splat, passed_block_handler);
1727 case block_type_proc:
1728 is_lambda = block_proc_is_lambda(block->as.proc);
1729 block = vm_proc_block(block->as.proc);
1732 VM_UNREACHABLE(invoke_block_from_c_proc);
1738 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler)
1740 return invoke_block_from_c_proc(ec, proc,
self, argc, argv, kw_splat, passed_block_handler, proc->is_lambda, NULL);
1747 return invoke_block_from_c_proc(ec, proc,
self, argc, argv, kw_splat, block_handler, TRUE, me);
1752 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler)
1754 VALUE self = vm_block_self(&proc->block);
1755 vm_block_handler_verify(passed_block_handler);
1757 if (proc->is_from_method) {
1758 return vm_invoke_bmethod(ec, proc,
self, argc, argv, kw_splat, passed_block_handler, NULL);
1761 return vm_invoke_proc(ec, proc,
self, argc, argv, kw_splat, passed_block_handler);
1767 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler)
1769 vm_block_handler_verify(passed_block_handler);
1771 if (proc->is_from_method) {
1772 return vm_invoke_bmethod(ec, proc,
self, argc, argv, kw_splat, passed_block_handler, NULL);
1775 return vm_invoke_proc(ec, proc,
self, argc, argv, kw_splat, passed_block_handler);
1784 while (cfp->pc == 0 || cfp->iseq == 0) {
1785 if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_IFUNC) {
1787 return ifunc->svar_lep;
1790 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1793 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
1798 return (
VALUE *)VM_CF_LEP(cfp);
1804 return lep_svar_get(ec, rb_vm_svar_lep(ec, cfp), key);
1810 lep_svar_set(ec, rb_vm_svar_lep(ec, cfp), key, val);
1816 return vm_cfp_svar_get(ec, ec->cfp, key);
1822 vm_cfp_svar_set(ec, ec->cfp, key, val);
1828 return vm_svar_get(GET_EC(), VM_SVAR_BACKREF);
1834 vm_svar_set(GET_EC(), VM_SVAR_BACKREF, val);
1840 return vm_svar_get(GET_EC(), VM_SVAR_LASTLINE);
1846 vm_svar_set(GET_EC(), VM_SVAR_LASTLINE, val);
1850 rb_lastline_set_up(
VALUE val,
unsigned int up)
1854 for(
unsigned int i = 0; i < up; i++) {
1855 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1857 vm_cfp_svar_set(GET_EC(), cfp, VM_SVAR_LASTLINE, val);
1883 return rb_vm_get_sourceline(cfp);
1891 rb_source_location(
int *pline)
1896 if (cfp && VM_FRAME_RUBYFRAME_P(cfp)) {
1897 if (pline) *pline = rb_vm_get_sourceline(cfp);
1898 return rb_iseq_path(cfp->iseq);
1901 if (pline) *pline = 0;
1907 rb_source_location_cstr(
int *pline)
1909 VALUE path = rb_source_location(pline);
1910 if (
NIL_P(path))
return NULL;
1918 return vm_ec_cref(ec);
1922 rb_vm_cref_replace_with_duplicated_cref(
void)
1926 rb_cref_t *cref = vm_cref_replace_with_duplicated_cref(cfp->ep);
1932 rb_vm_cref_in_context(
VALUE self,
VALUE cbase)
1937 if (!cfp || cfp->self !=
self)
return NULL;
1938 if (!vm_env_cref_by_cref(cfp->ep))
return NULL;
1939 cref = vm_get_cref(cfp->ep);
1940 if (CREF_CLASS(cref) != cbase)
return NULL;
1949 dp(CREF_CLASS(cref));
1950 printf(
"%ld\n", CREF_VISI(cref));
1951 cref = CREF_NEXT(cref);
1965 return vm_get_cbase(cfp->ep);
1971 make_localjump_error(
const char *mesg,
VALUE value,
int reason)
2003 rb_vm_localjump_error(
const char *mesg,
VALUE value,
int reason)
2005 VALUE exc = make_localjump_error(mesg, value, reason);
2010 rb_vm_make_jump_tag_but_local_jump(
enum ruby_tag_type state,
VALUE val)
2016 mesg =
"unexpected return";
2019 mesg =
"unexpected break";
2022 mesg =
"unexpected next";
2025 mesg =
"unexpected redo";
2029 mesg =
"retry outside of rescue clause";
2036 val = GET_EC()->tag->retval;
2038 return make_localjump_error(mesg, val, state);
2042 rb_vm_jump_tag_but_local_jump(
enum ruby_tag_type state)
2044 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
2046 EC_JUMP_TAG(GET_EC(), state);
2052 while (VM_ENV_LOCAL_P(cfp->ep)) {
2053 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2064 const VALUE *ep = VM_CF_PREV_EP(cfp);
2068 rb_vm_localjump_error(
"unexpected break", val, TAG_BREAK);
2071 ec->errinfo = (
VALUE)THROW_DATA_NEW(val, target_cfp, TAG_BREAK);
2072 EC_JUMP_TAG(ec, TAG_BREAK);
2078 vm_iter_break(GET_EC(),
Qnil);
2084 vm_iter_break(GET_EC(), val);
2089 short ruby_vm_redefined_flag[BOP_LAST_];
2090 static st_table *vm_opt_method_def_table = 0;
2091 static st_table *vm_opt_mid_table = 0;
2094 rb_free_vm_opt_tables(
void)
2096 st_free_table(vm_opt_method_def_table);
2097 st_free_table(vm_opt_mid_table);
2101 vm_redefinition_check_flag(
VALUE klass)
2103 if (klass ==
rb_cInteger)
return INTEGER_REDEFINED_OP_FLAG;
2104 if (klass ==
rb_cFloat)
return FLOAT_REDEFINED_OP_FLAG;
2105 if (klass ==
rb_cString)
return STRING_REDEFINED_OP_FLAG;
2106 if (klass ==
rb_cArray)
return ARRAY_REDEFINED_OP_FLAG;
2107 if (klass ==
rb_cHash)
return HASH_REDEFINED_OP_FLAG;
2108 if (klass ==
rb_cSymbol)
return SYMBOL_REDEFINED_OP_FLAG;
2110 if (klass ==
rb_cTime)
return TIME_REDEFINED_OP_FLAG;
2112 if (klass ==
rb_cRegexp)
return REGEXP_REDEFINED_OP_FLAG;
2113 if (klass ==
rb_cNilClass)
return NIL_REDEFINED_OP_FLAG;
2116 if (klass ==
rb_cProc)
return PROC_REDEFINED_OP_FLAG;
2121 rb_vm_check_optimizable_mid(
VALUE mid)
2123 if (!vm_opt_mid_table) {
2127 return st_lookup(vm_opt_mid_table, mid, NULL);
2133 if (me->called_id != me->def->original_id) {
2137 if (METHOD_ENTRY_BASIC(me))
return TRUE;
2140 switch (def->type) {
2141 case VM_METHOD_TYPE_CFUNC:
2142 case VM_METHOD_TYPE_OPTIMIZED:
2157 if (vm_redefinition_check_method_type(me)) {
2158 if (st_lookup(vm_opt_method_def_table, (st_data_t)me->def, &bop)) {
2159 int flag = vm_redefinition_check_flag(klass);
2163 "Redefining '%s#%s' disables interpreter and JIT optimizations",
2167 rb_yjit_bop_redefined(flag, (
enum ruby_basic_operators)bop);
2168 rb_rjit_bop_redefined(flag, (
enum ruby_basic_operators)bop);
2169 ruby_vm_redefined_flag[bop] |= flag;
2175 static enum rb_id_table_iterator_result
2176 check_redefined_method(
ID mid,
VALUE value,
void *data)
2182 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
2184 return ID_TABLE_CONTINUE;
2188 rb_vm_check_redefinition_by_prepend(
VALUE klass)
2190 if (!vm_redefinition_check_flag(klass))
return;
2191 rb_id_table_foreach(RCLASS_M_TBL(RCLASS_ORIGIN(klass)), check_redefined_method, (
void *)klass);
2195 add_opt_method_entry_bop(
const rb_method_entry_t *me,
ID mid,
enum ruby_basic_operators bop)
2197 st_insert(vm_opt_method_def_table, (st_data_t)me->def, (st_data_t)bop);
2198 st_insert(vm_opt_mid_table, (st_data_t)mid, (st_data_t)
Qtrue);
2202 add_opt_method(
VALUE klass,
ID mid,
enum ruby_basic_operators bop)
2206 if (me && vm_redefinition_check_method_type(me)) {
2207 add_opt_method_entry_bop(me, mid, bop);
2214 static enum ruby_basic_operators vm_redefinition_bop_for_id(
ID mid);
2219 if (me && vm_redefinition_check_method_type(me)) {
2220 ID mid = me->called_id;
2221 enum ruby_basic_operators bop = vm_redefinition_bop_for_id(mid);
2222 if ((
int)bop >= 0) {
2223 add_opt_method_entry_bop(me, mid, bop);
2229 vm_init_redefined_flag(
void)
2232 enum ruby_basic_operators bop;
2234 #define OP(mid_, bop_) (mid = id##mid_, bop = BOP_##bop_, ruby_vm_redefined_flag[bop] = 0)
2235 #define C(k) add_opt_method(rb_c##k, mid, bop)
2236 OP(PLUS, PLUS), (C(Integer), C(Float), C(String), C(Array));
2237 OP(MINUS, MINUS), (C(Integer), C(Float));
2238 OP(MULT, MULT), (C(Integer), C(Float));
2239 OP(DIV, DIV), (C(Integer), C(Float));
2240 OP(MOD, MOD), (C(Integer), C(Float));
2241 OP(Eq, EQ), (C(Integer), C(Float), C(String), C(Symbol));
2242 OP(Eqq, EQQ), (C(Integer), C(Float), C(Symbol), C(String),
2243 C(NilClass), C(TrueClass), C(FalseClass));
2244 OP(LT, LT), (C(Integer), C(Float));
2245 OP(LE, LE), (C(Integer), C(Float));
2246 OP(GT, GT), (C(Integer), C(Float));
2247 OP(GE, GE), (C(Integer), C(Float));
2248 OP(LTLT, LTLT), (C(String), C(Array));
2249 OP(AREF, AREF), (C(Array), C(Hash), C(Integer));
2250 OP(ASET, ASET), (C(Array), C(Hash));
2251 OP(Length, LENGTH), (C(Array), C(String), C(Hash));
2252 OP(Size, SIZE), (C(Array), C(String), C(Hash));
2253 OP(EmptyP, EMPTY_P), (C(Array), C(String), C(Hash));
2254 OP(Succ, SUCC), (C(Integer), C(String));
2255 OP(EqTilde, MATCH), (C(
Regexp), C(String));
2256 OP(Freeze, FREEZE), (C(String), C(Array), C(Hash));
2257 OP(UMinus, UMINUS), (C(String));
2258 OP(Max, MAX), (C(Array));
2259 OP(Min, MIN), (C(Array));
2260 OP(Hash, HASH), (C(Array));
2261 OP(Call, CALL), (C(Proc));
2262 OP(And, AND), (C(Integer));
2263 OP(Or, OR), (C(Integer));
2264 OP(NilP,
NIL_P), (C(NilClass));
2265 OP(Cmp, CMP), (C(Integer), C(Float), C(String));
2266 OP(Default, DEFAULT), (C(Hash));
2267 OP(IncludeP, INCLUDE_P), (C(Array));
2272 static enum ruby_basic_operators
2273 vm_redefinition_bop_for_id(
ID mid)
2276 #define OP(mid_, bop_) case id##mid_: return BOP_##bop_
2293 OP(EmptyP, EMPTY_P);
2306 OP(Default, DEFAULT);
2319 switch (VM_FRAME_TYPE(cfp)) {
2320 case VM_FRAME_MAGIC_METHOD:
return "method";
2321 case VM_FRAME_MAGIC_BLOCK:
return "block";
2322 case VM_FRAME_MAGIC_CLASS:
return "class";
2323 case VM_FRAME_MAGIC_TOP:
return "top";
2324 case VM_FRAME_MAGIC_CFUNC:
return "cfunc";
2325 case VM_FRAME_MAGIC_IFUNC:
return "ifunc";
2326 case VM_FRAME_MAGIC_EVAL:
return "eval";
2327 case VM_FRAME_MAGIC_RESCUE:
return "rescue";
2337 if (THROW_DATA_P(err) &&
2338 THROW_DATA_STATE(err) == TAG_BREAK &&
2339 THROW_DATA_CONSUMED_P(err) == FALSE) {
2340 return THROW_DATA_VAL(err);
2352 unsigned long type = VM_FRAME_TYPE(cfp);
2353 #define C(t) if (type == VM_FRAME_MAGIC_##t) return #t
2382 switch (VM_FRAME_TYPE(ec->cfp)) {
2383 case VM_FRAME_MAGIC_METHOD:
2384 RUBY_DTRACE_METHOD_RETURN_HOOK(ec, 0, 0);
2385 EXEC_EVENT_HOOK_AND_POP_FRAME(ec,
RUBY_EVENT_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2389 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2392 THROW_DATA_CONSUMED_SET(err);
2394 case VM_FRAME_MAGIC_BLOCK:
2395 if (VM_FRAME_BMETHOD_P(ec->cfp)) {
2396 VALUE bmethod_return_value = frame_return_value(err);
2397 if (cfp_returning_with_value) {
2399 bmethod_return_value = THROW_DATA_VAL(err);
2403 EXEC_EVENT_HOOK_AND_POP_FRAME(ec,
RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, bmethod_return_value);
2406 ec->cfp->self, 0, 0, 0, bmethod_return_value, TRUE);
2412 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2413 rb_vm_frame_method_entry(ec->cfp)->called_id,
2414 rb_vm_frame_method_entry(ec->cfp)->owner,
2415 bmethod_return_value);
2417 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
2418 local_hooks = me->def->body.bmethod.hooks;
2422 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2423 rb_vm_frame_method_entry(ec->cfp)->called_id,
2424 rb_vm_frame_method_entry(ec->cfp)->owner,
2425 bmethod_return_value, TRUE);
2427 THROW_DATA_CONSUMED_SET(err);
2430 EXEC_EVENT_HOOK_AND_POP_FRAME(ec,
RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2433 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2435 THROW_DATA_CONSUMED_SET(err);
2438 case VM_FRAME_MAGIC_CLASS:
2531 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2533 struct rb_vm_exec_context {
2541 vm_exec_bottom_main(
void *context)
2543 struct rb_vm_exec_context *ctx = context;
2546 ctx->result = vm_exec_loop(ec, TAG_NONE, ctx->tag, vm_exec_core(ec));
2550 vm_exec_bottom_rescue(
void *context)
2552 struct rb_vm_exec_context *ctx = context;
2555 ctx->result = vm_exec_loop(ec, rb_ec_tag_state(ec), ctx->tag, ec->errinfo);
2568 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2569 struct rb_vm_exec_context ctx = {
2577 rb_wasm_try_catch_init(&try_catch, vm_exec_bottom_main, vm_exec_bottom_rescue, &ctx);
2579 rb_wasm_try_catch_loop_run(&try_catch, &RB_VM_TAG_JMPBUF_GET(_tag.buf));
2581 result = ctx.result;
2583 enum ruby_tag_type state;
2584 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2585 if (UNDEF_P(result = jit_exec(ec))) {
2586 result = vm_exec_core(ec);
2589 result = vm_exec_loop(ec, TAG_NONE, &_tag, result);
2592 result = vm_exec_loop(ec, state, &_tag, ec->errinfo);
2604 if (state == TAG_NONE) {
2608 rb_ec_raised_reset(ec, RAISED_STACKOVERFLOW | RAISED_NOMEMORY);
2609 while (UNDEF_P(result = vm_exec_handle_exception(ec, state, result))) {
2612 if (UNDEF_P(result = jit_exec_exception(ec))) {
2613 result = vm_exec_core(ec);
2616 VM_ASSERT(ec->tag == tag);
2618 if ((state = tag->state) == TAG_NONE)
break;
2619 tag->state = TAG_NONE;
2634 unsigned long epc, cont_pc, cont_sp;
2639 cont_pc = cont_sp = 0;
2642 while (ec->cfp->pc == 0 || ec->cfp->iseq == 0) {
2643 if (UNLIKELY(VM_FRAME_TYPE(ec->cfp) == VM_FRAME_MAGIC_CFUNC)) {
2645 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2646 rb_vm_frame_method_entry(ec->cfp)->called_id,
2647 rb_vm_frame_method_entry(ec->cfp)->owner,
Qnil);
2648 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec,
2649 rb_vm_frame_method_entry(ec->cfp)->owner,
2650 rb_vm_frame_method_entry(ec->cfp)->def->original_id);
2652 rb_vm_pop_frame(ec);
2656 epc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
2659 if (state == TAG_BREAK || state == TAG_RETURN) {
2660 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2662 if (cfp == escape_cfp) {
2663 if (state == TAG_RETURN) {
2664 if (!VM_FRAME_FINISHED_P(cfp)) {
2665 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2666 THROW_DATA_STATE_SET(err, state = TAG_BREAK);
2669 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2670 if (ct)
for (i = 0; i < ct->size; i++) {
2671 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2672 if (entry->start < epc && entry->end >= epc) {
2673 if (entry->type == CATCH_TYPE_ENSURE) {
2674 catch_iseq = entry->iseq;
2675 cont_pc = entry->cont;
2676 cont_sp = entry->sp;
2681 if (catch_iseq == NULL) {
2683 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2685 hook_before_rewind(ec,
true, state, err);
2686 rb_vm_pop_frame(ec);
2687 return THROW_DATA_VAL(err);
2694 *cfp->sp++ = THROW_DATA_VAL(err);
2701 if (state == TAG_RAISE) {
2702 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2703 if (ct)
for (i = 0; i < ct->size; i++) {
2704 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2705 if (entry->start < epc && entry->end >= epc) {
2707 if (entry->type == CATCH_TYPE_RESCUE ||
2708 entry->type == CATCH_TYPE_ENSURE) {
2709 catch_iseq = entry->iseq;
2710 cont_pc = entry->cont;
2711 cont_sp = entry->sp;
2717 else if (state == TAG_RETRY) {
2718 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2719 if (ct)
for (i = 0; i < ct->size; i++) {
2720 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2721 if (entry->start < epc && entry->end >= epc) {
2723 if (entry->type == CATCH_TYPE_ENSURE) {
2724 catch_iseq = entry->iseq;
2725 cont_pc = entry->cont;
2726 cont_sp = entry->sp;
2729 else if (entry->type == CATCH_TYPE_RETRY) {
2731 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2732 if (cfp == escape_cfp) {
2733 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2741 else if ((state == TAG_BREAK && !escape_cfp) ||
2742 (state == TAG_REDO) ||
2743 (state == TAG_NEXT)) {
2744 type = (
const enum rb_catch_type[TAG_MASK]) {
2745 [TAG_BREAK] = CATCH_TYPE_BREAK,
2746 [TAG_NEXT] = CATCH_TYPE_NEXT,
2747 [TAG_REDO] = CATCH_TYPE_REDO,
2751 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2752 if (ct)
for (i = 0; i < ct->size; i++) {
2753 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2755 if (entry->start < epc && entry->end >= epc) {
2756 if (entry->type == CATCH_TYPE_ENSURE) {
2757 catch_iseq = entry->iseq;
2758 cont_pc = entry->cont;
2759 cont_sp = entry->sp;
2762 else if (entry->type ==
type) {
2763 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2764 cfp->sp = vm_base_ptr(cfp) + entry->sp;
2766 if (state != TAG_REDO) {
2767 *cfp->sp++ = THROW_DATA_VAL(err);
2770 VM_ASSERT(ec->tag->state == TAG_NONE);
2777 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2778 if (ct)
for (i = 0; i < ct->size; i++) {
2779 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2780 if (entry->start < epc && entry->end >= epc) {
2782 if (entry->type == CATCH_TYPE_ENSURE) {
2783 catch_iseq = entry->iseq;
2784 cont_pc = entry->cont;
2785 cont_sp = entry->sp;
2792 if (catch_iseq != NULL) {
2794 const int arg_size = 1;
2796 rb_iseq_check(catch_iseq);
2797 cfp->sp = vm_base_ptr(cfp) + cont_sp;
2798 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + cont_pc;
2801 cfp->sp[0] = (
VALUE)err;
2802 vm_push_frame(ec, catch_iseq, VM_FRAME_MAGIC_RESCUE,
2804 VM_GUARDED_PREV_EP(cfp->ep),
2806 ISEQ_BODY(catch_iseq)->iseq_encoded,
2807 cfp->sp + arg_size ,
2808 ISEQ_BODY(catch_iseq)->local_table_size - arg_size,
2809 ISEQ_BODY(catch_iseq)->stack_max);
2812 ec->tag->state = TAG_NONE;
2818 hook_before_rewind(ec, (cfp == escape_cfp), state, err);
2820 if (VM_FRAME_FINISHED_P(ec->cfp)) {
2821 rb_vm_pop_frame(ec);
2822 ec->errinfo = (
VALUE)err;
2823 ec->tag = ec->tag->prev;
2824 EC_JUMP_TAG(ec, state);
2827 rb_vm_pop_frame(ec);
2840 vm_set_top_stack(ec, iseq);
2846 rb_iseq_eval_main(
const rb_iseq_t *iseq)
2851 vm_set_main_stack(ec, iseq);
2862 if (idp) *idp = me->def->original_id;
2863 if (called_idp) *called_idp = me->called_id;
2864 if (klassp) *klassp = me->owner;
2875 return rb_vm_control_frame_id_and_class(ec->cfp, idp, called_idp, klassp);
2881 return rb_ec_frame_method_id_and_class(GET_EC(), idp, 0, klassp);
2890 const rb_iseq_t *iseq = rb_iseq_new(
Qnil, filename, filename,
Qnil, 0, ISEQ_TYPE_TOP);
2893 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH,
2894 recv, block_handler,
2895 (
VALUE)vm_cref_new_toplevel(ec),
2896 0, reg_cfp->sp, 0, 0);
2900 rb_vm_pop_frame(ec);
2907 rb_vm_update_references(
void *
ptr)
2918 if (vm->load_path_check_cache) {
2919 vm->load_path_check_cache =
rb_gc_location(vm->load_path_check_cache);
2924 vm->loaded_features_snapshot =
rb_gc_location(vm->loaded_features_snapshot);
2925 vm->loaded_features_realpaths =
rb_gc_location(vm->loaded_features_realpaths);
2926 vm->loaded_features_realpath_map =
rb_gc_location(vm->loaded_features_realpath_map);
2932 rb_gc_update_values(RUBY_NSIG, vm->trap_list.cmd);
2934 if (vm->coverages) {
2942 rb_vm_each_stack_value(
void *
ptr,
void (*cb)(
VALUE,
void*),
void *ctx)
2947 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2948 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
2949 rb_ractor_status_p(r, ractor_running));
2950 if (r->threads.cnt > 0) {
2952 ccan_list_for_each(&r->threads.set, th, lt_node) {
2953 VM_ASSERT(th != NULL);
2956 VALUE *p = ec->vm_stack;
2957 VALUE *sp = ec->cfp->sp;
2971 static enum rb_id_table_iterator_result
2972 vm_mark_negative_cme(
VALUE val,
void *dmy)
2975 return ID_TABLE_CONTINUE;
2978 void rb_thread_sched_mark_zombies(
rb_vm_t *vm);
2981 rb_vm_mark(
void *
ptr)
2983 RUBY_MARK_ENTER(
"vm");
2984 RUBY_GC_INFO(
"-------------------------------------------------\n");
2990 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2992 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
2993 rb_ractor_status_p(r, ractor_running));
2997 for (
struct global_object_list *list = vm->global_object_list; list; list = list->next) {
3015 if (vm->loading_table) {
3019 rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);
3021 rb_id_table_foreach_values(vm->negative_cme_table, vm_mark_negative_cme, NULL);
3023 for (i=0; i<VM_GLOBAL_CC_CACHE_TABLE_SIZE; i++) {
3024 const struct rb_callcache *cc = vm->global_cc_cache_table[i];
3027 if (!vm_cc_invalidated_p(cc)) {
3031 vm->global_cc_cache_table[i] = NULL;
3036 rb_thread_sched_mark_zombies(vm);
3040 RUBY_MARK_LEAVE(
"vm");
3043 #undef rb_vm_register_special_exception
3045 rb_vm_register_special_exception_str(
enum ruby_special_exceptions sp,
VALUE cls,
VALUE mesg)
3050 ((
VALUE *)vm->special_exceptions)[sp] = exc;
3051 rb_vm_register_global_object(exc);
3055 free_loading_table_entry(st_data_t key, st_data_t value, st_data_t arg)
3061 void rb_free_loaded_features_index(
rb_vm_t *vm);
3062 void rb_objspace_free_objects(
void *
objspace);
3067 RUBY_FREE_ENTER(
"vm");
3071 VALUE *stack = th->ec->vm_stack;
3072 if (rb_free_at_exit) {
3073 rb_free_encoded_insn_data();
3074 rb_free_global_enc_table();
3075 rb_free_loaded_builtin_table();
3077 rb_free_shared_fiber_pool();
3078 rb_free_static_symid_str();
3079 rb_free_transcoder_table();
3080 rb_free_vm_opt_tables();
3082 rb_free_rb_global_tbl();
3083 rb_free_loaded_features_index(vm);
3085 rb_id_table_free(vm->negative_cme_table);
3086 st_free_table(vm->overloaded_cme_table);
3088 rb_id_table_free(
RCLASS(rb_mRubyVMFrozenCore)->m_tbl);
3090 rb_shape_t *cursor = rb_shape_get_root_shape();
3091 rb_shape_t *end = rb_shape_get_shape_by_id(GET_SHAPE_TREE()->next_shape_id);
3092 while (cursor < end) {
3094 if (cursor->edges && !(((uintptr_t)cursor->edges) & 0x1))
3095 rb_id_table_free(cursor->edges);
3099 xfree(GET_SHAPE_TREE());
3101 st_free_table(vm->static_ext_inits);
3103 rb_vm_postponed_job_free();
3105 rb_id_table_free(vm->constant_cache);
3106 st_free_table(vm->unused_block_warning_table);
3111 #ifndef HAVE_SETPROCTITLE
3112 ruby_free_proctitle();
3116 rb_fiber_reset_root_local_storage(th);
3122 rb_vm_living_threads_init(vm);
3123 ruby_vm_run_at_exit_hooks(vm);
3124 if (vm->loading_table) {
3125 st_foreach(vm->loading_table, free_loading_table_entry, 0);
3126 st_free_table(vm->loading_table);
3127 vm->loading_table = 0;
3130 st_free_table(vm->ci_table);
3131 vm->ci_table = NULL;
3133 if (vm->frozen_strings) {
3134 st_free_table(vm->frozen_strings);
3135 vm->frozen_strings = 0;
3137 RB_ALTSTACK_FREE(vm->main_altstack);
3146 if (rb_free_at_exit) {
3147 rb_objspace_free_objects(
objspace);
3148 rb_free_generic_iv_tbl_();
3149 rb_free_default_rand_key();
3150 if (th && vm->fork_gen == 0) {
3161 ruby_current_vm_ptr = NULL;
3164 if (rb_free_at_exit) {
3165 rb_yjit_free_at_exit();
3169 RUBY_FREE_LEAVE(
"vm");
3173 size_t rb_vm_memsize_waiting_fds(
struct ccan_list_head *waiting_fds);
3174 size_t rb_vm_memsize_workqueue(
struct ccan_list_head *workqueue);
3178 static enum rb_id_table_iterator_result
3179 vm_memsize_constant_cache_i(
ID id,
VALUE ics,
void *size)
3181 *((
size_t *) size) += rb_st_memsize((
st_table *) ics);
3182 return ID_TABLE_CONTINUE;
3189 vm_memsize_constant_cache(
void)
3192 size_t size = rb_id_table_memsize(vm->constant_cache);
3194 rb_id_table_foreach(vm->constant_cache, vm_memsize_constant_cache_i, &size);
3205 at_exit = at_exit->next;
3214 vm_memsize_builtin_function_table(
const struct rb_builtin_function *builtin_function_table)
3222 vm_memsize(
const void *
ptr)
3228 rb_vm_memsize_waiting_fds(&vm->waiting_fds) +
3229 rb_st_memsize(vm->loaded_features_index) +
3230 rb_st_memsize(vm->loading_table) +
3231 rb_vm_memsize_postponed_job_queue() +
3232 rb_vm_memsize_workqueue(&vm->workqueue) +
3233 vm_memsize_at_exit_list(vm->at_exit) +
3234 rb_st_memsize(vm->ci_table) +
3235 rb_st_memsize(vm->frozen_strings) +
3236 vm_memsize_builtin_function_table(vm->builtin_function_table) +
3237 rb_id_table_memsize(vm->negative_cme_table) +
3238 rb_st_memsize(vm->overloaded_cme_table) +
3239 vm_memsize_constant_cache() +
3251 {0, 0, vm_memsize,},
3252 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3257 vm_default_params(
void)
3260 VALUE result = rb_hash_new_with_size(4);
3261 #define SET(name) rb_hash_aset(result, ID2SYM(rb_intern(#name)), SIZET2NUM(vm->default_params.name));
3262 SET(thread_vm_stack_size);
3263 SET(thread_machine_stack_size);
3264 SET(fiber_vm_stack_size);
3265 SET(fiber_machine_stack_size);
3272 get_param(
const char *name,
size_t default_value,
size_t min_value)
3275 size_t result = default_value;
3276 if ((envval = getenv(name)) != 0) {
3277 long val = atol(envval);
3278 if (val < (
long)min_value) {
3279 val = (long)min_value;
3281 result = (size_t)(((val -1 + RUBY_VM_SIZE_ALIGN) / RUBY_VM_SIZE_ALIGN) * RUBY_VM_SIZE_ALIGN);
3283 if (0) ruby_debug_printf(
"%s: %"PRIuSIZE
"\n", name, result);
3289 check_machine_stack_size(
size_t *sizep)
3291 #ifdef PTHREAD_STACK_MIN
3292 size_t size = *sizep;
3295 #ifdef PTHREAD_STACK_MIN
3296 if (size < (
size_t)PTHREAD_STACK_MIN) {
3297 *sizep = (size_t)PTHREAD_STACK_MIN * 2;
3303 vm_default_params_setup(
rb_vm_t *vm)
3305 vm->default_params.thread_vm_stack_size =
3306 get_param(
"RUBY_THREAD_VM_STACK_SIZE",
3307 RUBY_VM_THREAD_VM_STACK_SIZE,
3308 RUBY_VM_THREAD_VM_STACK_SIZE_MIN);
3310 vm->default_params.thread_machine_stack_size =
3311 get_param(
"RUBY_THREAD_MACHINE_STACK_SIZE",
3312 RUBY_VM_THREAD_MACHINE_STACK_SIZE,
3313 RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN);
3315 vm->default_params.fiber_vm_stack_size =
3316 get_param(
"RUBY_FIBER_VM_STACK_SIZE",
3317 RUBY_VM_FIBER_VM_STACK_SIZE,
3318 RUBY_VM_FIBER_VM_STACK_SIZE_MIN);
3320 vm->default_params.fiber_machine_stack_size =
3321 get_param(
"RUBY_FIBER_MACHINE_STACK_SIZE",
3322 RUBY_VM_FIBER_MACHINE_STACK_SIZE,
3323 RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN);
3326 check_machine_stack_size(&vm->default_params.thread_machine_stack_size);
3327 check_machine_stack_size(&vm->default_params.fiber_machine_stack_size);
3333 rb_vm_living_threads_init(vm);
3334 vm->thread_report_on_exception = 1;
3335 vm->src_encoding_index = -1;
3337 vm_default_params_setup(vm);
3347 VALUE *p = ec->vm_stack;
3348 VALUE *sp = ec->cfp->sp;
3352 for (i = 0; i < (long)(sp - p); i++) {
3355 if (ref != update) {
3360 while (cfp != limit_cfp) {
3361 const VALUE *ep = cfp->ep;
3366 if (!VM_ENV_LOCAL_P(ep)) {
3367 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3368 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3369 VM_FORCE_WRITE(&prev_ep[VM_ENV_DATA_INDEX_ENV],
rb_gc_location(prev_ep[VM_ENV_DATA_INDEX_ENV]));
3372 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3373 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ENV],
rb_gc_location(ep[VM_ENV_DATA_INDEX_ENV]));
3374 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ME_CREF],
rb_gc_location(ep[VM_ENV_DATA_INDEX_ME_CREF]));
3378 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3385 static enum rb_id_table_iterator_result
3386 mark_local_storage_i(
VALUE local,
void *data)
3389 return ID_TABLE_CONTINUE;
3398 VALUE *p = ec->vm_stack;
3399 VALUE *sp = ec->cfp->sp;
3403 VM_ASSERT(sp == ec->cfp->sp);
3404 rb_gc_mark_vm_stack_values((
long)(sp - p), p);
3406 while (cfp != limit_cfp) {
3407 const VALUE *ep = cfp->ep;
3408 VM_ASSERT(!!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) == vm_ep_in_heap_p_(ec, ep));
3410 if (VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
3415 if (!VM_ENV_LOCAL_P(ep)) {
3416 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3417 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3421 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3428 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3433 if (ec->machine.stack_start && ec->machine.stack_end &&
3436 rb_gc_mark_machine_context(ec);
3441 if (ec->local_storage) {
3442 rb_id_table_foreach_values(ec->local_storage, mark_local_storage_i, NULL);
3444 rb_gc_mark(ec->local_storage_recursive_hash);
3445 rb_gc_mark(ec->local_storage_recursive_hash_for_trace);
3453 void rb_threadptr_root_fiber_setup(
rb_thread_t *th);
3454 void rb_threadptr_root_fiber_release(
rb_thread_t *th);
3457 thread_compact(
void *
ptr)
3463 if (!th->root_fiber) {
3464 rb_execution_context_update(th->ec);
3469 thread_mark(
void *
ptr)
3472 RUBY_MARK_ENTER(
"thread");
3473 rb_fiber_mark_self(th->ec->fiber_ptr);
3476 switch (th->invoke_type) {
3477 case thread_invoke_type_proc:
3478 case thread_invoke_type_ractor_proc:
3482 case thread_invoke_type_func:
3493 rb_gc_mark(th->pending_interrupt_mask_stack);
3496 if (th->root_fiber) rb_fiber_mark_self(th->root_fiber);
3498 RUBY_ASSERT(th->ec == rb_fiberptr_get_ec(th->ec->fiber_ptr));
3506 rb_threadptr_interrupt_exec_task_mark(th);
3508 RUBY_MARK_LEAVE(
"thread");
3514 thread_free(
void *
ptr)
3517 RUBY_FREE_ENTER(
"thread");
3519 rb_threadptr_sched_free(th);
3521 if (th->locking_mutex !=
Qfalse) {
3522 rb_bug(
"thread_free: locking_mutex must be NULL (%p:%p)", (
void *)th, (
void *)th->locking_mutex);
3524 if (th->keeping_mutexes != NULL) {
3525 rb_bug(
"thread_free: keeping_mutexes must be NULL (%p:%p)", (
void *)th, (
void *)th->keeping_mutexes);
3530 rb_threadptr_root_fiber_release(th);
3532 if (th->vm && th->vm->ractor.main_thread == th) {
3533 RUBY_GC_INFO(
"MRI main thread\n");
3541 RUBY_FREE_LEAVE(
"thread");
3545 thread_memsize(
const void *
ptr)
3550 if (!th->root_fiber) {
3551 size += th->ec->vm_stack_size *
sizeof(
VALUE);
3553 if (th->ec->local_storage) {
3554 size += rb_id_table_memsize(th->ec->local_storage);
3559 #define thread_data_type ruby_threadptr_data_type
3568 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3572 rb_obj_is_thread(
VALUE obj)
3578 thread_alloc(
VALUE klass)
3587 ec->vm_stack = stack;
3588 ec->vm_stack_size = size;
3594 rb_ec_set_vm_stack(ec, stack, size);
3596 #if VM_CHECK_MODE > 0
3600 ec->cfp = (
void *)(ec->vm_stack + ec->vm_stack_size);
3604 VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME ,
3605 Qnil , VM_BLOCK_HANDLER_NONE ,
3607 0 , ec->vm_stack, 0, 0
3614 rb_ec_set_vm_stack(ec, NULL, 0);
3625 rb_threadptr_root_fiber_setup(th);
3629 th->scheduler =
Qnil;
3632 size_t size = vm->default_params.thread_vm_stack_size /
sizeof(
VALUE);
3633 rb_ec_initialize_vm_stack(th->ec,
ALLOC_N(
VALUE, size), size);
3636 VM_ASSERT(th->ec->cfp == NULL);
3637 VM_ASSERT(th->ec->vm_stack == NULL);
3638 VM_ASSERT(th->ec->vm_stack_size == 0);
3641 th->status = THREAD_RUNNABLE;
3642 th->last_status =
Qnil;
3643 th->top_wrapper = 0;
3644 th->top_self = vm->top_self;
3647 th->ec->errinfo =
Qnil;
3648 th->ec->root_svar =
Qfalse;
3649 th->ec->local_storage_recursive_hash =
Qnil;
3650 th->ec->local_storage_recursive_hash_for_trace =
Qnil;
3652 th->ec->storage =
Qnil;
3654 #if OPT_CALL_THREADED_CODE
3658 th->report_on_exception = vm->thread_report_on_exception;
3659 th->ext_config.ractor_safe =
true;
3661 ccan_list_head_init(&th->interrupt_exec_tasks);
3663 #if USE_RUBY_DEBUG_LOG
3667 RUBY_DEBUG_LOG(
"th:%u", th->serial);
3672 rb_thread_alloc(
VALUE klass)
3674 VALUE self = thread_alloc(klass);
3676 target_th->ractor = GET_RACTOR();
3677 th_init(target_th,
self, target_th->vm = GET_VM());
3681 #define REWIND_CFP(expr) do { \
3682 rb_execution_context_t *ec__ = GET_EC(); \
3683 VALUE *const curr_sp = (ec__->cfp++)->sp; \
3684 VALUE *const saved_sp = ec__->cfp->sp; \
3685 ec__->cfp->sp = curr_sp; \
3687 (ec__->cfp--)->sp = saved_sp; \
3714 rb_clear_method_cache(
self, mid);
3720 m_core_set_postexe(
VALUE self)
3729 core_hash_merge(
VALUE hash,
long argc,
const VALUE *argv)
3732 VM_ASSERT(argc % 2 == 0);
3738 m_core_hash_merge_ptr(
int argc,
VALUE *argv,
VALUE recv)
3740 VALUE hash = argv[0];
3742 REWIND_CFP(hash = core_hash_merge(hash, argc-1, argv+1));
3758 REWIND_CFP(hash = core_hash_merge_kwd(hash, kw));
3770 m_core_make_shareable_copy(
VALUE recv,
VALUE obj)
3778 return rb_ractor_ensure_shareable(obj, name);
3788 extern VALUE *rb_gc_stack_start;
3789 extern size_t rb_gc_stack_maxsize;
3797 rb_vm_bugreport(NULL, stderr);
3806 #ifdef HAVE_BACKTRACE
3807 #include <execinfo.h>
3808 #define MAX_NATIVE_TRACE 1024
3809 static void *trace[MAX_NATIVE_TRACE];
3810 int n = (int)backtrace(trace, MAX_NATIVE_TRACE);
3811 char **syms = backtrace_symbols(trace, n);
3818 for (i=0; i<n; i++) {
3826 #if VM_COLLECT_USAGE_DETAILS
3827 static VALUE usage_analysis_insn_start(
VALUE self);
3828 static VALUE usage_analysis_operand_start(
VALUE self);
3829 static VALUE usage_analysis_register_start(
VALUE self);
3830 static VALUE usage_analysis_insn_stop(
VALUE self);
3831 static VALUE usage_analysis_operand_stop(
VALUE self);
3832 static VALUE usage_analysis_register_stop(
VALUE self);
3833 static VALUE usage_analysis_insn_running(
VALUE self);
3834 static VALUE usage_analysis_operand_running(
VALUE self);
3835 static VALUE usage_analysis_register_running(
VALUE self);
3836 static VALUE usage_analysis_insn_clear(
VALUE self);
3837 static VALUE usage_analysis_operand_clear(
VALUE self);
3838 static VALUE usage_analysis_register_clear(
VALUE self);
3844 return rb_f_raise(c, v);
3893 vm_keep_script_lines(
VALUE self)
3895 return RBOOL(ruby_vm_keep_script_lines);
3910 vm_keep_script_lines_set(
VALUE self,
VALUE flags)
3912 ruby_vm_keep_script_lines =
RTEST(flags);
3941 #if USE_DEBUG_COUNTER
3963 rb_define_method(klass,
"make_shareable_copy", m_core_make_shareable_copy, 1);
3966 RBASIC_CLEAR_CLASS(klass);
3968 rb_vm_register_global_object(fcore);
3969 rb_mRubyVMFrozenCore = fcore;
4132 #if VM_COLLECT_USAGE_DETAILS
4134 #define define_usage_analysis_hash(name) \
4135 rb_define_const(rb_cRubyVM, "USAGE_ANALYSIS_" #name, rb_hash_new())
4136 define_usage_analysis_hash(
INSN);
4137 define_usage_analysis_hash(REGS);
4138 define_usage_analysis_hash(INSN_BIGRAM);
4160 #if OPT_DIRECT_THREADED_CODE
4162 #elif OPT_TOKEN_THREADED_CODE
4164 #elif OPT_CALL_THREADED_CODE
4168 #if OPT_OPERANDS_UNIFICATION
4171 #if OPT_INSTRUCTIONS_UNIFICATION
4174 #if OPT_INLINE_METHOD_CACHE
4182 rb_define_const(rb_cRubyVM,
"INSTRUCTION_NAMES", rb_insns_name_array());
4207 rb_vm_t *vm = ruby_current_vm_ptr;
4209 VALUE filename = rb_fstring_lit(
"<main>");
4210 const rb_iseq_t *iseq = rb_iseq_new(
Qnil, filename, filename,
Qnil, 0, ISEQ_TYPE_TOP);
4213 rb_ractor_main_setup(vm, th->ractor, th);
4220 vm->ractor.main_thread = th;
4221 vm->ractor.main_ractor = th->ractor;
4223 th->top_wrapper = 0;
4224 th->top_self = rb_vm_top_self();
4226 rb_vm_register_global_object((
VALUE)iseq);
4227 th->ec->cfp->iseq = iseq;
4228 th->ec->cfp->pc = ISEQ_BODY(iseq)->iseq_encoded;
4229 th->ec->cfp->self = th->top_self;
4231 VM_ENV_FLAGS_UNSET(th->ec->cfp->ep, VM_FRAME_FLAG_CFRAME);
4232 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));
4240 rb_objspace_gc_enable(vm->gc.objspace);
4243 vm_init_redefined_flag();
4247 OPTIMIZED_METHOD_TYPE_BLOCK_CALL, 0, METHOD_VISI_PUBLIC);
4249 rb_vm_register_global_object(rb_block_param_proxy);
4252 Init_vm_backtrace();
4256 rb_vm_set_progname(
VALUE filename)
4263 rb_iseq_pathobj_set(cfp->iseq, filename, rb_iseq_realpath(cfp->iseq));
4272 rb_vm_t *vm = ruby_mimcalloc(1,
sizeof(*vm));
4275 fputs(
"[FATAL] failed to allocate memory\n", stderr);
4282 rb_vm_postponed_job_queue_init(vm);
4283 ruby_current_vm_ptr = vm;
4284 rb_objspace_alloc();
4285 vm->negative_cme_table = rb_id_table_create(16);
4286 vm->overloaded_cme_table = st_init_numtable();
4287 vm->constant_cache = rb_id_table_create(0);
4288 vm->unused_block_warning_table = st_init_numtable();
4293 th->ractor = vm->ractor.main_ractor = rb_ractor_main_alloc();
4294 Init_native_thread(th);
4298 rb_ractor_set_current_ec(th->ractor, th->ec);
4300 ruby_thread_init_stack(th, native_main_thread_stack_top);
4306 vm_opt_method_def_table = st_init_numtable();
4307 vm_opt_mid_table = st_init_numtable();
4309 #ifdef RUBY_THREAD_WIN32_H
4317 native_main_thread_stack_top = addr;
4322 #include <sys/mman.h>
4326 #ifndef MARK_OBJECT_ARY_BUCKET_SIZE
4327 #define MARK_OBJECT_ARY_BUCKET_SIZE 1024
4337 pin_array_list_mark(
void *data)
4342 rb_gc_mark_vm_stack_values(array->len, array->array);
4346 pin_array_list_free(
void *data)
4349 xfree(array->array);
4353 pin_array_list_memsize(
const void *data)
4359 pin_array_list_update_references(
void *data)
4368 .dmark = pin_array_list_mark,
4369 .dfree = pin_array_list_free,
4370 .dsize = pin_array_list_memsize,
4371 .dcompact = pin_array_list_update_references,
4373 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
4377 pin_array_list_new(
VALUE next)
4382 array_list->array =
ALLOC_N(
VALUE, MARK_OBJECT_ARY_BUCKET_SIZE);
4392 if (array_list->len >= MARK_OBJECT_ARY_BUCKET_SIZE) {
4393 obj = pin_array_list_new(obj);
4397 RB_OBJ_WRITE(obj, &array_list->array[array_list->len], item);
4403 rb_vm_register_global_object(
VALUE obj)
4413 if (
FL_TEST(obj, RCLASS_IS_ROOT)) {
4416 FL_SET(obj, RCLASS_IS_ROOT);
4423 VALUE list = GET_VM()->mark_object_ary;
4424 VALUE head = pin_array_list_append(list, obj);
4426 GET_VM()->mark_object_ary = head;
4434 Init_vm_objects(
void)
4439 vm->mark_object_ary = pin_array_list_new(
Qnil);
4440 vm->loading_table = st_init_strtable();
4441 vm->ci_table = st_init_table(&vm_ci_hashtype);
4442 vm->frozen_strings = st_init_table_with_size(&rb_fstring_hash_type, 10000);
4447 void Init_builtin_yjit(
void) {}
4451 #include "yjit_hook.rbinc"
4455 void Init_builtin_rjit(
void) {}
4456 void Init_builtin_rjit_c(
void) {}
4462 main_to_s(
VALUE obj)
4468 rb_vm_top_self(
void)
4470 return GET_VM()->top_self;
4487 return &cr->verbose;
4497 bool rb_free_at_exit =
false;
4502 return rb_free_at_exit;
4511 rb_vm_fstring_table(
void)
4513 return GET_VM()->frozen_strings;
4516 #if VM_COLLECT_USAGE_DETAILS
4518 #define HASH_ASET(h, k, v) rb_hash_aset((h), (st_data_t)(k), (st_data_t)(v))
4532 vm_analysis_insn(
int insn)
4536 static int prev_insn = -1;
4542 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4543 CONST_ID(bigram_hash,
"USAGE_ANALYSIS_INSN_BIGRAM");
4547 HASH_ASET(uh,
INT2FIX(insn), ihash);
4555 if (prev_insn != -1) {
4574 vm_analysis_operand(
int insn,
int n,
VALUE op)
4584 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4589 HASH_ASET(uh,
INT2FIX(insn), ihash);
4593 HASH_ASET(ihash,
INT2FIX(n), ophash);
4596 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4606 vm_analysis_register(
int reg,
int isset)
4611 static const char regstrs[][5] = {
4619 static const char getsetstr[][4] = {
4623 static VALUE syms[
sizeof(regstrs) /
sizeof(regstrs[0])][2];
4627 CONST_ID(usage_hash,
"USAGE_ANALYSIS_REGS");
4632 for (i = 0; i < (int)(
sizeof(regstrs) /
sizeof(regstrs[0])); i++) {
4634 for (j = 0; j < 2; j++) {
4635 snprintf(buff, 0x10,
"%d %s %-4s", i, getsetstr[j], regstrs[i]);
4640 valstr = syms[reg][isset];
4651 static void (*ruby_vm_collect_usage_func_insn)(
int insn) = NULL;
4652 static void (*ruby_vm_collect_usage_func_operand)(
int insn,
int n,
VALUE op) = NULL;
4653 static void (*ruby_vm_collect_usage_func_register)(
int reg,
int isset) = NULL;
4657 usage_analysis_insn_start(
VALUE self)
4659 ruby_vm_collect_usage_func_insn = vm_analysis_insn;
4665 usage_analysis_operand_start(
VALUE self)
4667 ruby_vm_collect_usage_func_operand = vm_analysis_operand;
4673 usage_analysis_register_start(
VALUE self)
4675 ruby_vm_collect_usage_func_register = vm_analysis_register;
4681 usage_analysis_insn_stop(
VALUE self)
4683 ruby_vm_collect_usage_func_insn = 0;
4689 usage_analysis_operand_stop(
VALUE self)
4691 ruby_vm_collect_usage_func_operand = 0;
4697 usage_analysis_register_stop(
VALUE self)
4699 ruby_vm_collect_usage_func_register = 0;
4705 usage_analysis_insn_running(
VALUE self)
4707 return RBOOL(ruby_vm_collect_usage_func_insn != 0);
4712 usage_analysis_operand_running(
VALUE self)
4714 return RBOOL(ruby_vm_collect_usage_func_operand != 0);
4719 usage_analysis_register_running(
VALUE self)
4721 return RBOOL(ruby_vm_collect_usage_func_register != 0);
4725 usage_analysis_clear(
VALUE self,
ID usage_hash)
4737 usage_analysis_insn_clear(
VALUE self)
4742 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4743 CONST_ID(bigram_hash,
"USAGE_ANALYSIS_INSN_BIGRAM");
4744 usage_analysis_clear(rb_cRubyVM, usage_hash);
4745 return usage_analysis_clear(rb_cRubyVM, bigram_hash);
4750 usage_analysis_operand_clear(
VALUE self)
4754 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4755 return usage_analysis_clear(
self, usage_hash);
4760 usage_analysis_register_clear(
VALUE self)
4764 CONST_ID(usage_hash,
"USAGE_ANALYSIS_REGS");
4765 return usage_analysis_clear(
self, usage_hash);
4770 MAYBE_UNUSED(
static void (*ruby_vm_collect_usage_func_insn)(
int insn)) = 0;
4771 MAYBE_UNUSED(
static void (*ruby_vm_collect_usage_func_operand)(
int insn,
int n,
VALUE op)) = 0;
4772 MAYBE_UNUSED(
static void (*ruby_vm_collect_usage_func_register)(
int reg,
int isset)) = 0;
4776 #if VM_COLLECT_USAGE_DETAILS
4779 vm_collect_usage_insn(
int insn)
4781 if (RUBY_DTRACE_INSN_ENABLED()) {
4782 RUBY_DTRACE_INSN(rb_insns_name(insn));
4784 if (ruby_vm_collect_usage_func_insn)
4785 (*ruby_vm_collect_usage_func_insn)(insn);
4793 vm_collect_usage_operand(
int insn,
int n,
VALUE op)
4795 if (RUBY_DTRACE_INSN_OPERAND_ENABLED()) {
4798 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4800 RUBY_DTRACE_INSN_OPERAND(
RSTRING_PTR(valstr), rb_insns_name(insn));
4803 if (ruby_vm_collect_usage_func_operand)
4804 (*ruby_vm_collect_usage_func_operand)(insn, n, op);
4810 vm_collect_usage_register(
int reg,
int isset)
4812 if (ruby_vm_collect_usage_func_register)
4813 (*ruby_vm_collect_usage_func_register)(reg, isset);
4818 rb_vm_empty_cc(
void)
4820 return &vm_empty_cc;
4824 rb_vm_empty_cc_for_super(
void)
4826 return &vm_empty_cc_for_super;
4829 #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_class, 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 symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a frozen Ruby String instead of a C String.
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.