Ruby: vm.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

vm.c
1/**********************************************************************
2
3 Vm.c -
4
5 $Author$
6
7 Copyright (C) 2004-2007 Koichi Sasada
8
9**********************************************************************/
10
11#define vm_exec rb_vm_exec
12
13#include "eval_intern.h"
14#include "gc.h"
15#include "internal.h"
16#include "internal/compile.h"
17#include "internal/cont.h"
18#include "internal/error.h"
19#include "internal/eval.h"
20#include "internal/inits.h"
21#include "internal/object.h"
22#include "internal/parse.h"
23#include "internal/proc.h"
24#include "internal/re.h"
25#include "internal/symbol.h"
26#include "internal/thread.h"
27#include "internal/vm.h"
28#include "internal/sanitizers.h"
29#include "internal/variable.h"
30#include "iseq.h"
31#include "mjit.h"
32#include "yjit.h"
33#include "ruby/st.h"
34#include "ruby/vm.h"
35#include "vm_core.h"
36#include "vm_callinfo.h"
37#include "vm_debug.h"
38#include "vm_exec.h"
39#include "vm_insnhelper.h"
40#include "ractor_core.h"
41#include "vm_sync.h"
42
43#include "builtin.h"
44
45#ifndef MJIT_HEADER
46#include "probes.h"
47#else
48#include "probes.dmyh"
49#endif
50#include "probes_helper.h"
51
52#ifdef RUBY_ASSERT_CRITICAL_SECTION
53int ruby_assert_critical_section_entered = 0;
54#endif
55
56 VALUE rb_str_concat_literals(size_t, const VALUE*);
57
58/* :FIXME: This #ifdef is because we build pch in case of mswin and
59 * not in case of other situations. That distinction might change in
60 * a future. We would better make it detectable in something better
61 * than just _MSC_VER. */
62#ifdef _MSC_VER
63RUBY_FUNC_EXPORTED
64#else
65MJIT_FUNC_EXPORTED
66#endif
67 VALUE vm_exec(rb_execution_context_t *, bool);
68
69extern const char *const rb_debug_counter_names[];
70
71PUREFUNC(static inline const VALUE *VM_EP_LEP(const VALUE *));
72static inline const VALUE *
73VM_EP_LEP(const VALUE *ep)
74{
75 while (!VM_ENV_LOCAL_P(ep)) {
76 ep = VM_ENV_PREV_EP(ep);
77 }
78 return ep;
79}
80
81static inline const rb_control_frame_t *
82rb_vm_search_cf_from_ep(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, const VALUE * const ep)
83{
84 if (!ep) {
85 return NULL;
86 }
87 else {
88 const rb_control_frame_t * const eocfp = RUBY_VM_END_CONTROL_FRAME(ec); /* end of control frame pointer */
89
90 while (cfp < eocfp) {
91 if (cfp->ep == ep) {
92 return cfp;
93 }
94 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
95 }
96
97 return NULL;
98 }
99}
100
101const VALUE *
102rb_vm_ep_local_ep(const VALUE *ep)
103{
104 return VM_EP_LEP(ep);
105}
106
107PUREFUNC(static inline const VALUE *VM_CF_LEP(const rb_control_frame_t * const cfp));
108static inline const VALUE *
109VM_CF_LEP(const rb_control_frame_t * const cfp)
110{
111 return VM_EP_LEP(cfp->ep);
112}
113
114static inline const VALUE *
115VM_CF_PREV_EP(const rb_control_frame_t * const cfp)
116{
117 return VM_ENV_PREV_EP(cfp->ep);
118}
119
120PUREFUNC(static inline VALUE VM_CF_BLOCK_HANDLER(const rb_control_frame_t * const cfp));
121static inline VALUE
122VM_CF_BLOCK_HANDLER(const rb_control_frame_t * const cfp)
123{
124 const VALUE *ep = VM_CF_LEP(cfp);
125 return VM_ENV_BLOCK_HANDLER(ep);
126}
127
128int
129rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp)
130{
131 return VM_FRAME_CFRAME_KW_P(cfp);
132}
133
134 VALUE
135rb_vm_frame_block_handler(const rb_control_frame_t *cfp)
136{
137 return VM_CF_BLOCK_HANDLER(cfp);
138}
139
140#if VM_CHECK_MODE > 0
141static int
142VM_CFP_IN_HEAP_P(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
143{
144 const VALUE *start = ec->vm_stack;
145 const VALUE *end = (VALUE *)ec->vm_stack + ec->vm_stack_size;
146 VM_ASSERT(start != NULL);
147
148 if (start <= (VALUE *)cfp && (VALUE *)cfp < end) {
149 return FALSE;
150 }
151 else {
152 return TRUE;
153 }
154}
155
156static int
157VM_EP_IN_HEAP_P(const rb_execution_context_t *ec, const VALUE *ep)
158{
159 const VALUE *start = ec->vm_stack;
160 const VALUE *end = (VALUE *)ec->cfp;
161 VM_ASSERT(start != NULL);
162
163 if (start <= ep && ep < end) {
164 return FALSE;
165 }
166 else {
167 return TRUE;
168 }
169}
170
171static int
172vm_ep_in_heap_p_(const rb_execution_context_t *ec, const VALUE *ep)
173{
174 if (VM_EP_IN_HEAP_P(ec, ep)) {
175 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV]; /* VM_ENV_ENVVAL(ep); */
176
177 if (!UNDEF_P(envval)) {
178 const rb_env_t *env = (const rb_env_t *)envval;
179
180 VM_ASSERT(vm_assert_env(envval));
181 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
182 VM_ASSERT(env->ep == ep);
183 }
184 return TRUE;
185 }
186 else {
187 return FALSE;
188 }
189}
190
191int
192rb_vm_ep_in_heap_p(const VALUE *ep)
193{
194 const rb_execution_context_t *ec = GET_EC();
195 if (ec->vm_stack == NULL) return TRUE;
196 return vm_ep_in_heap_p_(ec, ep);
197}
198#endif
199
200static struct rb_captured_block *
201VM_CFP_TO_CAPTURED_BLOCK(const rb_control_frame_t *cfp)
202{
203 VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), cfp));
204 return (struct rb_captured_block *)&cfp->self;
205}
206
207static rb_control_frame_t *
208VM_CAPTURED_BLOCK_TO_CFP(const struct rb_captured_block *captured)
209{
210 rb_control_frame_t *cfp = ((rb_control_frame_t *)((VALUE *)(captured) - 3));
211 VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), cfp));
212 VM_ASSERT(sizeof(rb_control_frame_t)/sizeof(VALUE) == 8 + VM_DEBUG_BP_CHECK ? 1 : 0);
213 return cfp;
214}
215
216static int
217VM_BH_FROM_CFP_P(VALUE block_handler, const rb_control_frame_t *cfp)
218{
219 const struct rb_captured_block *captured = VM_CFP_TO_CAPTURED_BLOCK(cfp);
220 return VM_TAGGED_PTR_REF(block_handler, 0x03) == captured;
221}
222
223static VALUE
224vm_passed_block_handler(rb_execution_context_t *ec)
225{
226 VALUE block_handler = ec->passed_block_handler;
227 ec->passed_block_handler = VM_BLOCK_HANDLER_NONE;
228 vm_block_handler_verify(block_handler);
229 return block_handler;
230}
231
232static rb_cref_t *
233vm_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)
234{
235 VALUE refinements = Qnil;
236 int omod_shared = FALSE;
237 rb_cref_t *cref;
238
239 /* scope */
240 union {
241 rb_scope_visibility_t visi;
242 VALUE value;
243 } scope_visi;
244
245 scope_visi.visi.method_visi = visi;
246 scope_visi.visi.module_func = module_func;
247
248 /* refinements */
249 if (prev_cref != NULL && prev_cref != (void *)1 /* TODO: why CREF_NEXT(cref) is 1? */) {
250 refinements = CREF_REFINEMENTS(prev_cref);
251
252 if (!NIL_P(refinements)) {
253 omod_shared = TRUE;
254 CREF_OMOD_SHARED_SET(prev_cref);
255 }
256 }
257
258 VM_ASSERT(singleton || klass);
259
260 cref = (rb_cref_t *)rb_imemo_new(imemo_cref, klass, (VALUE)(use_prev_prev ? CREF_NEXT(prev_cref) : prev_cref), scope_visi.value, refinements);
261
262 if (pushed_by_eval) CREF_PUSHED_BY_EVAL_SET(cref);
263 if (omod_shared) CREF_OMOD_SHARED_SET(cref);
264 if (singleton) CREF_SINGLETON_SET(cref);
265
266 return cref;
267}
268
269static rb_cref_t *
270vm_cref_new(VALUE klass, rb_method_visibility_t visi, int module_func, rb_cref_t *prev_cref, int pushed_by_eval, int singleton)
271{
272 return vm_cref_new0(klass, visi, module_func, prev_cref, pushed_by_eval, FALSE, singleton);
273}
274
275static rb_cref_t *
276vm_cref_new_use_prev(VALUE klass, rb_method_visibility_t visi, int module_func, rb_cref_t *prev_cref, int pushed_by_eval)
277{
278 return vm_cref_new0(klass, visi, module_func, prev_cref, pushed_by_eval, TRUE, FALSE);
279}
280
281static int
282ref_delete_symkey(VALUE key, VALUE value, VALUE unused)
283{
284 return SYMBOL_P(key) ? ST_DELETE : ST_CONTINUE;
285}
286
287static rb_cref_t *
288vm_cref_dup(const rb_cref_t *cref)
289{
290 const rb_scope_visibility_t *visi = CREF_SCOPE_VISI(cref);
291 rb_cref_t *next_cref = CREF_NEXT(cref), *new_cref;
292 int pushed_by_eval = CREF_PUSHED_BY_EVAL(cref);
293 int singleton = CREF_SINGLETON(cref);
294
295 new_cref = vm_cref_new(cref->klass_or_self, visi->method_visi, visi->module_func, next_cref, pushed_by_eval, singleton);
296
297 if (!NIL_P(CREF_REFINEMENTS(cref))) {
298 VALUE ref = rb_hash_dup(CREF_REFINEMENTS(cref));
299 rb_hash_foreach(ref, ref_delete_symkey, Qnil);
300 CREF_REFINEMENTS_SET(new_cref, ref);
301 CREF_OMOD_SHARED_UNSET(new_cref);
302 }
303
304 return new_cref;
305}
306
307
308 rb_cref_t *
309rb_vm_cref_dup_without_refinements(const rb_cref_t *cref)
310{
311 const rb_scope_visibility_t *visi = CREF_SCOPE_VISI(cref);
312 rb_cref_t *next_cref = CREF_NEXT(cref), *new_cref;
313 int pushed_by_eval = CREF_PUSHED_BY_EVAL(cref);
314 int singleton = CREF_SINGLETON(cref);
315
316 new_cref = vm_cref_new(cref->klass_or_self, visi->method_visi, visi->module_func, next_cref, pushed_by_eval, singleton);
317
318 if (!NIL_P(CREF_REFINEMENTS(cref))) {
319 CREF_REFINEMENTS_SET(new_cref, Qnil);
320 CREF_OMOD_SHARED_UNSET(new_cref);
321 }
322
323 return new_cref;
324}
325
326static rb_cref_t *
327vm_cref_new_toplevel(rb_execution_context_t *ec)
328{
329 rb_cref_t *cref = vm_cref_new(rb_cObject, METHOD_VISI_PRIVATE /* toplevel visibility is private */, FALSE, NULL, FALSE, FALSE);
330 VALUE top_wrapper = rb_ec_thread_ptr(ec)->top_wrapper;
331
332 if (top_wrapper) {
333 cref = vm_cref_new(top_wrapper, METHOD_VISI_PRIVATE, FALSE, cref, FALSE, FALSE);
334 }
335
336 return cref;
337}
338
339 rb_cref_t *
340rb_vm_cref_new_toplevel(void)
341{
342 return vm_cref_new_toplevel(GET_EC());
343}
344
345static void
346vm_cref_dump(const char *mesg, const rb_cref_t *cref)
347{
348 ruby_debug_printf("vm_cref_dump: %s (%p)\n", mesg, (void *)cref);
349
350 while (cref) {
351 ruby_debug_printf("= cref| klass: %s\n", RSTRING_PTR(rb_class_path(CREF_CLASS(cref))));
352 cref = CREF_NEXT(cref);
353 }
354}
355
356void
357rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep)
358{
359 *((const VALUE **)&dst->as.captured.ep) = ep;
360 RB_OBJ_WRITTEN(obj, Qundef, VM_ENV_ENVVAL(ep));
361}
362
363static void
364vm_bind_update_env(VALUE bindval, rb_binding_t *bind, VALUE envval)
365{
366 const rb_env_t *env = (rb_env_t *)envval;
367 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, env->iseq);
368 rb_vm_block_ep_update(bindval, &bind->block, env->ep);
369}
370
371#if VM_COLLECT_USAGE_DETAILS
372static void vm_collect_usage_operand(int insn, int n, VALUE op);
373static void vm_collect_usage_insn(int insn);
374static void vm_collect_usage_register(int reg, int isset);
375#endif
376
377static VALUE vm_make_env_object(const rb_execution_context_t *ec, rb_control_frame_t *cfp);
378extern VALUE rb_vm_invoke_bmethod(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self,
379 int argc, const VALUE *argv, int kw_splat, VALUE block_handler,
380 const rb_callable_method_entry_t *me);
381static VALUE vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self, int argc, const VALUE *argv, int kw_splat, VALUE block_handler);
382
383#if USE_MJIT || USE_YJIT
384# ifdef MJIT_HEADER
385NOINLINE(static COLDFUNC VALUE mjit_check_iseq(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body));
386# else
387static inline VALUE mjit_check_iseq(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body);
388# endif
389static VALUE
390mjit_check_iseq(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body)
391{
392 uintptr_t mjit_state = (uintptr_t)(body->jit_func);
393 ASSUME(MJIT_FUNC_STATE_P(mjit_state));
394 switch ((enum rb_mjit_func_state)mjit_state) {
395 case MJIT_FUNC_NOT_COMPILED:
396 if (body->total_calls == mjit_opts.call_threshold) {
397 rb_mjit_add_iseq_to_process(iseq);
398 if (UNLIKELY(mjit_opts.wait && !MJIT_FUNC_STATE_P(body->jit_func))) {
399 return body->jit_func(ec, ec->cfp);
400 }
401 }
402 break;
403 case MJIT_FUNC_COMPILING:
404 case MJIT_FUNC_FAILED:
405 break;
406 }
407 return Qundef;
408}
409
410// Try to execute the current iseq in ec. Use JIT code if it is ready.
411// If it is not, add ISEQ to the compilation queue and return Qundef for MJIT.
412// YJIT compiles on the thread running the iseq.
413static inline VALUE
414jit_exec(rb_execution_context_t *ec)
415{
416 // Increment the ISEQ's call counter
417 const rb_iseq_t *iseq = ec->cfp->iseq;
418 struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
419 bool yjit_enabled = rb_yjit_enabled_p();
420 if (yjit_enabled || mjit_call_p) {
421 body->total_calls++;
422 }
423 else {
424 return Qundef;
425 }
426
427 // Trigger JIT compilation as needed
428 jit_func_t func;
429 if (yjit_enabled) {
430 if (body->total_calls == rb_yjit_call_threshold()) {
431 // If we couldn't generate any code for this iseq, then return
432 // Qundef so the interpreter will handle the call.
433 if (!rb_yjit_compile_iseq(iseq, ec)) {
434 return Qundef;
435 }
436 }
437 // YJIT tried compiling this function once before and couldn't do
438 // it, so return Qundef so the interpreter handles it.
439 if ((func = body->jit_func) == 0) {
440 return Qundef;
441 }
442 }
443 else if (UNLIKELY(MJIT_FUNC_STATE_P(func = body->jit_func))) {
444 return mjit_check_iseq(ec, iseq, body);
445 }
446
447 // Call the JIT code
448 return func(ec, ec->cfp); // SystemV x64 calling convention: ec -> RDI, cfp -> RSI
449}
450#endif
451
452#include "vm_insnhelper.c"
453
454#ifndef MJIT_HEADER
455
456#include "vm_exec.c"
457
458#include "vm_method.c"
459#endif /* #ifndef MJIT_HEADER */
460#include "vm_eval.c"
461#ifndef MJIT_HEADER
462
463#define PROCDEBUG 0
464
465 VALUE rb_cRubyVM;
466 VALUE rb_cThread;
467 VALUE rb_mRubyVMFrozenCore;
468 VALUE rb_block_param_proxy;
469
470 VALUE ruby_vm_const_missing_count = 0;
471 rb_vm_t *ruby_current_vm_ptr = NULL;
472 rb_ractor_t *ruby_single_main_ractor;
473bool ruby_vm_keep_script_lines;
474
475#ifdef RB_THREAD_LOCAL_SPECIFIER
476RB_THREAD_LOCAL_SPECIFIER rb_execution_context_t *ruby_current_ec;
477
478#ifdef __APPLE__
479 rb_execution_context_t *
480 rb_current_ec(void)
481 {
482 return ruby_current_ec;
483 }
484 void
485 rb_current_ec_set(rb_execution_context_t *ec)
486 {
487 ruby_current_ec = ec;
488 }
489#endif
490
491#else
492native_tls_key_t ruby_current_ec_key;
493#endif
494
495 rb_event_flag_t ruby_vm_event_flags;
496 rb_event_flag_t ruby_vm_event_enabled_global_flags;
497unsigned int ruby_vm_event_local_num;
498
499rb_serial_t ruby_vm_constant_cache_invalidations = 0;
500rb_serial_t ruby_vm_constant_cache_misses = 0;
501rb_serial_t ruby_vm_global_cvar_state = 1;
502
503static const struct rb_callcache vm_empty_cc = {
504 .flags = T_IMEMO | (imemo_callcache << FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
505 .klass = Qfalse,
506 .cme_ = NULL,
507 .call_ = vm_call_general,
508 .aux_ = {
509 .v = Qfalse,
510 }
511};
512
513static const struct rb_callcache vm_empty_cc_for_super = {
514 .flags = T_IMEMO | (imemo_callcache << FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
515 .klass = Qfalse,
516 .cme_ = NULL,
517 .call_ = vm_call_super_method,
518 .aux_ = {
519 .v = Qfalse,
520 }
521};
522
523static void thread_free(void *ptr);
524
525void
526rb_vm_inc_const_missing_count(void)
527{
528 ruby_vm_const_missing_count +=1;
529}
530
531MJIT_FUNC_EXPORTED int
532rb_dtrace_setup(rb_execution_context_t *ec, VALUE klass, ID id,
533 struct ruby_dtrace_method_hook_args *args)
534{
535 enum ruby_value_type type;
536 if (!klass) {
537 if (!ec) ec = GET_EC();
538 if (!rb_ec_frame_method_id_and_class(ec, &id, 0, &klass) || !klass)
539 return FALSE;
540 }
541 if (RB_TYPE_P(klass, T_ICLASS)) {
542 klass = RBASIC(klass)->klass;
543 }
544 else if (FL_TEST(klass, FL_SINGLETON)) {
545 klass = rb_attr_get(klass, id__attached__);
546 if (NIL_P(klass)) return FALSE;
547 }
548 type = BUILTIN_TYPE(klass);
549 if (type == T_CLASS || type == T_ICLASS || type == T_MODULE) {
550 VALUE name = rb_class_path(klass);
551 const char *classname, *filename;
552 const char *methodname = rb_id2name(id);
553 if (methodname && (filename = rb_source_location_cstr(&args->line_no)) != 0) {
554 if (NIL_P(name) || !(classname = StringValuePtr(name)))
555 classname = "<unknown>";
556 args->classname = classname;
557 args->methodname = methodname;
558 args->filename = filename;
559 args->klass = klass;
560 args->name = name;
561 return TRUE;
562 }
563 }
564 return FALSE;
565}
566
567/*
568 * call-seq:
569 * RubyVM.stat -> Hash
570 * RubyVM.stat(hsh) -> hsh
571 * RubyVM.stat(Symbol) -> Numeric
572 *
573 * Returns a Hash containing implementation-dependent counters inside the VM.
574 *
575 * This hash includes information about method/constant caches:
576 *
577 * {
578 * :constant_cache_invalidations=>2,
579 * :constant_cache_misses=>14,
580 * :global_cvar_state=>27
581 * }
582 *
583 * If <tt>USE_DEBUG_COUNTER</tt> is enabled, debug counters will be included.
584 *
585 * The contents of the hash are implementation specific and may be changed in
586 * the future.
587 *
588 * This method is only expected to work on C Ruby.
589 */
590static VALUE
591vm_stat(int argc, VALUE *argv, VALUE self)
592{
593 static VALUE sym_constant_cache_invalidations, sym_constant_cache_misses, sym_global_cvar_state, sym_next_shape_id;
594 VALUE arg = Qnil;
595 VALUE hash = Qnil, key = Qnil;
596
597 if (rb_check_arity(argc, 0, 1) == 1) {
598 arg = argv[0];
599 if (SYMBOL_P(arg))
600 key = arg;
601 else if (RB_TYPE_P(arg, T_HASH))
602 hash = arg;
603 else
604 rb_raise(rb_eTypeError, "non-hash or symbol given");
605 }
606 else {
607 hash = rb_hash_new();
608 }
609
610#define S(s) sym_##s = ID2SYM(rb_intern_const(#s))
611 S(constant_cache_invalidations);
612 S(constant_cache_misses);
613 S(global_cvar_state);
614 S(next_shape_id);
615#undef S
616
617#define SET(name, attr) \
618 if (key == sym_##name) \
619 return SERIALT2NUM(attr); \
620 else if (hash != Qnil) \
621 rb_hash_aset(hash, sym_##name, SERIALT2NUM(attr));
622
623 SET(constant_cache_invalidations, ruby_vm_constant_cache_invalidations);
624 SET(constant_cache_misses, ruby_vm_constant_cache_misses);
625 SET(global_cvar_state, ruby_vm_global_cvar_state);
626 SET(next_shape_id, (rb_serial_t)GET_VM()->next_shape_id);
627#undef SET
628
629#if USE_DEBUG_COUNTER
630 ruby_debug_counter_show_at_exit(FALSE);
631 for (size_t i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
632 const VALUE name = rb_sym_intern_ascii_cstr(rb_debug_counter_names[i]);
633 const VALUE boxed_value = SIZET2NUM(rb_debug_counter[i]);
634
635 if (key == name) {
636 return boxed_value;
637 }
638 else if (hash != Qnil) {
639 rb_hash_aset(hash, name, boxed_value);
640 }
641 }
642#endif
643
644 if (!NIL_P(key)) { /* matched key should return above */
645 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
646 }
647
648 return hash;
649}
650
651/* control stack frame */
652
653static void
654vm_set_top_stack(rb_execution_context_t *ec, const rb_iseq_t *iseq)
655{
656 if (ISEQ_BODY(iseq)->type != ISEQ_TYPE_TOP) {
657 rb_raise(rb_eTypeError, "Not a toplevel InstructionSequence");
658 }
659
660 /* for return */
661 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH, rb_ec_thread_ptr(ec)->top_self,
662 VM_BLOCK_HANDLER_NONE,
663 (VALUE)vm_cref_new_toplevel(ec), /* cref or me */
664 ISEQ_BODY(iseq)->iseq_encoded, ec->cfp->sp,
665 ISEQ_BODY(iseq)->local_table_size, ISEQ_BODY(iseq)->stack_max);
666}
667
668static void
669vm_set_eval_stack(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_cref_t *cref, const struct rb_block *base_block)
670{
671 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_EVAL | VM_FRAME_FLAG_FINISH,
672 vm_block_self(base_block), VM_GUARDED_PREV_EP(vm_block_ep(base_block)),
673 (VALUE)cref, /* cref or me */
674 ISEQ_BODY(iseq)->iseq_encoded,
675 ec->cfp->sp, ISEQ_BODY(iseq)->local_table_size,
676 ISEQ_BODY(iseq)->stack_max);
677}
678
679static void
680vm_set_main_stack(rb_execution_context_t *ec, const rb_iseq_t *iseq)
681{
682 VALUE toplevel_binding = rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING"));
683 rb_binding_t *bind;
684
685 GetBindingPtr(toplevel_binding, bind);
686 RUBY_ASSERT_MESG(bind, "TOPLEVEL_BINDING is not built");
687
688 vm_set_eval_stack(ec, iseq, 0, &bind->block);
689
690 /* save binding */
691 if (ISEQ_BODY(iseq)->local_table_size > 0) {
692 vm_bind_update_env(toplevel_binding, bind, vm_make_env_object(ec, ec->cfp));
693 }
694}
695
696 rb_control_frame_t *
697rb_vm_get_binding_creatable_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
698{
699 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
700 if (cfp->iseq) {
701 return (rb_control_frame_t *)cfp;
702 }
703 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
704 }
705 return 0;
706}
707
708MJIT_FUNC_EXPORTED rb_control_frame_t *
709rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
710{
711 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
712 if (VM_FRAME_RUBYFRAME_P(cfp)) {
713 return (rb_control_frame_t *)cfp;
714 }
715 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
716 }
717 return 0;
718}
719
720#endif /* #ifndef MJIT_HEADER */
721
722static rb_control_frame_t *
723vm_get_ruby_level_caller_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
724{
725 if (VM_FRAME_RUBYFRAME_P(cfp)) {
726 return (rb_control_frame_t *)cfp;
727 }
728
729 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
730
731 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
732 if (VM_FRAME_RUBYFRAME_P(cfp)) {
733 return (rb_control_frame_t *)cfp;
734 }
735
736 if (VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_PASSED) == FALSE) {
737 break;
738 }
739 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
740 }
741 return 0;
742}
743
744MJIT_STATIC void
745rb_vm_pop_cfunc_frame(void)
746{
747 rb_execution_context_t *ec = GET_EC();
748 rb_control_frame_t *cfp = ec->cfp;
749 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
750
751 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, cfp->self, me->def->original_id, me->called_id, me->owner, Qnil);
752 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
753 vm_pop_frame(ec, cfp, cfp->ep);
754}
755
756#ifndef MJIT_HEADER
757
758void
759rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp)
760{
761 /* check skipped frame */
762 while (ec->cfp != cfp) {
763#if VMDEBUG
764 printf("skipped frame: %s\n", vm_frametype_name(ec->cfp));
765#endif
766 if (VM_FRAME_TYPE(ec->cfp) != VM_FRAME_MAGIC_CFUNC) {
767 rb_vm_pop_frame(ec);
768 }
769 else { /* unlikely path */
770 rb_vm_pop_cfunc_frame();
771 }
772 }
773}
774
775/* at exit */
776
777void
778ruby_vm_at_exit(void (*func)(rb_vm_t *))
779{
780 rb_vm_t *vm = GET_VM();
781 rb_at_exit_list *nl = ALLOC(rb_at_exit_list);
782 nl->func = func;
783 nl->next = vm->at_exit;
784 vm->at_exit = nl;
785}
786
787static void
788ruby_vm_run_at_exit_hooks(rb_vm_t *vm)
789{
790 rb_at_exit_list *l = vm->at_exit;
791
792 while (l) {
793 rb_at_exit_list* t = l->next;
794 rb_vm_at_exit_func *func = l->func;
795 ruby_xfree(l);
796 l = t;
797 (*func)(vm);
798 }
799}
800
801/* Env */
802
803static VALUE check_env_value(const rb_env_t *env);
804
805static int
806check_env(const rb_env_t *env)
807{
808 fputs("---\n", stderr);
809 ruby_debug_printf("envptr: %p\n", (void *)&env->ep[0]);
810 ruby_debug_printf("envval: %10p ", (void *)env->ep[1]);
811 dp(env->ep[1]);
812 ruby_debug_printf("ep: %10p\n", (void *)env->ep);
813 if (rb_vm_env_prev_env(env)) {
814 fputs(">>\n", stderr);
815 check_env_value(rb_vm_env_prev_env(env));
816 fputs("<<\n", stderr);
817 }
818 return 1;
819}
820
821static VALUE
822check_env_value(const rb_env_t *env)
823{
824 if (check_env(env)) {
825 return (VALUE)env;
826 }
827 rb_bug("invalid env");
828 return Qnil; /* unreachable */
829}
830
831static VALUE
832vm_block_handler_escape(const rb_execution_context_t *ec, VALUE block_handler)
833{
834 switch (vm_block_handler_type(block_handler)) {
835 case block_handler_type_ifunc:
836 case block_handler_type_iseq:
837 return rb_vm_make_proc(ec, VM_BH_TO_CAPT_BLOCK(block_handler), rb_cProc);
838
839 case block_handler_type_symbol:
840 case block_handler_type_proc:
841 return block_handler;
842 }
843 VM_UNREACHABLE(vm_block_handler_escape);
844 return Qnil;
845}
846
847static VALUE
848vm_make_env_each(const rb_execution_context_t * const ec, rb_control_frame_t *const cfp)
849{
850 const VALUE * const ep = cfp->ep;
851 const rb_env_t *env;
852 const rb_iseq_t *env_iseq;
853 VALUE *env_body, *env_ep;
854 int local_size, env_size;
855
856 if (VM_ENV_ESCAPED_P(ep)) {
857 return VM_ENV_ENVVAL(ep);
858 }
859
860 if (!VM_ENV_LOCAL_P(ep)) {
861 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
862 if (!VM_ENV_ESCAPED_P(prev_ep)) {
863 rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
864
865 while (prev_cfp->ep != prev_ep) {
866 prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(prev_cfp);
867 VM_ASSERT(prev_cfp->ep != NULL);
868 }
869
870 vm_make_env_each(ec, prev_cfp);
871 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_SPECVAL], VM_GUARDED_PREV_EP(prev_cfp->ep));
872 }
873 }
874 else {
875 VALUE block_handler = VM_ENV_BLOCK_HANDLER(ep);
876
877 if (block_handler != VM_BLOCK_HANDLER_NONE) {
878 VALUE blockprocval = vm_block_handler_escape(ec, block_handler);
879 VM_STACK_ENV_WRITE(ep, VM_ENV_DATA_INDEX_SPECVAL, blockprocval);
880 }
881 }
882
883 if (!VM_FRAME_RUBYFRAME_P(cfp)) {
884 local_size = VM_ENV_DATA_SIZE;
885 }
886 else {
887 local_size = ISEQ_BODY(cfp->iseq)->local_table_size + VM_ENV_DATA_SIZE;
888 }
889
890 /*
891 * # local variables on a stack frame (N == local_size)
892 * [lvar1, lvar2, ..., lvarN, SPECVAL]
893 * ^
894 * ep[0]
895 *
896 * # moved local variables
897 * [lvar1, lvar2, ..., lvarN, SPECVAL, Envval, BlockProcval (if needed)]
898 * ^ ^
899 * env->env[0] ep[0]
900 */
901
902 env_size = local_size +
903 1 /* envval */;
904 env_body = ALLOC_N(VALUE, env_size);
905 MEMCPY(env_body, ep - (local_size - 1 /* specval */), VALUE, local_size);
906
907#if 0
908 for (i = 0; i < local_size; i++) {
909 if (VM_FRAME_RUBYFRAME_P(cfp)) {
910 /* clear value stack for GC */
911 ep[-local_size + i] = 0;
912 }
913 }
914#endif
915
916 env_iseq = VM_FRAME_RUBYFRAME_P(cfp) ? cfp->iseq : NULL;
917 env_ep = &env_body[local_size - 1 /* specval */];
918
919 env = vm_env_new(env_ep, env_body, env_size, env_iseq);
920
921 cfp->ep = env_ep;
922 VM_ENV_FLAGS_SET(env_ep, VM_ENV_FLAG_ESCAPED | VM_ENV_FLAG_WB_REQUIRED);
923 VM_STACK_ENV_WRITE(ep, 0, (VALUE)env); /* GC mark */
924 return (VALUE)env;
925}
926
927static VALUE
928vm_make_env_object(const rb_execution_context_t *ec, rb_control_frame_t *cfp)
929{
930 VALUE envval = vm_make_env_each(ec, cfp);
931
932 if (PROCDEBUG) {
933 check_env_value((const rb_env_t *)envval);
934 }
935
936 return envval;
937}
938
939void
940rb_vm_stack_to_heap(rb_execution_context_t *ec)
941{
942 rb_control_frame_t *cfp = ec->cfp;
943 while ((cfp = rb_vm_get_binding_creatable_next_cfp(ec, cfp)) != 0) {
944 vm_make_env_object(ec, cfp);
945 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
946 }
947}
948
949const rb_env_t *
950rb_vm_env_prev_env(const rb_env_t *env)
951{
952 const VALUE *ep = env->ep;
953
954 if (VM_ENV_LOCAL_P(ep)) {
955 return NULL;
956 }
957 else {
958 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
959 return VM_ENV_ENVVAL_PTR(prev_ep);
960 }
961}
962
963static int
964collect_local_variables_in_iseq(const rb_iseq_t *iseq, const struct local_var_list *vars)
965{
966 unsigned int i;
967 if (!iseq) return 0;
968 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
969 local_var_list_add(vars, ISEQ_BODY(iseq)->local_table[i]);
970 }
971 return 1;
972}
973
974static void
975collect_local_variables_in_env(const rb_env_t *env, const struct local_var_list *vars)
976{
977 do {
978 if (VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_ISOLATED)) break;
979 collect_local_variables_in_iseq(env->iseq, vars);
980 } while ((env = rb_vm_env_prev_env(env)) != NULL);
981}
982
983static int
984vm_collect_local_variables_in_heap(const VALUE *ep, const struct local_var_list *vars)
985{
986 if (VM_ENV_ESCAPED_P(ep)) {
987 collect_local_variables_in_env(VM_ENV_ENVVAL_PTR(ep), vars);
988 return 1;
989 }
990 else {
991 return 0;
992 }
993}
994
995 VALUE
996rb_vm_env_local_variables(const rb_env_t *env)
997{
998 struct local_var_list vars;
999 local_var_list_init(&vars);
1000 collect_local_variables_in_env(env, &vars);
1001 return local_var_list_finish(&vars);
1002}
1003
1004 VALUE
1005rb_iseq_local_variables(const rb_iseq_t *iseq)
1006{
1007 struct local_var_list vars;
1008 local_var_list_init(&vars);
1009 while (collect_local_variables_in_iseq(iseq, &vars)) {
1010 iseq = ISEQ_BODY(iseq)->parent_iseq;
1011 }
1012 return local_var_list_finish(&vars);
1013}
1014
1015/* Proc */
1016
1017static VALUE
1018vm_proc_create_from_captured(VALUE klass,
1019 const struct rb_captured_block *captured,
1020 enum rb_block_type block_type,
1021 int8_t is_from_method, int8_t is_lambda)
1022{
1023 VALUE procval = rb_proc_alloc(klass);
1024 rb_proc_t *proc = RTYPEDDATA_DATA(procval);
1025
1026 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), captured->ep));
1027
1028 /* copy block */
1029 RB_OBJ_WRITE(procval, &proc->block.as.captured.code.val, captured->code.val);
1030 RB_OBJ_WRITE(procval, &proc->block.as.captured.self, captured->self);
1031 rb_vm_block_ep_update(procval, &proc->block, captured->ep);
1032
1033 vm_block_type_set(&proc->block, block_type);
1034 proc->is_from_method = is_from_method;
1035 proc->is_lambda = is_lambda;
1036
1037 return procval;
1038}
1039
1040void
1041rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src)
1042{
1043 /* copy block */
1044 switch (vm_block_type(src)) {
1045 case block_type_iseq:
1046 case block_type_ifunc:
1047 RB_OBJ_WRITE(obj, &dst->as.captured.self, src->as.captured.self);
1048 RB_OBJ_WRITE(obj, &dst->as.captured.code.val, src->as.captured.code.val);
1049 rb_vm_block_ep_update(obj, dst, src->as.captured.ep);
1050 break;
1051 case block_type_symbol:
1052 RB_OBJ_WRITE(obj, &dst->as.symbol, src->as.symbol);
1053 break;
1054 case block_type_proc:
1055 RB_OBJ_WRITE(obj, &dst->as.proc, src->as.proc);
1056 break;
1057 }
1058}
1059
1060static VALUE
1061proc_create(VALUE klass, const struct rb_block *block, int8_t is_from_method, int8_t is_lambda)
1062{
1063 VALUE procval = rb_proc_alloc(klass);
1064 rb_proc_t *proc = RTYPEDDATA_DATA(procval);
1065
1066 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), vm_block_ep(block)));
1067 rb_vm_block_copy(procval, &proc->block, block);
1068 vm_block_type_set(&proc->block, block->type);
1069 proc->is_from_method = is_from_method;
1070 proc->is_lambda = is_lambda;
1071
1072 return procval;
1073}
1074
1075 VALUE
1076rb_proc_dup(VALUE self)
1077{
1078 VALUE procval;
1079 rb_proc_t *src;
1080
1081 GetProcPtr(self, src);
1082 procval = proc_create(rb_obj_class(self), &src->block, src->is_from_method, src->is_lambda);
1083 if (RB_OBJ_SHAREABLE_P(self)) FL_SET_RAW(procval, RUBY_FL_SHAREABLE);
1084 RB_GC_GUARD(self); /* for: body = rb_proc_dup(body) */
1085 return procval;
1086}
1087
1088 struct collect_outer_variable_name_data {
1089 VALUE ary;
1090 VALUE read_only;
1091 bool yield;
1092 bool isolate;
1093};
1094
1095static VALUE
1096ID2NUM(ID id)
1097{
1098 if (SIZEOF_VOIDP > SIZEOF_LONG)
1099 return ULL2NUM(id);
1100 else
1101 return ULONG2NUM(id);
1102}
1103
1104static ID
1105NUM2ID(VALUE num)
1106{
1107 if (SIZEOF_VOIDP > SIZEOF_LONG)
1108 return (ID)NUM2ULL(num);
1109 else
1110 return (ID)NUM2ULONG(num);
1111}
1112
1113static enum rb_id_table_iterator_result
1114collect_outer_variable_names(ID id, VALUE val, void *ptr)
1115{
1116 struct collect_outer_variable_name_data *data = (struct collect_outer_variable_name_data *)ptr;
1117
1118 if (id == rb_intern("yield")) {
1119 data->yield = true;
1120 }
1121 else {
1122 VALUE *store;
1123 if (data->isolate ||
1124 val == Qtrue /* write */) {
1125 store = &data->ary;
1126 }
1127 else {
1128 store = &data->read_only;
1129 }
1130 if (*store == Qfalse) *store = rb_ary_new();
1131 rb_ary_push(*store, ID2NUM(id));
1132 }
1133 return ID_TABLE_CONTINUE;
1134}
1135
1136static const rb_env_t *
1137env_copy(const VALUE *src_ep, VALUE read_only_variables)
1138{
1139 const rb_env_t *src_env = (rb_env_t *)VM_ENV_ENVVAL(src_ep);
1140 VM_ASSERT(src_env->ep == src_ep);
1141
1142 VALUE *env_body = ZALLOC_N(VALUE, src_env->env_size); // fill with Qfalse
1143 VALUE *ep = &env_body[src_env->env_size - 2];
1144 volatile VALUE prev_env = Qnil;
1145
1146 if (read_only_variables) {
1147 for (int i=RARRAY_LENINT(read_only_variables)-1; i>=0; i--) {
1148 ID id = NUM2ID(RARRAY_AREF(read_only_variables, i));
1149
1150 for (unsigned int j=0; j<ISEQ_BODY(src_env->iseq)->local_table_size; j++) {
1151 if (id == ISEQ_BODY(src_env->iseq)->local_table[j]) {
1152 VALUE v = src_env->env[j];
1153 if (!rb_ractor_shareable_p(v)) {
1154 VALUE name = rb_id2str(id);
1155 VALUE msg = rb_sprintf("can not make shareable Proc because it can refer"
1156 " unshareable object %+" PRIsVALUE " from ", v);
1157 if (name)
1158 rb_str_catf(msg, "variable `%" PRIsVALUE "'", name);
1159 else
1160 rb_str_cat_cstr(msg, "a hidden variable");
1161 rb_exc_raise(rb_exc_new_str(rb_eRactorIsolationError, msg));
1162 }
1163 env_body[j] = v;
1164 rb_ary_delete_at(read_only_variables, i);
1165 break;
1166 }
1167 }
1168 }
1169 }
1170
1171 ep[VM_ENV_DATA_INDEX_ME_CREF] = src_ep[VM_ENV_DATA_INDEX_ME_CREF];
1172 ep[VM_ENV_DATA_INDEX_FLAGS] = src_ep[VM_ENV_DATA_INDEX_FLAGS] | VM_ENV_FLAG_ISOLATED;
1173
1174 if (!VM_ENV_LOCAL_P(src_ep)) {
1175 const VALUE *prev_ep = VM_ENV_PREV_EP(src_env->ep);
1176 const rb_env_t *new_prev_env = env_copy(prev_ep, read_only_variables);
1177 prev_env = (VALUE)new_prev_env;
1178 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_GUARDED_PREV_EP(new_prev_env->ep);
1179 }
1180 else {
1181 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
1182 }
1183
1184 const rb_env_t *copied_env = vm_env_new(ep, env_body, src_env->env_size, src_env->iseq);
1185 RB_GC_GUARD(prev_env);
1186 return copied_env;
1187}
1188
1189static void
1190proc_isolate_env(VALUE self, rb_proc_t *proc, VALUE read_only_variables)
1191{
1192 const struct rb_captured_block *captured = &proc->block.as.captured;
1193 const rb_env_t *env = env_copy(captured->ep, read_only_variables);
1194 *((const VALUE **)&proc->block.as.captured.ep) = env->ep;
1195 RB_OBJ_WRITTEN(self, Qundef, env);
1196}
1197
1198static VALUE
1199proc_shared_outer_variables(struct rb_id_table *outer_variables, bool isolate, const char *message)
1200{
1201 struct collect_outer_variable_name_data data = {
1202 .isolate = isolate,
1203 .ary = Qfalse,
1204 .read_only = Qfalse,
1205 .yield = false,
1206 };
1207 rb_id_table_foreach(outer_variables, collect_outer_variable_names, (void *)&data);
1208
1209 if (data.ary != Qfalse) {
1210 VALUE str = rb_sprintf("can not %s because it accesses outer variables", message);
1211 VALUE ary = data.ary;
1212 const char *sep = " (";
1213 for (long i = 0; i < RARRAY_LEN(ary); i++) {
1214 VALUE name = rb_id2str(NUM2ID(RARRAY_AREF(ary, i)));
1215 if (!name) continue;
1216 rb_str_cat_cstr(str, sep);
1217 sep = ", ";
1218 rb_str_append(str, name);
1219 }
1220 if (*sep == ',') rb_str_cat_cstr(str, ")");
1221 rb_str_cat_cstr(str, data.yield ? " and uses `yield'." : ".");
1222 rb_exc_raise(rb_exc_new_str(rb_eArgError, str));
1223 }
1224 else if (data.yield) {
1225 rb_raise(rb_eArgError, "can not %s because it uses `yield'.", message);
1226 }
1227
1228 return data.read_only;
1229}
1230
1231 VALUE
1232rb_proc_isolate_bang(VALUE self)
1233{
1234 const rb_iseq_t *iseq = vm_proc_iseq(self);
1235
1236 if (iseq) {
1237 rb_proc_t *proc = (rb_proc_t *)RTYPEDDATA_DATA(self);
1238 if (proc->block.type != block_type_iseq) rb_raise(rb_eRuntimeError, "not supported yet");
1239
1240 if (ISEQ_BODY(iseq)->outer_variables) {
1241 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables, true, "isolate a Proc");
1242 }
1243
1244 proc_isolate_env(self, proc, Qfalse);
1245 proc->is_isolated = TRUE;
1246 }
1247
1248 FL_SET_RAW(self, RUBY_FL_SHAREABLE);
1249 return self;
1250}
1251
1252 VALUE
1253rb_proc_isolate(VALUE self)
1254{
1255 VALUE dst = rb_proc_dup(self);
1256 rb_proc_isolate_bang(dst);
1257 return dst;
1258}
1259
1260 VALUE
1261rb_proc_ractor_make_shareable(VALUE self)
1262{
1263 const rb_iseq_t *iseq = vm_proc_iseq(self);
1264
1265 if (iseq) {
1266 rb_proc_t *proc = (rb_proc_t *)RTYPEDDATA_DATA(self);
1267 if (proc->block.type != block_type_iseq) rb_raise(rb_eRuntimeError, "not supported yet");
1268
1269 if (!rb_ractor_shareable_p(vm_block_self(&proc->block))) {
1270 rb_raise(rb_eRactorIsolationError,
1271 "Proc's self is not shareable: %" PRIsVALUE,
1272 self);
1273 }
1274
1275 VALUE read_only_variables = Qfalse;
1276
1277 if (ISEQ_BODY(iseq)->outer_variables) {
1278 read_only_variables =
1279 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables, false, "make a Proc shareable");
1280 }
1281
1282 proc_isolate_env(self, proc, read_only_variables);
1283 proc->is_isolated = TRUE;
1284 }
1285
1286 FL_SET_RAW(self, RUBY_FL_SHAREABLE);
1287 return self;
1288}
1289
1290MJIT_FUNC_EXPORTED VALUE
1291rb_vm_make_proc_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda)
1292{
1293 VALUE procval;
1294
1295 if (!VM_ENV_ESCAPED_P(captured->ep)) {
1296 rb_control_frame_t *cfp = VM_CAPTURED_BLOCK_TO_CFP(captured);
1297 vm_make_env_object(ec, cfp);
1298 }
1299 VM_ASSERT(VM_EP_IN_HEAP_P(ec, captured->ep));
1300 VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq) ||
1301 imemo_type_p(captured->code.val, imemo_ifunc));
1302
1303 procval = vm_proc_create_from_captured(klass, captured,
1304 imemo_type(captured->code.val) == imemo_iseq ? block_type_iseq : block_type_ifunc, FALSE, is_lambda);
1305 return procval;
1306}
1307
1308/* Binding */
1309
1310 VALUE
1311rb_vm_make_binding(const rb_execution_context_t *ec, const rb_control_frame_t *src_cfp)
1312{
1313 rb_control_frame_t *cfp = rb_vm_get_binding_creatable_next_cfp(ec, src_cfp);
1314 rb_control_frame_t *ruby_level_cfp = rb_vm_get_ruby_level_next_cfp(ec, src_cfp);
1315 VALUE bindval, envval;
1316 rb_binding_t *bind;
1317
1318 if (cfp == 0 || ruby_level_cfp == 0) {
1319 rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
1320 }
1321 if (!VM_FRAME_RUBYFRAME_P(src_cfp) &&
1322 !VM_FRAME_RUBYFRAME_P(RUBY_VM_PREVIOUS_CONTROL_FRAME(src_cfp))) {
1323 rb_raise(rb_eRuntimeError, "Cannot create Binding object for non-Ruby caller");
1324 }
1325
1326 envval = vm_make_env_object(ec, cfp);
1327 bindval = rb_binding_alloc(rb_cBinding);
1328 GetBindingPtr(bindval, bind);
1329 vm_bind_update_env(bindval, bind, envval);
1330 RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, cfp->self);
1331 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, cfp->iseq);
1332 RB_OBJ_WRITE(bindval, &bind->pathobj, ISEQ_BODY(ruby_level_cfp->iseq)->location.pathobj);
1333 bind->first_lineno = rb_vm_get_sourceline(ruby_level_cfp);
1334
1335 return bindval;
1336}
1337
1338const VALUE *
1339rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars)
1340{
1341 VALUE envval, pathobj = bind->pathobj;
1342 VALUE path = pathobj_path(pathobj);
1343 VALUE realpath = pathobj_realpath(pathobj);
1344 const struct rb_block *base_block;
1345 const rb_env_t *env;
1346 rb_execution_context_t *ec = GET_EC();
1347 const rb_iseq_t *base_iseq, *iseq;
1348 rb_ast_body_t ast;
1349 NODE tmp_node;
1350
1351 if (dyncount < 0) return 0;
1352
1353 base_block = &bind->block;
1354 base_iseq = vm_block_iseq(base_block);
1355
1356 VALUE idtmp = 0;
1357 rb_ast_id_table_t *dyns = ALLOCV(idtmp, sizeof(rb_ast_id_table_t) + dyncount * sizeof(ID));
1358 dyns->size = dyncount;
1359 MEMCPY(dyns->ids, dynvars, ID, dyncount);
1360
1361 rb_node_init(&tmp_node, NODE_SCOPE, (VALUE)dyns, 0, 0);
1362 ast.root = &tmp_node;
1363 ast.compile_option = 0;
1364 ast.script_lines = INT2FIX(-1);
1365
1366 if (base_iseq) {
1367 iseq = rb_iseq_new(&ast, ISEQ_BODY(base_iseq)->location.label, path, realpath, base_iseq, ISEQ_TYPE_EVAL);
1368 }
1369 else {
1370 VALUE tempstr = rb_fstring_lit("<temp>");
1371 iseq = rb_iseq_new_top(&ast, tempstr, tempstr, tempstr, NULL);
1372 }
1373 tmp_node.nd_tbl = 0; /* reset table */
1374 ALLOCV_END(idtmp);
1375
1376 vm_set_eval_stack(ec, iseq, 0, base_block);
1377 vm_bind_update_env(bindval, bind, envval = vm_make_env_object(ec, ec->cfp));
1378 rb_vm_pop_frame(ec);
1379
1380 env = (const rb_env_t *)envval;
1381 return env->env;
1382}
1383
1384/* C -> Ruby: block */
1385
1386static inline VALUE
1387invoke_block(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE self, const struct rb_captured_block *captured, const rb_cref_t *cref, VALUE type, int opt_pc)
1388{
1389 int arg_size = ISEQ_BODY(iseq)->param.size;
1390
1391 vm_push_frame(ec, iseq, type | VM_FRAME_FLAG_FINISH, self,
1392 VM_GUARDED_PREV_EP(captured->ep),
1393 (VALUE)cref, /* cref or method */
1394 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1395 ec->cfp->sp + arg_size,
1396 ISEQ_BODY(iseq)->local_table_size - arg_size,
1397 ISEQ_BODY(iseq)->stack_max);
1398 return vm_exec(ec, true);
1399}
1400
1401static VALUE
1402invoke_bmethod(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE self, const struct rb_captured_block *captured, const rb_callable_method_entry_t *me, VALUE type, int opt_pc)
1403{
1404 /* bmethod */
1405 int arg_size = ISEQ_BODY(iseq)->param.size;
1406 VALUE ret;
1407
1408 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
1409
1410 vm_push_frame(ec, iseq, type | VM_FRAME_FLAG_BMETHOD, self,
1411 VM_GUARDED_PREV_EP(captured->ep),
1412 (VALUE)me,
1413 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1414 ec->cfp->sp + arg_size,
1415 ISEQ_BODY(iseq)->local_table_size - arg_size,
1416 ISEQ_BODY(iseq)->stack_max);
1417
1418 VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);
1419 ret = vm_exec(ec, true);
1420
1421 return ret;
1422}
1423
1424ALWAYS_INLINE(static VALUE
1425 invoke_iseq_block_from_c(rb_execution_context_t *ec, const struct rb_captured_block *captured,
1426 VALUE self, int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler,
1427 const rb_cref_t *cref, int is_lambda, const rb_callable_method_entry_t *me));
1428
1429static inline VALUE
1430invoke_iseq_block_from_c(rb_execution_context_t *ec, const struct rb_captured_block *captured,
1431 VALUE self, int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler,
1432 const rb_cref_t *cref, int is_lambda, const rb_callable_method_entry_t *me)
1433{
1434 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
1435 int i, opt_pc;
1436 VALUE type = VM_FRAME_MAGIC_BLOCK | (is_lambda ? VM_FRAME_FLAG_LAMBDA : 0);
1437 rb_control_frame_t *cfp = ec->cfp;
1438 VALUE *sp = cfp->sp;
1439
1440 stack_check(ec);
1441
1442 CHECK_VM_STACK_OVERFLOW(cfp, argc);
1443 vm_check_canary(ec, sp);
1444 cfp->sp = sp + argc;
1445 for (i=0; i<argc; i++) {
1446 sp[i] = argv[i];
1447 }
1448
1449 opt_pc = vm_yield_setup_args(ec, iseq, argc, sp, kw_splat, passed_block_handler,
1450 (is_lambda ? arg_setup_method : arg_setup_block));
1451 cfp->sp = sp;
1452
1453 if (me == NULL) {
1454 return invoke_block(ec, iseq, self, captured, cref, type, opt_pc);
1455 }
1456 else {
1457 return invoke_bmethod(ec, iseq, self, captured, me, type, opt_pc);
1458 }
1459}
1460
1461static inline VALUE
1462invoke_block_from_c_bh(rb_execution_context_t *ec, VALUE block_handler,
1463 int argc, const VALUE *argv,
1464 int kw_splat, VALUE passed_block_handler, const rb_cref_t *cref,
1465 int is_lambda, int force_blockarg)
1466{
1467 again:
1468 switch (vm_block_handler_type(block_handler)) {
1469 case block_handler_type_iseq:
1470 {
1471 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
1472 return invoke_iseq_block_from_c(ec, captured, captured->self,
1473 argc, argv, kw_splat, passed_block_handler,
1474 cref, is_lambda, NULL);
1475 }
1476 case block_handler_type_ifunc:
1477 return vm_yield_with_cfunc(ec, VM_BH_TO_IFUNC_BLOCK(block_handler),
1478 VM_BH_TO_IFUNC_BLOCK(block_handler)->self,
1479 argc, argv, kw_splat, passed_block_handler, NULL);
1480 case block_handler_type_symbol:
1481 return vm_yield_with_symbol(ec, VM_BH_TO_SYMBOL(block_handler),
1482 argc, argv, kw_splat, passed_block_handler);
1483 case block_handler_type_proc:
1484 if (force_blockarg == FALSE) {
1485 is_lambda = block_proc_is_lambda(VM_BH_TO_PROC(block_handler));
1486 }
1487 block_handler = vm_proc_to_block_handler(VM_BH_TO_PROC(block_handler));
1488 goto again;
1489 }
1490 VM_UNREACHABLE(invoke_block_from_c_splattable);
1491 return Qundef;
1492}
1493
1494static inline VALUE
1495check_block_handler(rb_execution_context_t *ec)
1496{
1497 VALUE block_handler = VM_CF_BLOCK_HANDLER(ec->cfp);
1498 vm_block_handler_verify(block_handler);
1499 if (UNLIKELY(block_handler == VM_BLOCK_HANDLER_NONE)) {
1500 rb_vm_localjump_error("no block given", Qnil, 0);
1501 }
1502
1503 return block_handler;
1504}
1505
1506static VALUE
1507vm_yield_with_cref(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat, const rb_cref_t *cref, int is_lambda)
1508{
1509 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1510 argc, argv, kw_splat, VM_BLOCK_HANDLER_NONE,
1511 cref, is_lambda, FALSE);
1512}
1513
1514static VALUE
1515vm_yield(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat)
1516{
1517 return vm_yield_with_cref(ec, argc, argv, kw_splat, NULL, FALSE);
1518}
1519
1520static VALUE
1521vm_yield_with_block(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE block_handler, int kw_splat)
1522{
1523 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1524 argc, argv, kw_splat, block_handler,
1525 NULL, FALSE, FALSE);
1526}
1527
1528static VALUE
1529vm_yield_force_blockarg(rb_execution_context_t *ec, VALUE args)
1530{
1531 return invoke_block_from_c_bh(ec, check_block_handler(ec), 1, &args,
1532 RB_NO_KEYWORDS, VM_BLOCK_HANDLER_NONE, NULL, FALSE, TRUE);
1533}
1534
1535ALWAYS_INLINE(static VALUE
1536 invoke_block_from_c_proc(rb_execution_context_t *ec, const rb_proc_t *proc,
1537 VALUE self, int argc, const VALUE *argv,
1538 int kw_splat, VALUE passed_block_handler, int is_lambda,
1539 const rb_callable_method_entry_t *me));
1540
1541static inline VALUE
1542invoke_block_from_c_proc(rb_execution_context_t *ec, const rb_proc_t *proc,
1543 VALUE self, int argc, const VALUE *argv,
1544 int kw_splat, VALUE passed_block_handler, int is_lambda,
1545 const rb_callable_method_entry_t *me)
1546{
1547 const struct rb_block *block = &proc->block;
1548
1549 again:
1550 switch (vm_block_type(block)) {
1551 case block_type_iseq:
1552 return invoke_iseq_block_from_c(ec, &block->as.captured, self, argc, argv, kw_splat, passed_block_handler, NULL, is_lambda, me);
1553 case block_type_ifunc:
1554 if (kw_splat == 1) {
1555 VALUE keyword_hash = argv[argc-1];
1556 if (!RB_TYPE_P(keyword_hash, T_HASH)) {
1557 keyword_hash = rb_to_hash_type(keyword_hash);
1558 }
1559 if (RHASH_EMPTY_P(keyword_hash)) {
1560 argc--;
1561 }
1562 else {
1563 ((VALUE *)argv)[argc-1] = rb_hash_dup(keyword_hash);
1564 }
1565 }
1566 return vm_yield_with_cfunc(ec, &block->as.captured, self, argc, argv, kw_splat, passed_block_handler, me);
1567 case block_type_symbol:
1568 return vm_yield_with_symbol(ec, block->as.symbol, argc, argv, kw_splat, passed_block_handler);
1569 case block_type_proc:
1570 is_lambda = block_proc_is_lambda(block->as.proc);
1571 block = vm_proc_block(block->as.proc);
1572 goto again;
1573 }
1574 VM_UNREACHABLE(invoke_block_from_c_proc);
1575 return Qundef;
1576}
1577
1578static VALUE
1579vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self,
1580 int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler)
1581{
1582 return invoke_block_from_c_proc(ec, proc, self, argc, argv, kw_splat, passed_block_handler, proc->is_lambda, NULL);
1583}
1584
1585MJIT_FUNC_EXPORTED VALUE
1586rb_vm_invoke_bmethod(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self,
1587 int argc, const VALUE *argv, int kw_splat, VALUE block_handler, const rb_callable_method_entry_t *me)
1588{
1589 return invoke_block_from_c_proc(ec, proc, self, argc, argv, kw_splat, block_handler, TRUE, me);
1590}
1591
1592MJIT_FUNC_EXPORTED VALUE
1593rb_vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc,
1594 int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler)
1595{
1596 VALUE self = vm_block_self(&proc->block);
1597 vm_block_handler_verify(passed_block_handler);
1598
1599 if (proc->is_from_method) {
1600 return rb_vm_invoke_bmethod(ec, proc, self, argc, argv, kw_splat, passed_block_handler, NULL);
1601 }
1602 else {
1603 return vm_invoke_proc(ec, proc, self, argc, argv, kw_splat, passed_block_handler);
1604 }
1605}
1606
1607 VALUE
1608rb_vm_invoke_proc_with_self(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self,
1609 int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler)
1610{
1611 vm_block_handler_verify(passed_block_handler);
1612
1613 if (proc->is_from_method) {
1614 return rb_vm_invoke_bmethod(ec, proc, self, argc, argv, kw_splat, passed_block_handler, NULL);
1615 }
1616 else {
1617 return vm_invoke_proc(ec, proc, self, argc, argv, kw_splat, passed_block_handler);
1618 }
1619}
1620
1621/* special variable */
1622
1623static rb_control_frame_t *
1624vm_normal_frame(const rb_execution_context_t *ec, rb_control_frame_t *cfp)
1625{
1626 while (cfp->pc == 0) {
1627 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1628 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
1629 return 0;
1630 }
1631 }
1632 return cfp;
1633}
1634
1635static VALUE
1636vm_cfp_svar_get(const rb_execution_context_t *ec, rb_control_frame_t *cfp, VALUE key)
1637{
1638 cfp = vm_normal_frame(ec, cfp);
1639 return lep_svar_get(ec, cfp ? VM_CF_LEP(cfp) : 0, key);
1640}
1641
1642static void
1643vm_cfp_svar_set(const rb_execution_context_t *ec, rb_control_frame_t *cfp, VALUE key, const VALUE val)
1644{
1645 cfp = vm_normal_frame(ec, cfp);
1646 lep_svar_set(ec, cfp ? VM_CF_LEP(cfp) : 0, key, val);
1647}
1648
1649static VALUE
1650vm_svar_get(const rb_execution_context_t *ec, VALUE key)
1651{
1652 return vm_cfp_svar_get(ec, ec->cfp, key);
1653}
1654
1655static void
1656vm_svar_set(const rb_execution_context_t *ec, VALUE key, VALUE val)
1657{
1658 vm_cfp_svar_set(ec, ec->cfp, key, val);
1659}
1660
1661 VALUE
1662rb_backref_get(void)
1663{
1664 return vm_svar_get(GET_EC(), VM_SVAR_BACKREF);
1665}
1666
1667void
1668rb_backref_set(VALUE val)
1669{
1670 vm_svar_set(GET_EC(), VM_SVAR_BACKREF, val);
1671}
1672
1673 VALUE
1674rb_lastline_get(void)
1675{
1676 return vm_svar_get(GET_EC(), VM_SVAR_LASTLINE);
1677}
1678
1679void
1680rb_lastline_set(VALUE val)
1681{
1682 vm_svar_set(GET_EC(), VM_SVAR_LASTLINE, val);
1683}
1684
1685/* misc */
1686
1687const char *
1688rb_sourcefile(void)
1689{
1690 const rb_execution_context_t *ec = GET_EC();
1691 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1692
1693 if (cfp) {
1694 return RSTRING_PTR(rb_iseq_path(cfp->iseq));
1695 }
1696 else {
1697 return 0;
1698 }
1699}
1700
1701int
1702rb_sourceline(void)
1703{
1704 const rb_execution_context_t *ec = GET_EC();
1705 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1706
1707 if (cfp) {
1708 return rb_vm_get_sourceline(cfp);
1709 }
1710 else {
1711 return 0;
1712 }
1713}
1714
1715 VALUE
1716rb_source_location(int *pline)
1717{
1718 const rb_execution_context_t *ec = GET_EC();
1719 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1720
1721 if (cfp && VM_FRAME_RUBYFRAME_P(cfp)) {
1722 if (pline) *pline = rb_vm_get_sourceline(cfp);
1723 return rb_iseq_path(cfp->iseq);
1724 }
1725 else {
1726 if (pline) *pline = 0;
1727 return Qnil;
1728 }
1729}
1730
1731MJIT_FUNC_EXPORTED const char *
1732rb_source_location_cstr(int *pline)
1733{
1734 VALUE path = rb_source_location(pline);
1735 if (NIL_P(path)) return NULL;
1736 return RSTRING_PTR(path);
1737}
1738
1739 rb_cref_t *
1740rb_vm_cref(void)
1741{
1742 const rb_execution_context_t *ec = GET_EC();
1743 return vm_ec_cref(ec);
1744}
1745
1746 rb_cref_t *
1747rb_vm_cref_replace_with_duplicated_cref(void)
1748{
1749 const rb_execution_context_t *ec = GET_EC();
1750 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1751 rb_cref_t *cref = vm_cref_replace_with_duplicated_cref(cfp->ep);
1752 ASSUME(cref);
1753 return cref;
1754}
1755
1756const rb_cref_t *
1757rb_vm_cref_in_context(VALUE self, VALUE cbase)
1758{
1759 const rb_execution_context_t *ec = GET_EC();
1760 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1761 const rb_cref_t *cref;
1762 if (!cfp || cfp->self != self) return NULL;
1763 if (!vm_env_cref_by_cref(cfp->ep)) return NULL;
1764 cref = vm_get_cref(cfp->ep);
1765 if (CREF_CLASS(cref) != cbase) return NULL;
1766 return cref;
1767}
1768
1769#if 0
1770void
1771debug_cref(rb_cref_t *cref)
1772{
1773 while (cref) {
1774 dp(CREF_CLASS(cref));
1775 printf("%ld\n", CREF_VISI(cref));
1776 cref = CREF_NEXT(cref);
1777 }
1778}
1779#endif
1780
1781 VALUE
1782rb_vm_cbase(void)
1783{
1784 const rb_execution_context_t *ec = GET_EC();
1785 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1786
1787 if (cfp == 0) {
1788 rb_raise(rb_eRuntimeError, "Can't call on top of Fiber or Thread");
1789 }
1790 return vm_get_cbase(cfp->ep);
1791}
1792
1793/* jump */
1794
1795static VALUE
1796make_localjump_error(const char *mesg, VALUE value, int reason)
1797{
1798 extern VALUE rb_eLocalJumpError;
1799 VALUE exc = rb_exc_new2(rb_eLocalJumpError, mesg);
1800 ID id;
1801
1802 switch (reason) {
1803 case TAG_BREAK:
1804 CONST_ID(id, "break");
1805 break;
1806 case TAG_REDO:
1807 CONST_ID(id, "redo");
1808 break;
1809 case TAG_RETRY:
1810 CONST_ID(id, "retry");
1811 break;
1812 case TAG_NEXT:
1813 CONST_ID(id, "next");
1814 break;
1815 case TAG_RETURN:
1816 CONST_ID(id, "return");
1817 break;
1818 default:
1819 CONST_ID(id, "noreason");
1820 break;
1821 }
1822 rb_iv_set(exc, "@exit_value", value);
1823 rb_iv_set(exc, "@reason", ID2SYM(id));
1824 return exc;
1825}
1826
1827MJIT_FUNC_EXPORTED void
1828rb_vm_localjump_error(const char *mesg, VALUE value, int reason)
1829{
1830 VALUE exc = make_localjump_error(mesg, value, reason);
1831 rb_exc_raise(exc);
1832}
1833
1834 VALUE
1835rb_vm_make_jump_tag_but_local_jump(int state, VALUE val)
1836{
1837 const char *mesg;
1838
1839 switch (state) {
1840 case TAG_RETURN:
1841 mesg = "unexpected return";
1842 break;
1843 case TAG_BREAK:
1844 mesg = "unexpected break";
1845 break;
1846 case TAG_NEXT:
1847 mesg = "unexpected next";
1848 break;
1849 case TAG_REDO:
1850 mesg = "unexpected redo";
1851 val = Qnil;
1852 break;
1853 case TAG_RETRY:
1854 mesg = "retry outside of rescue clause";
1855 val = Qnil;
1856 break;
1857 default:
1858 return Qnil;
1859 }
1860 if (UNDEF_P(val)) {
1861 val = GET_EC()->tag->retval;
1862 }
1863 return make_localjump_error(mesg, val, state);
1864}
1865
1866void
1867rb_vm_jump_tag_but_local_jump(int state)
1868{
1869 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state, Qundef);
1870 if (!NIL_P(exc)) rb_exc_raise(exc);
1871 EC_JUMP_TAG(GET_EC(), state);
1872}
1873
1874static rb_control_frame_t *
1875next_not_local_frame(rb_control_frame_t *cfp)
1876{
1877 while (VM_ENV_LOCAL_P(cfp->ep)) {
1878 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1879 }
1880 return cfp;
1881}
1882
1883NORETURN(static void vm_iter_break(rb_execution_context_t *ec, VALUE val));
1884
1885static void
1886vm_iter_break(rb_execution_context_t *ec, VALUE val)
1887{
1888 rb_control_frame_t *cfp = next_not_local_frame(ec->cfp);
1889 const VALUE *ep = VM_CF_PREV_EP(cfp);
1890 const rb_control_frame_t *target_cfp = rb_vm_search_cf_from_ep(ec, cfp, ep);
1891
1892 if (!target_cfp) {
1893 rb_vm_localjump_error("unexpected break", val, TAG_BREAK);
1894 }
1895
1896 ec->errinfo = (VALUE)THROW_DATA_NEW(val, target_cfp, TAG_BREAK);
1897 EC_JUMP_TAG(ec, TAG_BREAK);
1898}
1899
1900void
1901rb_iter_break(void)
1902{
1903 vm_iter_break(GET_EC(), Qnil);
1904}
1905
1906void
1907rb_iter_break_value(VALUE val)
1908{
1909 vm_iter_break(GET_EC(), val);
1910}
1911
1912/* optimization: redefine management */
1913
1914short ruby_vm_redefined_flag[BOP_LAST_];
1915static st_table *vm_opt_method_def_table = 0;
1916static st_table *vm_opt_mid_table = 0;
1917
1918static int
1919vm_redefinition_check_flag(VALUE klass)
1920{
1921 if (klass == rb_cInteger) return INTEGER_REDEFINED_OP_FLAG;
1922 if (klass == rb_cFloat) return FLOAT_REDEFINED_OP_FLAG;
1923 if (klass == rb_cString) return STRING_REDEFINED_OP_FLAG;
1924 if (klass == rb_cArray) return ARRAY_REDEFINED_OP_FLAG;
1925 if (klass == rb_cHash) return HASH_REDEFINED_OP_FLAG;
1926 if (klass == rb_cSymbol) return SYMBOL_REDEFINED_OP_FLAG;
1927#if 0
1928 if (klass == rb_cTime) return TIME_REDEFINED_OP_FLAG;
1929#endif
1930 if (klass == rb_cRegexp) return REGEXP_REDEFINED_OP_FLAG;
1931 if (klass == rb_cNilClass) return NIL_REDEFINED_OP_FLAG;
1932 if (klass == rb_cTrueClass) return TRUE_REDEFINED_OP_FLAG;
1933 if (klass == rb_cFalseClass) return FALSE_REDEFINED_OP_FLAG;
1934 if (klass == rb_cProc) return PROC_REDEFINED_OP_FLAG;
1935 return 0;
1936}
1937
1938int
1939rb_vm_check_optimizable_mid(VALUE mid)
1940{
1941 if (!vm_opt_mid_table) {
1942 return FALSE;
1943 }
1944
1945 return st_lookup(vm_opt_mid_table, mid, NULL);
1946}
1947
1948static int
1949vm_redefinition_check_method_type(const rb_method_entry_t *me)
1950{
1951 if (me->called_id != me->def->original_id) {
1952 return FALSE;
1953 }
1954
1955 const rb_method_definition_t *def = me->def;
1956 switch (def->type) {
1957 case VM_METHOD_TYPE_CFUNC:
1958 case VM_METHOD_TYPE_OPTIMIZED:
1959 return TRUE;
1960 default:
1961 return FALSE;
1962 }
1963}
1964
1965static void
1966rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me, VALUE klass)
1967{
1968 st_data_t bop;
1969 if (RB_TYPE_P(klass, T_ICLASS) && FL_TEST(klass, RICLASS_IS_ORIGIN) &&
1970 RB_TYPE_P(RBASIC_CLASS(klass), T_CLASS)) {
1971 klass = RBASIC_CLASS(klass);
1972 }
1973 if (vm_redefinition_check_method_type(me)) {
1974 if (st_lookup(vm_opt_method_def_table, (st_data_t)me->def, &bop)) {
1975 int flag = vm_redefinition_check_flag(klass);
1976 if (flag != 0) {
1977 rb_yjit_bop_redefined(flag, (enum ruby_basic_operators)bop);
1978 rb_mjit_bop_redefined(flag, (enum ruby_basic_operators)bop);
1979 ruby_vm_redefined_flag[bop] |= flag;
1980 }
1981 }
1982 }
1983}
1984
1985static enum rb_id_table_iterator_result
1986check_redefined_method(ID mid, VALUE value, void *data)
1987{
1988 VALUE klass = (VALUE)data;
1989 const rb_method_entry_t *me = (rb_method_entry_t *)value;
1990 const rb_method_entry_t *newme = rb_method_entry(klass, mid);
1991
1992 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
1993
1994 return ID_TABLE_CONTINUE;
1995}
1996
1997void
1998rb_vm_check_redefinition_by_prepend(VALUE klass)
1999{
2000 if (!vm_redefinition_check_flag(klass)) return;
2001 rb_id_table_foreach(RCLASS_M_TBL(RCLASS_ORIGIN(klass)), check_redefined_method, (void *)klass);
2002}
2003
2004static void
2005add_opt_method(VALUE klass, ID mid, VALUE bop)
2006{
2007 const rb_method_entry_t *me = rb_method_entry_at(klass, mid);
2008
2009 if (me && vm_redefinition_check_method_type(me)) {
2010 st_insert(vm_opt_method_def_table, (st_data_t)me->def, (st_data_t)bop);
2011 st_insert(vm_opt_mid_table, (st_data_t)mid, (st_data_t)Qtrue);
2012 }
2013 else {
2014 rb_bug("undefined optimized method: %s", rb_id2name(mid));
2015 }
2016}
2017
2018static void
2019vm_init_redefined_flag(void)
2020{
2021 ID mid;
2022 VALUE bop;
2023
2024 vm_opt_method_def_table = st_init_numtable();
2025 vm_opt_mid_table = st_init_numtable();
2026
2027#define OP(mid_, bop_) (mid = id##mid_, bop = BOP_##bop_, ruby_vm_redefined_flag[bop] = 0)
2028#define C(k) add_opt_method(rb_c##k, mid, bop)
2029 OP(PLUS, PLUS), (C(Integer), C(Float), C(String), C(Array));
2030 OP(MINUS, MINUS), (C(Integer), C(Float));
2031 OP(MULT, MULT), (C(Integer), C(Float));
2032 OP(DIV, DIV), (C(Integer), C(Float));
2033 OP(MOD, MOD), (C(Integer), C(Float));
2034 OP(Eq, EQ), (C(Integer), C(Float), C(String), C(Symbol));
2035 OP(Eqq, EQQ), (C(Integer), C(Float), C(Symbol), C(String),
2036 C(NilClass), C(TrueClass), C(FalseClass));
2037 OP(LT, LT), (C(Integer), C(Float));
2038 OP(LE, LE), (C(Integer), C(Float));
2039 OP(GT, GT), (C(Integer), C(Float));
2040 OP(GE, GE), (C(Integer), C(Float));
2041 OP(LTLT, LTLT), (C(String), C(Array));
2042 OP(AREF, AREF), (C(Array), C(Hash), C(Integer));
2043 OP(ASET, ASET), (C(Array), C(Hash));
2044 OP(Length, LENGTH), (C(Array), C(String), C(Hash));
2045 OP(Size, SIZE), (C(Array), C(String), C(Hash));
2046 OP(EmptyP, EMPTY_P), (C(Array), C(String), C(Hash));
2047 OP(Succ, SUCC), (C(Integer), C(String));
2048 OP(EqTilde, MATCH), (C(Regexp), C(String));
2049 OP(Freeze, FREEZE), (C(String));
2050 OP(UMinus, UMINUS), (C(String));
2051 OP(Max, MAX), (C(Array));
2052 OP(Min, MIN), (C(Array));
2053 OP(Call, CALL), (C(Proc));
2054 OP(And, AND), (C(Integer));
2055 OP(Or, OR), (C(Integer));
2056 OP(NilP, NIL_P), (C(NilClass));
2057 OP(Cmp, CMP), (C(Integer), C(Float), C(String));
2058 OP(Default, DEFAULT), (C(Hash));
2059#undef C
2060#undef OP
2061}
2062
2063/* for vm development */
2064
2065#if VMDEBUG
2066static const char *
2067vm_frametype_name(const rb_control_frame_t *cfp)
2068{
2069 switch (VM_FRAME_TYPE(cfp)) {
2070 case VM_FRAME_MAGIC_METHOD: return "method";
2071 case VM_FRAME_MAGIC_BLOCK: return "block";
2072 case VM_FRAME_MAGIC_CLASS: return "class";
2073 case VM_FRAME_MAGIC_TOP: return "top";
2074 case VM_FRAME_MAGIC_CFUNC: return "cfunc";
2075 case VM_FRAME_MAGIC_IFUNC: return "ifunc";
2076 case VM_FRAME_MAGIC_EVAL: return "eval";
2077 case VM_FRAME_MAGIC_RESCUE: return "rescue";
2078 default:
2079 rb_bug("unknown frame");
2080 }
2081}
2082#endif
2083
2084static VALUE
2085frame_return_value(const struct vm_throw_data *err)
2086{
2087 if (THROW_DATA_P(err) &&
2088 THROW_DATA_STATE(err) == TAG_BREAK &&
2089 THROW_DATA_CONSUMED_P(err) == FALSE) {
2090 return THROW_DATA_VAL(err);
2091 }
2092 else {
2093 return Qnil;
2094 }
2095}
2096
2097#if 0
2098/* for debug */
2099static const char *
2100frame_name(const rb_control_frame_t *cfp)
2101{
2102 unsigned long type = VM_FRAME_TYPE(cfp);
2103#define C(t) if (type == VM_FRAME_MAGIC_##t) return #t
2104 C(METHOD);
2105 C(BLOCK);
2106 C(CLASS);
2107 C(TOP);
2108 C(CFUNC);
2109 C(PROC);
2110 C(IFUNC);
2111 C(EVAL);
2112 C(LAMBDA);
2113 C(RESCUE);
2114 C(DUMMY);
2115#undef C
2116 return "unknown";
2117}
2118#endif
2119
2120// cfp_returning_with_value:
2121// Whether cfp is the last frame in the unwinding process for a non-local return.
2122static void
2123hook_before_rewind(rb_execution_context_t *ec, const rb_control_frame_t *cfp,
2124 bool cfp_returning_with_value, int state, struct vm_throw_data *err)
2125{
2126 if (state == TAG_RAISE && RBASIC(err)->klass == rb_eSysStackError) {
2127 return;
2128 }
2129 else {
2130 const rb_iseq_t *iseq = cfp->iseq;
2131 rb_hook_list_t *local_hooks = iseq->aux.exec.local_hooks;
2132
2133 switch (VM_FRAME_TYPE(ec->cfp)) {
2134 case VM_FRAME_MAGIC_METHOD:
2135 RUBY_DTRACE_METHOD_RETURN_HOOK(ec, 0, 0);
2136 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2137
2138 if (UNLIKELY(local_hooks && local_hooks->events & RUBY_EVENT_RETURN)) {
2139 rb_exec_event_hook_orig(ec, local_hooks, RUBY_EVENT_RETURN,
2140 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2141 }
2142
2143 THROW_DATA_CONSUMED_SET(err);
2144 break;
2145 case VM_FRAME_MAGIC_BLOCK:
2146 if (VM_FRAME_BMETHOD_P(ec->cfp)) {
2147 VALUE bmethod_return_value = frame_return_value(err);
2148 if (cfp_returning_with_value) {
2149 // Non-local return terminating at a BMETHOD control frame.
2150 bmethod_return_value = THROW_DATA_VAL(err);
2151 }
2152
2153
2154 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, bmethod_return_value);
2155 if (UNLIKELY(local_hooks && local_hooks->events & RUBY_EVENT_B_RETURN)) {
2156 rb_exec_event_hook_orig(ec, local_hooks, RUBY_EVENT_B_RETURN,
2157 ec->cfp->self, 0, 0, 0, bmethod_return_value, TRUE);
2158 }
2159
2160 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(ec->cfp);
2161
2162 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_RETURN, ec->cfp->self,
2163 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2164 rb_vm_frame_method_entry(ec->cfp)->called_id,
2165 rb_vm_frame_method_entry(ec->cfp)->owner,
2166 bmethod_return_value);
2167
2168 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
2169 local_hooks = me->def->body.bmethod.hooks;
2170
2171 if (UNLIKELY(local_hooks && local_hooks->events & RUBY_EVENT_RETURN)) {
2172 rb_exec_event_hook_orig(ec, local_hooks, RUBY_EVENT_RETURN, ec->cfp->self,
2173 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2174 rb_vm_frame_method_entry(ec->cfp)->called_id,
2175 rb_vm_frame_method_entry(ec->cfp)->owner,
2176 bmethod_return_value, TRUE);
2177 }
2178 THROW_DATA_CONSUMED_SET(err);
2179 }
2180 else {
2181 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2182 if (UNLIKELY(local_hooks && local_hooks->events & RUBY_EVENT_B_RETURN)) {
2183 rb_exec_event_hook_orig(ec, local_hooks, RUBY_EVENT_B_RETURN,
2184 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2185 }
2186 THROW_DATA_CONSUMED_SET(err);
2187 }
2188 break;
2189 case VM_FRAME_MAGIC_CLASS:
2190 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_END, ec->cfp->self, 0, 0, 0, Qnil);
2191 break;
2192 }
2193 }
2194}
2195
2196/* evaluator body */
2197
2198/* finish
2199 VMe (h1) finish
2200 VM finish F1 F2
2201 cfunc finish F1 F2 C1
2202 rb_funcall finish F1 F2 C1
2203 VMe finish F1 F2 C1
2204 VM finish F1 F2 C1 F3
2205
2206 F1 - F3 : pushed by VM
2207 C1 : pushed by send insn (CFUNC)
2208
2209 struct CONTROL_FRAME {
2210 VALUE *pc; // cfp[0], program counter
2211 VALUE *sp; // cfp[1], stack pointer
2212 rb_iseq_t *iseq; // cfp[2], iseq
2213 VALUE self; // cfp[3], self
2214 const VALUE *ep; // cfp[4], env pointer
2215 const void *block_code; // cfp[5], block code
2216 };
2217
2218 struct rb_captured_block {
2219 VALUE self;
2220 VALUE *ep;
2221 union code;
2222 };
2223
2224 struct METHOD_ENV {
2225 VALUE param0;
2226 ...
2227 VALUE paramN;
2228 VALUE lvar1;
2229 ...
2230 VALUE lvarM;
2231 VALUE cref; // ep[-2]
2232 VALUE special; // ep[-1]
2233 VALUE flags; // ep[ 0] == lep[0]
2234 };
2235
2236 struct BLOCK_ENV {
2237 VALUE block_param0;
2238 ...
2239 VALUE block_paramN;
2240 VALUE block_lvar1;
2241 ...
2242 VALUE block_lvarM;
2243 VALUE cref; // ep[-2]
2244 VALUE special; // ep[-1]
2245 VALUE flags; // ep[ 0]
2246 };
2247
2248 struct CLASS_ENV {
2249 VALUE class_lvar0;
2250 ...
2251 VALUE class_lvarN;
2252 VALUE cref;
2253 VALUE prev_ep; // for frame jump
2254 VALUE flags;
2255 };
2256
2257 struct C_METHOD_CONTROL_FRAME {
2258 VALUE *pc; // 0
2259 VALUE *sp; // stack pointer
2260 rb_iseq_t *iseq; // cmi
2261 VALUE self; // ?
2262 VALUE *ep; // ep == lep
2263 void *code; //
2264 };
2265
2266 struct C_BLOCK_CONTROL_FRAME {
2267 VALUE *pc; // point only "finish" insn
2268 VALUE *sp; // sp
2269 rb_iseq_t *iseq; // ?
2270 VALUE self; //
2271 VALUE *ep; // ep
2272 void *code; //
2273 };
2274
2275 If jit_exec is already called before calling vm_exec, `jit_enable_p` should
2276 be FALSE to avoid calling `jit_exec` twice.
2277 */
2278
2279static inline VALUE
2280vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
2281 VALUE errinfo, VALUE *initial);
2282
2283// for non-Emscripten Wasm build, use vm_exec with optimized setjmp for runtime performance
2284#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2285
2286struct rb_vm_exec_context {
2287 rb_execution_context_t *ec;
2288 struct rb_vm_tag *tag;
2289 VALUE initial;
2290 VALUE result;
2291 enum ruby_tag_type state;
2292 bool jit_enable_p;
2293};
2294
2295static void
2296vm_exec_enter_vm_loop(rb_execution_context_t *ec, struct rb_vm_exec_context *ctx,
2297 struct rb_vm_tag *_tag, bool skip_first_ex_handle) {
2298 if (skip_first_ex_handle) {
2299 goto vm_loop_start;
2300 }
2301
2302 ctx->result = ec->errinfo;
2303 rb_ec_raised_reset(ec, RAISED_STACKOVERFLOW | RAISED_NOMEMORY);
2304 while (UNDEF_P(ctx->result = vm_exec_handle_exception(ec, ctx->state, ctx->result, &ctx->initial))) {
2305 /* caught a jump, exec the handler */
2306 ctx->result = vm_exec_core(ec, ctx->initial);
2307 vm_loop_start:
2308 VM_ASSERT(ec->tag == _tag);
2309 /* when caught `throw`, `tag.state` is set. */
2310 if ((ctx->state = _tag->state) == TAG_NONE) break;
2311 _tag->state = TAG_NONE;
2312 }
2313}
2314
2315static void
2316vm_exec_bottom_main(void *context)
2317{
2318 struct rb_vm_exec_context *ctx = (struct rb_vm_exec_context *)context;
2319
2320 ctx->state = TAG_NONE;
2321 if (!ctx->jit_enable_p || UNDEF_P(ctx->result = jit_exec(ctx->ec))) {
2322 ctx->result = vm_exec_core(ctx->ec, ctx->initial);
2323 }
2324 vm_exec_enter_vm_loop(ctx->ec, ctx, ctx->tag, true);
2325}
2326
2327static void
2328vm_exec_bottom_rescue(void *context)
2329{
2330 struct rb_vm_exec_context *ctx = (struct rb_vm_exec_context *)context;
2331 ctx->state = rb_ec_tag_state(ctx->ec);
2332 vm_exec_enter_vm_loop(ctx->ec, ctx, ctx->tag, false);
2333}
2334
2335 VALUE
2336vm_exec(rb_execution_context_t *ec, bool jit_enable_p)
2337{
2338 struct rb_vm_exec_context ctx = {
2339 .ec = ec,
2340 .initial = 0, .result = Qundef,
2341 .jit_enable_p = jit_enable_p,
2342 };
2343 struct rb_wasm_try_catch try_catch;
2344
2345 EC_PUSH_TAG(ec);
2346
2347 _tag.retval = Qnil;
2348 ctx.tag = &_tag;
2349
2350 EC_REPUSH_TAG();
2351
2352 rb_wasm_try_catch_init(&try_catch, vm_exec_bottom_main, vm_exec_bottom_rescue, &ctx);
2353
2354 rb_wasm_try_catch_loop_run(&try_catch, &_tag.buf);
2355
2356 EC_POP_TAG();
2357 return ctx.result;
2358}
2359
2360#else
2361
2362 VALUE
2363vm_exec(rb_execution_context_t *ec, bool jit_enable_p)
2364{
2365 enum ruby_tag_type state;
2366 VALUE result = Qundef;
2367 VALUE initial = 0;
2368
2369 EC_PUSH_TAG(ec);
2370
2371 _tag.retval = Qnil;
2372 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2373 if (!jit_enable_p || UNDEF_P(result = jit_exec(ec))) {
2374 result = vm_exec_core(ec, initial);
2375 }
2376 goto vm_loop_start; /* fallback to the VM */
2377 }
2378 else {
2379 result = ec->errinfo;
2380 rb_ec_raised_reset(ec, RAISED_STACKOVERFLOW | RAISED_NOMEMORY);
2381 while (UNDEF_P(result = vm_exec_handle_exception(ec, state, result, &initial))) {
2382 /* caught a jump, exec the handler */
2383 result = vm_exec_core(ec, initial);
2384 vm_loop_start:
2385 VM_ASSERT(ec->tag == &_tag);
2386 /* when caught `throw`, `tag.state` is set. */
2387 if ((state = _tag.state) == TAG_NONE) break;
2388 _tag.state = TAG_NONE;
2389 }
2390 }
2391 EC_POP_TAG();
2392 return result;
2393}
2394#endif
2395
2396static inline VALUE
2397vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
2398 VALUE errinfo, VALUE *initial)
2399{
2400 struct vm_throw_data *err = (struct vm_throw_data *)errinfo;
2401
2402 for (;;) {
2403 unsigned int i;
2404 const struct iseq_catch_table_entry *entry;
2405 const struct iseq_catch_table *ct;
2406 unsigned long epc, cont_pc, cont_sp;
2407 const rb_iseq_t *catch_iseq;
2408 rb_control_frame_t *cfp;
2409 VALUE type;
2410 const rb_control_frame_t *escape_cfp;
2411
2412 cont_pc = cont_sp = 0;
2413 catch_iseq = NULL;
2414
2415 while (ec->cfp->pc == 0 || ec->cfp->iseq == 0) {
2416 if (UNLIKELY(VM_FRAME_TYPE(ec->cfp) == VM_FRAME_MAGIC_CFUNC)) {
2417 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_C_RETURN, ec->cfp->self,
2418 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2419 rb_vm_frame_method_entry(ec->cfp)->called_id,
2420 rb_vm_frame_method_entry(ec->cfp)->owner, Qnil);
2421 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec,
2422 rb_vm_frame_method_entry(ec->cfp)->owner,
2423 rb_vm_frame_method_entry(ec->cfp)->def->original_id);
2424 }
2425 rb_vm_pop_frame(ec);
2426 }
2427
2428 cfp = ec->cfp;
2429 epc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
2430
2431 escape_cfp = NULL;
2432 if (state == TAG_BREAK || state == TAG_RETURN) {
2433 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2434
2435 if (cfp == escape_cfp) {
2436 if (state == TAG_RETURN) {
2437 if (!VM_FRAME_FINISHED_P(cfp)) {
2438 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2439 THROW_DATA_STATE_SET(err, state = TAG_BREAK);
2440 }
2441 else {
2442 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2443 if (ct) for (i = 0; i < ct->size; i++) {
2444 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2445 if (entry->start < epc && entry->end >= epc) {
2446 if (entry->type == CATCH_TYPE_ENSURE) {
2447 catch_iseq = entry->iseq;
2448 cont_pc = entry->cont;
2449 cont_sp = entry->sp;
2450 break;
2451 }
2452 }
2453 }
2454 if (catch_iseq == NULL) {
2455 ec->errinfo = Qnil;
2456 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2457 // cfp == escape_cfp here so calling with cfp_returning_with_value = true
2458 hook_before_rewind(ec, ec->cfp, true, state, err);
2459 rb_vm_pop_frame(ec);
2460 return THROW_DATA_VAL(err);
2461 }
2462 }
2463 /* through */
2464 }
2465 else {
2466 /* TAG_BREAK */
2467#if OPT_STACK_CACHING
2468 *initial = THROW_DATA_VAL(err);
2469#else
2470 *ec->cfp->sp++ = THROW_DATA_VAL(err);
2471#endif
2472 ec->errinfo = Qnil;
2473 return Qundef;
2474 }
2475 }
2476 }
2477
2478 if (state == TAG_RAISE) {
2479 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2480 if (ct) for (i = 0; i < ct->size; i++) {
2481 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2482 if (entry->start < epc && entry->end >= epc) {
2483
2484 if (entry->type == CATCH_TYPE_RESCUE ||
2485 entry->type == CATCH_TYPE_ENSURE) {
2486 catch_iseq = entry->iseq;
2487 cont_pc = entry->cont;
2488 cont_sp = entry->sp;
2489 break;
2490 }
2491 }
2492 }
2493 }
2494 else if (state == TAG_RETRY) {
2495 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2496 if (ct) for (i = 0; i < ct->size; i++) {
2497 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2498 if (entry->start < epc && entry->end >= epc) {
2499
2500 if (entry->type == CATCH_TYPE_ENSURE) {
2501 catch_iseq = entry->iseq;
2502 cont_pc = entry->cont;
2503 cont_sp = entry->sp;
2504 break;
2505 }
2506 else if (entry->type == CATCH_TYPE_RETRY) {
2507 const rb_control_frame_t *escape_cfp;
2508 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2509 if (cfp == escape_cfp) {
2510 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2511 ec->errinfo = Qnil;
2512 return Qundef;
2513 }
2514 }
2515 }
2516 }
2517 }
2518 else if ((state == TAG_BREAK && !escape_cfp) ||
2519 (state == TAG_REDO) ||
2520 (state == TAG_NEXT)) {
2521 type = (const enum rb_catch_type[TAG_MASK]) {
2522 [TAG_BREAK] = CATCH_TYPE_BREAK,
2523 [TAG_NEXT] = CATCH_TYPE_NEXT,
2524 [TAG_REDO] = CATCH_TYPE_REDO,
2525 /* otherwise = dontcare */
2526 }[state];
2527
2528 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2529 if (ct) for (i = 0; i < ct->size; i++) {
2530 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2531
2532 if (entry->start < epc && entry->end >= epc) {
2533 if (entry->type == CATCH_TYPE_ENSURE) {
2534 catch_iseq = entry->iseq;
2535 cont_pc = entry->cont;
2536 cont_sp = entry->sp;
2537 break;
2538 }
2539 else if (entry->type == type) {
2540 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2541 cfp->sp = vm_base_ptr(cfp) + entry->sp;
2542
2543 if (state != TAG_REDO) {
2544#if OPT_STACK_CACHING
2545 *initial = THROW_DATA_VAL(err);
2546#else
2547 *ec->cfp->sp++ = THROW_DATA_VAL(err);
2548#endif
2549 }
2550 ec->errinfo = Qnil;
2551 VM_ASSERT(ec->tag->state == TAG_NONE);
2552 return Qundef;
2553 }
2554 }
2555 }
2556 }
2557 else {
2558 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2559 if (ct) for (i = 0; i < ct->size; i++) {
2560 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2561 if (entry->start < epc && entry->end >= epc) {
2562
2563 if (entry->type == CATCH_TYPE_ENSURE) {
2564 catch_iseq = entry->iseq;
2565 cont_pc = entry->cont;
2566 cont_sp = entry->sp;
2567 break;
2568 }
2569 }
2570 }
2571 }
2572
2573 if (catch_iseq != NULL) { /* found catch table */
2574 /* enter catch scope */
2575 const int arg_size = 1;
2576
2577 rb_iseq_check(catch_iseq);
2578 cfp->sp = vm_base_ptr(cfp) + cont_sp;
2579 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + cont_pc;
2580
2581 /* push block frame */
2582 cfp->sp[0] = (VALUE)err;
2583 vm_push_frame(ec, catch_iseq, VM_FRAME_MAGIC_RESCUE,
2584 cfp->self,
2585 VM_GUARDED_PREV_EP(cfp->ep),
2586 0, /* cref or me */
2587 ISEQ_BODY(catch_iseq)->iseq_encoded,
2588 cfp->sp + arg_size /* push value */,
2589 ISEQ_BODY(catch_iseq)->local_table_size - arg_size,
2590 ISEQ_BODY(catch_iseq)->stack_max);
2591
2592 state = 0;
2593 ec->tag->state = TAG_NONE;
2594 ec->errinfo = Qnil;
2595
2596 return Qundef;
2597 }
2598 else {
2599 hook_before_rewind(ec, ec->cfp, (cfp == escape_cfp), state, err);
2600
2601 if (VM_FRAME_FINISHED_P(ec->cfp)) {
2602 rb_vm_pop_frame(ec);
2603 ec->errinfo = (VALUE)err;
2604 ec->tag = ec->tag->prev;
2605 EC_JUMP_TAG(ec, state);
2606 }
2607 else {
2608 rb_vm_pop_frame(ec);
2609 }
2610 }
2611 }
2612}
2613
2614/* misc */
2615
2616 VALUE
2617rb_iseq_eval(const rb_iseq_t *iseq)
2618{
2619 rb_execution_context_t *ec = GET_EC();
2620 VALUE val;
2621 vm_set_top_stack(ec, iseq);
2622 val = vm_exec(ec, true);
2623 return val;
2624}
2625
2626 VALUE
2627rb_iseq_eval_main(const rb_iseq_t *iseq)
2628{
2629 rb_execution_context_t *ec = GET_EC();
2630 VALUE val;
2631
2632 vm_set_main_stack(ec, iseq);
2633 val = vm_exec(ec, true);
2634 return val;
2635}
2636
2637int
2638rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp)
2639{
2640 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
2641
2642 if (me) {
2643 if (idp) *idp = me->def->original_id;
2644 if (called_idp) *called_idp = me->called_id;
2645 if (klassp) *klassp = me->owner;
2646 return TRUE;
2647 }
2648 else {
2649 return FALSE;
2650 }
2651}
2652
2653int
2654rb_ec_frame_method_id_and_class(const rb_execution_context_t *ec, ID *idp, ID *called_idp, VALUE *klassp)
2655{
2656 return rb_vm_control_frame_id_and_class(ec->cfp, idp, called_idp, klassp);
2657}
2658
2659int
2660rb_frame_method_id_and_class(ID *idp, VALUE *klassp)
2661{
2662 return rb_ec_frame_method_id_and_class(GET_EC(), idp, 0, klassp);
2663}
2664
2665 VALUE
2666rb_vm_call_cfunc(VALUE recv, VALUE (*func)(VALUE), VALUE arg,
2667 VALUE block_handler, VALUE filename)
2668{
2669 rb_execution_context_t *ec = GET_EC();
2670 const rb_control_frame_t *reg_cfp = ec->cfp;
2671 const rb_iseq_t *iseq = rb_iseq_new(0, filename, filename, Qnil, 0, ISEQ_TYPE_TOP);
2672 VALUE val;
2673
2674 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH,
2675 recv, block_handler,
2676 (VALUE)vm_cref_new_toplevel(ec), /* cref or me */
2677 0, reg_cfp->sp, 0, 0);
2678
2679 val = (*func)(arg);
2680
2681 rb_vm_pop_frame(ec);
2682 return val;
2683}
2684
2685/* vm */
2686
2687void
2688rb_vm_update_references(void *ptr)
2689{
2690 if (ptr) {
2691 rb_vm_t *vm = ptr;
2692
2693 rb_gc_update_tbl_refs(vm->frozen_strings);
2694 vm->mark_object_ary = rb_gc_location(vm->mark_object_ary);
2695 vm->load_path = rb_gc_location(vm->load_path);
2696 vm->load_path_snapshot = rb_gc_location(vm->load_path_snapshot);
2697
2698 if (vm->load_path_check_cache) {
2699 vm->load_path_check_cache = rb_gc_location(vm->load_path_check_cache);
2700 }
2701
2702 vm->expanded_load_path = rb_gc_location(vm->expanded_load_path);
2703 vm->loaded_features = rb_gc_location(vm->loaded_features);
2704 vm->loaded_features_snapshot = rb_gc_location(vm->loaded_features_snapshot);
2705 vm->loaded_features_realpaths = rb_gc_location(vm->loaded_features_realpaths);
2706 vm->loaded_features_realpath_map = rb_gc_location(vm->loaded_features_realpath_map);
2707 vm->top_self = rb_gc_location(vm->top_self);
2708 vm->orig_progname = rb_gc_location(vm->orig_progname);
2709
2710 rb_gc_update_tbl_refs(vm->overloaded_cme_table);
2711
2712 if (vm->coverages) {
2713 vm->coverages = rb_gc_location(vm->coverages);
2714 vm->me2counter = rb_gc_location(vm->me2counter);
2715 }
2716 }
2717}
2718
2719void
2720rb_vm_each_stack_value(void *ptr, void (*cb)(VALUE, void*), void *ctx)
2721{
2722 if (ptr) {
2723 rb_vm_t *vm = ptr;
2724 rb_ractor_t *r = 0;
2725 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2726 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
2727 rb_ractor_status_p(r, ractor_running));
2728 if (r->threads.cnt > 0) {
2729 rb_thread_t *th = 0;
2730 ccan_list_for_each(&r->threads.set, th, lt_node) {
2731 VM_ASSERT(th != NULL);
2732 rb_execution_context_t * ec = th->ec;
2733 if (ec->vm_stack) {
2734 VALUE *p = ec->vm_stack;
2735 VALUE *sp = ec->cfp->sp;
2736 while (p < sp) {
2737 if (!rb_special_const_p(*p)) {
2738 cb(*p, ctx);
2739 }
2740 p++;
2741 }
2742 }
2743 }
2744 }
2745 }
2746 }
2747}
2748
2749static enum rb_id_table_iterator_result
2750vm_mark_negative_cme(VALUE val, void *dmy)
2751{
2752 rb_gc_mark(val);
2753 return ID_TABLE_CONTINUE;
2754}
2755
2756void
2757rb_vm_mark(void *ptr)
2758{
2759 RUBY_MARK_ENTER("vm");
2760 RUBY_GC_INFO("-------------------------------------------------\n");
2761 if (ptr) {
2762 rb_vm_t *vm = ptr;
2763 rb_ractor_t *r = 0;
2764 long i, len;
2765 const VALUE *obj_ary;
2766
2767 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2768 // ractor.set only contains blocking or running ractors
2769 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
2770 rb_ractor_status_p(r, ractor_running));
2771 rb_gc_mark(rb_ractor_self(r));
2772 }
2773
2774 rb_gc_mark_movable(vm->mark_object_ary);
2775
2776 len = RARRAY_LEN(vm->mark_object_ary);
2777 obj_ary = RARRAY_CONST_PTR(vm->mark_object_ary);
2778 for (i=0; i < len; i++) {
2779 const VALUE *ptr;
2780 long j, jlen;
2781
2782 rb_gc_mark(*obj_ary);
2783 jlen = RARRAY_LEN(*obj_ary);
2784 ptr = RARRAY_CONST_PTR(*obj_ary);
2785 for (j=0; j < jlen; j++) {
2786 rb_gc_mark(*ptr++);
2787 }
2788 obj_ary++;
2789 }
2790
2791 rb_gc_mark_movable(vm->load_path);
2792 rb_gc_mark_movable(vm->load_path_snapshot);
2793 RUBY_MARK_MOVABLE_UNLESS_NULL(vm->load_path_check_cache);
2794 rb_gc_mark_movable(vm->expanded_load_path);
2795 rb_gc_mark_movable(vm->loaded_features);
2796 rb_gc_mark_movable(vm->loaded_features_snapshot);
2797 rb_gc_mark_movable(vm->loaded_features_realpaths);
2798 rb_gc_mark_movable(vm->loaded_features_realpath_map);
2799 rb_gc_mark_movable(vm->top_self);
2800 rb_gc_mark_movable(vm->orig_progname);
2801 RUBY_MARK_MOVABLE_UNLESS_NULL(vm->coverages);
2802 RUBY_MARK_MOVABLE_UNLESS_NULL(vm->me2counter);
2803 /* Prevent classes from moving */
2804 rb_mark_tbl(vm->defined_module_hash);
2805
2806 if (vm->loading_table) {
2807 rb_mark_tbl(vm->loading_table);
2808 }
2809
2810 rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);
2811
2812 rb_id_table_foreach_values(vm->negative_cme_table, vm_mark_negative_cme, NULL);
2813 rb_mark_tbl_no_pin(vm->overloaded_cme_table);
2814 for (i=0; i<VM_GLOBAL_CC_CACHE_TABLE_SIZE; i++) {
2815 const struct rb_callcache *cc = vm->global_cc_cache_table[i];
2816
2817 if (cc != NULL) {
2818 if (!vm_cc_invalidated_p(cc)) {
2819 rb_gc_mark((VALUE)cc);
2820 }
2821 else {
2822 vm->global_cc_cache_table[i] = NULL;
2823 }
2824 }
2825 }
2826
2827 mjit_mark();
2828 }
2829
2830 RUBY_MARK_LEAVE("vm");
2831}
2832
2833#undef rb_vm_register_special_exception
2834void
2835rb_vm_register_special_exception_str(enum ruby_special_exceptions sp, VALUE cls, VALUE mesg)
2836{
2837 rb_vm_t *vm = GET_VM();
2838 VALUE exc = rb_exc_new3(cls, rb_obj_freeze(mesg));
2839 OBJ_FREEZE(exc);
2840 ((VALUE *)vm->special_exceptions)[sp] = exc;
2841 rb_gc_register_mark_object(exc);
2842}
2843
2844int
2845rb_vm_add_root_module(VALUE module)
2846{
2847 rb_vm_t *vm = GET_VM();
2848
2849 st_insert(vm->defined_module_hash, (st_data_t)module, (st_data_t)module);
2850
2851 return TRUE;
2852}
2853
2854static int
2855free_loading_table_entry(st_data_t key, st_data_t value, st_data_t arg)
2856{
2857 xfree((char *)key);
2858 return ST_DELETE;
2859}
2860
2861int
2862ruby_vm_destruct(rb_vm_t *vm)
2863{
2864 RUBY_FREE_ENTER("vm");
2865
2866 if (vm) {
2867 rb_thread_t *th = vm->ractor.main_thread;
2868 struct rb_objspace *objspace = vm->objspace;
2869 vm->ractor.main_thread = NULL;
2870
2871 if (th) {
2872 rb_fiber_reset_root_local_storage(th);
2873 thread_free(th);
2874 }
2875 rb_vm_living_threads_init(vm);
2876 ruby_vm_run_at_exit_hooks(vm);
2877 if (vm->loading_table) {
2878 st_foreach(vm->loading_table, free_loading_table_entry, 0);
2879 st_free_table(vm->loading_table);
2880 vm->loading_table = 0;
2881 }
2882 if (vm->frozen_strings) {
2883 st_free_table(vm->frozen_strings);
2884 vm->frozen_strings = 0;
2885 }
2886 RB_ALTSTACK_FREE(vm->main_altstack);
2887 if (objspace) {
2888 rb_objspace_free(objspace);
2889 }
2890 rb_native_mutex_destroy(&vm->waitpid_lock);
2891 rb_native_mutex_destroy(&vm->workqueue_lock);
2892 /* after freeing objspace, you *can't* use ruby_xfree() */
2893 ruby_mimfree(vm);
2894 ruby_current_vm_ptr = NULL;
2895 }
2896 RUBY_FREE_LEAVE("vm");
2897 return 0;
2898}
2899
2900size_t rb_vm_memsize_waiting_list(struct ccan_list_head *waiting_list); // process.c
2901size_t rb_vm_memsize_waiting_fds(struct ccan_list_head *waiting_fds); // thread.c
2902size_t rb_vm_memsize_postponed_job_buffer(void); // vm_trace.c
2903size_t rb_vm_memsize_workqueue(struct ccan_list_head *workqueue); // vm_trace.c
2904
2905// Used for VM memsize reporting. Returns the size of the at_exit list by
2906// looping through the linked list and adding up the size of the structs.
2907static enum rb_id_table_iterator_result
2908vm_memsize_constant_cache_i(ID id, VALUE ics, void *size)
2909{
2910 *((size_t *) size) += rb_st_memsize((st_table *) ics);
2911 return ID_TABLE_CONTINUE;
2912}
2913
2914// Returns a size_t representing the memory footprint of the VM's constant
2915// cache, which is the memsize of the table as well as the memsize of all of the
2916// nested tables.
2917static size_t
2918vm_memsize_constant_cache(void)
2919{
2920 rb_vm_t *vm = GET_VM();
2921 size_t size = rb_id_table_memsize(vm->constant_cache);
2922
2923 rb_id_table_foreach(vm->constant_cache, vm_memsize_constant_cache_i, &size);
2924 return size;
2925}
2926
2927static size_t
2928vm_memsize_at_exit_list(rb_at_exit_list *at_exit)
2929{
2930 size_t size = 0;
2931
2932 while (at_exit) {
2933 size += sizeof(rb_at_exit_list);
2934 at_exit = at_exit->next;
2935 }
2936
2937 return size;
2938}
2939
2940// Used for VM memsize reporting. Returns the size of the builtin function
2941// table if it has been defined.
2942static size_t
2943vm_memsize_builtin_function_table(const struct rb_builtin_function *builtin_function_table)
2944{
2945 return builtin_function_table == NULL ? 0 : sizeof(struct rb_builtin_function);
2946}
2947
2948// Reports the memsize of the VM struct object and the structs that are
2949// associated with it.
2950static size_t
2951vm_memsize(const void *ptr)
2952{
2953 rb_vm_t *vm = GET_VM();
2954
2955 return (
2956 sizeof(rb_vm_t) +
2957 rb_vm_memsize_waiting_list(&vm->waiting_pids) +
2958 rb_vm_memsize_waiting_list(&vm->waiting_grps) +
2959 rb_vm_memsize_waiting_fds(&vm->waiting_fds) +
2960 rb_st_memsize(vm->loaded_features_index) +
2961 rb_st_memsize(vm->loading_table) +
2962 rb_st_memsize(vm->ensure_rollback_table) +
2963 rb_vm_memsize_postponed_job_buffer() +
2964 rb_vm_memsize_workqueue(&vm->workqueue) +
2965 rb_st_memsize(vm->defined_module_hash) +
2966 vm_memsize_at_exit_list(vm->at_exit) +
2967 rb_st_memsize(vm->frozen_strings) +
2968 vm_memsize_builtin_function_table(vm->builtin_function_table) +
2969 rb_id_table_memsize(vm->negative_cme_table) +
2970 rb_st_memsize(vm->overloaded_cme_table) +
2971 vm_memsize_constant_cache()
2972 );
2973
2974 // TODO
2975 // struct { struct ccan_list_head set; } ractor;
2976 // void *main_altstack; #ifdef USE_SIGALTSTACK
2977 // struct rb_objspace *objspace;
2978}
2979
2980static const rb_data_type_t vm_data_type = {
2981 "VM",
2982 {0, 0, vm_memsize,},
2983 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2984};
2985
2986
2987static VALUE
2988vm_default_params(void)
2989{
2990 rb_vm_t *vm = GET_VM();
2991 VALUE result = rb_hash_new_with_size(4);
2992#define SET(name) rb_hash_aset(result, ID2SYM(rb_intern(#name)), SIZET2NUM(vm->default_params.name));
2993 SET(thread_vm_stack_size);
2994 SET(thread_machine_stack_size);
2995 SET(fiber_vm_stack_size);
2996 SET(fiber_machine_stack_size);
2997#undef SET
2998 rb_obj_freeze(result);
2999 return result;
3000}
3001
3002static size_t
3003get_param(const char *name, size_t default_value, size_t min_value)
3004{
3005 const char *envval;
3006 size_t result = default_value;
3007 if ((envval = getenv(name)) != 0) {
3008 long val = atol(envval);
3009 if (val < (long)min_value) {
3010 val = (long)min_value;
3011 }
3012 result = (size_t)(((val -1 + RUBY_VM_SIZE_ALIGN) / RUBY_VM_SIZE_ALIGN) * RUBY_VM_SIZE_ALIGN);
3013 }
3014 if (0) ruby_debug_printf("%s: %"PRIuSIZE"\n", name, result); /* debug print */
3015
3016 return result;
3017}
3018
3019static void
3020check_machine_stack_size(size_t *sizep)
3021{
3022#ifdef PTHREAD_STACK_MIN
3023 size_t size = *sizep;
3024#endif
3025
3026#ifdef PTHREAD_STACK_MIN
3027 if (size < (size_t)PTHREAD_STACK_MIN) {
3028 *sizep = (size_t)PTHREAD_STACK_MIN * 2;
3029 }
3030#endif
3031}
3032
3033static void
3034vm_default_params_setup(rb_vm_t *vm)
3035{
3036 vm->default_params.thread_vm_stack_size =
3037 get_param("RUBY_THREAD_VM_STACK_SIZE",
3038 RUBY_VM_THREAD_VM_STACK_SIZE,
3039 RUBY_VM_THREAD_VM_STACK_SIZE_MIN);
3040
3041 vm->default_params.thread_machine_stack_size =
3042 get_param("RUBY_THREAD_MACHINE_STACK_SIZE",
3043 RUBY_VM_THREAD_MACHINE_STACK_SIZE,
3044 RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN);
3045
3046 vm->default_params.fiber_vm_stack_size =
3047 get_param("RUBY_FIBER_VM_STACK_SIZE",
3048 RUBY_VM_FIBER_VM_STACK_SIZE,
3049 RUBY_VM_FIBER_VM_STACK_SIZE_MIN);
3050
3051 vm->default_params.fiber_machine_stack_size =
3052 get_param("RUBY_FIBER_MACHINE_STACK_SIZE",
3053 RUBY_VM_FIBER_MACHINE_STACK_SIZE,
3054 RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN);
3055
3056 /* environment dependent check */
3057 check_machine_stack_size(&vm->default_params.thread_machine_stack_size);
3058 check_machine_stack_size(&vm->default_params.fiber_machine_stack_size);
3059}
3060
3061static void
3062vm_init2(rb_vm_t *vm)
3063{
3064 MEMZERO(vm, rb_vm_t, 1);
3065 rb_vm_living_threads_init(vm);
3066 vm->thread_report_on_exception = 1;
3067 vm->src_encoding_index = -1;
3068
3069 vm_default_params_setup(vm);
3070}
3071
3072void
3073rb_execution_context_update(rb_execution_context_t *ec)
3074{
3075 /* update VM stack */
3076 if (ec->vm_stack) {
3077 long i;
3078 VM_ASSERT(ec->cfp);
3079 VALUE *p = ec->vm_stack;
3080 VALUE *sp = ec->cfp->sp;
3081 rb_control_frame_t *cfp = ec->cfp;
3082 rb_control_frame_t *limit_cfp = (void *)(ec->vm_stack + ec->vm_stack_size);
3083
3084 for (i = 0; i < (long)(sp - p); i++) {
3085 VALUE ref = p[i];
3086 VALUE update = rb_gc_location(ref);
3087 if (ref != update) {
3088 p[i] = update;
3089 }
3090 }
3091
3092 while (cfp != limit_cfp) {
3093 const VALUE *ep = cfp->ep;
3094 cfp->self = rb_gc_location(cfp->self);
3095 cfp->iseq = (rb_iseq_t *)rb_gc_location((VALUE)cfp->iseq);
3096 cfp->block_code = (void *)rb_gc_location((VALUE)cfp->block_code);
3097
3098 if (!VM_ENV_LOCAL_P(ep)) {
3099 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3100 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3101 VM_FORCE_WRITE(&prev_ep[VM_ENV_DATA_INDEX_ENV], rb_gc_location(prev_ep[VM_ENV_DATA_INDEX_ENV]));
3102 }
3103
3104 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3105 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ENV], rb_gc_location(ep[VM_ENV_DATA_INDEX_ENV]));
3106 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ME_CREF], rb_gc_location(ep[VM_ENV_DATA_INDEX_ME_CREF]));
3107 }
3108 }
3109
3110 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3111 }
3112 }
3113
3114 ec->storage = rb_gc_location(ec->storage);
3115}
3116
3117static enum rb_id_table_iterator_result
3118mark_local_storage_i(VALUE local, void *data)
3119{
3120 rb_gc_mark(local);
3121 return ID_TABLE_CONTINUE;
3122}
3123
3124void
3125rb_execution_context_mark(const rb_execution_context_t *ec)
3126{
3127 /* mark VM stack */
3128 if (ec->vm_stack) {
3129 VM_ASSERT(ec->cfp);
3130 VALUE *p = ec->vm_stack;
3131 VALUE *sp = ec->cfp->sp;
3132 rb_control_frame_t *cfp = ec->cfp;
3133 rb_control_frame_t *limit_cfp = (void *)(ec->vm_stack + ec->vm_stack_size);
3134
3135 VM_ASSERT(sp == ec->cfp->sp);
3136 rb_gc_mark_vm_stack_values((long)(sp - p), p);
3137
3138 while (cfp != limit_cfp) {
3139 const VALUE *ep = cfp->ep;
3140 VM_ASSERT(!!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) == vm_ep_in_heap_p_(ec, ep));
3141
3142 if (VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
3143 rb_gc_mark_movable(cfp->self);
3144 rb_gc_mark_movable((VALUE)cfp->iseq);
3145 rb_gc_mark_movable((VALUE)cfp->block_code);
3146
3147 if (!VM_ENV_LOCAL_P(ep)) {
3148 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3149 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3150 rb_gc_mark_movable(prev_ep[VM_ENV_DATA_INDEX_ENV]);
3151 }
3152
3153 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3154 rb_gc_mark_movable(ep[VM_ENV_DATA_INDEX_ENV]);
3155 rb_gc_mark(ep[VM_ENV_DATA_INDEX_ME_CREF]);
3156 }
3157 }
3158 }
3159
3160 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3161 }
3162 }
3163
3164 /* mark machine stack */
3165 if (ec->machine.stack_start && ec->machine.stack_end &&
3166 ec != GET_EC() /* marked for current ec at the first stage of marking */
3167 ) {
3168 rb_gc_mark_machine_stack(ec);
3169 rb_gc_mark_locations((VALUE *)&ec->machine.regs,
3170 (VALUE *)(&ec->machine.regs) +
3171 sizeof(ec->machine.regs) / (sizeof(VALUE)));
3172 }
3173
3174 RUBY_MARK_UNLESS_NULL(ec->errinfo);
3175 RUBY_MARK_UNLESS_NULL(ec->root_svar);
3176 if (ec->local_storage) {
3177 rb_id_table_foreach_values(ec->local_storage, mark_local_storage_i, NULL);
3178 }
3179 RUBY_MARK_UNLESS_NULL(ec->local_storage_recursive_hash);
3180 RUBY_MARK_UNLESS_NULL(ec->local_storage_recursive_hash_for_trace);
3181 RUBY_MARK_UNLESS_NULL(ec->private_const_reference);
3182
3183 RUBY_MARK_MOVABLE_UNLESS_NULL(ec->storage);
3184}
3185
3186void rb_fiber_mark_self(rb_fiber_t *fib);
3187void rb_fiber_update_self(rb_fiber_t *fib);
3188void rb_threadptr_root_fiber_setup(rb_thread_t *th);
3189void rb_threadptr_root_fiber_release(rb_thread_t *th);
3190
3191static void
3192thread_compact(void *ptr)
3193{
3194 rb_thread_t *th = ptr;
3195
3196 th->self = rb_gc_location(th->self);
3197
3198 if (!th->root_fiber) {
3199 rb_execution_context_update(th->ec);
3200 }
3201}
3202
3203static void
3204thread_mark(void *ptr)
3205{
3206 rb_thread_t *th = ptr;
3207 RUBY_MARK_ENTER("thread");
3208 rb_fiber_mark_self(th->ec->fiber_ptr);
3209
3210 /* mark ruby objects */
3211 switch (th->invoke_type) {
3212 case thread_invoke_type_proc:
3213 case thread_invoke_type_ractor_proc:
3214 RUBY_MARK_UNLESS_NULL(th->invoke_arg.proc.proc);
3215 RUBY_MARK_UNLESS_NULL(th->invoke_arg.proc.args);
3216 break;
3217 case thread_invoke_type_func:
3218 rb_gc_mark_maybe((VALUE)th->invoke_arg.func.arg);
3219 break;
3220 default:
3221 break;
3222 }
3223
3224 rb_gc_mark(rb_ractor_self(th->ractor));
3225 RUBY_MARK_UNLESS_NULL(th->thgroup);
3226 RUBY_MARK_UNLESS_NULL(th->value);
3227 RUBY_MARK_UNLESS_NULL(th->pending_interrupt_queue);
3228 RUBY_MARK_UNLESS_NULL(th->pending_interrupt_mask_stack);
3229 RUBY_MARK_UNLESS_NULL(th->top_self);
3230 RUBY_MARK_UNLESS_NULL(th->top_wrapper);
3231 if (th->root_fiber) rb_fiber_mark_self(th->root_fiber);
3232
3233 RUBY_ASSERT(th->ec == rb_fiberptr_get_ec(th->ec->fiber_ptr));
3234 RUBY_MARK_UNLESS_NULL(th->stat_insn_usage);
3235 RUBY_MARK_UNLESS_NULL(th->last_status);
3236 RUBY_MARK_UNLESS_NULL(th->locking_mutex);
3237 RUBY_MARK_UNLESS_NULL(th->name);
3238
3239 RUBY_MARK_UNLESS_NULL(th->scheduler);
3240
3241 RUBY_MARK_LEAVE("thread");
3242}
3243
3244static void
3245thread_free(void *ptr)
3246{
3247 rb_thread_t *th = ptr;
3248 RUBY_FREE_ENTER("thread");
3249
3250 if (th->locking_mutex != Qfalse) {
3251 rb_bug("thread_free: locking_mutex must be NULL (%p:%p)", (void *)th, (void *)th->locking_mutex);
3252 }
3253 if (th->keeping_mutexes != NULL) {
3254 rb_bug("thread_free: keeping_mutexes must be NULL (%p:%p)", (void *)th, (void *)th->keeping_mutexes);
3255 }
3256
3257 rb_threadptr_root_fiber_release(th);
3258
3259 if (th->vm && th->vm->ractor.main_thread == th) {
3260 RUBY_GC_INFO("MRI main thread\n");
3261 }
3262 else {
3263 ruby_xfree(th->nt); // TODO
3264 ruby_xfree(th);
3265 }
3266
3267 RUBY_FREE_LEAVE("thread");
3268}
3269
3270static size_t
3271thread_memsize(const void *ptr)
3272{
3273 const rb_thread_t *th = ptr;
3274 size_t size = sizeof(rb_thread_t);
3275
3276 if (!th->root_fiber) {
3277 size += th->ec->vm_stack_size * sizeof(VALUE);
3278 }
3279 if (th->ec->local_storage) {
3280 size += rb_id_table_memsize(th->ec->local_storage);
3281 }
3282 return size;
3283}
3284
3285#define thread_data_type ruby_threadptr_data_type
3286const rb_data_type_t ruby_threadptr_data_type = {
3287 "VM/thread",
3288 {
3289 thread_mark,
3290 thread_free,
3291 thread_memsize,
3292 thread_compact,
3293 },
3294 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3295};
3296
3297 VALUE
3298rb_obj_is_thread(VALUE obj)
3299{
3300 return RBOOL(rb_typeddata_is_kind_of(obj, &thread_data_type));
3301}
3302
3303static VALUE
3304thread_alloc(VALUE klass)
3305{
3306 rb_thread_t *th;
3307 return TypedData_Make_Struct(klass, rb_thread_t, &thread_data_type, th);
3308}
3309
3310inline void
3311rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size)
3312{
3313 ec->vm_stack = stack;
3314 ec->vm_stack_size = size;
3315}
3316
3317void
3318rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size)
3319{
3320 rb_ec_set_vm_stack(ec, stack, size);
3321
3322 ec->cfp = (void *)(ec->vm_stack + ec->vm_stack_size);
3323
3324 vm_push_frame(ec,
3325 NULL /* dummy iseq */,
3326 VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME /* dummy frame */,
3327 Qnil /* dummy self */, VM_BLOCK_HANDLER_NONE /* dummy block ptr */,
3328 0 /* dummy cref/me */,
3329 0 /* dummy pc */, ec->vm_stack, 0, 0
3330 );
3331}
3332
3333void
3334rb_ec_clear_vm_stack(rb_execution_context_t *ec)
3335{
3336 rb_ec_set_vm_stack(ec, NULL, 0);
3337
3338 // Avoid dangling pointers:
3339 ec->cfp = NULL;
3340}
3341
3342static void
3343th_init(rb_thread_t *th, VALUE self, rb_vm_t *vm)
3344{
3345 th->self = self;
3346
3347 rb_threadptr_root_fiber_setup(th);
3348
3349 /* All threads are blocking until a non-blocking fiber is scheduled */
3350 th->blocking = 1;
3351 th->scheduler = Qnil;
3352
3353 if (self == 0) {
3354 size_t size = vm->default_params.thread_vm_stack_size / sizeof(VALUE);
3355 rb_ec_initialize_vm_stack(th->ec, ALLOC_N(VALUE, size), size);
3356 }
3357 else {
3358 VM_ASSERT(th->ec->cfp == NULL);
3359 VM_ASSERT(th->ec->vm_stack == NULL);
3360 VM_ASSERT(th->ec->vm_stack_size == 0);
3361 }
3362
3363 th->status = THREAD_RUNNABLE;
3364 th->last_status = Qnil;
3365 th->top_wrapper = 0;
3366 th->top_self = vm->top_self; // 0 while self == 0
3367 th->value = Qundef;
3368
3369 th->ec->errinfo = Qnil;
3370 th->ec->root_svar = Qfalse;
3371 th->ec->local_storage_recursive_hash = Qnil;
3372 th->ec->local_storage_recursive_hash_for_trace = Qnil;
3373
3374 th->ec->storage = Qnil;
3375
3376#if OPT_CALL_THREADED_CODE
3377 th->retval = Qundef;
3378#endif
3379 th->name = Qnil;
3380 th->report_on_exception = vm->thread_report_on_exception;
3381 th->ext_config.ractor_safe = true;
3382
3383#if USE_RUBY_DEBUG_LOG
3384 static rb_atomic_t thread_serial = 0;
3385 th->serial = RUBY_ATOMIC_FETCH_ADD(thread_serial, 1);
3386#endif
3387}
3388
3389 VALUE
3390rb_thread_alloc(VALUE klass)
3391{
3392 VALUE self = thread_alloc(klass);
3393 rb_thread_t *target_th = rb_thread_ptr(self);
3394 target_th->ractor = GET_RACTOR();
3395 th_init(target_th, self, target_th->vm = GET_VM());
3396 return self;
3397}
3398
3399#define REWIND_CFP(expr) do { \
3400 rb_execution_context_t *ec__ = GET_EC(); \
3401 VALUE *const curr_sp = (ec__->cfp++)->sp; \
3402 VALUE *const saved_sp = ec__->cfp->sp; \
3403 ec__->cfp->sp = curr_sp; \
3404 expr; \
3405 (ec__->cfp--)->sp = saved_sp; \
3406} while (0)
3407
3408static VALUE
3409m_core_set_method_alias(VALUE self, VALUE cbase, VALUE sym1, VALUE sym2)
3410{
3411 REWIND_CFP({
3412 rb_alias(cbase, SYM2ID(sym1), SYM2ID(sym2));
3413 });
3414 return Qnil;
3415}
3416
3417static VALUE
3418m_core_set_variable_alias(VALUE self, VALUE sym1, VALUE sym2)
3419{
3420 REWIND_CFP({
3421 rb_alias_variable(SYM2ID(sym1), SYM2ID(sym2));
3422 });
3423 return Qnil;
3424}
3425
3426static VALUE
3427m_core_undef_method(VALUE self, VALUE cbase, VALUE sym)
3428{
3429 REWIND_CFP({
3430 ID mid = SYM2ID(sym);
3431 rb_undef(cbase, mid);
3432 rb_clear_method_cache(self, mid);
3433 });
3434 return Qnil;
3435}
3436
3437static VALUE
3438m_core_set_postexe(VALUE self)
3439{
3440 rb_set_end_proc(rb_call_end_proc, rb_block_proc());
3441 return Qnil;
3442}
3443
3444static VALUE core_hash_merge_kwd(VALUE hash, VALUE kw);
3445
3446static VALUE
3447core_hash_merge(VALUE hash, long argc, const VALUE *argv)
3448{
3449 Check_Type(hash, T_HASH);
3450 VM_ASSERT(argc % 2 == 0);
3451 rb_hash_bulk_insert(argc, argv, hash);
3452 return hash;
3453}
3454
3455static VALUE
3456m_core_hash_merge_ptr(int argc, VALUE *argv, VALUE recv)
3457{
3458 VALUE hash = argv[0];
3459
3460 REWIND_CFP(hash = core_hash_merge(hash, argc-1, argv+1));
3461
3462 return hash;
3463}
3464
3465static int
3466kwmerge_i(VALUE key, VALUE value, VALUE hash)
3467{
3468 rb_hash_aset(hash, key, value);
3469 return ST_CONTINUE;
3470}
3471
3472static VALUE
3473m_core_hash_merge_kwd(VALUE recv, VALUE hash, VALUE kw)
3474{
3475 REWIND_CFP(hash = core_hash_merge_kwd(hash, kw));
3476 return hash;
3477}
3478
3479static VALUE
3480m_core_make_shareable(VALUE recv, VALUE obj)
3481{
3482 return rb_ractor_make_shareable(obj);
3483}
3484
3485static VALUE
3486m_core_make_shareable_copy(VALUE recv, VALUE obj)
3487{
3488 return rb_ractor_make_shareable_copy(obj);
3489}
3490
3491static VALUE
3492m_core_ensure_shareable(VALUE recv, VALUE obj, VALUE name)
3493{
3494 return rb_ractor_ensure_shareable(obj, name);
3495}
3496
3497static VALUE
3498core_hash_merge_kwd(VALUE hash, VALUE kw)
3499{
3500 rb_hash_foreach(rb_to_hash_type(kw), kwmerge_i, hash);
3501 return hash;
3502}
3503
3504extern VALUE *rb_gc_stack_start;
3505extern size_t rb_gc_stack_maxsize;
3506
3507/* debug functions */
3508
3509/* :nodoc: */
3510static VALUE
3511sdr(VALUE self)
3512{
3513 rb_vm_bugreport(NULL);
3514 return Qnil;
3515}
3516
3517/* :nodoc: */
3518static VALUE
3519nsdr(VALUE self)
3520{
3521 VALUE ary = rb_ary_new();
3522#ifdef HAVE_BACKTRACE
3523#include <execinfo.h>
3524#define MAX_NATIVE_TRACE 1024
3525 static void *trace[MAX_NATIVE_TRACE];
3526 int n = (int)backtrace(trace, MAX_NATIVE_TRACE);
3527 char **syms = backtrace_symbols(trace, n);
3528 int i;
3529
3530 if (syms == 0) {
3531 rb_memerror();
3532 }
3533
3534 for (i=0; i<n; i++) {
3535 rb_ary_push(ary, rb_str_new2(syms[i]));
3536 }
3537 free(syms); /* OK */
3538#endif
3539 return ary;
3540}
3541
3542#if VM_COLLECT_USAGE_DETAILS
3543static VALUE usage_analysis_insn_start(VALUE self);
3544static VALUE usage_analysis_operand_start(VALUE self);
3545static VALUE usage_analysis_register_start(VALUE self);
3546static VALUE usage_analysis_insn_stop(VALUE self);
3547static VALUE usage_analysis_operand_stop(VALUE self);
3548static VALUE usage_analysis_register_stop(VALUE self);
3549static VALUE usage_analysis_insn_running(VALUE self);
3550static VALUE usage_analysis_operand_running(VALUE self);
3551static VALUE usage_analysis_register_running(VALUE self);
3552static VALUE usage_analysis_insn_clear(VALUE self);
3553static VALUE usage_analysis_operand_clear(VALUE self);
3554static VALUE usage_analysis_register_clear(VALUE self);
3555#endif
3556
3557static VALUE
3558f_raise(int c, VALUE *v, VALUE _)
3559{
3560 return rb_f_raise(c, v);
3561}
3562
3563static VALUE
3564f_proc(VALUE _)
3565{
3566 return rb_block_proc();
3567}
3568
3569static VALUE
3570f_lambda(VALUE _)
3571{
3572 return rb_block_lambda();
3573}
3574
3575static VALUE
3576f_sprintf(int c, const VALUE *v, VALUE _)
3577{
3578 return rb_f_sprintf(c, v);
3579}
3580
3581static VALUE
3582vm_mtbl(VALUE self, VALUE obj, VALUE sym)
3583{
3584 vm_mtbl_dump(CLASS_OF(obj), RTEST(sym) ? SYM2ID(sym) : 0);
3585 return Qnil;
3586}
3587
3588static VALUE
3589vm_mtbl2(VALUE self, VALUE obj, VALUE sym)
3590{
3591 vm_mtbl_dump(obj, RTEST(sym) ? SYM2ID(sym) : 0);
3592 return Qnil;
3593}
3594
3595/*
3596 * call-seq:
3597 * RubyVM.keep_script_lines -> true or false
3598 *
3599 * Return current +keep_script_lines+ status. Now it only returns
3600 * +true+ of +false+, but it can return other objects in future.
3601 *
3602 * Note that this is an API for ruby internal use, debugging,
3603 * and research. Do not use this for any other purpose.
3604 * The compatibility is not guaranteed.
3605 */
3606static VALUE
3607vm_keep_script_lines(VALUE self)
3608{
3609 return RBOOL(ruby_vm_keep_script_lines);
3610}
3611
3612/*
3613 * call-seq:
3614 * RubyVM.keep_script_lines = true / false
3615 *
3616 * It set +keep_script_lines+ flag. If the flag is set, all
3617 * loaded scripts are recorded in a interpreter process.
3618 *
3619 * Note that this is an API for ruby internal use, debugging,
3620 * and research. Do not use this for any other purpose.
3621 * The compatibility is not guaranteed.
3622 */
3623static VALUE
3624vm_keep_script_lines_set(VALUE self, VALUE flags)
3625{
3626 ruby_vm_keep_script_lines = RTEST(flags);
3627 return flags;
3628}
3629
3630void
3631Init_VM(void)
3632{
3633 VALUE opts;
3634 VALUE klass;
3635 VALUE fcore;
3636
3637 /*
3638 * Document-class: RubyVM
3639 *
3640 * The RubyVM module only exists on MRI. +RubyVM+ is not defined in
3641 * other Ruby implementations such as JRuby and TruffleRuby.
3642 *
3643 * The RubyVM module provides some access to MRI internals.
3644 * This module is for very limited purposes, such as debugging,
3645 * prototyping, and research. Normal users must not use it.
3646 * This module is not portable between Ruby implementations.
3647 */
3648 rb_cRubyVM = rb_define_class("RubyVM", rb_cObject);
3649 rb_undef_alloc_func(rb_cRubyVM);
3650 rb_undef_method(CLASS_OF(rb_cRubyVM), "new");
3651 rb_define_singleton_method(rb_cRubyVM, "stat", vm_stat, -1);
3652 rb_define_singleton_method(rb_cRubyVM, "keep_script_lines", vm_keep_script_lines, 0);
3653 rb_define_singleton_method(rb_cRubyVM, "keep_script_lines=", vm_keep_script_lines_set, 1);
3654
3655#if USE_DEBUG_COUNTER
3656 rb_define_singleton_method(rb_cRubyVM, "reset_debug_counters", rb_debug_counter_reset, 0);
3657 rb_define_singleton_method(rb_cRubyVM, "show_debug_counters", rb_debug_counter_show, 0);
3658#endif
3659
3660 /* FrozenCore (hidden) */
3661 fcore = rb_class_new(rb_cBasicObject);
3662 rb_set_class_path(fcore, rb_cRubyVM, "FrozenCore");
3663 RBASIC(fcore)->flags = T_ICLASS;
3664 klass = rb_singleton_class(fcore);
3665 rb_define_method_id(klass, id_core_set_method_alias, m_core_set_method_alias, 3);
3666 rb_define_method_id(klass, id_core_set_variable_alias, m_core_set_variable_alias, 2);
3667 rb_define_method_id(klass, id_core_undef_method, m_core_undef_method, 2);
3668 rb_define_method_id(klass, id_core_set_postexe, m_core_set_postexe, 0);
3669 rb_define_method_id(klass, id_core_hash_merge_ptr, m_core_hash_merge_ptr, -1);
3670 rb_define_method_id(klass, id_core_hash_merge_kwd, m_core_hash_merge_kwd, 2);
3671 rb_define_method_id(klass, id_core_raise, f_raise, -1);
3672 rb_define_method_id(klass, id_core_sprintf, f_sprintf, -1);
3673 rb_define_method_id(klass, idProc, f_proc, 0);
3674 rb_define_method_id(klass, idLambda, f_lambda, 0);
3675 rb_define_method(klass, "make_shareable", m_core_make_shareable, 1);
3676 rb_define_method(klass, "make_shareable_copy", m_core_make_shareable_copy, 1);
3677 rb_define_method(klass, "ensure_shareable", m_core_ensure_shareable, 2);
3678 rb_obj_freeze(fcore);
3679 RBASIC_CLEAR_CLASS(klass);
3680 rb_obj_freeze(klass);
3681 rb_gc_register_mark_object(fcore);
3682 rb_mRubyVMFrozenCore = fcore;
3683
3684 /*
3685 * Document-class: Thread
3686 *
3687 * Threads are the Ruby implementation for a concurrent programming model.
3688 *
3689 * Programs that require multiple threads of execution are a perfect
3690 * candidate for Ruby's Thread class.
3691 *
3692 * For example, we can create a new thread separate from the main thread's
3693 * execution using ::new.
3694 *
3695 * thr = Thread.new { puts "What's the big deal" }
3696 *
3697 * Then we are able to pause the execution of the main thread and allow
3698 * our new thread to finish, using #join:
3699 *
3700 * thr.join #=> "What's the big deal"
3701 *
3702 * If we don't call +thr.join+ before the main thread terminates, then all
3703 * other threads including +thr+ will be killed.
3704 *
3705 * Alternatively, you can use an array for handling multiple threads at
3706 * once, like in the following example:
3707 *
3708 * threads = []
3709 * threads << Thread.new { puts "What's the big deal" }
3710 * threads << Thread.new { 3.times { puts "Threads are fun!" } }
3711 *
3712 * After creating a few threads we wait for them all to finish
3713 * consecutively.
3714 *
3715 * threads.each { |thr| thr.join }
3716 *
3717 * To retrieve the last value of a thread, use #value
3718 *
3719 * thr = Thread.new { sleep 1; "Useful value" }
3720 * thr.value #=> "Useful value"
3721 *
3722 * === Thread initialization
3723 *
3724 * In order to create new threads, Ruby provides ::new, ::start, and
3725 * ::fork. A block must be provided with each of these methods, otherwise
3726 * a ThreadError will be raised.
3727 *
3728 * When subclassing the Thread class, the +initialize+ method of your
3729 * subclass will be ignored by ::start and ::fork. Otherwise, be sure to
3730 * call super in your +initialize+ method.
3731 *
3732 * === Thread termination
3733 *
3734 * For terminating threads, Ruby provides a variety of ways to do this.
3735 *
3736 * The class method ::kill, is meant to exit a given thread:
3737 *
3738 * thr = Thread.new { sleep }
3739 * Thread.kill(thr) # sends exit() to thr
3740 *
3741 * Alternatively, you can use the instance method #exit, or any of its
3742 * aliases #kill or #terminate.
3743 *
3744 * thr.exit
3745 *
3746 * === Thread status
3747 *
3748 * Ruby provides a few instance methods for querying the state of a given
3749 * thread. To get a string with the current thread's state use #status
3750 *
3751 * thr = Thread.new { sleep }
3752 * thr.status # => "sleep"
3753 * thr.exit
3754 * thr.status # => false
3755 *
3756 * You can also use #alive? to tell if the thread is running or sleeping,
3757 * and #stop? if the thread is dead or sleeping.
3758 *
3759 * === Thread variables and scope
3760 *
3761 * Since threads are created with blocks, the same rules apply to other
3762 * Ruby blocks for variable scope. Any local variables created within this
3763 * block are accessible to only this thread.
3764 *
3765 * ==== Fiber-local vs. Thread-local
3766 *
3767 * Each fiber has its own bucket for Thread#[] storage. When you set a
3768 * new fiber-local it is only accessible within this Fiber. To illustrate:
3769 *
3770 * Thread.new {
3771 * Thread.current[:foo] = "bar"
3772 * Fiber.new {
3773 * p Thread.current[:foo] # => nil
3774 * }.resume
3775 * }.join
3776 *
3777 * This example uses #[] for getting and #[]= for setting fiber-locals,
3778 * you can also use #keys to list the fiber-locals for a given
3779 * thread and #key? to check if a fiber-local exists.
3780 *
3781 * When it comes to thread-locals, they are accessible within the entire
3782 * scope of the thread. Given the following example:
3783 *
3784 * Thread.new{
3785 * Thread.current.thread_variable_set(:foo, 1)
3786 * p Thread.current.thread_variable_get(:foo) # => 1
3787 * Fiber.new{
3788 * Thread.current.thread_variable_set(:foo, 2)
3789 * p Thread.current.thread_variable_get(:foo) # => 2
3790 * }.resume
3791 * p Thread.current.thread_variable_get(:foo) # => 2
3792 * }.join
3793 *
3794 * You can see that the thread-local +:foo+ carried over into the fiber
3795 * and was changed to +2+ by the end of the thread.
3796 *
3797 * This example makes use of #thread_variable_set to create new
3798 * thread-locals, and #thread_variable_get to reference them.
3799 *
3800 * There is also #thread_variables to list all thread-locals, and
3801 * #thread_variable? to check if a given thread-local exists.
3802 *
3803 * === Exception handling
3804 *
3805 * When an unhandled exception is raised inside a thread, it will
3806 * terminate. By default, this exception will not propagate to other
3807 * threads. The exception is stored and when another thread calls #value
3808 * or #join, the exception will be re-raised in that thread.
3809 *
3810 * t = Thread.new{ raise 'something went wrong' }
3811 * t.value #=> RuntimeError: something went wrong
3812 *
3813 * An exception can be raised from outside the thread using the
3814 * Thread#raise instance method, which takes the same parameters as
3815 * Kernel#raise.
3816 *
3817 * Setting Thread.abort_on_exception = true, Thread#abort_on_exception =
3818 * true, or $DEBUG = true will cause a subsequent unhandled exception
3819 * raised in a thread to be automatically re-raised in the main thread.
3820 *
3821 * With the addition of the class method ::handle_interrupt, you can now
3822 * handle exceptions asynchronously with threads.
3823 *
3824 * === Scheduling
3825 *
3826 * Ruby provides a few ways to support scheduling threads in your program.
3827 *
3828 * The first way is by using the class method ::stop, to put the current
3829 * running thread to sleep and schedule the execution of another thread.
3830 *
3831 * Once a thread is asleep, you can use the instance method #wakeup to
3832 * mark your thread as eligible for scheduling.
3833 *
3834 * You can also try ::pass, which attempts to pass execution to another
3835 * thread but is dependent on the OS whether a running thread will switch
3836 * or not. The same goes for #priority, which lets you hint to the thread
3837 * scheduler which threads you want to take precedence when passing
3838 * execution. This method is also dependent on the OS and may be ignored
3839 * on some platforms.
3840 *
3841 */
3842 rb_cThread = rb_define_class("Thread", rb_cObject);
3843 rb_undef_alloc_func(rb_cThread);
3844
3845#if VM_COLLECT_USAGE_DETAILS
3846 /* ::RubyVM::USAGE_ANALYSIS_* */
3847#define define_usage_analysis_hash(name) /* shut up rdoc -C */ \
3848 rb_define_const(rb_cRubyVM, "USAGE_ANALYSIS_" #name, rb_hash_new())
3849 define_usage_analysis_hash(INSN);
3850 define_usage_analysis_hash(REGS);
3851 define_usage_analysis_hash(INSN_BIGRAM);
3852
3853 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_INSN_START", usage_analysis_insn_start, 0);
3854 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_OPERAND_START", usage_analysis_operand_start, 0);
3855 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_REGISTER_START", usage_analysis_register_start, 0);
3856 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_INSN_STOP", usage_analysis_insn_stop, 0);
3857 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_OPERAND_STOP", usage_analysis_operand_stop, 0);
3858 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_REGISTER_STOP", usage_analysis_register_stop, 0);
3859 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_INSN_RUNNING", usage_analysis_insn_running, 0);
3860 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_OPERAND_RUNNING", usage_analysis_operand_running, 0);
3861 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_REGISTER_RUNNING", usage_analysis_register_running, 0);
3862 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_INSN_CLEAR", usage_analysis_insn_clear, 0);
3863 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_OPERAND_CLEAR", usage_analysis_operand_clear, 0);
3864 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_REGISTER_CLEAR", usage_analysis_register_clear, 0);
3865#endif
3866
3867 /* ::RubyVM::OPTS
3868 * An Array of VM build options.
3869 * This constant is MRI specific.
3870 */
3871 rb_define_const(rb_cRubyVM, "OPTS", opts = rb_ary_new());
3872
3873#if OPT_DIRECT_THREADED_CODE
3874 rb_ary_push(opts, rb_str_new2("direct threaded code"));
3875#elif OPT_TOKEN_THREADED_CODE
3876 rb_ary_push(opts, rb_str_new2("token threaded code"));
3877#elif OPT_CALL_THREADED_CODE
3878 rb_ary_push(opts, rb_str_new2("call threaded code"));
3879#endif
3880
3881#if OPT_STACK_CACHING
3882 rb_ary_push(opts, rb_str_new2("stack caching"));
3883#endif
3884#if OPT_OPERANDS_UNIFICATION
3885 rb_ary_push(opts, rb_str_new2("operands unification"));
3886#endif
3887#if OPT_INSTRUCTIONS_UNIFICATION
3888 rb_ary_push(opts, rb_str_new2("instructions unification"));
3889#endif
3890#if OPT_INLINE_METHOD_CACHE
3891 rb_ary_push(opts, rb_str_new2("inline method cache"));
3892#endif
3893#if OPT_BLOCKINLINING
3894 rb_ary_push(opts, rb_str_new2("block inlining"));
3895#endif
3896
3897 /* ::RubyVM::INSTRUCTION_NAMES
3898 * A list of bytecode instruction names in MRI.
3899 * This constant is MRI specific.
3900 */
3901 rb_define_const(rb_cRubyVM, "INSTRUCTION_NAMES", rb_insns_name_array());
3902
3903 /* ::RubyVM::DEFAULT_PARAMS
3904 * This constant exposes the VM's default parameters.
3905 * Note that changing these values does not affect VM execution.
3906 * Specification is not stable and you should not depend on this value.
3907 * Of course, this constant is MRI specific.
3908 */
3909 rb_define_const(rb_cRubyVM, "DEFAULT_PARAMS", vm_default_params());
3910
3911 /* debug functions ::RubyVM::SDR(), ::RubyVM::NSDR() */
3912#if VMDEBUG
3913 rb_define_singleton_method(rb_cRubyVM, "SDR", sdr, 0);
3914 rb_define_singleton_method(rb_cRubyVM, "NSDR", nsdr, 0);
3915 rb_define_singleton_method(rb_cRubyVM, "mtbl", vm_mtbl, 2);
3916 rb_define_singleton_method(rb_cRubyVM, "mtbl2", vm_mtbl2, 2);
3917#else
3918 (void)sdr;
3919 (void)nsdr;
3920 (void)vm_mtbl;
3921 (void)vm_mtbl2;
3922#endif
3923
3924 /* VM bootstrap: phase 2 */
3925 {
3926 rb_vm_t *vm = ruby_current_vm_ptr;
3927 rb_thread_t *th = GET_THREAD();
3928 VALUE filename = rb_fstring_lit("<main>");
3929 const rb_iseq_t *iseq = rb_iseq_new(0, filename, filename, Qnil, 0, ISEQ_TYPE_TOP);
3930
3931 // Ractor setup
3932 rb_ractor_main_setup(vm, th->ractor, th);
3933
3934 /* create vm object */
3935 vm->self = TypedData_Wrap_Struct(rb_cRubyVM, &vm_data_type, vm);
3936
3937 /* create main thread */
3938 th->self = TypedData_Wrap_Struct(rb_cThread, &thread_data_type, th);
3939 vm->ractor.main_thread = th;
3940 vm->ractor.main_ractor = th->ractor;
3941 th->vm = vm;
3942 th->top_wrapper = 0;
3943 th->top_self = rb_vm_top_self();
3944
3945 rb_gc_register_mark_object((VALUE)iseq);
3946 th->ec->cfp->iseq = iseq;
3947 th->ec->cfp->pc = ISEQ_BODY(iseq)->iseq_encoded;
3948 th->ec->cfp->self = th->top_self;
3949
3950 VM_ENV_FLAGS_UNSET(th->ec->cfp->ep, VM_FRAME_FLAG_CFRAME);
3951 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));
3952
3953 /*
3954 * The Binding of the top level scope
3955 */
3956 rb_define_global_const("TOPLEVEL_BINDING", rb_binding_new());
3957
3958 rb_objspace_gc_enable(vm->objspace);
3959 }
3960 vm_init_redefined_flag();
3961
3962 rb_block_param_proxy = rb_obj_alloc(rb_cObject);
3963 rb_add_method_optimized(rb_singleton_class(rb_block_param_proxy), idCall,
3964 OPTIMIZED_METHOD_TYPE_BLOCK_CALL, 0, METHOD_VISI_PUBLIC);
3965 rb_obj_freeze(rb_block_param_proxy);
3966 rb_gc_register_mark_object(rb_block_param_proxy);
3967
3968 /* vm_backtrace.c */
3969 Init_vm_backtrace();
3970}
3971
3972void
3973rb_vm_set_progname(VALUE filename)
3974{
3975 rb_thread_t *th = GET_VM()->ractor.main_thread;
3976 rb_control_frame_t *cfp = (void *)(th->ec->vm_stack + th->ec->vm_stack_size);
3977 --cfp;
3978
3979 rb_iseq_pathobj_set(cfp->iseq, rb_str_dup(filename), rb_iseq_realpath(cfp->iseq));
3980}
3981
3982extern const struct st_hash_type rb_fstring_hash_type;
3983
3984void
3985Init_BareVM(void)
3986{
3987 /* VM bootstrap: phase 1 */
3988 rb_vm_t * vm = ruby_mimmalloc(sizeof(*vm));
3989 rb_thread_t * th = ruby_mimmalloc(sizeof(*th));
3990 if (!vm || !th) {
3991 fputs("[FATAL] failed to allocate memory\n", stderr);
3992 exit(EXIT_FAILURE);
3993 }
3994
3995 // setup the VM
3996 MEMZERO(th, rb_thread_t, 1);
3997 vm_init2(vm);
3998
3999 vm->objspace = rb_objspace_alloc();
4000 ruby_current_vm_ptr = vm;
4001 vm->negative_cme_table = rb_id_table_create(16);
4002 vm->overloaded_cme_table = st_init_numtable();
4003 vm->constant_cache = rb_id_table_create(0);
4004
4005 // setup main thread
4006 th->nt = ZALLOC(struct rb_native_thread);
4007 th->vm = vm;
4008 th->ractor = vm->ractor.main_ractor = rb_ractor_main_alloc();
4009 Init_native_thread(th);
4010 th_init(th, 0, vm);
4011
4012 rb_ractor_set_current_ec(th->ractor, th->ec);
4013 ruby_thread_init_stack(th);
4014
4015 // setup ractor system
4016 rb_native_mutex_initialize(&vm->ractor.sync.lock);
4017 rb_native_cond_initialize(&vm->ractor.sync.barrier_cond);
4018 rb_native_cond_initialize(&vm->ractor.sync.terminate_cond);
4019}
4020
4021#ifndef _WIN32
4022#include <unistd.h>
4023#include <sys/mman.h>
4024#endif
4025
4026void
4027Init_vm_objects(void)
4028{
4029 rb_vm_t *vm = GET_VM();
4030
4031 vm->defined_module_hash = st_init_numtable();
4032
4033 /* initialize mark object array, hash */
4034 vm->mark_object_ary = rb_ary_hidden_new(128);
4035 vm->loading_table = st_init_strtable();
4036 vm->frozen_strings = st_init_table_with_size(&rb_fstring_hash_type, 10000);
4037
4038#ifdef HAVE_MMAP
4039 vm->shape_list = (rb_shape_t *)mmap(NULL, rb_size_mul_or_raise(SHAPE_BITMAP_SIZE * 32, sizeof(rb_shape_t), rb_eRuntimeError),
4040 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4041 if (vm->shape_list == MAP_FAILED) {
4042 vm->shape_list = 0;
4043 }
4044#else
4045 vm->shape_list = xcalloc(SHAPE_BITMAP_SIZE * 32, sizeof(rb_shape_t));
4046#endif
4047
4048 if (!vm->shape_list) {
4049 rb_memerror();
4050 }
4051}
4052
4053/* Stub for builtin function when not building YJIT units*/
4054#if !USE_YJIT
4055void Init_builtin_yjit(void) {}
4056#endif
4057
4058/* top self */
4059
4060static VALUE
4061main_to_s(VALUE obj)
4062{
4063 return rb_str_new2("main");
4064}
4065
4066 VALUE
4067rb_vm_top_self(void)
4068{
4069 return GET_VM()->top_self;
4070}
4071
4072void
4073Init_top_self(void)
4074{
4075 rb_vm_t *vm = GET_VM();
4076
4077 vm->top_self = rb_obj_alloc(rb_cObject);
4078 rb_define_singleton_method(rb_vm_top_self(), "to_s", main_to_s, 0);
4079 rb_define_alias(rb_singleton_class(rb_vm_top_self()), "inspect", "to_s");
4080}
4081
4082 VALUE *
4083rb_ruby_verbose_ptr(void)
4084{
4085 rb_ractor_t *cr = GET_RACTOR();
4086 return &cr->verbose;
4087}
4088
4089 VALUE *
4090rb_ruby_debug_ptr(void)
4091{
4092 rb_ractor_t *cr = GET_RACTOR();
4093 return &cr->debug;
4094}
4095
4096/* iseq.c */
4097 VALUE rb_insn_operand_intern(const rb_iseq_t *iseq,
4098 VALUE insn, int op_no, VALUE op,
4099 int len, size_t pos, VALUE *pnop, VALUE child);
4100
4101 st_table *
4102rb_vm_fstring_table(void)
4103{
4104 return GET_VM()->frozen_strings;
4105}
4106
4107#if VM_COLLECT_USAGE_DETAILS
4108
4109#define HASH_ASET(h, k, v) rb_hash_aset((h), (st_data_t)(k), (st_data_t)(v))
4110
4111/* uh = {
4112 * insn(Fixnum) => ihash(Hash)
4113 * }
4114 * ihash = {
4115 * -1(Fixnum) => count, # insn usage
4116 * 0(Fixnum) => ophash, # operand usage
4117 * }
4118 * ophash = {
4119 * val(interned string) => count(Fixnum)
4120 * }
4121 */
4122static void
4123vm_analysis_insn(int insn)
4124{
4125 ID usage_hash;
4126 ID bigram_hash;
4127 static int prev_insn = -1;
4128
4129 VALUE uh;
4130 VALUE ihash;
4131 VALUE cv;
4132
4133 CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
4134 CONST_ID(bigram_hash, "USAGE_ANALYSIS_INSN_BIGRAM");
4135 uh = rb_const_get(rb_cRubyVM, usage_hash);
4136 if (NIL_P(ihash = rb_hash_aref(uh, INT2FIX(insn)))) {
4137 ihash = rb_hash_new();
4138 HASH_ASET(uh, INT2FIX(insn), ihash);
4139 }
4140 if (NIL_P(cv = rb_hash_aref(ihash, INT2FIX(-1)))) {
4141 cv = INT2FIX(0);
4142 }
4143 HASH_ASET(ihash, INT2FIX(-1), INT2FIX(FIX2INT(cv) + 1));
4144
4145 /* calc bigram */
4146 if (prev_insn != -1) {
4147 VALUE bi;
4148 VALUE ary[2];
4149 VALUE cv;
4150
4151 ary[0] = INT2FIX(prev_insn);
4152 ary[1] = INT2FIX(insn);
4153 bi = rb_ary_new4(2, &ary[0]);
4154
4155 uh = rb_const_get(rb_cRubyVM, bigram_hash);
4156 if (NIL_P(cv = rb_hash_aref(uh, bi))) {
4157 cv = INT2FIX(0);
4158 }
4159 HASH_ASET(uh, bi, INT2FIX(FIX2INT(cv) + 1));
4160 }
4161 prev_insn = insn;
4162}
4163
4164static void
4165vm_analysis_operand(int insn, int n, VALUE op)
4166{
4167 ID usage_hash;
4168
4169 VALUE uh;
4170 VALUE ihash;
4171 VALUE ophash;
4172 VALUE valstr;
4173 VALUE cv;
4174
4175 CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
4176
4177 uh = rb_const_get(rb_cRubyVM, usage_hash);
4178 if (NIL_P(ihash = rb_hash_aref(uh, INT2FIX(insn)))) {
4179 ihash = rb_hash_new();
4180 HASH_ASET(uh, INT2FIX(insn), ihash);
4181 }
4182 if (NIL_P(ophash = rb_hash_aref(ihash, INT2FIX(n)))) {
4183 ophash = rb_hash_new();
4184 HASH_ASET(ihash, INT2FIX(n), ophash);
4185 }
4186 /* intern */
4187 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4188
4189 /* set count */
4190 if (NIL_P(cv = rb_hash_aref(ophash, valstr))) {
4191 cv = INT2FIX(0);
4192 }
4193 HASH_ASET(ophash, valstr, INT2FIX(FIX2INT(cv) + 1));
4194}
4195
4196static void
4197vm_analysis_register(int reg, int isset)
4198{
4199 ID usage_hash;
4200 VALUE uh;
4201 VALUE valstr;
4202 static const char regstrs[][5] = {
4203 "pc", /* 0 */
4204 "sp", /* 1 */
4205 "ep", /* 2 */
4206 "cfp", /* 3 */
4207 "self", /* 4 */
4208 "iseq", /* 5 */
4209 };
4210 static const char getsetstr[][4] = {
4211 "get",
4212 "set",
4213 };
4214 static VALUE syms[sizeof(regstrs) / sizeof(regstrs[0])][2];
4215
4216 VALUE cv;
4217
4218 CONST_ID(usage_hash, "USAGE_ANALYSIS_REGS");
4219 if (syms[0] == 0) {
4220 char buff[0x10];
4221 int i;
4222
4223 for (i = 0; i < (int)(sizeof(regstrs) / sizeof(regstrs[0])); i++) {
4224 int j;
4225 for (j = 0; j < 2; j++) {
4226 snprintf(buff, 0x10, "%d %s %-4s", i, getsetstr[j], regstrs[i]);
4227 syms[i][j] = ID2SYM(rb_intern(buff));
4228 }
4229 }
4230 }
4231 valstr = syms[reg][isset];
4232
4233 uh = rb_const_get(rb_cRubyVM, usage_hash);
4234 if (NIL_P(cv = rb_hash_aref(uh, valstr))) {
4235 cv = INT2FIX(0);
4236 }
4237 HASH_ASET(uh, valstr, INT2FIX(FIX2INT(cv) + 1));
4238}
4239
4240#undef HASH_ASET
4241
4242static void (*ruby_vm_collect_usage_func_insn)(int insn) = NULL;
4243static void (*ruby_vm_collect_usage_func_operand)(int insn, int n, VALUE op) = NULL;
4244static void (*ruby_vm_collect_usage_func_register)(int reg, int isset) = NULL;
4245
4246/* :nodoc: */
4247static VALUE
4248usage_analysis_insn_start(VALUE self)
4249{
4250 ruby_vm_collect_usage_func_insn = vm_analysis_insn;
4251 return Qnil;
4252}
4253
4254/* :nodoc: */
4255static VALUE
4256usage_analysis_operand_start(VALUE self)
4257{
4258 ruby_vm_collect_usage_func_operand = vm_analysis_operand;
4259 return Qnil;
4260}
4261
4262/* :nodoc: */
4263static VALUE
4264usage_analysis_register_start(VALUE self)
4265{
4266 ruby_vm_collect_usage_func_register = vm_analysis_register;
4267 return Qnil;
4268}
4269
4270/* :nodoc: */
4271static VALUE
4272usage_analysis_insn_stop(VALUE self)
4273{
4274 ruby_vm_collect_usage_func_insn = 0;
4275 return Qnil;
4276}
4277
4278/* :nodoc: */
4279static VALUE
4280usage_analysis_operand_stop(VALUE self)
4281{
4282 ruby_vm_collect_usage_func_operand = 0;
4283 return Qnil;
4284}
4285
4286/* :nodoc: */
4287static VALUE
4288usage_analysis_register_stop(VALUE self)
4289{
4290 ruby_vm_collect_usage_func_register = 0;
4291 return Qnil;
4292}
4293
4294/* :nodoc: */
4295static VALUE
4296usage_analysis_insn_running(VALUE self)
4297{
4298 return RBOOL(ruby_vm_collect_usage_func_insn != 0);
4299}
4300
4301/* :nodoc: */
4302static VALUE
4303usage_analysis_operand_running(VALUE self)
4304{
4305 return RBOOL(ruby_vm_collect_usage_func_operand != 0);
4306}
4307
4308/* :nodoc: */
4309static VALUE
4310usage_analysis_register_running(VALUE self)
4311{
4312 return RBOOL(ruby_vm_collect_usage_func_register != 0);
4313}
4314
4315static VALUE
4316usage_analysis_clear(VALUE self, ID usage_hash)
4317{
4318 VALUE uh;
4319 uh = rb_const_get(self, usage_hash);
4320 rb_hash_clear(uh);
4321
4322 return Qtrue;
4323}
4324
4325
4326/* :nodoc: */
4327static VALUE
4328usage_analysis_insn_clear(VALUE self)
4329{
4330 ID usage_hash;
4331 ID bigram_hash;
4332
4333 CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
4334 CONST_ID(bigram_hash, "USAGE_ANALYSIS_INSN_BIGRAM");
4335 usage_analysis_clear(rb_cRubyVM, usage_hash);
4336 return usage_analysis_clear(rb_cRubyVM, bigram_hash);
4337}
4338
4339/* :nodoc: */
4340static VALUE
4341usage_analysis_operand_clear(VALUE self)
4342{
4343 ID usage_hash;
4344
4345 CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
4346 return usage_analysis_clear(self, usage_hash);
4347}
4348
4349/* :nodoc: */
4350static VALUE
4351usage_analysis_register_clear(VALUE self)
4352{
4353 ID usage_hash;
4354
4355 CONST_ID(usage_hash, "USAGE_ANALYSIS_REGS");
4356 return usage_analysis_clear(self, usage_hash);
4357}
4358
4359#else
4360
4361MAYBE_UNUSED(static void (*ruby_vm_collect_usage_func_insn)(int insn)) = 0;
4362MAYBE_UNUSED(static void (*ruby_vm_collect_usage_func_operand)(int insn, int n, VALUE op)) = 0;
4363MAYBE_UNUSED(static void (*ruby_vm_collect_usage_func_register)(int reg, int isset)) = 0;
4364
4365#endif
4366
4367#if VM_COLLECT_USAGE_DETAILS
4368/* @param insn instruction number */
4369static void
4370vm_collect_usage_insn(int insn)
4371{
4372 if (RUBY_DTRACE_INSN_ENABLED()) {
4373 RUBY_DTRACE_INSN(rb_insns_name(insn));
4374 }
4375 if (ruby_vm_collect_usage_func_insn)
4376 (*ruby_vm_collect_usage_func_insn)(insn);
4377}
4378
4379/* @param insn instruction number
4380 * @param n n-th operand
4381 * @param op operand value
4382 */
4383static void
4384vm_collect_usage_operand(int insn, int n, VALUE op)
4385{
4386 if (RUBY_DTRACE_INSN_OPERAND_ENABLED()) {
4387 VALUE valstr;
4388
4389 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4390
4391 RUBY_DTRACE_INSN_OPERAND(RSTRING_PTR(valstr), rb_insns_name(insn));
4392 RB_GC_GUARD(valstr);
4393 }
4394 if (ruby_vm_collect_usage_func_operand)
4395 (*ruby_vm_collect_usage_func_operand)(insn, n, op);
4396}
4397
4398/* @param reg register id. see code of vm_analysis_register() */
4399/* @param isset 0: read, 1: write */
4400static void
4401vm_collect_usage_register(int reg, int isset)
4402{
4403 if (ruby_vm_collect_usage_func_register)
4404 (*ruby_vm_collect_usage_func_register)(reg, isset);
4405}
4406#endif
4407
4408MJIT_FUNC_EXPORTED const struct rb_callcache *
4409rb_vm_empty_cc(void)
4410{
4411 return &vm_empty_cc;
4412}
4413
4414MJIT_FUNC_EXPORTED const struct rb_callcache *
4415rb_vm_empty_cc_for_super(void)
4416{
4417 return &vm_empty_cc_for_super;
4418}
4419
4420#endif /* #ifndef MJIT_HEADER */
4421
4422#include "vm_call_iseq_optimized.inc" /* required from vm_insnhelper.c */
RUBY_ASSERT_MESG
#define RUBY_ASSERT_MESG(expr, mesg)
Asserts that the expression is truthy.
Definition assert.h:159
RUBY_ASSERT
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:177
rb_atomic_t
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
RUBY_ATOMIC_FETCH_ADD
#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...
Definition atomic.h:91
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:670
rb_define_method_id
#define rb_define_method_id(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:673
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition cxxanyargs.hpp:685
RUBY_EVENT_END
#define RUBY_EVENT_END
Encountered an end of a class clause.
Definition event.h:36
RUBY_EVENT_B_RETURN
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
Definition event.h:52
RUBY_EVENT_RETURN
#define RUBY_EVENT_RETURN
Encountered a return statement.
Definition event.h:38
RUBY_EVENT_C_RETURN
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition event.h:40
rb_event_flag_t
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:103
RUBY_FL_SHAREABLE
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:298
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:923
rb_class_new
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition class.c:325
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2241
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2289
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2113
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
FL_SINGLETON
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
NUM2ULONG
#define NUM2ULONG
Old name of RB_NUM2ULONG.
Definition long.h:52
ALLOCV
#define ALLOCV
Old name of RB_ALLOCV.
Definition memory.h:398
ALLOC
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:394
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
T_IMEMO
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
OBJ_FREEZE
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:143
ULONG2NUM
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
SYM2ID
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
ZALLOC
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:396
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
rb_ary_new4
#define rb_ary_new4
Old name of rb_ary_new_from_values.
Definition array.h:653
SIZET2NUM
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
rb_exc_new2
#define rb_exc_new2
Old name of rb_exc_new_cstr.
Definition error.h:37
FIX2INT
#define FIX2INT
Old name of RB_FIX2INT.
Definition int.h:41
T_MODULE
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
ZALLOC_N
#define ZALLOC_N
Old name of RB_ZALLOC_N.
Definition memory.h:395
ASSUME
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
T_ICLASS
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
T_HASH
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
ALLOC_N
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:393
rb_exc_new3
#define rb_exc_new3
Old name of rb_exc_new_str.
Definition error.h:38
ULL2NUM
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
NUM2ULL
#define NUM2ULL
Old name of RB_NUM2ULL.
Definition long_long.h:35
T_CLASS
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
BUILTIN_TYPE
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
FL_TEST
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:139
xcalloc
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
FL_USHIFT
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
Definition fl_type.h:70
CONST_ID
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
FL_SET_RAW
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:138
ALLOCV_END
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:400
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
rb_eLocalJumpError
VALUE rb_eLocalJumpError
LocalJumpError exception.
Definition eval.c:49
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition error.c:3150
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:688
rb_typeddata_is_kind_of
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
Definition error.c:1041
rb_bug
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition error.c:794
rb_iter_break
void rb_iter_break(void)
Breaks from a block.
Definition vm.c:1901
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1091
rb_iter_break_value
void rb_iter_break_value(VALUE val)
Identical to rb_iter_break(), except it additionally takes the "value" of this breakage.
Definition vm.c:1907
rb_eRuntimeError
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1089
rb_ruby_verbose_ptr
VALUE * rb_ruby_verbose_ptr(void)
This is an implementation detail of ruby_verbose.
Definition vm.c:4083
rb_exc_new_str
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.
Definition error.c:1142
rb_eArgError
VALUE rb_eArgError
ArgumentError exception.
Definition error.c:1092
rb_ruby_debug_ptr
VALUE * rb_ruby_debug_ptr(void)
This is an implementation detail of ruby_debug.
Definition vm.c:4090
rb_eSysStackError
VALUE rb_eSysStackError
SystemStackError exception.
Definition eval.c:50
rb_cTime
VALUE rb_cTime
Time class.
Definition time.c:672
rb_cArray
VALUE rb_cArray
Array class.
Definition array.c:40
rb_obj_alloc
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:1940
rb_cInteger
VALUE rb_cInteger
Module class.
Definition numeric.c:192
rb_cNilClass
VALUE rb_cNilClass
NilClass class.
Definition object.c:57
rb_cBinding
VALUE rb_cBinding
Binding class.
Definition proc.c:51
rb_cRegexp
VALUE rb_cRegexp
Regexp class.
Definition re.c:2544
rb_cHash
VALUE rb_cHash
Hash class.
Definition hash.c:94
rb_cFalseClass
VALUE rb_cFalseClass
FalseClass class.
Definition object.c:59
rb_obj_class
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:191
rb_cSymbol
VALUE rb_cSymbol
Sumbol class.
Definition string.c:80
rb_cBasicObject
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:50
rb_cThread
VALUE rb_cThread
Thread class.
Definition vm.c:466
rb_obj_freeze
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1183
rb_cFloat
VALUE rb_cFloat
Float class.
Definition numeric.c:191
rb_cProc
VALUE rb_cProc
Proc class.
Definition proc.c:52
rb_cTrueClass
VALUE rb_cTrueClass
TrueClass class.
Definition object.c:58
rb_cString
VALUE rb_cString
String class.
Definition string.c:79
RB_OBJ_WRITTEN
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition rgengc.h:232
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition rgengc.h:220
rb_undef
void rb_undef(VALUE mod, ID mid)
Inserts a method entry that hides previous method definition of the given name.
Definition vm_method.c:1757
rb_check_arity
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:280
rb_backref_get
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
Definition vm.c:1662
rb_lastline_set
void rb_lastline_set(VALUE str)
Updates $_.
Definition vm.c:1680
rb_lastline_get
VALUE rb_lastline_get(void)
Queries the last line, or the $_.
Definition vm.c:1674
rb_backref_set
void rb_backref_set(VALUE md)
Updates $~.
Definition vm.c:1668
rb_block_proc
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:848
rb_block_lambda
VALUE rb_block_lambda(void)
Identical to rb_proc_new(), except it returns a lambda.
Definition proc.c:867
rb_binding_new
VALUE rb_binding_new(void)
Snapshots the current execution context and turn it into an instance of rb_cBinding.
Definition proc.c:385
rb_str_append
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3353
rb_str_dup
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:1834
rb_str_cat_cstr
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1656
rb_const_get
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:2896
rb_attr_get
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
Definition variable.c:1226
rb_set_class_path
void rb_set_class_path(VALUE klass, VALUE space, const char *name)
Names a class.
Definition variable.c:251
rb_alias_variable
void rb_alias_variable(ID dst, ID src)
Aliases a global variable.
Definition variable.c:859
rb_class_path
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:188
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1142
rb_sourcefile
const char * rb_sourcefile(void)
Resembles __FILE__.
Definition vm.c:1688
rb_alias
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2140
rb_frame_method_id_and_class
int rb_frame_method_id_and_class(ID *idp, VALUE *klassp)
Resembles __method__.
Definition vm.c:2660
rb_sourceline
int rb_sourceline(void)
Resembles __LINE__.
Definition vm.c:1702
rb_intern
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition symbol.c:796
rb_sym2str
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
Definition symbol.c:943
rb_id2name
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
Definition symbol.c:960
rb_define_global_const
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
Definition variable.c:3452
rb_define_const
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
Definition variable.c:3440
rb_iv_set
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
Assigns to an instance variable.
Definition variable.c:3947
rb_ractor_make_shareable_copy
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...
Definition ractor.c:2531
rb_ractor_shareable_p
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
RB_OBJ_SHAREABLE_P
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:235
rb_ractor_make_shareable
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
Definition ractor.c:2522
vm.h
ruby_vm_at_exit
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.
Definition vm.c:778
ruby_vm_destruct
int ruby_vm_destruct(ruby_vm_t *vm)
Destructs the passed VM.
Definition vm.c:2862
rb_f_sprintf
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Identical to rb_str_format(), except how the arguments are arranged.
Definition sprintf.c:208
rb_sprintf
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition sprintf.c:1219
rb_str_catf
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 ...
Definition sprintf.c:1242
MEMCPY
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:366
MEMZERO
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:354
RB_GC_GUARD
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:161
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
ruby::backward::cxxanyargs::rb_hash_foreach
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
Definition cxxanyargs.hpp:482
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:68
RARRAY_LENINT
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:343
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:583
RARRAY_CONST_PTR
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:69
RBASIC_CLASS
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:152
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:92
StringValuePtr
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition rstring.h:82
RSTRING_PTR
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition rstring.h:498
RTYPEDDATA_DATA
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
Definition rtypeddata.h:102
TypedData_Wrap_Struct
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:441
TypedData_Make_Struct
#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...
Definition rtypeddata.h:489
RB_NO_KEYWORDS
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
rb_special_const_p
static VALUE rb_special_const_p(VALUE obj)
Identical to RB_SPECIAL_CONST_P, except it returns a VALUE.
Definition special_consts.h:347
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
_
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
METHOD
Definition proc.c:37
RNode
Definition node.h:156
collect_outer_variable_name_data
Definition vm.c:1088
iseq_catch_table_entry
Definition iseq.h:263
iseq_catch_table
Definition iseq.h:276
iseq_insn_data
Definition compile.c:89
local_var_list
Definition vm_eval.c:15
rb_ast_body_struct
Definition node.h:404
rb_ast_id_table
Definition node.h:151
rb_at_exit_list
Definition vm_core.h:593
rb_binding_t
Definition vm_core.h:1163
rb_block
Definition vm_core.h:809
rb_builtin_function
Definition builtin.h:6
rb_callable_method_entry_struct
Definition method.h:62
rb_callcache
Definition vm_callinfo.h:275
rb_captured_block
Definition vm_core.h:785
rb_control_frame_struct
Definition vm_core.h:818
rb_cref_struct
CREF (Class REFerence)
Definition method.h:44
rb_data_type_struct
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:190
rb_env_t
Definition vm_core.h:1150
rb_execution_context_struct
Definition vm_core.h:899
rb_fiber_struct
Definition cont.c:258
rb_hook_list_struct
Definition vm_core.h:603
rb_id_table
Definition id_table.c:41
rb_iseq_constant_body
Definition vm_core.h:371
rb_iseq_struct
Definition vm_core.h:526
rb_method_definition_struct
Definition method.h:179
rb_method_entry_struct
Definition method.h:54
rb_native_thread
Definition thread_none.h:11
rb_objspace
Definition gc.c:731
rb_proc_t
Definition vm_core.h:1137
rb_ractor_struct
Definition ractor_core.h:105
rb_scope_visi_struct
Definition method.h:38
rb_shape
Definition shape.h:42
rb_thread_struct
Definition vm_core.h:984
rb_vm_struct
Definition vm_core.h:615
rb_vm_tag
Definition vm_core.h:859
rb_wasm_try_catch
Definition setjmp.h:64
re_pattern_buffer
Definition onigmo.h:757
ruby_dtrace_method_hook_args
Definition probes_helper.h:6
st_hash_type
Definition st.h:61
st_table
Definition st.h:79
vm_throw_data
THROW_DATA.
Definition imemo.h:62
rb_native_cond_initialize
void rb_native_cond_initialize(rb_nativethread_cond_t *cond)
Fills the passed condition variable with an initial value.
rb_native_mutex_initialize
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
rb_native_mutex_destroy
void rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_destroy.
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Check_Type
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:432
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:375
ruby_value_type
ruby_value_type
C-level type of an object.
Definition value_type.h:112