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;
587 unsigned int ruby_vm_event_local_num;
589 rb_serial_t ruby_vm_constant_cache_invalidations = 0;
590 rb_serial_t ruby_vm_constant_cache_misses = 0;
591 rb_serial_t ruby_vm_global_cvar_state = 1;
594 .flags =
T_IMEMO | (imemo_callcache <<
FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
597 .call_ = vm_call_general,
603 static const struct rb_callcache vm_empty_cc_for_super = {
604 .flags =
T_IMEMO | (imemo_callcache <<
FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
607 .call_ = vm_call_super_method,
613 static void thread_free(
void *
ptr);
616 rb_vm_inc_const_missing_count(
void)
618 ruby_vm_const_missing_count +=1;
627 if (!ec) ec = GET_EC();
628 if (!rb_ec_frame_method_id_and_class(ec, &
id, 0, &klass) || !klass)
632 klass =
RBASIC(klass)->klass;
634 else if (RCLASS_SINGLETON_P(klass)) {
635 klass = RCLASS_ATTACHED_OBJECT(klass);
636 if (
NIL_P(klass))
return FALSE;
641 const char *classname, *filename;
643 if (methodname && (filename = rb_source_location_cstr(&args->line_no)) != 0) {
645 classname =
"<unknown>";
646 args->classname = classname;
647 args->methodname = methodname;
648 args->filename = filename;
657 extern unsigned int redblack_buffer_size;
685 static VALUE sym_constant_cache_invalidations, sym_constant_cache_misses, sym_global_cvar_state, sym_next_shape_id;
686 static VALUE sym_shape_cache_size;
703 #define S(s) sym_##s = ID2SYM(rb_intern_const(#s))
704 S(constant_cache_invalidations);
705 S(constant_cache_misses);
706 S(global_cvar_state);
711 #define SET(name, attr) \
712 if (key == sym_##name) \
713 return SERIALT2NUM(attr); \
714 else if (hash != Qnil) \
715 rb_hash_aset(hash, sym_##name, SERIALT2NUM(attr));
717 SET(constant_cache_invalidations, ruby_vm_constant_cache_invalidations);
718 SET(constant_cache_misses, ruby_vm_constant_cache_misses);
719 SET(global_cvar_state, ruby_vm_global_cvar_state);
720 SET(next_shape_id, (rb_serial_t)GET_SHAPE_TREE()->next_shape_id);
721 SET(shape_cache_size, (rb_serial_t)GET_SHAPE_TREE()->cache_size);
724 #if USE_DEBUG_COUNTER
725 ruby_debug_counter_show_at_exit(FALSE);
726 for (
size_t i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
727 const VALUE name = rb_sym_intern_ascii_cstr(rb_debug_counter_names[i]);
733 else if (hash !=
Qnil) {
751 if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_TOP) {
756 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH, rb_ec_thread_ptr(ec)->top_self,
757 VM_BLOCK_HANDLER_NONE,
758 (
VALUE)vm_cref_new_toplevel(ec),
759 ISEQ_BODY(iseq)->iseq_encoded, ec->cfp->sp,
760 ISEQ_BODY(iseq)->local_table_size, ISEQ_BODY(iseq)->stack_max);
766 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_EVAL | VM_FRAME_FLAG_FINISH,
767 vm_block_self(base_block), VM_GUARDED_PREV_EP(vm_block_ep(base_block)),
769 ISEQ_BODY(iseq)->iseq_encoded,
770 ec->cfp->sp, ISEQ_BODY(iseq)->local_table_size,
771 ISEQ_BODY(iseq)->stack_max);
780 GetBindingPtr(toplevel_binding, bind);
783 vm_set_eval_stack(ec, iseq, 0, &bind->block);
786 if (ISEQ_BODY(iseq)->local_table_size > 0) {
787 vm_bind_update_env(toplevel_binding, bind, vm_make_env_object(ec, ec->cfp));
794 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
798 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
806 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
807 if (VM_FRAME_RUBYFRAME_P(cfp)) {
810 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
818 if (VM_FRAME_RUBYFRAME_P(cfp)) {
822 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
824 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
825 if (VM_FRAME_RUBYFRAME_P(cfp)) {
829 if (VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_PASSED) == FALSE) {
832 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
838 rb_vm_pop_cfunc_frame(
void)
845 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
846 vm_pop_frame(ec, cfp, cfp->ep);
853 while (ec->cfp != cfp) {
855 printf(
"skipped frame: %s\n", vm_frametype_name(ec->cfp));
857 if (VM_FRAME_TYPE(ec->cfp) != VM_FRAME_MAGIC_CFUNC) {
861 rb_vm_pop_cfunc_frame();
874 nl->next = vm->at_exit;
879 ruby_vm_run_at_exit_hooks(
rb_vm_t *vm)
885 rb_vm_at_exit_func *func = l->func;
899 fputs(
"---\n", stderr);
900 ruby_debug_printf(
"envptr: %p\n", (
void *)&env->ep[0]);
901 ruby_debug_printf(
"envval: %10p ", (
void *)env->ep[1]);
903 ruby_debug_printf(
"ep: %10p\n", (
void *)env->ep);
904 if (rb_vm_env_prev_env(env)) {
905 fputs(
">>\n", stderr);
906 check_env_value(rb_vm_env_prev_env(env));
907 fputs(
"<<\n", stderr);
913 check_env_value(
const rb_env_t *env)
915 if (check_env(env)) {
925 switch (vm_block_handler_type(block_handler)) {
926 case block_handler_type_ifunc:
927 case block_handler_type_iseq:
928 return rb_vm_make_proc(ec, VM_BH_TO_CAPT_BLOCK(block_handler),
rb_cProc);
930 case block_handler_type_symbol:
931 case block_handler_type_proc:
932 return block_handler;
934 VM_UNREACHABLE(vm_block_handler_escape);
941 const VALUE *
const ep = cfp->ep;
942 VALUE *env_body, *env_ep;
943 int local_size, env_size;
945 if (VM_ENV_ESCAPED_P(ep)) {
946 return VM_ENV_ENVVAL(ep);
949 if (!VM_ENV_LOCAL_P(ep)) {
950 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
951 if (!VM_ENV_ESCAPED_P(prev_ep)) {
954 while (prev_cfp->ep != prev_ep) {
955 prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(prev_cfp);
956 VM_ASSERT(prev_cfp->ep != NULL);
959 vm_make_env_each(ec, prev_cfp);
960 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_SPECVAL], VM_GUARDED_PREV_EP(prev_cfp->ep));
964 VALUE block_handler = VM_ENV_BLOCK_HANDLER(ep);
966 if (block_handler != VM_BLOCK_HANDLER_NONE) {
967 VALUE blockprocval = vm_block_handler_escape(ec, block_handler);
968 VM_STACK_ENV_WRITE(ep, VM_ENV_DATA_INDEX_SPECVAL, blockprocval);
972 if (!VM_FRAME_RUBYFRAME_P(cfp)) {
973 local_size = VM_ENV_DATA_SIZE;
976 local_size = ISEQ_BODY(cfp->iseq)->local_table_size;
977 if (ISEQ_BODY(cfp->iseq)->param.flags.forwardable && VM_ENV_LOCAL_P(cfp->ep)) {
978 int ci_offset = local_size - ISEQ_BODY(cfp->iseq)->param.size + VM_ENV_DATA_SIZE;
981 local_size += vm_ci_argc(ci);
983 local_size += VM_ENV_DATA_SIZE;
998 env_size = local_size +
1006 MEMCPY(env_body, ep - (local_size - 1 ),
VALUE, local_size);
1008 env_ep = &env_body[local_size - 1 ];
1009 env_ep[VM_ENV_DATA_INDEX_ENV] = (
VALUE)env;
1011 env->iseq = (
rb_iseq_t *)(VM_FRAME_RUBYFRAME_P(cfp) ? cfp->iseq : NULL);
1013 env->env = env_body;
1014 env->env_size = env_size;
1017 VM_ENV_FLAGS_SET(env_ep, VM_ENV_FLAG_ESCAPED | VM_ENV_FLAG_WB_REQUIRED);
1018 VM_STACK_ENV_WRITE(ep, 0, (
VALUE)env);
1021 for (i = 0; i < local_size; i++) {
1022 if (VM_FRAME_RUBYFRAME_P(cfp)) {
1024 ep[-local_size + i] = 0;
1031 rb_yjit_invalidate_ep_is_bp(env->iseq);
1040 VALUE envval = vm_make_env_each(ec, cfp);
1043 check_env_value((
const rb_env_t *)envval);
1053 while ((cfp = rb_vm_get_binding_creatable_next_cfp(ec, cfp)) != 0) {
1054 vm_make_env_object(ec, cfp);
1055 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1060 rb_vm_env_prev_env(
const rb_env_t *env)
1062 const VALUE *ep = env->ep;
1064 if (VM_ENV_LOCAL_P(ep)) {
1068 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
1069 return VM_ENV_ENVVAL_PTR(prev_ep);
1077 if (!iseq)
return 0;
1078 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1079 local_var_list_add(vars, ISEQ_BODY(iseq)->local_table[i]);
1088 if (VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_ISOLATED))
break;
1089 collect_local_variables_in_iseq(env->iseq, vars);
1090 }
while ((env = rb_vm_env_prev_env(env)) != NULL);
1096 if (VM_ENV_ESCAPED_P(ep)) {
1097 collect_local_variables_in_env(VM_ENV_ENVVAL_PTR(ep), vars);
1106 rb_vm_env_local_variables(
const rb_env_t *env)
1109 local_var_list_init(&vars);
1110 collect_local_variables_in_env(env, &vars);
1111 return local_var_list_finish(&vars);
1115 rb_iseq_local_variables(
const rb_iseq_t *iseq)
1118 local_var_list_init(&vars);
1119 while (collect_local_variables_in_iseq(iseq, &vars)) {
1120 iseq = ISEQ_BODY(iseq)->parent_iseq;
1122 return local_var_list_finish(&vars);
1128 vm_proc_create_from_captured(
VALUE klass,
1130 enum rb_block_type block_type,
1131 int8_t is_from_method, int8_t is_lambda)
1133 VALUE procval = rb_proc_alloc(klass);
1136 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), captured->ep));
1139 RB_OBJ_WRITE(procval, &proc->block.as.captured.code.val, captured->code.val);
1140 RB_OBJ_WRITE(procval, &proc->block.as.captured.self, captured->self);
1141 rb_vm_block_ep_update(procval, &proc->block, captured->ep);
1143 vm_block_type_set(&proc->block, block_type);
1144 proc->is_from_method = is_from_method;
1145 proc->is_lambda = is_lambda;
1154 switch (vm_block_type(src)) {
1155 case block_type_iseq:
1156 case block_type_ifunc:
1157 RB_OBJ_WRITE(obj, &dst->as.captured.self, src->as.captured.self);
1158 RB_OBJ_WRITE(obj, &dst->as.captured.code.val, src->as.captured.code.val);
1159 rb_vm_block_ep_update(obj, dst, src->as.captured.ep);
1161 case block_type_symbol:
1164 case block_type_proc:
1171 proc_create(
VALUE klass,
const struct rb_block *block, int8_t is_from_method, int8_t is_lambda)
1173 VALUE procval = rb_proc_alloc(klass);
1176 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), vm_block_ep(block)));
1177 rb_vm_block_copy(procval, &proc->block, block);
1178 vm_block_type_set(&proc->block, block->type);
1179 proc->is_from_method = is_from_method;
1180 proc->is_lambda = is_lambda;
1186 rb_proc_dup(
VALUE self)
1191 GetProcPtr(
self, src);
1192 procval = proc_create(
rb_obj_class(
self), &src->block, src->is_from_method, src->is_lambda);
1208 if (SIZEOF_VOIDP > SIZEOF_LONG)
1217 if (SIZEOF_VOIDP > SIZEOF_LONG)
1223 static enum rb_id_table_iterator_result
1224 collect_outer_variable_names(
ID id,
VALUE val,
void *
ptr)
1233 if (data->isolate ||
1238 store = &data->read_only;
1243 return ID_TABLE_CONTINUE;
1247 env_copy(
const VALUE *src_ep,
VALUE read_only_variables)
1250 VM_ASSERT(src_env->ep == src_ep);
1253 VALUE *ep = &env_body[src_env->env_size - 2];
1254 const rb_env_t *copied_env = vm_env_new(ep, env_body, src_env->env_size, src_env->iseq);
1257 RB_OBJ_WRITE(copied_env, &ep[VM_ENV_DATA_INDEX_ME_CREF], src_ep[VM_ENV_DATA_INDEX_ME_CREF]);
1258 ep[VM_ENV_DATA_INDEX_FLAGS] = src_ep[VM_ENV_DATA_INDEX_FLAGS] | VM_ENV_FLAG_ISOLATED;
1259 if (!VM_ENV_LOCAL_P(src_ep)) {
1260 VM_ENV_FLAGS_SET(ep, VM_ENV_FLAG_LOCAL);
1263 if (read_only_variables) {
1264 for (
int i=
RARRAY_LENINT(read_only_variables)-1; i>=0; i--) {
1267 for (
unsigned int j=0; j<ISEQ_BODY(src_env->iseq)->local_table_size; j++) {
1268 if (
id == ISEQ_BODY(src_env->iseq)->local_table[j]) {
1269 VALUE v = src_env->env[j];
1272 VALUE msg =
rb_sprintf(
"can not make shareable Proc because it can refer"
1273 " unshareable object %+" PRIsVALUE
" from ", v);
1275 rb_str_catf(msg,
"variable '%" PRIsVALUE
"'", name);
1288 if (!VM_ENV_LOCAL_P(src_ep)) {
1289 const VALUE *prev_ep = VM_ENV_PREV_EP(src_env->ep);
1290 const rb_env_t *new_prev_env = env_copy(prev_ep, read_only_variables);
1291 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_GUARDED_PREV_EP(new_prev_env->ep);
1293 VM_ENV_FLAGS_UNSET(ep, VM_ENV_FLAG_LOCAL);
1296 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
1306 const rb_env_t *env = env_copy(captured->ep, read_only_variables);
1307 *((
const VALUE **)&proc->block.as.captured.ep) = env->ep;
1312 proc_shared_outer_variables(
struct rb_id_table *outer_variables,
bool isolate,
const char *message)
1320 rb_id_table_foreach(outer_variables, collect_outer_variable_names, (
void *)&data);
1322 if (data.ary !=
Qfalse) {
1323 VALUE str =
rb_sprintf(
"can not %s because it accesses outer variables", message);
1324 VALUE ary = data.ary;
1325 const char *sep =
" (";
1328 if (!name)
continue;
1337 else if (data.yield) {
1341 return data.read_only;
1345 rb_proc_isolate_bang(
VALUE self)
1347 const rb_iseq_t *iseq = vm_proc_iseq(
self);
1353 if (ISEQ_BODY(iseq)->outer_variables) {
1354 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables,
true,
"isolate a Proc");
1357 proc_isolate_env(
self, proc,
Qfalse);
1358 proc->is_isolated = TRUE;
1366 rb_proc_isolate(
VALUE self)
1368 VALUE dst = rb_proc_dup(
self);
1369 rb_proc_isolate_bang(dst);
1374 rb_proc_ractor_make_shareable(
VALUE self)
1376 const rb_iseq_t *iseq = vm_proc_iseq(
self);
1384 "Proc's self is not shareable: %" PRIsVALUE,
1390 if (ISEQ_BODY(iseq)->outer_variables) {
1391 read_only_variables =
1392 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables,
false,
"make a Proc shareable");
1395 proc_isolate_env(
self, proc, read_only_variables);
1396 proc->is_isolated = TRUE;
1407 enum imemo_type code_type = imemo_type(captured->code.val);
1409 if (!VM_ENV_ESCAPED_P(captured->ep)) {
1411 vm_make_env_object(ec, cfp);
1414 VM_ASSERT(VM_EP_IN_HEAP_P(ec, captured->ep));
1415 VM_ASSERT(code_type == imemo_iseq || code_type == imemo_ifunc);
1417 procval = vm_proc_create_from_captured(klass, captured,
1418 code_type == imemo_iseq ? block_type_iseq : block_type_ifunc,
1421 if (code_type == imemo_ifunc) {
1423 if (ifunc->svar_lep) {
1424 VALUE ep0 = ifunc->svar_lep[0];
1428 ifunc->svar_lep = (
VALUE *)env->ep;
1432 if (ep0 & VM_ENV_FLAG_ESCAPED) {
1436 ifunc->svar_lep = NULL;
1452 VALUE bindval, envval;
1455 if (cfp == 0 || ruby_level_cfp == 0) {
1458 if (!VM_FRAME_RUBYFRAME_P(src_cfp) &&
1459 !VM_FRAME_RUBYFRAME_P(RUBY_VM_PREVIOUS_CONTROL_FRAME(src_cfp))) {
1463 envval = vm_make_env_object(ec, cfp);
1465 GetBindingPtr(bindval, bind);
1466 vm_bind_update_env(bindval, bind, envval);
1467 RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, cfp->self);
1468 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, cfp->iseq);
1469 RB_OBJ_WRITE(bindval, &bind->pathobj, ISEQ_BODY(ruby_level_cfp->iseq)->location.pathobj);
1470 bind->first_lineno = rb_vm_get_sourceline(ruby_level_cfp);
1476 rb_binding_add_dynavars(
VALUE bindval,
rb_binding_t *bind,
int dyncount,
const ID *dynvars)
1478 VALUE envval, pathobj = bind->pathobj;
1479 VALUE path = pathobj_path(pathobj);
1480 VALUE realpath = pathobj_realpath(pathobj);
1487 if (dyncount < 0)
return 0;
1489 base_block = &bind->block;
1490 base_iseq = vm_block_iseq(base_block);
1494 dyns->size = dyncount;
1495 MEMCPY(dyns->ids, dynvars,
ID, dyncount);
1497 rb_node_init(RNODE(&tmp_node), NODE_SCOPE);
1498 tmp_node.nd_tbl = dyns;
1499 tmp_node.nd_body = 0;
1500 tmp_node.nd_args = 0;
1502 VALUE ast_value = rb_ruby_ast_new(RNODE(&tmp_node));
1505 iseq = rb_iseq_new(ast_value, ISEQ_BODY(base_iseq)->location.label, path, realpath, base_iseq, ISEQ_TYPE_EVAL);
1508 VALUE tempstr = rb_fstring_lit(
"<temp>");
1509 iseq = rb_iseq_new_top(ast_value, tempstr, tempstr, tempstr, NULL);
1511 tmp_node.nd_tbl = 0;
1514 vm_set_eval_stack(ec, iseq, 0, base_block);
1515 vm_bind_update_env(bindval, bind, envval = vm_make_env_object(ec, ec->cfp));
1516 rb_vm_pop_frame(ec);
1527 int arg_size = ISEQ_BODY(iseq)->param.size;
1529 vm_push_frame(ec, iseq,
type | VM_FRAME_FLAG_FINISH,
self,
1530 VM_GUARDED_PREV_EP(captured->ep),
1532 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1533 ec->cfp->sp + arg_size,
1534 ISEQ_BODY(iseq)->local_table_size - arg_size,
1535 ISEQ_BODY(iseq)->stack_max);
1542 int arg_size = ISEQ_BODY(iseq)->param.size;
1544 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
1546 vm_push_frame(ec, iseq,
type | VM_FRAME_FLAG_BMETHOD,
self,
1547 VM_GUARDED_PREV_EP(captured->ep),
1549 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1550 ec->cfp->sp + 1 + arg_size,
1551 ISEQ_BODY(iseq)->local_table_size - arg_size,
1552 ISEQ_BODY(iseq)->stack_max);
1554 VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);
1557 ALWAYS_INLINE(
static VALUE
1559 VALUE self,
int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler,
1564 VALUE self,
int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler,
1567 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
1569 VALUE type = VM_FRAME_MAGIC_BLOCK | (is_lambda ? VM_FRAME_FLAG_LAMBDA : 0);
1571 VALUE *sp = cfp->sp;
1572 int flags = (kw_splat ? VM_CALL_KW_SPLAT : 0);
1578 if (UNLIKELY(argc > VM_ARGC_STACK_MAX) &&
1579 (VM_ARGC_STACK_MAX >= 1 ||
1581 (argc != 1 || is_lambda))) {
1582 use_argv = vm_argv_ruby_array(av, argv, &flags, &argc, kw_splat);
1585 CHECK_VM_STACK_OVERFLOW(cfp, argc + 1);
1586 vm_check_canary(ec, sp);
1588 VALUE *stack_argv = sp;
1593 cfp->sp = stack_argv + argc;
1596 opt_pc = vm_yield_setup_args(ec, iseq, argc, stack_argv, flags, passed_block_handler,
1597 (is_lambda ? arg_setup_method : arg_setup_block));
1601 invoke_block(ec, iseq,
self, captured, cref,
type, opt_pc);
1604 invoke_bmethod(ec, iseq,
self, captured, me,
type, opt_pc);
1612 int argc,
const VALUE *argv,
1614 int is_lambda,
int force_blockarg)
1617 switch (vm_block_handler_type(block_handler)) {
1618 case block_handler_type_iseq:
1621 return invoke_iseq_block_from_c(ec, captured, captured->self,
1622 argc, argv, kw_splat, passed_block_handler,
1623 cref, is_lambda, NULL);
1625 case block_handler_type_ifunc:
1626 return vm_yield_with_cfunc(ec, VM_BH_TO_IFUNC_BLOCK(block_handler),
1627 VM_BH_TO_IFUNC_BLOCK(block_handler)->
self,
1628 argc, argv, kw_splat, passed_block_handler, NULL);
1629 case block_handler_type_symbol:
1630 return vm_yield_with_symbol(ec, VM_BH_TO_SYMBOL(block_handler),
1631 argc, argv, kw_splat, passed_block_handler);
1632 case block_handler_type_proc:
1633 if (force_blockarg == FALSE) {
1634 is_lambda = block_proc_is_lambda(VM_BH_TO_PROC(block_handler));
1636 block_handler = vm_proc_to_block_handler(VM_BH_TO_PROC(block_handler));
1639 VM_UNREACHABLE(invoke_block_from_c_splattable);
1646 VALUE block_handler = VM_CF_BLOCK_HANDLER(ec->cfp);
1647 vm_block_handler_verify(block_handler);
1648 if (UNLIKELY(block_handler == VM_BLOCK_HANDLER_NONE)) {
1649 rb_vm_localjump_error(
"no block given",
Qnil, 0);
1652 return block_handler;
1658 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1659 argc, argv, kw_splat, VM_BLOCK_HANDLER_NONE,
1660 cref, is_lambda, FALSE);
1666 return vm_yield_with_cref(ec, argc, argv, kw_splat, NULL, FALSE);
1672 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1673 argc, argv, kw_splat, block_handler,
1674 NULL, FALSE, FALSE);
1680 return invoke_block_from_c_bh(ec, check_block_handler(ec), 1, &args,
1684 ALWAYS_INLINE(
static VALUE
1687 int kw_splat,
VALUE passed_block_handler,
int is_lambda,
1693 int kw_splat,
VALUE passed_block_handler,
int is_lambda,
1696 const struct rb_block *block = &proc->block;
1699 switch (vm_block_type(block)) {
1700 case block_type_iseq:
1701 return invoke_iseq_block_from_c(ec, &block->as.captured,
self, argc, argv, kw_splat, passed_block_handler, NULL, is_lambda, me);
1702 case block_type_ifunc:
1703 if (kw_splat == 1) {
1704 VALUE keyword_hash = argv[argc-1];
1706 keyword_hash = rb_to_hash_type(keyword_hash);
1715 return vm_yield_with_cfunc(ec, &block->as.captured,
self, argc, argv, kw_splat, passed_block_handler, me);
1716 case block_type_symbol:
1717 return vm_yield_with_symbol(ec, block->as.symbol, argc, argv, kw_splat, passed_block_handler);
1718 case block_type_proc:
1719 is_lambda = block_proc_is_lambda(block->as.proc);
1720 block = vm_proc_block(block->as.proc);
1723 VM_UNREACHABLE(invoke_block_from_c_proc);
1729 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler)
1731 return invoke_block_from_c_proc(ec, proc,
self, argc, argv, kw_splat, passed_block_handler, proc->is_lambda, NULL);
1738 return invoke_block_from_c_proc(ec, proc,
self, argc, argv, kw_splat, block_handler, TRUE, me);
1743 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler)
1745 VALUE self = vm_block_self(&proc->block);
1746 vm_block_handler_verify(passed_block_handler);
1748 if (proc->is_from_method) {
1749 return vm_invoke_bmethod(ec, proc,
self, argc, argv, kw_splat, passed_block_handler, NULL);
1752 return vm_invoke_proc(ec, proc,
self, argc, argv, kw_splat, passed_block_handler);
1758 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler)
1760 vm_block_handler_verify(passed_block_handler);
1762 if (proc->is_from_method) {
1763 return vm_invoke_bmethod(ec, proc,
self, argc, argv, kw_splat, passed_block_handler, NULL);
1766 return vm_invoke_proc(ec, proc,
self, argc, argv, kw_splat, passed_block_handler);
1775 while (cfp->pc == 0 || cfp->iseq == 0) {
1776 if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_IFUNC) {
1778 return ifunc->svar_lep;
1781 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1784 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
1789 return (
VALUE *)VM_CF_LEP(cfp);
1795 return lep_svar_get(ec, rb_vm_svar_lep(ec, cfp), key);
1801 lep_svar_set(ec, rb_vm_svar_lep(ec, cfp), key, val);
1807 return vm_cfp_svar_get(ec, ec->cfp, key);
1813 vm_cfp_svar_set(ec, ec->cfp, key, val);
1819 return vm_svar_get(GET_EC(), VM_SVAR_BACKREF);
1825 vm_svar_set(GET_EC(), VM_SVAR_BACKREF, val);
1831 return vm_svar_get(GET_EC(), VM_SVAR_LASTLINE);
1837 vm_svar_set(GET_EC(), VM_SVAR_LASTLINE, val);
1841 rb_lastline_set_up(
VALUE val,
unsigned int up)
1845 for(
unsigned int i = 0; i < up; i++) {
1846 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1848 vm_cfp_svar_set(GET_EC(), cfp, VM_SVAR_LASTLINE, val);
1874 return rb_vm_get_sourceline(cfp);
1882 rb_source_location(
int *pline)
1887 if (cfp && VM_FRAME_RUBYFRAME_P(cfp)) {
1888 if (pline) *pline = rb_vm_get_sourceline(cfp);
1889 return rb_iseq_path(cfp->iseq);
1892 if (pline) *pline = 0;
1898 rb_source_location_cstr(
int *pline)
1900 VALUE path = rb_source_location(pline);
1901 if (
NIL_P(path))
return NULL;
1909 return vm_ec_cref(ec);
1913 rb_vm_cref_replace_with_duplicated_cref(
void)
1917 rb_cref_t *cref = vm_cref_replace_with_duplicated_cref(cfp->ep);
1923 rb_vm_cref_in_context(
VALUE self,
VALUE cbase)
1928 if (!cfp || cfp->self !=
self)
return NULL;
1929 if (!vm_env_cref_by_cref(cfp->ep))
return NULL;
1930 cref = vm_get_cref(cfp->ep);
1931 if (CREF_CLASS(cref) != cbase)
return NULL;
1940 dp(CREF_CLASS(cref));
1941 printf(
"%ld\n", CREF_VISI(cref));
1942 cref = CREF_NEXT(cref);
1956 return vm_get_cbase(cfp->ep);
1962 make_localjump_error(
const char *mesg,
VALUE value,
int reason)
1994 rb_vm_localjump_error(
const char *mesg,
VALUE value,
int reason)
1996 VALUE exc = make_localjump_error(mesg, value, reason);
2001 rb_vm_make_jump_tag_but_local_jump(
enum ruby_tag_type state,
VALUE val)
2007 mesg =
"unexpected return";
2010 mesg =
"unexpected break";
2013 mesg =
"unexpected next";
2016 mesg =
"unexpected redo";
2020 mesg =
"retry outside of rescue clause";
2027 val = GET_EC()->tag->retval;
2029 return make_localjump_error(mesg, val, state);
2033 rb_vm_jump_tag_but_local_jump(
enum ruby_tag_type state)
2035 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
2037 EC_JUMP_TAG(GET_EC(), state);
2043 while (VM_ENV_LOCAL_P(cfp->ep)) {
2044 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2055 const VALUE *ep = VM_CF_PREV_EP(cfp);
2059 rb_vm_localjump_error(
"unexpected break", val, TAG_BREAK);
2062 ec->errinfo = (
VALUE)THROW_DATA_NEW(val, target_cfp, TAG_BREAK);
2063 EC_JUMP_TAG(ec, TAG_BREAK);
2069 vm_iter_break(GET_EC(),
Qnil);
2075 vm_iter_break(GET_EC(), val);
2080 short ruby_vm_redefined_flag[BOP_LAST_];
2081 static st_table *vm_opt_method_def_table = 0;
2082 static st_table *vm_opt_mid_table = 0;
2085 rb_free_vm_opt_tables(
void)
2087 st_free_table(vm_opt_method_def_table);
2088 st_free_table(vm_opt_mid_table);
2092 vm_redefinition_check_flag(
VALUE klass)
2094 if (klass ==
rb_cInteger)
return INTEGER_REDEFINED_OP_FLAG;
2095 if (klass ==
rb_cFloat)
return FLOAT_REDEFINED_OP_FLAG;
2096 if (klass ==
rb_cString)
return STRING_REDEFINED_OP_FLAG;
2097 if (klass ==
rb_cArray)
return ARRAY_REDEFINED_OP_FLAG;
2098 if (klass ==
rb_cHash)
return HASH_REDEFINED_OP_FLAG;
2099 if (klass ==
rb_cSymbol)
return SYMBOL_REDEFINED_OP_FLAG;
2101 if (klass ==
rb_cTime)
return TIME_REDEFINED_OP_FLAG;
2103 if (klass ==
rb_cRegexp)
return REGEXP_REDEFINED_OP_FLAG;
2104 if (klass ==
rb_cNilClass)
return NIL_REDEFINED_OP_FLAG;
2107 if (klass ==
rb_cProc)
return PROC_REDEFINED_OP_FLAG;
2112 rb_vm_check_optimizable_mid(
VALUE mid)
2114 if (!vm_opt_mid_table) {
2118 return st_lookup(vm_opt_mid_table, mid, NULL);
2124 if (me->called_id != me->def->original_id) {
2128 if (METHOD_ENTRY_BASIC(me))
return TRUE;
2131 switch (def->type) {
2132 case VM_METHOD_TYPE_CFUNC:
2133 case VM_METHOD_TYPE_OPTIMIZED:
2148 if (vm_redefinition_check_method_type(me)) {
2149 if (st_lookup(vm_opt_method_def_table, (st_data_t)me->def, &bop)) {
2150 int flag = vm_redefinition_check_flag(klass);
2154 "Redefining '%s#%s' disables interpreter and JIT optimizations",
2158 rb_yjit_bop_redefined(flag, (
enum ruby_basic_operators)bop);
2159 rb_rjit_bop_redefined(flag, (
enum ruby_basic_operators)bop);
2160 ruby_vm_redefined_flag[bop] |= flag;
2166 static enum rb_id_table_iterator_result
2167 check_redefined_method(
ID mid,
VALUE value,
void *data)
2173 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
2175 return ID_TABLE_CONTINUE;
2179 rb_vm_check_redefinition_by_prepend(
VALUE klass)
2181 if (!vm_redefinition_check_flag(klass))
return;
2182 rb_id_table_foreach(RCLASS_M_TBL(RCLASS_ORIGIN(klass)), check_redefined_method, (
void *)klass);
2186 add_opt_method_entry_bop(
const rb_method_entry_t *me,
ID mid,
enum ruby_basic_operators bop)
2188 st_insert(vm_opt_method_def_table, (st_data_t)me->def, (st_data_t)bop);
2189 st_insert(vm_opt_mid_table, (st_data_t)mid, (st_data_t)
Qtrue);
2193 add_opt_method(
VALUE klass,
ID mid,
enum ruby_basic_operators bop)
2197 if (me && vm_redefinition_check_method_type(me)) {
2198 add_opt_method_entry_bop(me, mid, bop);
2205 static enum ruby_basic_operators vm_redefinition_bop_for_id(
ID mid);
2210 if (me && vm_redefinition_check_method_type(me)) {
2211 ID mid = me->called_id;
2212 enum ruby_basic_operators bop = vm_redefinition_bop_for_id(mid);
2213 if ((
int)bop >= 0) {
2214 add_opt_method_entry_bop(me, mid, bop);
2220 vm_init_redefined_flag(
void)
2223 enum ruby_basic_operators bop;
2225 #define OP(mid_, bop_) (mid = id##mid_, bop = BOP_##bop_, ruby_vm_redefined_flag[bop] = 0)
2226 #define C(k) add_opt_method(rb_c##k, mid, bop)
2227 OP(PLUS, PLUS), (C(Integer), C(Float), C(String), C(Array));
2228 OP(MINUS, MINUS), (C(Integer), C(Float));
2229 OP(MULT, MULT), (C(Integer), C(Float));
2230 OP(DIV, DIV), (C(Integer), C(Float));
2231 OP(MOD, MOD), (C(Integer), C(Float));
2232 OP(Eq, EQ), (C(Integer), C(Float), C(String), C(Symbol));
2233 OP(Eqq, EQQ), (C(Integer), C(Float), C(Symbol), C(String),
2234 C(NilClass), C(TrueClass), C(FalseClass));
2235 OP(LT, LT), (C(Integer), C(Float));
2236 OP(LE, LE), (C(Integer), C(Float));
2237 OP(GT, GT), (C(Integer), C(Float));
2238 OP(GE, GE), (C(Integer), C(Float));
2239 OP(LTLT, LTLT), (C(String), C(Array));
2240 OP(AREF, AREF), (C(Array), C(Hash), C(Integer));
2241 OP(ASET, ASET), (C(Array), C(Hash));
2242 OP(Length, LENGTH), (C(Array), C(String), C(Hash));
2243 OP(Size, SIZE), (C(Array), C(String), C(Hash));
2244 OP(EmptyP, EMPTY_P), (C(Array), C(String), C(Hash));
2245 OP(Succ, SUCC), (C(Integer), C(String));
2246 OP(EqTilde, MATCH), (C(
Regexp), C(String));
2247 OP(Freeze, FREEZE), (C(String), C(Array), C(Hash));
2248 OP(UMinus, UMINUS), (C(String));
2249 OP(Max, MAX), (C(Array));
2250 OP(Min, MIN), (C(Array));
2251 OP(Hash, HASH), (C(Array));
2252 OP(Call, CALL), (C(Proc));
2253 OP(And, AND), (C(Integer));
2254 OP(Or, OR), (C(Integer));
2255 OP(NilP,
NIL_P), (C(NilClass));
2256 OP(Cmp, CMP), (C(Integer), C(Float), C(String));
2257 OP(Default, DEFAULT), (C(Hash));
2262 static enum ruby_basic_operators
2263 vm_redefinition_bop_for_id(
ID mid)
2266 #define OP(mid_, bop_) case id##mid_: return BOP_##bop_
2283 OP(EmptyP, EMPTY_P);
2296 OP(Default, DEFAULT);
2309 switch (VM_FRAME_TYPE(cfp)) {
2310 case VM_FRAME_MAGIC_METHOD:
return "method";
2311 case VM_FRAME_MAGIC_BLOCK:
return "block";
2312 case VM_FRAME_MAGIC_CLASS:
return "class";
2313 case VM_FRAME_MAGIC_TOP:
return "top";
2314 case VM_FRAME_MAGIC_CFUNC:
return "cfunc";
2315 case VM_FRAME_MAGIC_IFUNC:
return "ifunc";
2316 case VM_FRAME_MAGIC_EVAL:
return "eval";
2317 case VM_FRAME_MAGIC_RESCUE:
return "rescue";
2327 if (THROW_DATA_P(err) &&
2328 THROW_DATA_STATE(err) == TAG_BREAK &&
2329 THROW_DATA_CONSUMED_P(err) == FALSE) {
2330 return THROW_DATA_VAL(err);
2342 unsigned long type = VM_FRAME_TYPE(cfp);
2343 #define C(t) if (type == VM_FRAME_MAGIC_##t) return #t
2372 switch (VM_FRAME_TYPE(ec->cfp)) {
2373 case VM_FRAME_MAGIC_METHOD:
2374 RUBY_DTRACE_METHOD_RETURN_HOOK(ec, 0, 0);
2375 EXEC_EVENT_HOOK_AND_POP_FRAME(ec,
RUBY_EVENT_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2379 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2382 THROW_DATA_CONSUMED_SET(err);
2384 case VM_FRAME_MAGIC_BLOCK:
2385 if (VM_FRAME_BMETHOD_P(ec->cfp)) {
2386 VALUE bmethod_return_value = frame_return_value(err);
2387 if (cfp_returning_with_value) {
2389 bmethod_return_value = THROW_DATA_VAL(err);
2393 EXEC_EVENT_HOOK_AND_POP_FRAME(ec,
RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, bmethod_return_value);
2396 ec->cfp->self, 0, 0, 0, bmethod_return_value, TRUE);
2402 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2403 rb_vm_frame_method_entry(ec->cfp)->called_id,
2404 rb_vm_frame_method_entry(ec->cfp)->owner,
2405 bmethod_return_value);
2407 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
2408 local_hooks = me->def->body.bmethod.hooks;
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, TRUE);
2417 THROW_DATA_CONSUMED_SET(err);
2420 EXEC_EVENT_HOOK_AND_POP_FRAME(ec,
RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2423 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2425 THROW_DATA_CONSUMED_SET(err);
2428 case VM_FRAME_MAGIC_CLASS:
2521 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2523 struct rb_vm_exec_context {
2531 vm_exec_bottom_main(
void *context)
2533 struct rb_vm_exec_context *ctx = context;
2536 ctx->result = vm_exec_loop(ec, TAG_NONE, ctx->tag, vm_exec_core(ec));
2540 vm_exec_bottom_rescue(
void *context)
2542 struct rb_vm_exec_context *ctx = context;
2545 ctx->result = vm_exec_loop(ec, rb_ec_tag_state(ec), ctx->tag, ec->errinfo);
2558 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2559 struct rb_vm_exec_context ctx = {
2567 rb_wasm_try_catch_init(&try_catch, vm_exec_bottom_main, vm_exec_bottom_rescue, &ctx);
2569 rb_wasm_try_catch_loop_run(&try_catch, &RB_VM_TAG_JMPBUF_GET(_tag.buf));
2571 result = ctx.result;
2573 enum ruby_tag_type state;
2574 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2575 if (UNDEF_P(result = jit_exec(ec))) {
2576 result = vm_exec_core(ec);
2579 result = vm_exec_loop(ec, TAG_NONE, &_tag, result);
2582 result = vm_exec_loop(ec, state, &_tag, ec->errinfo);
2594 if (state == TAG_NONE) {
2598 rb_ec_raised_reset(ec, RAISED_STACKOVERFLOW | RAISED_NOMEMORY);
2599 while (UNDEF_P(result = vm_exec_handle_exception(ec, state, result))) {
2602 if (UNDEF_P(result = jit_exec_exception(ec))) {
2603 result = vm_exec_core(ec);
2606 VM_ASSERT(ec->tag == tag);
2608 if ((state = tag->state) == TAG_NONE)
break;
2609 tag->state = TAG_NONE;
2624 unsigned long epc, cont_pc, cont_sp;
2629 cont_pc = cont_sp = 0;
2632 while (ec->cfp->pc == 0 || ec->cfp->iseq == 0) {
2633 if (UNLIKELY(VM_FRAME_TYPE(ec->cfp) == VM_FRAME_MAGIC_CFUNC)) {
2635 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2636 rb_vm_frame_method_entry(ec->cfp)->called_id,
2637 rb_vm_frame_method_entry(ec->cfp)->owner,
Qnil);
2638 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec,
2639 rb_vm_frame_method_entry(ec->cfp)->owner,
2640 rb_vm_frame_method_entry(ec->cfp)->def->original_id);
2642 rb_vm_pop_frame(ec);
2646 epc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
2649 if (state == TAG_BREAK || state == TAG_RETURN) {
2650 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2652 if (cfp == escape_cfp) {
2653 if (state == TAG_RETURN) {
2654 if (!VM_FRAME_FINISHED_P(cfp)) {
2655 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2656 THROW_DATA_STATE_SET(err, state = TAG_BREAK);
2659 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2660 if (ct)
for (i = 0; i < ct->size; i++) {
2661 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2662 if (entry->start < epc && entry->end >= epc) {
2663 if (entry->type == CATCH_TYPE_ENSURE) {
2664 catch_iseq = entry->iseq;
2665 cont_pc = entry->cont;
2666 cont_sp = entry->sp;
2671 if (catch_iseq == NULL) {
2673 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2675 hook_before_rewind(ec,
true, state, err);
2676 rb_vm_pop_frame(ec);
2677 return THROW_DATA_VAL(err);
2684 *cfp->sp++ = THROW_DATA_VAL(err);
2691 if (state == TAG_RAISE) {
2692 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2693 if (ct)
for (i = 0; i < ct->size; i++) {
2694 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2695 if (entry->start < epc && entry->end >= epc) {
2697 if (entry->type == CATCH_TYPE_RESCUE ||
2698 entry->type == CATCH_TYPE_ENSURE) {
2699 catch_iseq = entry->iseq;
2700 cont_pc = entry->cont;
2701 cont_sp = entry->sp;
2707 else if (state == TAG_RETRY) {
2708 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2709 if (ct)
for (i = 0; i < ct->size; i++) {
2710 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2711 if (entry->start < epc && entry->end >= epc) {
2713 if (entry->type == CATCH_TYPE_ENSURE) {
2714 catch_iseq = entry->iseq;
2715 cont_pc = entry->cont;
2716 cont_sp = entry->sp;
2719 else if (entry->type == CATCH_TYPE_RETRY) {
2721 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2722 if (cfp == escape_cfp) {
2723 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2731 else if ((state == TAG_BREAK && !escape_cfp) ||
2732 (state == TAG_REDO) ||
2733 (state == TAG_NEXT)) {
2734 type = (
const enum rb_catch_type[TAG_MASK]) {
2735 [TAG_BREAK] = CATCH_TYPE_BREAK,
2736 [TAG_NEXT] = CATCH_TYPE_NEXT,
2737 [TAG_REDO] = CATCH_TYPE_REDO,
2741 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2742 if (ct)
for (i = 0; i < ct->size; i++) {
2743 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2745 if (entry->start < epc && entry->end >= epc) {
2746 if (entry->type == CATCH_TYPE_ENSURE) {
2747 catch_iseq = entry->iseq;
2748 cont_pc = entry->cont;
2749 cont_sp = entry->sp;
2752 else if (entry->type ==
type) {
2753 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2754 cfp->sp = vm_base_ptr(cfp) + entry->sp;
2756 if (state != TAG_REDO) {
2757 *cfp->sp++ = THROW_DATA_VAL(err);
2760 VM_ASSERT(ec->tag->state == TAG_NONE);
2767 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2768 if (ct)
for (i = 0; i < ct->size; i++) {
2769 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2770 if (entry->start < epc && entry->end >= epc) {
2772 if (entry->type == CATCH_TYPE_ENSURE) {
2773 catch_iseq = entry->iseq;
2774 cont_pc = entry->cont;
2775 cont_sp = entry->sp;
2782 if (catch_iseq != NULL) {
2784 const int arg_size = 1;
2786 rb_iseq_check(catch_iseq);
2787 cfp->sp = vm_base_ptr(cfp) + cont_sp;
2788 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + cont_pc;
2791 cfp->sp[0] = (
VALUE)err;
2792 vm_push_frame(ec, catch_iseq, VM_FRAME_MAGIC_RESCUE,
2794 VM_GUARDED_PREV_EP(cfp->ep),
2796 ISEQ_BODY(catch_iseq)->iseq_encoded,
2797 cfp->sp + arg_size ,
2798 ISEQ_BODY(catch_iseq)->local_table_size - arg_size,
2799 ISEQ_BODY(catch_iseq)->stack_max);
2802 ec->tag->state = TAG_NONE;
2808 hook_before_rewind(ec, (cfp == escape_cfp), state, err);
2810 if (VM_FRAME_FINISHED_P(ec->cfp)) {
2811 rb_vm_pop_frame(ec);
2812 ec->errinfo = (
VALUE)err;
2813 ec->tag = ec->tag->prev;
2814 EC_JUMP_TAG(ec, state);
2817 rb_vm_pop_frame(ec);
2830 vm_set_top_stack(ec, iseq);
2836 rb_iseq_eval_main(
const rb_iseq_t *iseq)
2841 vm_set_main_stack(ec, iseq);
2852 if (idp) *idp = me->def->original_id;
2853 if (called_idp) *called_idp = me->called_id;
2854 if (klassp) *klassp = me->owner;
2865 return rb_vm_control_frame_id_and_class(ec->cfp, idp, called_idp, klassp);
2871 return rb_ec_frame_method_id_and_class(GET_EC(), idp, 0, klassp);
2880 const rb_iseq_t *iseq = rb_iseq_new(
Qnil, filename, filename,
Qnil, 0, ISEQ_TYPE_TOP);
2883 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH,
2884 recv, block_handler,
2885 (
VALUE)vm_cref_new_toplevel(ec),
2886 0, reg_cfp->sp, 0, 0);
2890 rb_vm_pop_frame(ec);
2897 rb_vm_update_references(
void *
ptr)
2908 if (vm->load_path_check_cache) {
2909 vm->load_path_check_cache =
rb_gc_location(vm->load_path_check_cache);
2914 vm->loaded_features_snapshot =
rb_gc_location(vm->loaded_features_snapshot);
2915 vm->loaded_features_realpaths =
rb_gc_location(vm->loaded_features_realpaths);
2916 vm->loaded_features_realpath_map =
rb_gc_location(vm->loaded_features_realpath_map);
2922 rb_gc_update_values(RUBY_NSIG, vm->trap_list.cmd);
2924 if (vm->coverages) {
2932 rb_vm_each_stack_value(
void *
ptr,
void (*cb)(
VALUE,
void*),
void *ctx)
2937 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2938 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
2939 rb_ractor_status_p(r, ractor_running));
2940 if (r->threads.cnt > 0) {
2942 ccan_list_for_each(&r->threads.set, th, lt_node) {
2943 VM_ASSERT(th != NULL);
2946 VALUE *p = ec->vm_stack;
2947 VALUE *sp = ec->cfp->sp;
2961 static enum rb_id_table_iterator_result
2962 vm_mark_negative_cme(
VALUE val,
void *dmy)
2965 return ID_TABLE_CONTINUE;
2968 void rb_thread_sched_mark_zombies(
rb_vm_t *vm);
2971 rb_vm_mark(
void *
ptr)
2973 RUBY_MARK_ENTER(
"vm");
2974 RUBY_GC_INFO(
"-------------------------------------------------\n");
2980 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2982 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
2983 rb_ractor_status_p(r, ractor_running));
2987 for (
struct global_object_list *list = vm->global_object_list; list; list = list->next) {
3005 if (vm->loading_table) {
3009 rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);
3011 rb_id_table_foreach_values(vm->negative_cme_table, vm_mark_negative_cme, NULL);
3013 for (i=0; i<VM_GLOBAL_CC_CACHE_TABLE_SIZE; i++) {
3014 const struct rb_callcache *cc = vm->global_cc_cache_table[i];
3017 if (!vm_cc_invalidated_p(cc)) {
3021 vm->global_cc_cache_table[i] = NULL;
3026 rb_thread_sched_mark_zombies(vm);
3030 RUBY_MARK_LEAVE(
"vm");
3033 #undef rb_vm_register_special_exception
3035 rb_vm_register_special_exception_str(
enum ruby_special_exceptions sp,
VALUE cls,
VALUE mesg)
3040 ((
VALUE *)vm->special_exceptions)[sp] = exc;
3041 rb_vm_register_global_object(exc);
3045 free_loading_table_entry(st_data_t key, st_data_t value, st_data_t arg)
3051 void rb_free_loaded_features_index(
rb_vm_t *vm);
3052 void rb_objspace_free_objects(
void *objspace);
3057 RUBY_FREE_ENTER(
"vm");
3061 VALUE *stack = th->ec->vm_stack;
3062 if (rb_free_at_exit) {
3063 rb_free_encoded_insn_data();
3064 rb_free_global_enc_table();
3065 rb_free_loaded_builtin_table();
3067 rb_free_shared_fiber_pool();
3068 rb_free_static_symid_str();
3069 rb_free_transcoder_table();
3070 rb_free_vm_opt_tables();
3072 rb_free_rb_global_tbl();
3073 rb_free_loaded_features_index(vm);
3075 rb_id_table_free(vm->negative_cme_table);
3076 st_free_table(vm->overloaded_cme_table);
3078 rb_id_table_free(
RCLASS(rb_mRubyVMFrozenCore)->m_tbl);
3080 rb_shape_t *cursor = rb_shape_get_root_shape();
3081 rb_shape_t *end = rb_shape_get_shape_by_id(GET_SHAPE_TREE()->next_shape_id);
3082 while (cursor < end) {
3084 if (cursor->edges && !(((uintptr_t)cursor->edges) & 0x1))
3085 rb_id_table_free(cursor->edges);
3089 xfree(GET_SHAPE_TREE());
3091 st_free_table(vm->static_ext_inits);
3093 rb_vm_postponed_job_free();
3095 rb_id_table_free(vm->constant_cache);
3096 st_free_table(vm->unused_block_warning_table);
3103 #ifndef HAVE_SETPROCTITLE
3104 ruby_free_proctitle();
3109 rb_fiber_reset_root_local_storage(th);
3116 rb_vm_living_threads_init(vm);
3117 ruby_vm_run_at_exit_hooks(vm);
3118 if (vm->loading_table) {
3119 st_foreach(vm->loading_table, free_loading_table_entry, 0);
3120 st_free_table(vm->loading_table);
3121 vm->loading_table = 0;
3124 st_free_table(vm->ci_table);
3125 vm->ci_table = NULL;
3127 if (vm->frozen_strings) {
3128 st_free_table(vm->frozen_strings);
3129 vm->frozen_strings = 0;
3131 RB_ALTSTACK_FREE(vm->main_altstack);
3140 if (rb_free_at_exit) {
3141 rb_objspace_free_objects(objspace);
3142 rb_free_generic_iv_tbl_();
3143 rb_free_default_rand_key();
3144 if (th && vm->fork_gen == 0) {
3150 rb_objspace_free(objspace);
3155 ruby_current_vm_ptr = NULL;
3157 RUBY_FREE_LEAVE(
"vm");
3161 size_t rb_vm_memsize_waiting_fds(
struct ccan_list_head *waiting_fds);
3162 size_t rb_vm_memsize_workqueue(
struct ccan_list_head *workqueue);
3166 static enum rb_id_table_iterator_result
3167 vm_memsize_constant_cache_i(
ID id,
VALUE ics,
void *size)
3169 *((
size_t *) size) += rb_st_memsize((
st_table *) ics);
3170 return ID_TABLE_CONTINUE;
3177 vm_memsize_constant_cache(
void)
3180 size_t size = rb_id_table_memsize(vm->constant_cache);
3182 rb_id_table_foreach(vm->constant_cache, vm_memsize_constant_cache_i, &size);
3193 at_exit = at_exit->next;
3202 vm_memsize_builtin_function_table(
const struct rb_builtin_function *builtin_function_table)
3210 vm_memsize(
const void *
ptr)
3216 rb_vm_memsize_waiting_fds(&vm->waiting_fds) +
3217 rb_st_memsize(vm->loaded_features_index) +
3218 rb_st_memsize(vm->loading_table) +
3219 rb_vm_memsize_postponed_job_queue() +
3220 rb_vm_memsize_workqueue(&vm->workqueue) +
3221 vm_memsize_at_exit_list(vm->at_exit) +
3222 rb_st_memsize(vm->ci_table) +
3223 rb_st_memsize(vm->frozen_strings) +
3224 vm_memsize_builtin_function_table(vm->builtin_function_table) +
3225 rb_id_table_memsize(vm->negative_cme_table) +
3226 rb_st_memsize(vm->overloaded_cme_table) +
3227 vm_memsize_constant_cache() +
3239 {0, 0, vm_memsize,},
3240 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3245 vm_default_params(
void)
3248 VALUE result = rb_hash_new_with_size(4);
3249 #define SET(name) rb_hash_aset(result, ID2SYM(rb_intern(#name)), SIZET2NUM(vm->default_params.name));
3250 SET(thread_vm_stack_size);
3251 SET(thread_machine_stack_size);
3252 SET(fiber_vm_stack_size);
3253 SET(fiber_machine_stack_size);
3260 get_param(
const char *name,
size_t default_value,
size_t min_value)
3263 size_t result = default_value;
3264 if ((envval = getenv(name)) != 0) {
3265 long val = atol(envval);
3266 if (val < (
long)min_value) {
3267 val = (long)min_value;
3269 result = (size_t)(((val -1 + RUBY_VM_SIZE_ALIGN) / RUBY_VM_SIZE_ALIGN) * RUBY_VM_SIZE_ALIGN);
3271 if (0) ruby_debug_printf(
"%s: %"PRIuSIZE
"\n", name, result);
3277 check_machine_stack_size(
size_t *sizep)
3279 #ifdef PTHREAD_STACK_MIN
3280 size_t size = *sizep;
3283 #ifdef PTHREAD_STACK_MIN
3284 if (size < (
size_t)PTHREAD_STACK_MIN) {
3285 *sizep = (size_t)PTHREAD_STACK_MIN * 2;
3291 vm_default_params_setup(
rb_vm_t *vm)
3293 vm->default_params.thread_vm_stack_size =
3294 get_param(
"RUBY_THREAD_VM_STACK_SIZE",
3295 RUBY_VM_THREAD_VM_STACK_SIZE,
3296 RUBY_VM_THREAD_VM_STACK_SIZE_MIN);
3298 vm->default_params.thread_machine_stack_size =
3299 get_param(
"RUBY_THREAD_MACHINE_STACK_SIZE",
3300 RUBY_VM_THREAD_MACHINE_STACK_SIZE,
3301 RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN);
3303 vm->default_params.fiber_vm_stack_size =
3304 get_param(
"RUBY_FIBER_VM_STACK_SIZE",
3305 RUBY_VM_FIBER_VM_STACK_SIZE,
3306 RUBY_VM_FIBER_VM_STACK_SIZE_MIN);
3308 vm->default_params.fiber_machine_stack_size =
3309 get_param(
"RUBY_FIBER_MACHINE_STACK_SIZE",
3310 RUBY_VM_FIBER_MACHINE_STACK_SIZE,
3311 RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN);
3314 check_machine_stack_size(&vm->default_params.thread_machine_stack_size);
3315 check_machine_stack_size(&vm->default_params.fiber_machine_stack_size);
3321 rb_vm_living_threads_init(vm);
3322 vm->thread_report_on_exception = 1;
3323 vm->src_encoding_index = -1;
3325 vm_default_params_setup(vm);
3335 VALUE *p = ec->vm_stack;
3336 VALUE *sp = ec->cfp->sp;
3340 for (i = 0; i < (long)(sp - p); i++) {
3343 if (ref != update) {
3348 while (cfp != limit_cfp) {
3349 const VALUE *ep = cfp->ep;
3354 if (!VM_ENV_LOCAL_P(ep)) {
3355 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3356 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3357 VM_FORCE_WRITE(&prev_ep[VM_ENV_DATA_INDEX_ENV],
rb_gc_location(prev_ep[VM_ENV_DATA_INDEX_ENV]));
3360 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3361 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ENV],
rb_gc_location(ep[VM_ENV_DATA_INDEX_ENV]));
3362 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ME_CREF],
rb_gc_location(ep[VM_ENV_DATA_INDEX_ME_CREF]));
3366 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3373 static enum rb_id_table_iterator_result
3374 mark_local_storage_i(
VALUE local,
void *data)
3377 return ID_TABLE_CONTINUE;
3386 VALUE *p = ec->vm_stack;
3387 VALUE *sp = ec->cfp->sp;
3391 VM_ASSERT(sp == ec->cfp->sp);
3392 rb_gc_mark_vm_stack_values((
long)(sp - p), p);
3394 while (cfp != limit_cfp) {
3395 const VALUE *ep = cfp->ep;
3396 VM_ASSERT(!!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) == vm_ep_in_heap_p_(ec, ep));
3398 if (VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
3403 if (!VM_ENV_LOCAL_P(ep)) {
3404 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3405 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3409 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3416 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3421 if (ec->machine.stack_start && ec->machine.stack_end &&
3424 rb_gc_mark_machine_context(ec);
3429 if (ec->local_storage) {
3430 rb_id_table_foreach_values(ec->local_storage, mark_local_storage_i, NULL);
3432 rb_gc_mark(ec->local_storage_recursive_hash);
3433 rb_gc_mark(ec->local_storage_recursive_hash_for_trace);
3441 void rb_threadptr_root_fiber_setup(
rb_thread_t *th);
3442 void rb_threadptr_root_fiber_release(
rb_thread_t *th);
3445 thread_compact(
void *
ptr)
3451 if (!th->root_fiber) {
3452 rb_execution_context_update(th->ec);
3457 thread_mark(
void *
ptr)
3460 RUBY_MARK_ENTER(
"thread");
3461 rb_fiber_mark_self(th->ec->fiber_ptr);
3464 switch (th->invoke_type) {
3465 case thread_invoke_type_proc:
3466 case thread_invoke_type_ractor_proc:
3470 case thread_invoke_type_func:
3481 rb_gc_mark(th->pending_interrupt_mask_stack);
3484 if (th->root_fiber) rb_fiber_mark_self(th->root_fiber);
3486 RUBY_ASSERT(th->ec == rb_fiberptr_get_ec(th->ec->fiber_ptr));
3494 RUBY_MARK_LEAVE(
"thread");
3500 thread_free(
void *
ptr)
3503 RUBY_FREE_ENTER(
"thread");
3505 rb_threadptr_sched_free(th);
3507 if (th->locking_mutex !=
Qfalse) {
3508 rb_bug(
"thread_free: locking_mutex must be NULL (%p:%p)", (
void *)th, (
void *)th->locking_mutex);
3510 if (th->keeping_mutexes != NULL) {
3511 rb_bug(
"thread_free: keeping_mutexes must be NULL (%p:%p)", (
void *)th, (
void *)th->keeping_mutexes);
3516 rb_threadptr_root_fiber_release(th);
3518 if (th->vm && th->vm->ractor.main_thread == th) {
3519 RUBY_GC_INFO(
"MRI main thread\n");
3527 RUBY_FREE_LEAVE(
"thread");
3531 thread_memsize(
const void *
ptr)
3536 if (!th->root_fiber) {
3537 size += th->ec->vm_stack_size *
sizeof(
VALUE);
3539 if (th->ec->local_storage) {
3540 size += rb_id_table_memsize(th->ec->local_storage);
3545 #define thread_data_type ruby_threadptr_data_type
3554 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3558 rb_obj_is_thread(
VALUE obj)
3564 thread_alloc(
VALUE klass)
3573 ec->vm_stack = stack;
3574 ec->vm_stack_size = size;
3580 rb_ec_set_vm_stack(ec, stack, size);
3582 #if VM_CHECK_MODE > 0
3586 ec->cfp = (
void *)(ec->vm_stack + ec->vm_stack_size);
3590 VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME ,
3591 Qnil , VM_BLOCK_HANDLER_NONE ,
3593 0 , ec->vm_stack, 0, 0
3600 rb_ec_set_vm_stack(ec, NULL, 0);
3611 rb_threadptr_root_fiber_setup(th);
3615 th->scheduler =
Qnil;
3618 size_t size = vm->default_params.thread_vm_stack_size /
sizeof(
VALUE);
3619 rb_ec_initialize_vm_stack(th->ec,
ALLOC_N(
VALUE, size), size);
3622 VM_ASSERT(th->ec->cfp == NULL);
3623 VM_ASSERT(th->ec->vm_stack == NULL);
3624 VM_ASSERT(th->ec->vm_stack_size == 0);
3627 th->status = THREAD_RUNNABLE;
3628 th->last_status =
Qnil;
3629 th->top_wrapper = 0;
3630 th->top_self = vm->top_self;
3633 th->ec->errinfo =
Qnil;
3634 th->ec->root_svar =
Qfalse;
3635 th->ec->local_storage_recursive_hash =
Qnil;
3636 th->ec->local_storage_recursive_hash_for_trace =
Qnil;
3638 th->ec->storage =
Qnil;
3640 #if OPT_CALL_THREADED_CODE
3644 th->report_on_exception = vm->thread_report_on_exception;
3645 th->ext_config.ractor_safe =
true;
3647 #if USE_RUBY_DEBUG_LOG
3651 RUBY_DEBUG_LOG(
"th:%u", th->serial);
3656 rb_thread_alloc(
VALUE klass)
3658 VALUE self = thread_alloc(klass);
3660 target_th->ractor = GET_RACTOR();
3661 th_init(target_th,
self, target_th->vm = GET_VM());
3665 #define REWIND_CFP(expr) do { \
3666 rb_execution_context_t *ec__ = GET_EC(); \
3667 VALUE *const curr_sp = (ec__->cfp++)->sp; \
3668 VALUE *const saved_sp = ec__->cfp->sp; \
3669 ec__->cfp->sp = curr_sp; \
3671 (ec__->cfp--)->sp = saved_sp; \
3698 rb_clear_method_cache(
self, mid);
3704 m_core_set_postexe(
VALUE self)
3713 core_hash_merge(
VALUE hash,
long argc,
const VALUE *argv)
3716 VM_ASSERT(argc % 2 == 0);
3722 m_core_hash_merge_ptr(
int argc,
VALUE *argv,
VALUE recv)
3724 VALUE hash = argv[0];
3726 REWIND_CFP(hash = core_hash_merge(hash, argc-1, argv+1));
3742 REWIND_CFP(hash = core_hash_merge_kwd(hash, kw));
3754 m_core_make_shareable_copy(
VALUE recv,
VALUE obj)
3762 return rb_ractor_ensure_shareable(obj, name);
3772 extern VALUE *rb_gc_stack_start;
3773 extern size_t rb_gc_stack_maxsize;
3781 rb_vm_bugreport(NULL, stderr);
3790 #ifdef HAVE_BACKTRACE
3791 #include <execinfo.h>
3792 #define MAX_NATIVE_TRACE 1024
3793 static void *trace[MAX_NATIVE_TRACE];
3794 int n = (int)backtrace(trace, MAX_NATIVE_TRACE);
3795 char **syms = backtrace_symbols(trace, n);
3802 for (i=0; i<n; i++) {
3810 #if VM_COLLECT_USAGE_DETAILS
3811 static VALUE usage_analysis_insn_start(
VALUE self);
3812 static VALUE usage_analysis_operand_start(
VALUE self);
3813 static VALUE usage_analysis_register_start(
VALUE self);
3814 static VALUE usage_analysis_insn_stop(
VALUE self);
3815 static VALUE usage_analysis_operand_stop(
VALUE self);
3816 static VALUE usage_analysis_register_stop(
VALUE self);
3817 static VALUE usage_analysis_insn_running(
VALUE self);
3818 static VALUE usage_analysis_operand_running(
VALUE self);
3819 static VALUE usage_analysis_register_running(
VALUE self);
3820 static VALUE usage_analysis_insn_clear(
VALUE self);
3821 static VALUE usage_analysis_operand_clear(
VALUE self);
3822 static VALUE usage_analysis_register_clear(
VALUE self);
3828 return rb_f_raise(c, v);
3877 vm_keep_script_lines(
VALUE self)
3879 return RBOOL(ruby_vm_keep_script_lines);
3894 vm_keep_script_lines_set(
VALUE self,
VALUE flags)
3896 ruby_vm_keep_script_lines =
RTEST(flags);
3925 #if USE_DEBUG_COUNTER
3947 rb_define_method(klass,
"make_shareable_copy", m_core_make_shareable_copy, 1);
3950 RBASIC_CLEAR_CLASS(klass);
3952 rb_vm_register_global_object(fcore);
3953 rb_mRubyVMFrozenCore = fcore;
4116 #if VM_COLLECT_USAGE_DETAILS
4118 #define define_usage_analysis_hash(name) \
4119 rb_define_const(rb_cRubyVM, "USAGE_ANALYSIS_" #name, rb_hash_new())
4120 define_usage_analysis_hash(
INSN);
4121 define_usage_analysis_hash(REGS);
4122 define_usage_analysis_hash(INSN_BIGRAM);
4144 #if OPT_DIRECT_THREADED_CODE
4146 #elif OPT_TOKEN_THREADED_CODE
4148 #elif OPT_CALL_THREADED_CODE
4152 #if OPT_OPERANDS_UNIFICATION
4155 #if OPT_INSTRUCTIONS_UNIFICATION
4158 #if OPT_INLINE_METHOD_CACHE
4166 rb_define_const(rb_cRubyVM,
"INSTRUCTION_NAMES", rb_insns_name_array());
4191 rb_vm_t *vm = ruby_current_vm_ptr;
4193 VALUE filename = rb_fstring_lit(
"<main>");
4194 const rb_iseq_t *iseq = rb_iseq_new(
Qnil, filename, filename,
Qnil, 0, ISEQ_TYPE_TOP);
4197 rb_ractor_main_setup(vm, th->ractor, th);
4204 vm->ractor.main_thread = th;
4205 vm->ractor.main_ractor = th->ractor;
4207 th->top_wrapper = 0;
4208 th->top_self = rb_vm_top_self();
4210 rb_vm_register_global_object((
VALUE)iseq);
4211 th->ec->cfp->iseq = iseq;
4212 th->ec->cfp->pc = ISEQ_BODY(iseq)->iseq_encoded;
4213 th->ec->cfp->self = th->top_self;
4215 VM_ENV_FLAGS_UNSET(th->ec->cfp->ep, VM_FRAME_FLAG_CFRAME);
4216 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));
4224 rb_objspace_gc_enable(vm->gc.objspace);
4227 vm_init_redefined_flag();
4231 OPTIMIZED_METHOD_TYPE_BLOCK_CALL, 0, METHOD_VISI_PUBLIC);
4233 rb_vm_register_global_object(rb_block_param_proxy);
4236 Init_vm_backtrace();
4240 rb_vm_set_progname(
VALUE filename)
4247 rb_iseq_pathobj_set(cfp->iseq, filename, rb_iseq_realpath(cfp->iseq));
4256 rb_vm_t *vm = ruby_mimcalloc(1,
sizeof(*vm));
4259 fputs(
"[FATAL] failed to allocate memory\n", stderr);
4266 rb_vm_postponed_job_queue_init(vm);
4267 ruby_current_vm_ptr = vm;
4268 rb_objspace_alloc();
4269 vm->negative_cme_table = rb_id_table_create(16);
4270 vm->overloaded_cme_table = st_init_numtable();
4271 vm->constant_cache = rb_id_table_create(0);
4272 vm->unused_block_warning_table = st_init_numtable();
4275 const char *s = getenv(
"RUBY_TRY_UNUSED_BLOCK_WARNING_STRICT");
4276 if (s && strcmp(s,
"1") == 0) {
4277 vm->unused_block_warning_strict =
true;
4283 th->ractor = vm->ractor.main_ractor = rb_ractor_main_alloc();
4284 Init_native_thread(th);
4288 rb_ractor_set_current_ec(th->ractor, th->ec);
4290 ruby_thread_init_stack(th, native_main_thread_stack_top);
4296 vm_opt_method_def_table = st_init_numtable();
4297 vm_opt_mid_table = st_init_numtable();
4299 #ifdef RUBY_THREAD_WIN32_H
4307 native_main_thread_stack_top = addr;
4312 #include <sys/mman.h>
4316 #ifndef MARK_OBJECT_ARY_BUCKET_SIZE
4317 #define MARK_OBJECT_ARY_BUCKET_SIZE 1024
4327 pin_array_list_mark(
void *data)
4332 rb_gc_mark_vm_stack_values(array->len, array->array);
4336 pin_array_list_free(
void *data)
4339 xfree(array->array);
4343 pin_array_list_memsize(
const void *data)
4349 pin_array_list_update_references(
void *data)
4358 .dmark = pin_array_list_mark,
4359 .dfree = pin_array_list_free,
4360 .dsize = pin_array_list_memsize,
4361 .dcompact = pin_array_list_update_references,
4363 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
4367 pin_array_list_new(
VALUE next)
4372 array_list->array =
ALLOC_N(
VALUE, MARK_OBJECT_ARY_BUCKET_SIZE);
4382 if (array_list->len >= MARK_OBJECT_ARY_BUCKET_SIZE) {
4383 obj = pin_array_list_new(obj);
4387 RB_OBJ_WRITE(obj, &array_list->array[array_list->len], item);
4393 rb_vm_register_global_object(
VALUE obj)
4403 if (
FL_TEST(obj, RCLASS_IS_ROOT)) {
4406 FL_SET(obj, RCLASS_IS_ROOT);
4413 VALUE list = GET_VM()->mark_object_ary;
4414 VALUE head = pin_array_list_append(list, obj);
4416 GET_VM()->mark_object_ary = head;
4424 Init_vm_objects(
void)
4429 vm->mark_object_ary = pin_array_list_new(
Qnil);
4430 vm->loading_table = st_init_strtable();
4431 vm->ci_table = st_init_table(&vm_ci_hashtype);
4432 vm->frozen_strings = st_init_table_with_size(&rb_fstring_hash_type, 10000);
4437 void Init_builtin_yjit(
void) {}
4441 #include "yjit_hook.rbinc"
4445 void Init_builtin_rjit(
void) {}
4446 void Init_builtin_rjit_c(
void) {}
4452 main_to_s(
VALUE obj)
4458 rb_vm_top_self(
void)
4460 return GET_VM()->top_self;
4477 return &cr->verbose;
4487 bool rb_free_at_exit =
false;
4492 return rb_free_at_exit;
4501 rb_vm_fstring_table(
void)
4503 return GET_VM()->frozen_strings;
4506 #if VM_COLLECT_USAGE_DETAILS
4508 #define HASH_ASET(h, k, v) rb_hash_aset((h), (st_data_t)(k), (st_data_t)(v))
4522 vm_analysis_insn(
int insn)
4526 static int prev_insn = -1;
4532 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4533 CONST_ID(bigram_hash,
"USAGE_ANALYSIS_INSN_BIGRAM");
4537 HASH_ASET(uh,
INT2FIX(insn), ihash);
4545 if (prev_insn != -1) {
4564 vm_analysis_operand(
int insn,
int n,
VALUE op)
4574 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4579 HASH_ASET(uh,
INT2FIX(insn), ihash);
4583 HASH_ASET(ihash,
INT2FIX(n), ophash);
4586 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4596 vm_analysis_register(
int reg,
int isset)
4601 static const char regstrs[][5] = {
4609 static const char getsetstr[][4] = {
4613 static VALUE syms[
sizeof(regstrs) /
sizeof(regstrs[0])][2];
4617 CONST_ID(usage_hash,
"USAGE_ANALYSIS_REGS");
4622 for (i = 0; i < (int)(
sizeof(regstrs) /
sizeof(regstrs[0])); i++) {
4624 for (j = 0; j < 2; j++) {
4625 snprintf(buff, 0x10,
"%d %s %-4s", i, getsetstr[j], regstrs[i]);
4630 valstr = syms[reg][isset];
4641 static void (*ruby_vm_collect_usage_func_insn)(
int insn) = NULL;
4642 static void (*ruby_vm_collect_usage_func_operand)(
int insn,
int n,
VALUE op) = NULL;
4643 static void (*ruby_vm_collect_usage_func_register)(
int reg,
int isset) = NULL;
4647 usage_analysis_insn_start(
VALUE self)
4649 ruby_vm_collect_usage_func_insn = vm_analysis_insn;
4655 usage_analysis_operand_start(
VALUE self)
4657 ruby_vm_collect_usage_func_operand = vm_analysis_operand;
4663 usage_analysis_register_start(
VALUE self)
4665 ruby_vm_collect_usage_func_register = vm_analysis_register;
4671 usage_analysis_insn_stop(
VALUE self)
4673 ruby_vm_collect_usage_func_insn = 0;
4679 usage_analysis_operand_stop(
VALUE self)
4681 ruby_vm_collect_usage_func_operand = 0;
4687 usage_analysis_register_stop(
VALUE self)
4689 ruby_vm_collect_usage_func_register = 0;
4695 usage_analysis_insn_running(
VALUE self)
4697 return RBOOL(ruby_vm_collect_usage_func_insn != 0);
4702 usage_analysis_operand_running(
VALUE self)
4704 return RBOOL(ruby_vm_collect_usage_func_operand != 0);
4709 usage_analysis_register_running(
VALUE self)
4711 return RBOOL(ruby_vm_collect_usage_func_register != 0);
4715 usage_analysis_clear(
VALUE self,
ID usage_hash)
4727 usage_analysis_insn_clear(
VALUE self)
4732 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4733 CONST_ID(bigram_hash,
"USAGE_ANALYSIS_INSN_BIGRAM");
4734 usage_analysis_clear(rb_cRubyVM, usage_hash);
4735 return usage_analysis_clear(rb_cRubyVM, bigram_hash);
4740 usage_analysis_operand_clear(
VALUE self)
4744 CONST_ID(usage_hash,
"USAGE_ANALYSIS_INSN");
4745 return usage_analysis_clear(
self, usage_hash);
4750 usage_analysis_register_clear(
VALUE self)
4754 CONST_ID(usage_hash,
"USAGE_ANALYSIS_REGS");
4755 return usage_analysis_clear(
self, usage_hash);
4760 MAYBE_UNUSED(
static void (*ruby_vm_collect_usage_func_insn)(
int insn)) = 0;
4761 MAYBE_UNUSED(
static void (*ruby_vm_collect_usage_func_operand)(
int insn,
int n,
VALUE op)) = 0;
4762 MAYBE_UNUSED(
static void (*ruby_vm_collect_usage_func_register)(
int reg,
int isset)) = 0;
4766 #if VM_COLLECT_USAGE_DETAILS
4769 vm_collect_usage_insn(
int insn)
4771 if (RUBY_DTRACE_INSN_ENABLED()) {
4772 RUBY_DTRACE_INSN(rb_insns_name(insn));
4774 if (ruby_vm_collect_usage_func_insn)
4775 (*ruby_vm_collect_usage_func_insn)(insn);
4783 vm_collect_usage_operand(
int insn,
int n,
VALUE op)
4785 if (RUBY_DTRACE_INSN_OPERAND_ENABLED()) {
4788 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4790 RUBY_DTRACE_INSN_OPERAND(
RSTRING_PTR(valstr), rb_insns_name(insn));
4793 if (ruby_vm_collect_usage_func_operand)
4794 (*ruby_vm_collect_usage_func_operand)(insn, n, op);
4800 vm_collect_usage_register(
int reg,
int isset)
4802 if (ruby_vm_collect_usage_func_register)
4803 (*ruby_vm_collect_usage_func_register)(reg, isset);
4808 rb_vm_empty_cc(
void)
4810 return &vm_empty_cc;
4814 rb_vm_empty_cc_for_super(
void)
4816 return &vm_empty_cc_for_super;
4819 #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.