debase-ruby_core_source 3.2.0 → 3.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +10 -0
  4. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/addr2line.h +20 -0
  5. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/builtin.h +117 -0
  6. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/ccan/build_assert/build_assert.h +40 -0
  7. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/ccan/check_type/check_type.h +63 -0
  8. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/ccan/container_of/container_of.h +142 -0
  9. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/ccan/list/list.h +789 -0
  10. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/ccan/str/str.h +17 -0
  11. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/constant.h +53 -0
  12. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/darray.h +246 -0
  13. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/debug_counter.h +423 -0
  14. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/dln.h +31 -0
  15. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/encindex.h +70 -0
  16. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/eval_intern.h +341 -0
  17. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/hrtime.h +227 -0
  18. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/id.h +343 -0
  19. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/id_table.h +39 -0
  20. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/insns.inc +249 -0
  21. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/insns_info.inc +9133 -0
  22. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/array.h +144 -0
  23. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/basic_operators.h +63 -0
  24. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/bignum.h +244 -0
  25. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/bits.h +568 -0
  26. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/class.h +226 -0
  27. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/cmdlineopt.h +63 -0
  28. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/compar.h +29 -0
  29. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/compile.h +34 -0
  30. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/compilers.h +107 -0
  31. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/complex.h +29 -0
  32. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/cont.h +32 -0
  33. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/dir.h +16 -0
  34. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/enc.h +19 -0
  35. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/encoding.h +32 -0
  36. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/enum.h +18 -0
  37. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/enumerator.h +21 -0
  38. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/error.h +213 -0
  39. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/eval.h +32 -0
  40. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/file.h +38 -0
  41. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/fixnum.h +184 -0
  42. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/gc.h +361 -0
  43. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/hash.h +190 -0
  44. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/imemo.h +241 -0
  45. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/inits.h +50 -0
  46. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/io.h +137 -0
  47. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/load.h +18 -0
  48. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/loadpath.h +16 -0
  49. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/math.h +23 -0
  50. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/missing.h +18 -0
  51. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/numeric.h +273 -0
  52. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/object.h +59 -0
  53. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/parse.h +133 -0
  54. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/proc.h +30 -0
  55. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/process.h +124 -0
  56. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/ractor.h +6 -0
  57. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/random.h +16 -0
  58. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/range.h +40 -0
  59. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/rational.h +71 -0
  60. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/re.h +28 -0
  61. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/ruby_parser.h +69 -0
  62. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/sanitizers.h +186 -0
  63. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/serial.h +23 -0
  64. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/signal.h +25 -0
  65. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/static_assert.h +16 -0
  66. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/string.h +160 -0
  67. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/struct.h +127 -0
  68. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/symbol.h +42 -0
  69. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/thread.h +74 -0
  70. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/time.h +34 -0
  71. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/transcode.h +20 -0
  72. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/util.h +27 -0
  73. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/variable.h +71 -0
  74. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/vm.h +129 -0
  75. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal/warnings.h +16 -0
  76. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/internal.h +112 -0
  77. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/iseq.h +334 -0
  78. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/known_errors.inc +791 -0
  79. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/method.h +254 -0
  80. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/node.h +125 -0
  81. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/node_name.inc +210 -0
  82. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/optinsn.inc +128 -0
  83. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/optunifs.inc +43 -0
  84. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/parse.h +232 -0
  85. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/parser_bits.h +564 -0
  86. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/parser_node.h +133 -0
  87. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/parser_st.h +162 -0
  88. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/parser_value.h +106 -0
  89. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/probes_helper.h +42 -0
  90. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/ractor_core.h +387 -0
  91. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/regenc.h +254 -0
  92. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/regint.h +996 -0
  93. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/regparse.h +370 -0
  94. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/revision.h +5 -0
  95. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/rjit.h +103 -0
  96. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/rjit_c.h +165 -0
  97. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/ruby_assert.h +14 -0
  98. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/ruby_atomic.h +23 -0
  99. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/rubyparser.h +628 -0
  100. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/shape.h +232 -0
  101. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/siphash.h +48 -0
  102. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/symbol.h +119 -0
  103. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/thread_none.h +20 -0
  104. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/thread_pthread.h +126 -0
  105. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/thread_win32.h +58 -0
  106. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/timev.h +58 -0
  107. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/transcode_data.h +138 -0
  108. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/variable.h +30 -0
  109. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/version.h +65 -0
  110. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vm.inc +5396 -0
  111. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vm_call_iseq_optimized.inc +244 -0
  112. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vm_callinfo.h +608 -0
  113. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vm_core.h +2089 -0
  114. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vm_debug.h +122 -0
  115. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vm_exec.h +199 -0
  116. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vm_insnhelper.h +271 -0
  117. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vm_opts.h +67 -0
  118. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vm_sync.h +137 -0
  119. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/vmtc.inc +243 -0
  120. data/lib/debase/ruby_core_source/ruby-3.3.0-preview2/yjit.h +70 -0
  121. data/lib/debase/ruby_core_source/version.rb +1 -1
  122. metadata +120 -3
@@ -0,0 +1,2089 @@
1
+ #ifndef RUBY_VM_CORE_H
2
+ #define RUBY_VM_CORE_H
3
+ /**********************************************************************
4
+
5
+ vm_core.h -
6
+
7
+ $Author$
8
+ created at: 04/01/01 19:41:38 JST
9
+
10
+ Copyright (C) 2004-2007 Koichi Sasada
11
+
12
+ **********************************************************************/
13
+
14
+ /*
15
+ * Enable check mode.
16
+ * 1: enable local assertions.
17
+ */
18
+ #ifndef VM_CHECK_MODE
19
+
20
+ // respect RUBY_DUBUG: if given n is 0, then use RUBY_DEBUG
21
+ #define N_OR_RUBY_DEBUG(n) (((n) > 0) ? (n) : RUBY_DEBUG)
22
+
23
+ #define VM_CHECK_MODE N_OR_RUBY_DEBUG(0)
24
+ #endif
25
+
26
+ /**
27
+ * VM Debug Level
28
+ *
29
+ * debug level:
30
+ * 0: no debug output
31
+ * 1: show instruction name
32
+ * 2: show stack frame when control stack frame is changed
33
+ * 3: show stack status
34
+ * 4: show register
35
+ * 5:
36
+ * 10: gc check
37
+ */
38
+
39
+ #ifndef VMDEBUG
40
+ #define VMDEBUG 0
41
+ #endif
42
+
43
+ #if 0
44
+ #undef VMDEBUG
45
+ #define VMDEBUG 3
46
+ #endif
47
+
48
+ #include "ruby/internal/config.h"
49
+
50
+ #include <stddef.h>
51
+ #include <signal.h>
52
+ #include <stdarg.h>
53
+
54
+ #include "ruby_assert.h"
55
+
56
+ #define RVALUE_SIZE (sizeof(struct RBasic) + sizeof(VALUE[RBIMPL_RVALUE_EMBED_LEN_MAX]))
57
+
58
+ #if VM_CHECK_MODE > 0
59
+ #define VM_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(VM_CHECK_MODE > 0, expr, #expr)
60
+ #define VM_UNREACHABLE(func) rb_bug(#func ": unreachable")
61
+ #define RUBY_ASSERT_CRITICAL_SECTION
62
+ #define RUBY_DEBUG_THREAD_SCHEDULE() rb_thread_schedule()
63
+ #else
64
+ #define VM_ASSERT(expr) ((void)0)
65
+ #define VM_UNREACHABLE(func) UNREACHABLE
66
+ #define RUBY_DEBUG_THREAD_SCHEDULE()
67
+ #endif
68
+
69
+ #define RUBY_ASSERT_MUTEX_OWNED(mutex) VM_ASSERT(rb_mutex_owned_p(mutex))
70
+
71
+ #if defined(RUBY_ASSERT_CRITICAL_SECTION)
72
+ // TODO add documentation
73
+ extern int ruby_assert_critical_section_entered;
74
+ #define RUBY_ASSERT_CRITICAL_SECTION_ENTER() do{ruby_assert_critical_section_entered += 1;}while(false)
75
+ #define RUBY_ASSERT_CRITICAL_SECTION_LEAVE() do{VM_ASSERT(ruby_assert_critical_section_entered > 0);ruby_assert_critical_section_entered -= 1;}while(false)
76
+ #else
77
+ #define RUBY_ASSERT_CRITICAL_SECTION_ENTER()
78
+ #define RUBY_ASSERT_CRITICAL_SECTION_LEAVE()
79
+ #endif
80
+
81
+ #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
82
+ # include "wasm/setjmp.h"
83
+ #else
84
+ # include <setjmp.h>
85
+ #endif
86
+
87
+ #if defined(__linux__) || defined(__FreeBSD__)
88
+ # define RB_THREAD_T_HAS_NATIVE_ID
89
+ #endif
90
+
91
+ #include "ruby/internal/stdbool.h"
92
+ #include "ccan/list/list.h"
93
+ #include "id.h"
94
+ #include "internal.h"
95
+ #include "internal/array.h"
96
+ #include "internal/basic_operators.h"
97
+ #include "internal/serial.h"
98
+ #include "internal/vm.h"
99
+ #include "method.h"
100
+ #include "node.h"
101
+ #include "ruby/ruby.h"
102
+ #include "ruby/st.h"
103
+ #include "ruby_atomic.h"
104
+ #include "vm_opts.h"
105
+
106
+ #include "ruby/thread_native.h"
107
+
108
+ /*
109
+ * implementation selector of get_insn_info algorithm
110
+ * 0: linear search
111
+ * 1: binary search
112
+ * 2: succinct bitvector
113
+ */
114
+ #ifndef VM_INSN_INFO_TABLE_IMPL
115
+ # define VM_INSN_INFO_TABLE_IMPL 2
116
+ #endif
117
+
118
+ #if defined(NSIG_MAX) /* POSIX issue 8 */
119
+ # undef NSIG
120
+ # define NSIG NSIG_MAX
121
+ #elif defined(_SIG_MAXSIG) /* FreeBSD */
122
+ # undef NSIG
123
+ # define NSIG _SIG_MAXSIG
124
+ #elif defined(_SIGMAX) /* QNX */
125
+ # define NSIG (_SIGMAX + 1)
126
+ #elif defined(NSIG) /* 99% of everything else */
127
+ # /* take it */
128
+ #else /* Last resort */
129
+ # define NSIG (sizeof(sigset_t) * CHAR_BIT + 1)
130
+ #endif
131
+
132
+ #define RUBY_NSIG NSIG
133
+
134
+ #if defined(SIGCLD)
135
+ # define RUBY_SIGCHLD (SIGCLD)
136
+ #elif defined(SIGCHLD)
137
+ # define RUBY_SIGCHLD (SIGCHLD)
138
+ #endif
139
+
140
+ #if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
141
+ # define USE_SIGALTSTACK
142
+ void *rb_allocate_sigaltstack(void);
143
+ void *rb_register_sigaltstack(void *);
144
+ # define RB_ALTSTACK_INIT(var, altstack) var = rb_register_sigaltstack(altstack)
145
+ # define RB_ALTSTACK_FREE(var) free(var)
146
+ # define RB_ALTSTACK(var) var
147
+ #else /* noop */
148
+ # define RB_ALTSTACK_INIT(var, altstack)
149
+ # define RB_ALTSTACK_FREE(var)
150
+ # define RB_ALTSTACK(var) (0)
151
+ #endif
152
+
153
+ #include THREAD_IMPL_H
154
+ #define RUBY_VM_THREAD_MODEL 2
155
+
156
+ /*****************/
157
+ /* configuration */
158
+ /*****************/
159
+
160
+ /* gcc ver. check */
161
+ #if defined(__GNUC__) && __GNUC__ >= 2
162
+
163
+ #if OPT_TOKEN_THREADED_CODE
164
+ #if OPT_DIRECT_THREADED_CODE
165
+ #undef OPT_DIRECT_THREADED_CODE
166
+ #endif
167
+ #endif
168
+
169
+ #else /* defined(__GNUC__) && __GNUC__ >= 2 */
170
+
171
+ /* disable threaded code options */
172
+ #if OPT_DIRECT_THREADED_CODE
173
+ #undef OPT_DIRECT_THREADED_CODE
174
+ #endif
175
+ #if OPT_TOKEN_THREADED_CODE
176
+ #undef OPT_TOKEN_THREADED_CODE
177
+ #endif
178
+ #endif
179
+
180
+ /* call threaded code */
181
+ #if OPT_CALL_THREADED_CODE
182
+ #if OPT_DIRECT_THREADED_CODE
183
+ #undef OPT_DIRECT_THREADED_CODE
184
+ #endif /* OPT_DIRECT_THREADED_CODE */
185
+ #endif /* OPT_CALL_THREADED_CODE */
186
+
187
+ void rb_vm_encoded_insn_data_table_init(void);
188
+ typedef unsigned long rb_num_t;
189
+ typedef signed long rb_snum_t;
190
+
191
+ enum ruby_tag_type {
192
+ RUBY_TAG_NONE = 0x0,
193
+ RUBY_TAG_RETURN = 0x1,
194
+ RUBY_TAG_BREAK = 0x2,
195
+ RUBY_TAG_NEXT = 0x3,
196
+ RUBY_TAG_RETRY = 0x4,
197
+ RUBY_TAG_REDO = 0x5,
198
+ RUBY_TAG_RAISE = 0x6,
199
+ RUBY_TAG_THROW = 0x7,
200
+ RUBY_TAG_FATAL = 0x8,
201
+ RUBY_TAG_MASK = 0xf
202
+ };
203
+
204
+ #define TAG_NONE RUBY_TAG_NONE
205
+ #define TAG_RETURN RUBY_TAG_RETURN
206
+ #define TAG_BREAK RUBY_TAG_BREAK
207
+ #define TAG_NEXT RUBY_TAG_NEXT
208
+ #define TAG_RETRY RUBY_TAG_RETRY
209
+ #define TAG_REDO RUBY_TAG_REDO
210
+ #define TAG_RAISE RUBY_TAG_RAISE
211
+ #define TAG_THROW RUBY_TAG_THROW
212
+ #define TAG_FATAL RUBY_TAG_FATAL
213
+ #define TAG_MASK RUBY_TAG_MASK
214
+
215
+ enum ruby_vm_throw_flags {
216
+ VM_THROW_NO_ESCAPE_FLAG = 0x8000,
217
+ VM_THROW_STATE_MASK = 0xff
218
+ };
219
+
220
+ /* forward declarations */
221
+ struct rb_thread_struct;
222
+ struct rb_control_frame_struct;
223
+
224
+ /* iseq data type */
225
+ typedef struct rb_compile_option_struct rb_compile_option_t;
226
+
227
+ union ic_serial_entry {
228
+ rb_serial_t raw;
229
+ VALUE data[2];
230
+ };
231
+
232
+ // imemo_constcache
233
+ struct iseq_inline_constant_cache_entry {
234
+ VALUE flags;
235
+
236
+ VALUE value; // v0
237
+ VALUE _unused1; // v1
238
+ VALUE _unused2; // v2
239
+ const rb_cref_t *ic_cref; // v3
240
+ };
241
+ STATIC_ASSERT(sizeof_iseq_inline_constant_cache_entry,
242
+ (offsetof(struct iseq_inline_constant_cache_entry, ic_cref) +
243
+ sizeof(const rb_cref_t *)) <= RVALUE_SIZE);
244
+
245
+ struct iseq_inline_constant_cache {
246
+ struct iseq_inline_constant_cache_entry *entry;
247
+
248
+ /**
249
+ * A null-terminated list of ids, used to represent a constant's path
250
+ * idNULL is used to represent the :: prefix, and 0 is used to donate the end
251
+ * of the list.
252
+ *
253
+ * For example
254
+ * FOO {rb_intern("FOO"), 0}
255
+ * FOO::BAR {rb_intern("FOO"), rb_intern("BAR"), 0}
256
+ * ::FOO {idNULL, rb_intern("FOO"), 0}
257
+ * ::FOO::BAR {idNULL, rb_intern("FOO"), rb_intern("BAR"), 0}
258
+ */
259
+ const ID *segments;
260
+ };
261
+
262
+ struct iseq_inline_iv_cache_entry {
263
+ uintptr_t value; // attr_index in lower bits, dest_shape_id in upper bits
264
+ ID iv_set_name;
265
+ };
266
+
267
+ struct iseq_inline_cvar_cache_entry {
268
+ struct rb_cvar_class_tbl_entry *entry;
269
+ };
270
+
271
+ union iseq_inline_storage_entry {
272
+ struct {
273
+ struct rb_thread_struct *running_thread;
274
+ VALUE value;
275
+ } once;
276
+ struct iseq_inline_constant_cache ic_cache;
277
+ struct iseq_inline_iv_cache_entry iv_cache;
278
+ };
279
+
280
+ struct rb_calling_info {
281
+ const struct rb_call_data *cd;
282
+ const struct rb_callcache *cc;
283
+ VALUE block_handler;
284
+ VALUE recv;
285
+ int argc;
286
+ bool kw_splat;
287
+ VALUE heap_argv;
288
+ };
289
+
290
+ #ifndef VM_ARGC_STACK_MAX
291
+ #define VM_ARGC_STACK_MAX 128
292
+ #endif
293
+
294
+ # define CALLING_ARGC(calling) ((calling)->heap_argv ? RARRAY_LENINT((calling)->heap_argv) : (calling)->argc)
295
+
296
+ struct rb_execution_context_struct;
297
+
298
+ #if 1
299
+ #define CoreDataFromValue(obj, type) (type*)DATA_PTR(obj)
300
+ #else
301
+ #define CoreDataFromValue(obj, type) (type*)rb_data_object_get(obj)
302
+ #endif
303
+ #define GetCoreDataFromValue(obj, type, ptr) ((ptr) = CoreDataFromValue((obj), type))
304
+
305
+ typedef struct rb_iseq_location_struct {
306
+ VALUE pathobj; /* String (path) or Array [path, realpath]. Frozen. */
307
+ VALUE base_label; /* String */
308
+ VALUE label; /* String */
309
+ int first_lineno;
310
+ int node_id;
311
+ rb_code_location_t code_location;
312
+ } rb_iseq_location_t;
313
+
314
+ #define PATHOBJ_PATH 0
315
+ #define PATHOBJ_REALPATH 1
316
+
317
+ static inline VALUE
318
+ pathobj_path(VALUE pathobj)
319
+ {
320
+ if (RB_TYPE_P(pathobj, T_STRING)) {
321
+ return pathobj;
322
+ }
323
+ else {
324
+ VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
325
+ return RARRAY_AREF(pathobj, PATHOBJ_PATH);
326
+ }
327
+ }
328
+
329
+ static inline VALUE
330
+ pathobj_realpath(VALUE pathobj)
331
+ {
332
+ if (RB_TYPE_P(pathobj, T_STRING)) {
333
+ return pathobj;
334
+ }
335
+ else {
336
+ VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
337
+ return RARRAY_AREF(pathobj, PATHOBJ_REALPATH);
338
+ }
339
+ }
340
+
341
+ /* Forward declarations */
342
+ struct rb_rjit_unit;
343
+
344
+ typedef uintptr_t iseq_bits_t;
345
+
346
+ #define ISEQ_IS_SIZE(body) (body->ic_size + body->ivc_size + body->ise_size + body->icvarc_size)
347
+
348
+ /* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
349
+ #define ISEQ_IS_IC_ENTRY(body, idx) (body->is_entries[(idx) + body->ise_size + body->icvarc_size + body->ivc_size].ic_cache);
350
+
351
+ /* instruction sequence type */
352
+ enum rb_iseq_type {
353
+ ISEQ_TYPE_TOP,
354
+ ISEQ_TYPE_METHOD,
355
+ ISEQ_TYPE_BLOCK,
356
+ ISEQ_TYPE_CLASS,
357
+ ISEQ_TYPE_RESCUE,
358
+ ISEQ_TYPE_ENSURE,
359
+ ISEQ_TYPE_EVAL,
360
+ ISEQ_TYPE_MAIN,
361
+ ISEQ_TYPE_PLAIN
362
+ };
363
+
364
+ // Attributes specified by Primitive.attr!
365
+ enum rb_builtin_attr {
366
+ // The iseq does not call methods.
367
+ BUILTIN_ATTR_LEAF = 0x01,
368
+ // The iseq does not allocate objects.
369
+ BUILTIN_ATTR_NO_GC = 0x02,
370
+ // This iseq only contains single `opt_invokebuiltin_delegate_leave` instruction with 0 arguments.
371
+ BUILTIN_ATTR_SINGLE_NOARG_INLINE = 0x04,
372
+ };
373
+
374
+ typedef VALUE (*rb_jit_func_t)(struct rb_execution_context_struct *, struct rb_control_frame_struct *);
375
+
376
+ struct rb_iseq_constant_body {
377
+ enum rb_iseq_type type;
378
+
379
+ unsigned int iseq_size;
380
+ VALUE *iseq_encoded; /* encoded iseq (insn addr and operands) */
381
+
382
+ /**
383
+ * parameter information
384
+ *
385
+ * def m(a1, a2, ..., aM, # mandatory
386
+ * b1=(...), b2=(...), ..., bN=(...), # optional
387
+ * *c, # rest
388
+ * d1, d2, ..., dO, # post
389
+ * e1:(...), e2:(...), ..., eK:(...), # keyword
390
+ * **f, # keyword_rest
391
+ * &g) # block
392
+ * =>
393
+ *
394
+ * lead_num = M
395
+ * opt_num = N
396
+ * rest_start = M+N
397
+ * post_start = M+N+(*1)
398
+ * post_num = O
399
+ * keyword_num = K
400
+ * block_start = M+N+(*1)+O+K
401
+ * keyword_bits = M+N+(*1)+O+K+(&1)
402
+ * size = M+N+O+(*1)+K+(&1)+(**1) // parameter size.
403
+ */
404
+
405
+ struct {
406
+ struct {
407
+ unsigned int has_lead : 1;
408
+ unsigned int has_opt : 1;
409
+ unsigned int has_rest : 1;
410
+ unsigned int has_post : 1;
411
+ unsigned int has_kw : 1;
412
+ unsigned int has_kwrest : 1;
413
+ unsigned int has_block : 1;
414
+
415
+ unsigned int ambiguous_param0 : 1; /* {|a|} */
416
+ unsigned int accepts_no_kwarg : 1;
417
+ unsigned int ruby2_keywords: 1;
418
+ } flags;
419
+
420
+ unsigned int size;
421
+
422
+ int lead_num;
423
+ int opt_num;
424
+ int rest_start;
425
+ int post_start;
426
+ int post_num;
427
+ int block_start;
428
+
429
+ const VALUE *opt_table; /* (opt_num + 1) entries. */
430
+ /* opt_num and opt_table:
431
+ *
432
+ * def foo o1=e1, o2=e2, ..., oN=eN
433
+ * #=>
434
+ * # prologue code
435
+ * A1: e1
436
+ * A2: e2
437
+ * ...
438
+ * AN: eN
439
+ * AL: body
440
+ * opt_num = N
441
+ * opt_table = [A1, A2, ..., AN, AL]
442
+ */
443
+
444
+ const struct rb_iseq_param_keyword {
445
+ int num;
446
+ int required_num;
447
+ int bits_start;
448
+ int rest_start;
449
+ const ID *table;
450
+ VALUE *default_values;
451
+ } *keyword;
452
+ } param;
453
+
454
+ rb_iseq_location_t location;
455
+
456
+ /* insn info, must be freed */
457
+ struct iseq_insn_info {
458
+ const struct iseq_insn_info_entry *body;
459
+ unsigned int *positions;
460
+ unsigned int size;
461
+ #if VM_INSN_INFO_TABLE_IMPL == 2
462
+ struct succ_index_table *succ_index_table;
463
+ #endif
464
+ } insns_info;
465
+
466
+ const ID *local_table; /* must free */
467
+
468
+ /* catch table */
469
+ struct iseq_catch_table *catch_table;
470
+
471
+ /* for child iseq */
472
+ const struct rb_iseq_struct *parent_iseq;
473
+ struct rb_iseq_struct *local_iseq; /* local_iseq->flip_cnt can be modified */
474
+
475
+ union iseq_inline_storage_entry *is_entries; /* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
476
+ struct rb_call_data *call_data; //struct rb_call_data calls[ci_size];
477
+
478
+ struct {
479
+ rb_snum_t flip_count;
480
+ VALUE script_lines;
481
+ VALUE coverage;
482
+ VALUE pc2branchindex;
483
+ VALUE *original_iseq;
484
+ } variable;
485
+
486
+ unsigned int local_table_size;
487
+ unsigned int ic_size; // Number of IC caches
488
+ unsigned int ise_size; // Number of ISE caches
489
+ unsigned int ivc_size; // Number of IVC caches
490
+ unsigned int icvarc_size; // Number of ICVARC caches
491
+ unsigned int ci_size;
492
+ unsigned int stack_max; /* for stack overflow check */
493
+
494
+ unsigned int builtin_attrs; // Union of rb_builtin_attr
495
+
496
+ union {
497
+ iseq_bits_t * list; /* Find references for GC */
498
+ iseq_bits_t single;
499
+ } mark_bits;
500
+
501
+ struct rb_id_table *outer_variables;
502
+
503
+ const rb_iseq_t *mandatory_only_iseq;
504
+
505
+ #if USE_RJIT || USE_YJIT
506
+ // Function pointer for JIT code on jit_exec()
507
+ rb_jit_func_t jit_entry;
508
+ // Number of calls on jit_exec()
509
+ long unsigned jit_entry_calls;
510
+ #endif
511
+
512
+ #if USE_YJIT
513
+ // Function pointer for JIT code on jit_exec_exception()
514
+ rb_jit_func_t jit_exception;
515
+ // Number of calls on jit_exec_exception()
516
+ long unsigned jit_exception_calls;
517
+ #endif
518
+
519
+ #if USE_RJIT
520
+ // RJIT stores some data on each iseq.
521
+ VALUE rjit_blocks;
522
+ #endif
523
+
524
+ #if USE_YJIT
525
+ // YJIT stores some data on each iseq.
526
+ void *yjit_payload;
527
+ #endif
528
+ };
529
+
530
+ /* T_IMEMO/iseq */
531
+ /* typedef rb_iseq_t is in method.h */
532
+ struct rb_iseq_struct {
533
+ VALUE flags; /* 1 */
534
+ VALUE wrapper; /* 2 */
535
+
536
+ struct rb_iseq_constant_body *body; /* 3 */
537
+
538
+ union { /* 4, 5 words */
539
+ struct iseq_compile_data *compile_data; /* used at compile time */
540
+
541
+ struct {
542
+ VALUE obj;
543
+ int index;
544
+ } loader;
545
+
546
+ struct {
547
+ struct rb_hook_list_struct *local_hooks;
548
+ rb_event_flag_t global_trace_events;
549
+ } exec;
550
+ } aux;
551
+ };
552
+
553
+ #define ISEQ_BODY(iseq) ((iseq)->body)
554
+
555
+ #if !defined(USE_LAZY_LOAD) || !(USE_LAZY_LOAD+0)
556
+ #define USE_LAZY_LOAD 0
557
+ #endif
558
+
559
+ #if !USE_LAZY_LOAD
560
+ static inline const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq) {return 0;}
561
+ #endif
562
+ const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq);
563
+
564
+ static inline const rb_iseq_t *
565
+ rb_iseq_check(const rb_iseq_t *iseq)
566
+ {
567
+ if (USE_LAZY_LOAD && ISEQ_BODY(iseq) == NULL) {
568
+ rb_iseq_complete((rb_iseq_t *)iseq);
569
+ }
570
+ return iseq;
571
+ }
572
+
573
+ static inline const rb_iseq_t *
574
+ def_iseq_ptr(rb_method_definition_t *def)
575
+ {
576
+ //TODO: re-visit. to check the bug, enable this assertion.
577
+ #if VM_CHECK_MODE > 0
578
+ if (def->type != VM_METHOD_TYPE_ISEQ) rb_bug("def_iseq_ptr: not iseq (%d)", def->type);
579
+ #endif
580
+ return rb_iseq_check(def->body.iseq.iseqptr);
581
+ }
582
+
583
+ enum ruby_special_exceptions {
584
+ ruby_error_reenter,
585
+ ruby_error_nomemory,
586
+ ruby_error_sysstack,
587
+ ruby_error_stackfatal,
588
+ ruby_error_stream_closed,
589
+ ruby_special_error_count
590
+ };
591
+
592
+ #define GetVMPtr(obj, ptr) \
593
+ GetCoreDataFromValue((obj), rb_vm_t, (ptr))
594
+
595
+ struct rb_vm_struct;
596
+ typedef void rb_vm_at_exit_func(struct rb_vm_struct*);
597
+
598
+ typedef struct rb_at_exit_list {
599
+ rb_vm_at_exit_func *func;
600
+ struct rb_at_exit_list *next;
601
+ } rb_at_exit_list;
602
+
603
+ struct rb_objspace;
604
+ struct rb_objspace *rb_objspace_alloc(void);
605
+ void rb_objspace_free(struct rb_objspace *);
606
+ void rb_objspace_call_finalizer(struct rb_objspace *);
607
+
608
+ typedef struct rb_hook_list_struct {
609
+ struct rb_event_hook_struct *hooks;
610
+ rb_event_flag_t events;
611
+ unsigned int running;
612
+ bool need_clean;
613
+ bool is_local;
614
+ } rb_hook_list_t;
615
+
616
+
617
+ // see builtin.h for definition
618
+ typedef const struct rb_builtin_function *RB_BUILTIN;
619
+
620
+ typedef struct rb_vm_struct {
621
+ VALUE self;
622
+
623
+ struct {
624
+ struct ccan_list_head set;
625
+ unsigned int cnt;
626
+ unsigned int blocking_cnt;
627
+
628
+ struct rb_ractor_struct *main_ractor;
629
+ struct rb_thread_struct *main_thread; // == vm->ractor.main_ractor->threads.main
630
+
631
+ struct {
632
+ // monitor
633
+ rb_nativethread_lock_t lock;
634
+ struct rb_ractor_struct *lock_owner;
635
+ unsigned int lock_rec;
636
+
637
+ // barrier
638
+ bool barrier_waiting;
639
+ unsigned int barrier_cnt;
640
+ rb_nativethread_cond_t barrier_cond;
641
+
642
+ // join at exit
643
+ rb_nativethread_cond_t terminate_cond;
644
+ bool terminate_waiting;
645
+ } sync;
646
+ } ractor;
647
+
648
+ #ifdef USE_SIGALTSTACK
649
+ void *main_altstack;
650
+ #endif
651
+
652
+ rb_serial_t fork_gen;
653
+ struct ccan_list_head waiting_fds; /* <=> struct waiting_fd */
654
+
655
+ /* set in single-threaded processes only: */
656
+ volatile int ubf_async_safe;
657
+
658
+ unsigned int running: 1;
659
+ unsigned int thread_abort_on_exception: 1;
660
+ unsigned int thread_report_on_exception: 1;
661
+ unsigned int thread_ignore_deadlock: 1;
662
+
663
+ /* object management */
664
+ VALUE mark_object_ary;
665
+ const VALUE special_exceptions[ruby_special_error_count];
666
+
667
+ /* load */
668
+ VALUE top_self;
669
+ VALUE load_path;
670
+ VALUE load_path_snapshot;
671
+ VALUE load_path_check_cache;
672
+ VALUE expanded_load_path;
673
+ VALUE loaded_features;
674
+ VALUE loaded_features_snapshot;
675
+ VALUE loaded_features_realpaths;
676
+ VALUE loaded_features_realpath_map;
677
+ struct st_table *loaded_features_index;
678
+ struct st_table *loading_table;
679
+ // For running the init function of statically linked
680
+ // extensions when they are loaded
681
+ struct st_table *static_ext_inits;
682
+
683
+ /* signal */
684
+ struct {
685
+ VALUE cmd[RUBY_NSIG];
686
+ } trap_list;
687
+
688
+ /* relation table of ensure - rollback for callcc */
689
+ struct st_table *ensure_rollback_table;
690
+
691
+ /* postponed_job (async-signal-safe, NOT thread-safe) */
692
+ struct rb_postponed_job_struct *postponed_job_buffer;
693
+ rb_atomic_t postponed_job_index;
694
+
695
+ int src_encoding_index;
696
+
697
+ /* workqueue (thread-safe, NOT async-signal-safe) */
698
+ struct ccan_list_head workqueue; /* <=> rb_workqueue_job.jnode */
699
+ rb_nativethread_lock_t workqueue_lock;
700
+
701
+ VALUE orig_progname, progname;
702
+ VALUE coverages, me2counter;
703
+ int coverage_mode;
704
+
705
+ st_table * defined_module_hash;
706
+
707
+ struct rb_objspace *objspace;
708
+
709
+ rb_at_exit_list *at_exit;
710
+
711
+ st_table *frozen_strings;
712
+
713
+ const struct rb_builtin_function *builtin_function_table;
714
+ int builtin_inline_index;
715
+
716
+ struct rb_id_table *negative_cme_table;
717
+ st_table *overloaded_cme_table; // cme -> overloaded_cme
718
+
719
+ // This id table contains a mapping from ID to ICs. It does this with ID
720
+ // keys and nested st_tables as values. The nested tables have ICs as keys
721
+ // and Qtrue as values. It is used when inline constant caches need to be
722
+ // invalidated or ISEQs are being freed.
723
+ struct rb_id_table *constant_cache;
724
+
725
+ #ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
726
+ #define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
727
+ #endif
728
+ const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE]; // vm_eval.c
729
+
730
+ #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
731
+ uint32_t clock;
732
+ #endif
733
+
734
+ /* params */
735
+ struct { /* size in byte */
736
+ size_t thread_vm_stack_size;
737
+ size_t thread_machine_stack_size;
738
+ size_t fiber_vm_stack_size;
739
+ size_t fiber_machine_stack_size;
740
+ } default_params;
741
+
742
+ } rb_vm_t;
743
+
744
+ /* default values */
745
+
746
+ #define RUBY_VM_SIZE_ALIGN 4096
747
+
748
+ #define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
749
+ #define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
750
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
751
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
752
+
753
+ #define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
754
+ #define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
755
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE)) /* 256 KB or 512 KB */
756
+ #if defined(__powerpc64__) || defined(__ppc64__) // macOS has __ppc64__
757
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE)) /* 128 KB or 256 KB */
758
+ #else
759
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
760
+ #endif
761
+
762
+ #if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer)
763
+ /* It seems sanitizers consume A LOT of machine stacks */
764
+ #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
765
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
766
+ #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
767
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
768
+ #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
769
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
770
+ #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
771
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
772
+ #endif
773
+
774
+ #ifndef VM_DEBUG_BP_CHECK
775
+ #define VM_DEBUG_BP_CHECK 0
776
+ #endif
777
+
778
+ #ifndef VM_DEBUG_VERIFY_METHOD_CACHE
779
+ #define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
780
+ #endif
781
+
782
+ struct rb_captured_block {
783
+ VALUE self;
784
+ const VALUE *ep;
785
+ union {
786
+ const rb_iseq_t *iseq;
787
+ const struct vm_ifunc *ifunc;
788
+ VALUE val;
789
+ } code;
790
+ };
791
+
792
+ enum rb_block_handler_type {
793
+ block_handler_type_iseq,
794
+ block_handler_type_ifunc,
795
+ block_handler_type_symbol,
796
+ block_handler_type_proc
797
+ };
798
+
799
+ enum rb_block_type {
800
+ block_type_iseq,
801
+ block_type_ifunc,
802
+ block_type_symbol,
803
+ block_type_proc
804
+ };
805
+
806
+ struct rb_block {
807
+ union {
808
+ struct rb_captured_block captured;
809
+ VALUE symbol;
810
+ VALUE proc;
811
+ } as;
812
+ enum rb_block_type type;
813
+ };
814
+
815
+ typedef struct rb_control_frame_struct {
816
+ const VALUE *pc; // cfp[0]
817
+ VALUE *sp; // cfp[1]
818
+ const rb_iseq_t *iseq; // cfp[2]
819
+ VALUE self; // cfp[3] / block[0]
820
+ const VALUE *ep; // cfp[4] / block[1]
821
+ const void *block_code; // cfp[5] / block[2] -- iseq, ifunc, or forwarded block handler
822
+ void *jit_return; // cfp[6] -- return address for JIT code
823
+ #if VM_DEBUG_BP_CHECK
824
+ VALUE *bp_check; // cfp[7]
825
+ #endif
826
+ } rb_control_frame_t;
827
+
828
+ extern const rb_data_type_t ruby_threadptr_data_type;
829
+
830
+ static inline struct rb_thread_struct *
831
+ rb_thread_ptr(VALUE thval)
832
+ {
833
+ return (struct rb_thread_struct *)rb_check_typeddata(thval, &ruby_threadptr_data_type);
834
+ }
835
+
836
+ enum rb_thread_status {
837
+ THREAD_RUNNABLE,
838
+ THREAD_STOPPED,
839
+ THREAD_STOPPED_FOREVER,
840
+ THREAD_KILLED
841
+ };
842
+
843
+ #ifdef RUBY_JMP_BUF
844
+ typedef RUBY_JMP_BUF rb_jmpbuf_t;
845
+ #else
846
+ typedef void *rb_jmpbuf_t[5];
847
+ #endif
848
+
849
+ /*
850
+ the members which are written in EC_PUSH_TAG() should be placed at
851
+ the beginning and the end, so that entire region is accessible.
852
+ */
853
+ struct rb_vm_tag {
854
+ VALUE tag;
855
+ VALUE retval;
856
+ rb_jmpbuf_t buf;
857
+ struct rb_vm_tag *prev;
858
+ enum ruby_tag_type state;
859
+ unsigned int lock_rec;
860
+ };
861
+
862
+ STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(struct rb_vm_tag, buf) > 0);
863
+ STATIC_ASSERT(rb_vm_tag_buf_end,
864
+ offsetof(struct rb_vm_tag, buf) + sizeof(rb_jmpbuf_t) <
865
+ sizeof(struct rb_vm_tag));
866
+
867
+ struct rb_unblock_callback {
868
+ rb_unblock_function_t *func;
869
+ void *arg;
870
+ };
871
+
872
+ struct rb_mutex_struct;
873
+
874
+ typedef struct rb_ensure_entry {
875
+ VALUE marker;
876
+ VALUE (*e_proc)(VALUE);
877
+ VALUE data2;
878
+ } rb_ensure_entry_t;
879
+
880
+ typedef struct rb_ensure_list {
881
+ struct rb_ensure_list *next;
882
+ struct rb_ensure_entry entry;
883
+ } rb_ensure_list_t;
884
+
885
+ typedef struct rb_fiber_struct rb_fiber_t;
886
+
887
+ struct rb_waiting_list {
888
+ struct rb_waiting_list *next;
889
+ struct rb_thread_struct *thread;
890
+ struct rb_fiber_struct *fiber;
891
+ };
892
+
893
+ struct rb_execution_context_struct {
894
+ /* execution information */
895
+ VALUE *vm_stack; /* must free, must mark */
896
+ size_t vm_stack_size; /* size in word (byte size / sizeof(VALUE)) */
897
+ rb_control_frame_t *cfp;
898
+
899
+ struct rb_vm_tag *tag;
900
+
901
+ /* interrupt flags */
902
+ rb_atomic_t interrupt_flag;
903
+ rb_atomic_t interrupt_mask; /* size should match flag */
904
+ #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
905
+ uint32_t checked_clock;
906
+ #endif
907
+
908
+ rb_fiber_t *fiber_ptr;
909
+ struct rb_thread_struct *thread_ptr;
910
+
911
+ /* storage (ec (fiber) local) */
912
+ struct rb_id_table *local_storage;
913
+ VALUE local_storage_recursive_hash;
914
+ VALUE local_storage_recursive_hash_for_trace;
915
+
916
+ /* Inheritable fiber storage. */
917
+ VALUE storage;
918
+
919
+ /* eval env */
920
+ const VALUE *root_lep;
921
+ VALUE root_svar;
922
+
923
+ /* ensure & callcc */
924
+ rb_ensure_list_t *ensure_list;
925
+
926
+ /* trace information */
927
+ struct rb_trace_arg_struct *trace_arg;
928
+
929
+ /* temporary places */
930
+ VALUE errinfo;
931
+ VALUE passed_block_handler; /* for rb_iterate */
932
+
933
+ uint8_t raised_flag; /* only 3 bits needed */
934
+
935
+ /* n.b. only 7 bits needed, really: */
936
+ BITFIELD(enum method_missing_reason, method_missing_reason, 8);
937
+
938
+ VALUE private_const_reference;
939
+
940
+ /* for GC */
941
+ struct {
942
+ VALUE *stack_start;
943
+ VALUE *stack_end;
944
+ size_t stack_maxsize;
945
+ RUBY_ALIGNAS(SIZEOF_VALUE) jmp_buf regs;
946
+ } machine;
947
+ };
948
+
949
+ #ifndef rb_execution_context_t
950
+ typedef struct rb_execution_context_struct rb_execution_context_t;
951
+ #define rb_execution_context_t rb_execution_context_t
952
+ #endif
953
+
954
+ // for builtin.h
955
+ #define VM_CORE_H_EC_DEFINED 1
956
+
957
+ // Set the vm_stack pointer in the execution context.
958
+ void rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
959
+
960
+ // Initialize the vm_stack pointer in the execution context and push the initial stack frame.
961
+ // @param ec the execution context to update.
962
+ // @param stack a pointer to the stack to use.
963
+ // @param size the size of the stack, as in `VALUE stack[size]`.
964
+ void rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
965
+
966
+ // Clear (set to `NULL`) the vm_stack pointer.
967
+ // @param ec the execution context to update.
968
+ void rb_ec_clear_vm_stack(rb_execution_context_t *ec);
969
+
970
+ struct rb_ext_config {
971
+ bool ractor_safe;
972
+ };
973
+
974
+ typedef struct rb_ractor_struct rb_ractor_t;
975
+
976
+ struct rb_native_thread;
977
+
978
+ typedef struct rb_thread_struct {
979
+ struct ccan_list_node lt_node; // managed by a ractor
980
+ VALUE self;
981
+ rb_ractor_t *ractor;
982
+ rb_vm_t *vm;
983
+ struct rb_native_thread *nt;
984
+ rb_execution_context_t *ec;
985
+
986
+ struct rb_thread_sched_item sched;
987
+ rb_atomic_t serial; // only for RUBY_DEBUG_LOG()
988
+
989
+ VALUE last_status; /* $? */
990
+
991
+ /* for cfunc */
992
+ struct rb_calling_info *calling;
993
+
994
+ /* for load(true) */
995
+ VALUE top_self;
996
+ VALUE top_wrapper;
997
+
998
+ /* thread control */
999
+
1000
+ BITFIELD(enum rb_thread_status, status, 2);
1001
+ /* bit flags */
1002
+ unsigned int has_dedicated_nt : 1;
1003
+ unsigned int to_kill : 1;
1004
+ unsigned int abort_on_exception: 1;
1005
+ unsigned int report_on_exception: 1;
1006
+ unsigned int pending_interrupt_queue_checked: 1;
1007
+ int8_t priority; /* -3 .. 3 (RUBY_THREAD_PRIORITY_{MIN,MAX}) */
1008
+ uint32_t running_time_us; /* 12500..800000 */
1009
+
1010
+ void *blocking_region_buffer;
1011
+
1012
+ VALUE thgroup;
1013
+ VALUE value;
1014
+
1015
+ /* temporary place of retval on OPT_CALL_THREADED_CODE */
1016
+ #if OPT_CALL_THREADED_CODE
1017
+ VALUE retval;
1018
+ #endif
1019
+
1020
+ /* async errinfo queue */
1021
+ VALUE pending_interrupt_queue;
1022
+ VALUE pending_interrupt_mask_stack;
1023
+
1024
+ /* interrupt management */
1025
+ rb_nativethread_lock_t interrupt_lock;
1026
+ struct rb_unblock_callback unblock;
1027
+ VALUE locking_mutex;
1028
+ struct rb_mutex_struct *keeping_mutexes;
1029
+
1030
+ struct rb_waiting_list *join_list;
1031
+
1032
+ union {
1033
+ struct {
1034
+ VALUE proc;
1035
+ VALUE args;
1036
+ int kw_splat;
1037
+ } proc;
1038
+ struct {
1039
+ VALUE (*func)(void *);
1040
+ void *arg;
1041
+ } func;
1042
+ } invoke_arg;
1043
+
1044
+ enum thread_invoke_type {
1045
+ thread_invoke_type_none = 0,
1046
+ thread_invoke_type_proc,
1047
+ thread_invoke_type_ractor_proc,
1048
+ thread_invoke_type_func
1049
+ } invoke_type;
1050
+
1051
+ /* statistics data for profiler */
1052
+ VALUE stat_insn_usage;
1053
+
1054
+ /* fiber */
1055
+ rb_fiber_t *root_fiber;
1056
+
1057
+ VALUE scheduler;
1058
+ unsigned int blocking;
1059
+
1060
+ /* misc */
1061
+ VALUE name;
1062
+
1063
+ struct rb_ext_config ext_config;
1064
+ } rb_thread_t;
1065
+
1066
+ static inline unsigned int
1067
+ rb_th_serial(const rb_thread_t *th)
1068
+ {
1069
+ return th ? (unsigned int)th->serial : 0;
1070
+ }
1071
+
1072
+ typedef enum {
1073
+ VM_DEFINECLASS_TYPE_CLASS = 0x00,
1074
+ VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1075
+ VM_DEFINECLASS_TYPE_MODULE = 0x02,
1076
+ /* 0x03..0x06 is reserved */
1077
+ VM_DEFINECLASS_TYPE_MASK = 0x07
1078
+ } rb_vm_defineclass_type_t;
1079
+
1080
+ #define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1081
+ #define VM_DEFINECLASS_FLAG_SCOPED 0x08
1082
+ #define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1083
+ #define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1084
+ #define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1085
+ ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1086
+
1087
+ /* iseq.c */
1088
+ RUBY_SYMBOL_EXPORT_BEGIN
1089
+
1090
+ /* node -> iseq */
1091
+ rb_iseq_t *rb_iseq_new (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, enum rb_iseq_type);
1092
+ rb_iseq_t *rb_iseq_new_top (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent);
1093
+ rb_iseq_t *rb_iseq_new_main (const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt);
1094
+ rb_iseq_t *rb_iseq_new_eval (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth);
1095
+ rb_iseq_t *rb_iseq_new_with_opt(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth,
1096
+ enum rb_iseq_type, const rb_compile_option_t*);
1097
+
1098
+ struct iseq_link_anchor;
1099
+ struct rb_iseq_new_with_callback_callback_func {
1100
+ VALUE flags;
1101
+ VALUE reserved;
1102
+ void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *);
1103
+ const void *data;
1104
+ };
1105
+ static inline struct rb_iseq_new_with_callback_callback_func *
1106
+ rb_iseq_new_with_callback_new_callback(
1107
+ void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *), const void *ptr)
1108
+ {
1109
+ VALUE memo = rb_imemo_new(imemo_ifunc, (VALUE)func, (VALUE)ptr, Qundef, Qfalse);
1110
+ return (struct rb_iseq_new_with_callback_callback_func *)memo;
1111
+ }
1112
+ rb_iseq_t *rb_iseq_new_with_callback(const struct rb_iseq_new_with_callback_callback_func * ifunc,
1113
+ VALUE name, VALUE path, VALUE realpath, int first_lineno,
1114
+ const rb_iseq_t *parent, enum rb_iseq_type, const rb_compile_option_t*);
1115
+
1116
+ VALUE rb_iseq_disasm(const rb_iseq_t *iseq);
1117
+ int rb_iseq_disasm_insn(VALUE str, const VALUE *iseqval, size_t pos, const rb_iseq_t *iseq, VALUE child);
1118
+
1119
+ VALUE rb_iseq_coverage(const rb_iseq_t *iseq);
1120
+
1121
+ RUBY_EXTERN VALUE rb_cISeq;
1122
+ RUBY_EXTERN VALUE rb_cRubyVM;
1123
+ RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
1124
+ RUBY_EXTERN VALUE rb_block_param_proxy;
1125
+ RUBY_SYMBOL_EXPORT_END
1126
+
1127
+ #define GetProcPtr(obj, ptr) \
1128
+ GetCoreDataFromValue((obj), rb_proc_t, (ptr))
1129
+
1130
+ typedef struct {
1131
+ const struct rb_block block;
1132
+ unsigned int is_from_method: 1; /* bool */
1133
+ unsigned int is_lambda: 1; /* bool */
1134
+ unsigned int is_isolated: 1; /* bool */
1135
+ } rb_proc_t;
1136
+
1137
+ RUBY_SYMBOL_EXPORT_BEGIN
1138
+ VALUE rb_proc_isolate(VALUE self);
1139
+ VALUE rb_proc_isolate_bang(VALUE self);
1140
+ VALUE rb_proc_ractor_make_shareable(VALUE self);
1141
+ RUBY_SYMBOL_EXPORT_END
1142
+
1143
+ typedef struct {
1144
+ VALUE flags; /* imemo header */
1145
+ rb_iseq_t *iseq;
1146
+ const VALUE *ep;
1147
+ const VALUE *env;
1148
+ unsigned int env_size;
1149
+ } rb_env_t;
1150
+
1151
+ extern const rb_data_type_t ruby_binding_data_type;
1152
+
1153
+ #define GetBindingPtr(obj, ptr) \
1154
+ GetCoreDataFromValue((obj), rb_binding_t, (ptr))
1155
+
1156
+ typedef struct {
1157
+ const struct rb_block block;
1158
+ const VALUE pathobj;
1159
+ int first_lineno;
1160
+ } rb_binding_t;
1161
+
1162
+ /* used by compile time and send insn */
1163
+
1164
+ enum vm_check_match_type {
1165
+ VM_CHECKMATCH_TYPE_WHEN = 1,
1166
+ VM_CHECKMATCH_TYPE_CASE = 2,
1167
+ VM_CHECKMATCH_TYPE_RESCUE = 3
1168
+ };
1169
+
1170
+ #define VM_CHECKMATCH_TYPE_MASK 0x03
1171
+ #define VM_CHECKMATCH_ARRAY 0x04
1172
+
1173
+ enum vm_special_object_type {
1174
+ VM_SPECIAL_OBJECT_VMCORE = 1,
1175
+ VM_SPECIAL_OBJECT_CBASE,
1176
+ VM_SPECIAL_OBJECT_CONST_BASE
1177
+ };
1178
+
1179
+ enum vm_svar_index {
1180
+ VM_SVAR_LASTLINE = 0, /* $_ */
1181
+ VM_SVAR_BACKREF = 1, /* $~ */
1182
+
1183
+ VM_SVAR_EXTRA_START = 2,
1184
+ VM_SVAR_FLIPFLOP_START = 2 /* flipflop */
1185
+ };
1186
+
1187
+ /* inline cache */
1188
+ typedef struct iseq_inline_constant_cache *IC;
1189
+ typedef struct iseq_inline_iv_cache_entry *IVC;
1190
+ typedef struct iseq_inline_cvar_cache_entry *ICVARC;
1191
+ typedef union iseq_inline_storage_entry *ISE;
1192
+ typedef const struct rb_callinfo *CALL_INFO;
1193
+ typedef const struct rb_callcache *CALL_CACHE;
1194
+ typedef struct rb_call_data *CALL_DATA;
1195
+
1196
+ typedef VALUE CDHASH;
1197
+
1198
+ #ifndef FUNC_FASTCALL
1199
+ #define FUNC_FASTCALL(x) x
1200
+ #endif
1201
+
1202
+ typedef rb_control_frame_t *
1203
+ (FUNC_FASTCALL(*rb_insn_func_t))(rb_execution_context_t *, rb_control_frame_t *);
1204
+
1205
+ #define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1206
+ #define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1207
+
1208
+ #define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1209
+ #define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1210
+ #define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1211
+
1212
+ enum vm_frame_env_flags {
1213
+ /* Frame/Environment flag bits:
1214
+ * MMMM MMMM MMMM MMMM ____ FFFF FFFE EEEX (LSB)
1215
+ *
1216
+ * X : tag for GC marking (It seems as Fixnum)
1217
+ * EEE : 4 bits Env flags
1218
+ * FF..: 7 bits Frame flags
1219
+ * MM..: 15 bits frame magic (to check frame corruption)
1220
+ */
1221
+
1222
+ /* frame types */
1223
+ VM_FRAME_MAGIC_METHOD = 0x11110001,
1224
+ VM_FRAME_MAGIC_BLOCK = 0x22220001,
1225
+ VM_FRAME_MAGIC_CLASS = 0x33330001,
1226
+ VM_FRAME_MAGIC_TOP = 0x44440001,
1227
+ VM_FRAME_MAGIC_CFUNC = 0x55550001,
1228
+ VM_FRAME_MAGIC_IFUNC = 0x66660001,
1229
+ VM_FRAME_MAGIC_EVAL = 0x77770001,
1230
+ VM_FRAME_MAGIC_RESCUE = 0x78880001,
1231
+ VM_FRAME_MAGIC_DUMMY = 0x79990001,
1232
+
1233
+ VM_FRAME_MAGIC_MASK = 0x7fff0001,
1234
+
1235
+ /* frame flag */
1236
+ VM_FRAME_FLAG_FINISH = 0x0020,
1237
+ VM_FRAME_FLAG_BMETHOD = 0x0040,
1238
+ VM_FRAME_FLAG_CFRAME = 0x0080,
1239
+ VM_FRAME_FLAG_LAMBDA = 0x0100,
1240
+ VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1241
+ VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1242
+ VM_FRAME_FLAG_PASSED = 0x0800,
1243
+
1244
+ /* env flag */
1245
+ VM_ENV_FLAG_LOCAL = 0x0002,
1246
+ VM_ENV_FLAG_ESCAPED = 0x0004,
1247
+ VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1248
+ VM_ENV_FLAG_ISOLATED = 0x0010,
1249
+ };
1250
+
1251
+ #define VM_ENV_DATA_SIZE ( 3)
1252
+
1253
+ #define VM_ENV_DATA_INDEX_ME_CREF (-2) /* ep[-2] */
1254
+ #define VM_ENV_DATA_INDEX_SPECVAL (-1) /* ep[-1] */
1255
+ #define VM_ENV_DATA_INDEX_FLAGS ( 0) /* ep[ 0] */
1256
+ #define VM_ENV_DATA_INDEX_ENV ( 1) /* ep[ 1] */
1257
+
1258
+ #define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1259
+
1260
+ static inline void VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value);
1261
+
1262
+ static inline void
1263
+ VM_ENV_FLAGS_SET(const VALUE *ep, VALUE flag)
1264
+ {
1265
+ VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1266
+ VM_ASSERT(FIXNUM_P(flags));
1267
+ VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1268
+ }
1269
+
1270
+ static inline void
1271
+ VM_ENV_FLAGS_UNSET(const VALUE *ep, VALUE flag)
1272
+ {
1273
+ VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1274
+ VM_ASSERT(FIXNUM_P(flags));
1275
+ VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1276
+ }
1277
+
1278
+ static inline unsigned long
1279
+ VM_ENV_FLAGS(const VALUE *ep, long flag)
1280
+ {
1281
+ VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1282
+ VM_ASSERT(FIXNUM_P(flags));
1283
+ return flags & flag;
1284
+ }
1285
+
1286
+ static inline unsigned long
1287
+ VM_FRAME_TYPE(const rb_control_frame_t *cfp)
1288
+ {
1289
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1290
+ }
1291
+
1292
+ static inline int
1293
+ VM_FRAME_LAMBDA_P(const rb_control_frame_t *cfp)
1294
+ {
1295
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1296
+ }
1297
+
1298
+ static inline int
1299
+ VM_FRAME_CFRAME_KW_P(const rb_control_frame_t *cfp)
1300
+ {
1301
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1302
+ }
1303
+
1304
+ static inline int
1305
+ VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
1306
+ {
1307
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1308
+ }
1309
+
1310
+ static inline int
1311
+ VM_FRAME_BMETHOD_P(const rb_control_frame_t *cfp)
1312
+ {
1313
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1314
+ }
1315
+
1316
+ static inline int
1317
+ rb_obj_is_iseq(VALUE iseq)
1318
+ {
1319
+ return imemo_type_p(iseq, imemo_iseq);
1320
+ }
1321
+
1322
+ #if VM_CHECK_MODE > 0
1323
+ #define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1324
+ #endif
1325
+
1326
+ static inline int
1327
+ VM_FRAME_CFRAME_P(const rb_control_frame_t *cfp)
1328
+ {
1329
+ int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1330
+ VM_ASSERT(RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) != cframe_p ||
1331
+ (VM_FRAME_TYPE(cfp) & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY);
1332
+ return cframe_p;
1333
+ }
1334
+
1335
+ static inline int
1336
+ VM_FRAME_RUBYFRAME_P(const rb_control_frame_t *cfp)
1337
+ {
1338
+ return !VM_FRAME_CFRAME_P(cfp);
1339
+ }
1340
+
1341
+ #define RUBYVM_CFUNC_FRAME_P(cfp) \
1342
+ (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1343
+
1344
+ #define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1345
+ #define VM_BLOCK_HANDLER_NONE 0
1346
+
1347
+ static inline int
1348
+ VM_ENV_LOCAL_P(const VALUE *ep)
1349
+ {
1350
+ return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1351
+ }
1352
+
1353
+ static inline const VALUE *
1354
+ VM_ENV_PREV_EP(const VALUE *ep)
1355
+ {
1356
+ VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1357
+ return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1358
+ }
1359
+
1360
+ static inline VALUE
1361
+ VM_ENV_BLOCK_HANDLER(const VALUE *ep)
1362
+ {
1363
+ VM_ASSERT(VM_ENV_LOCAL_P(ep));
1364
+ return ep[VM_ENV_DATA_INDEX_SPECVAL];
1365
+ }
1366
+
1367
+ #if VM_CHECK_MODE > 0
1368
+ int rb_vm_ep_in_heap_p(const VALUE *ep);
1369
+ #endif
1370
+
1371
+ static inline int
1372
+ VM_ENV_ESCAPED_P(const VALUE *ep)
1373
+ {
1374
+ VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1375
+ return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1376
+ }
1377
+
1378
+ #if VM_CHECK_MODE > 0
1379
+ static inline int
1380
+ vm_assert_env(VALUE obj)
1381
+ {
1382
+ VM_ASSERT(imemo_type_p(obj, imemo_env));
1383
+ return 1;
1384
+ }
1385
+ #endif
1386
+
1387
+ RBIMPL_ATTR_NONNULL((1))
1388
+ static inline VALUE
1389
+ VM_ENV_ENVVAL(const VALUE *ep)
1390
+ {
1391
+ VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1392
+ VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1393
+ VM_ASSERT(vm_assert_env(envval));
1394
+ return envval;
1395
+ }
1396
+
1397
+ RBIMPL_ATTR_NONNULL((1))
1398
+ static inline const rb_env_t *
1399
+ VM_ENV_ENVVAL_PTR(const VALUE *ep)
1400
+ {
1401
+ return (const rb_env_t *)VM_ENV_ENVVAL(ep);
1402
+ }
1403
+
1404
+ static inline const rb_env_t *
1405
+ vm_env_new(VALUE *env_ep, VALUE *env_body, unsigned int env_size, const rb_iseq_t *iseq)
1406
+ {
1407
+ rb_env_t *env = (rb_env_t *)rb_imemo_new(imemo_env, (VALUE)env_ep, (VALUE)env_body, 0, (VALUE)iseq);
1408
+ env->env_size = env_size;
1409
+ env_ep[VM_ENV_DATA_INDEX_ENV] = (VALUE)env;
1410
+ return env;
1411
+ }
1412
+
1413
+ static inline void
1414
+ VM_FORCE_WRITE(const VALUE *ptr, VALUE v)
1415
+ {
1416
+ *((VALUE *)ptr) = v;
1417
+ }
1418
+
1419
+ static inline void
1420
+ VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value)
1421
+ {
1422
+ VM_ASSERT(RB_SPECIAL_CONST_P(special_const_value));
1423
+ VM_FORCE_WRITE(ptr, special_const_value);
1424
+ }
1425
+
1426
+ static inline void
1427
+ VM_STACK_ENV_WRITE(const VALUE *ep, int index, VALUE v)
1428
+ {
1429
+ VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1430
+ VM_FORCE_WRITE(&ep[index], v);
1431
+ }
1432
+
1433
+ const VALUE *rb_vm_ep_local_ep(const VALUE *ep);
1434
+ const VALUE *rb_vm_proc_local_ep(VALUE proc);
1435
+ void rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep);
1436
+ void rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src);
1437
+
1438
+ VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp);
1439
+
1440
+ #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1441
+ #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1442
+
1443
+ #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1444
+ ((void *)(ecfp) > (void *)(cfp))
1445
+
1446
+ static inline const rb_control_frame_t *
1447
+ RUBY_VM_END_CONTROL_FRAME(const rb_execution_context_t *ec)
1448
+ {
1449
+ return (rb_control_frame_t *)(ec->vm_stack + ec->vm_stack_size);
1450
+ }
1451
+
1452
+ static inline int
1453
+ RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
1454
+ {
1455
+ return !RUBY_VM_VALID_CONTROL_FRAME_P(cfp, RUBY_VM_END_CONTROL_FRAME(ec));
1456
+ }
1457
+
1458
+ static inline int
1459
+ VM_BH_ISEQ_BLOCK_P(VALUE block_handler)
1460
+ {
1461
+ if ((block_handler & 0x03) == 0x01) {
1462
+ #if VM_CHECK_MODE > 0
1463
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1464
+ VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq));
1465
+ #endif
1466
+ return 1;
1467
+ }
1468
+ else {
1469
+ return 0;
1470
+ }
1471
+ }
1472
+
1473
+ static inline VALUE
1474
+ VM_BH_FROM_ISEQ_BLOCK(const struct rb_captured_block *captured)
1475
+ {
1476
+ VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1477
+ VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1478
+ return block_handler;
1479
+ }
1480
+
1481
+ static inline const struct rb_captured_block *
1482
+ VM_BH_TO_ISEQ_BLOCK(VALUE block_handler)
1483
+ {
1484
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1485
+ VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1486
+ return captured;
1487
+ }
1488
+
1489
+ static inline int
1490
+ VM_BH_IFUNC_P(VALUE block_handler)
1491
+ {
1492
+ if ((block_handler & 0x03) == 0x03) {
1493
+ #if VM_CHECK_MODE > 0
1494
+ struct rb_captured_block *captured = (void *)(block_handler & ~0x03);
1495
+ VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc));
1496
+ #endif
1497
+ return 1;
1498
+ }
1499
+ else {
1500
+ return 0;
1501
+ }
1502
+ }
1503
+
1504
+ static inline VALUE
1505
+ VM_BH_FROM_IFUNC_BLOCK(const struct rb_captured_block *captured)
1506
+ {
1507
+ VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1508
+ VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1509
+ return block_handler;
1510
+ }
1511
+
1512
+ static inline const struct rb_captured_block *
1513
+ VM_BH_TO_IFUNC_BLOCK(VALUE block_handler)
1514
+ {
1515
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1516
+ VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1517
+ return captured;
1518
+ }
1519
+
1520
+ static inline const struct rb_captured_block *
1521
+ VM_BH_TO_CAPT_BLOCK(VALUE block_handler)
1522
+ {
1523
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1524
+ VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1525
+ return captured;
1526
+ }
1527
+
1528
+ static inline enum rb_block_handler_type
1529
+ vm_block_handler_type(VALUE block_handler)
1530
+ {
1531
+ if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1532
+ return block_handler_type_iseq;
1533
+ }
1534
+ else if (VM_BH_IFUNC_P(block_handler)) {
1535
+ return block_handler_type_ifunc;
1536
+ }
1537
+ else if (SYMBOL_P(block_handler)) {
1538
+ return block_handler_type_symbol;
1539
+ }
1540
+ else {
1541
+ VM_ASSERT(rb_obj_is_proc(block_handler));
1542
+ return block_handler_type_proc;
1543
+ }
1544
+ }
1545
+
1546
+ static inline void
1547
+ vm_block_handler_verify(MAYBE_UNUSED(VALUE block_handler))
1548
+ {
1549
+ VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1550
+ (vm_block_handler_type(block_handler), 1));
1551
+ }
1552
+
1553
+ static inline enum rb_block_type
1554
+ vm_block_type(const struct rb_block *block)
1555
+ {
1556
+ #if VM_CHECK_MODE > 0
1557
+ switch (block->type) {
1558
+ case block_type_iseq:
1559
+ VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq));
1560
+ break;
1561
+ case block_type_ifunc:
1562
+ VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc));
1563
+ break;
1564
+ case block_type_symbol:
1565
+ VM_ASSERT(SYMBOL_P(block->as.symbol));
1566
+ break;
1567
+ case block_type_proc:
1568
+ VM_ASSERT(rb_obj_is_proc(block->as.proc));
1569
+ break;
1570
+ }
1571
+ #endif
1572
+ return block->type;
1573
+ }
1574
+
1575
+ static inline void
1576
+ vm_block_type_set(const struct rb_block *block, enum rb_block_type type)
1577
+ {
1578
+ struct rb_block *mb = (struct rb_block *)block;
1579
+ mb->type = type;
1580
+ }
1581
+
1582
+ static inline const struct rb_block *
1583
+ vm_proc_block(VALUE procval)
1584
+ {
1585
+ VM_ASSERT(rb_obj_is_proc(procval));
1586
+ return &((rb_proc_t *)RTYPEDDATA_DATA(procval))->block;
1587
+ }
1588
+
1589
+ static inline const rb_iseq_t *vm_block_iseq(const struct rb_block *block);
1590
+ static inline const VALUE *vm_block_ep(const struct rb_block *block);
1591
+
1592
+ static inline const rb_iseq_t *
1593
+ vm_proc_iseq(VALUE procval)
1594
+ {
1595
+ return vm_block_iseq(vm_proc_block(procval));
1596
+ }
1597
+
1598
+ static inline const VALUE *
1599
+ vm_proc_ep(VALUE procval)
1600
+ {
1601
+ return vm_block_ep(vm_proc_block(procval));
1602
+ }
1603
+
1604
+ static inline const rb_iseq_t *
1605
+ vm_block_iseq(const struct rb_block *block)
1606
+ {
1607
+ switch (vm_block_type(block)) {
1608
+ case block_type_iseq: return rb_iseq_check(block->as.captured.code.iseq);
1609
+ case block_type_proc: return vm_proc_iseq(block->as.proc);
1610
+ case block_type_ifunc:
1611
+ case block_type_symbol: return NULL;
1612
+ }
1613
+ VM_UNREACHABLE(vm_block_iseq);
1614
+ return NULL;
1615
+ }
1616
+
1617
+ static inline const VALUE *
1618
+ vm_block_ep(const struct rb_block *block)
1619
+ {
1620
+ switch (vm_block_type(block)) {
1621
+ case block_type_iseq:
1622
+ case block_type_ifunc: return block->as.captured.ep;
1623
+ case block_type_proc: return vm_proc_ep(block->as.proc);
1624
+ case block_type_symbol: return NULL;
1625
+ }
1626
+ VM_UNREACHABLE(vm_block_ep);
1627
+ return NULL;
1628
+ }
1629
+
1630
+ static inline VALUE
1631
+ vm_block_self(const struct rb_block *block)
1632
+ {
1633
+ switch (vm_block_type(block)) {
1634
+ case block_type_iseq:
1635
+ case block_type_ifunc:
1636
+ return block->as.captured.self;
1637
+ case block_type_proc:
1638
+ return vm_block_self(vm_proc_block(block->as.proc));
1639
+ case block_type_symbol:
1640
+ return Qundef;
1641
+ }
1642
+ VM_UNREACHABLE(vm_block_self);
1643
+ return Qundef;
1644
+ }
1645
+
1646
+ static inline VALUE
1647
+ VM_BH_TO_SYMBOL(VALUE block_handler)
1648
+ {
1649
+ VM_ASSERT(SYMBOL_P(block_handler));
1650
+ return block_handler;
1651
+ }
1652
+
1653
+ static inline VALUE
1654
+ VM_BH_FROM_SYMBOL(VALUE symbol)
1655
+ {
1656
+ VM_ASSERT(SYMBOL_P(symbol));
1657
+ return symbol;
1658
+ }
1659
+
1660
+ static inline VALUE
1661
+ VM_BH_TO_PROC(VALUE block_handler)
1662
+ {
1663
+ VM_ASSERT(rb_obj_is_proc(block_handler));
1664
+ return block_handler;
1665
+ }
1666
+
1667
+ static inline VALUE
1668
+ VM_BH_FROM_PROC(VALUE procval)
1669
+ {
1670
+ VM_ASSERT(rb_obj_is_proc(procval));
1671
+ return procval;
1672
+ }
1673
+
1674
+ /* VM related object allocate functions */
1675
+ VALUE rb_thread_alloc(VALUE klass);
1676
+ VALUE rb_binding_alloc(VALUE klass);
1677
+ VALUE rb_proc_alloc(VALUE klass);
1678
+ VALUE rb_proc_dup(VALUE self);
1679
+
1680
+ /* for debug */
1681
+ extern void rb_vmdebug_stack_dump_raw(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1682
+ extern void rb_vmdebug_debug_print_pre(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, const VALUE *_pc);
1683
+ extern void rb_vmdebug_debug_print_post(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1684
+
1685
+ #define SDR() rb_vmdebug_stack_dump_raw(GET_EC(), GET_EC()->cfp)
1686
+ #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_EC(), (cfp))
1687
+ void rb_vm_bugreport(const void *);
1688
+ typedef void (*ruby_sighandler_t)(int);
1689
+ RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 4, 5)
1690
+ NORETURN(void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *, const char *fmt, ...));
1691
+
1692
+ /* functions about thread/vm execution */
1693
+ RUBY_SYMBOL_EXPORT_BEGIN
1694
+ VALUE rb_iseq_eval(const rb_iseq_t *iseq);
1695
+ VALUE rb_iseq_eval_main(const rb_iseq_t *iseq);
1696
+ VALUE rb_iseq_path(const rb_iseq_t *iseq);
1697
+ VALUE rb_iseq_realpath(const rb_iseq_t *iseq);
1698
+ RUBY_SYMBOL_EXPORT_END
1699
+
1700
+ VALUE rb_iseq_pathobj_new(VALUE path, VALUE realpath);
1701
+ void rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath);
1702
+
1703
+ int rb_ec_frame_method_id_and_class(const rb_execution_context_t *ec, ID *idp, ID *called_idp, VALUE *klassp);
1704
+ void rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause);
1705
+
1706
+ VALUE rb_vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, int argc, const VALUE *argv, int kw_splat, VALUE block_handler);
1707
+
1708
+ VALUE rb_vm_make_proc_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda);
1709
+ static inline VALUE
1710
+ rb_vm_make_proc(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1711
+ {
1712
+ return rb_vm_make_proc_lambda(ec, captured, klass, 0);
1713
+ }
1714
+
1715
+ static inline VALUE
1716
+ rb_vm_make_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1717
+ {
1718
+ return rb_vm_make_proc_lambda(ec, captured, klass, 1);
1719
+ }
1720
+
1721
+ VALUE rb_vm_make_binding(const rb_execution_context_t *ec, const rb_control_frame_t *src_cfp);
1722
+ VALUE rb_vm_env_local_variables(const rb_env_t *env);
1723
+ const rb_env_t *rb_vm_env_prev_env(const rb_env_t *env);
1724
+ const VALUE *rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars);
1725
+ void rb_vm_inc_const_missing_count(void);
1726
+ VALUE rb_vm_call_kw(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc,
1727
+ const VALUE *argv, const rb_callable_method_entry_t *me, int kw_splat);
1728
+ void rb_vm_pop_frame_no_int(rb_execution_context_t *ec);
1729
+ void rb_vm_pop_frame(rb_execution_context_t *ec);
1730
+
1731
+ void rb_thread_start_timer_thread(void);
1732
+ void rb_thread_stop_timer_thread(void);
1733
+ void rb_thread_reset_timer_thread(void);
1734
+ void rb_thread_wakeup_timer_thread(int);
1735
+
1736
+ static inline void
1737
+ rb_vm_living_threads_init(rb_vm_t *vm)
1738
+ {
1739
+ ccan_list_head_init(&vm->waiting_fds);
1740
+ ccan_list_head_init(&vm->workqueue);
1741
+ ccan_list_head_init(&vm->ractor.set);
1742
+ }
1743
+
1744
+ typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
1745
+ rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1746
+ rb_control_frame_t *rb_vm_get_binding_creatable_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1747
+ VALUE *rb_vm_svar_lep(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1748
+ int rb_vm_get_sourceline(const rb_control_frame_t *);
1749
+ void rb_vm_stack_to_heap(rb_execution_context_t *ec);
1750
+ void ruby_thread_init_stack(rb_thread_t *th);
1751
+ rb_thread_t * ruby_thread_from_native(void);
1752
+ int ruby_thread_set_native(rb_thread_t *th);
1753
+ int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp);
1754
+ void rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp);
1755
+ void rb_vm_env_write(const VALUE *ep, int index, VALUE v);
1756
+ VALUE rb_vm_bh_to_procval(const rb_execution_context_t *ec, VALUE block_handler);
1757
+
1758
+ void rb_vm_register_special_exception_str(enum ruby_special_exceptions sp, VALUE exception_class, VALUE mesg);
1759
+
1760
+ #define rb_vm_register_special_exception(sp, e, m) \
1761
+ rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
1762
+
1763
+ void rb_gc_mark_machine_stack(const rb_execution_context_t *ec);
1764
+
1765
+ void rb_vm_rewrite_cref(rb_cref_t *node, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr);
1766
+
1767
+ const rb_callable_method_entry_t *rb_vm_frame_method_entry(const rb_control_frame_t *cfp);
1768
+
1769
+ #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
1770
+
1771
+ #define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) do { \
1772
+ STATIC_ASSERT(sizeof_sp, sizeof(*(sp)) == sizeof(VALUE)); \
1773
+ STATIC_ASSERT(sizeof_cfp, sizeof(*(cfp)) == sizeof(rb_control_frame_t)); \
1774
+ const struct rb_control_frame_struct *bound = (void *)&(sp)[(margin)]; \
1775
+ if (UNLIKELY((cfp) <= &bound[1])) { \
1776
+ vm_stackoverflow(); \
1777
+ } \
1778
+ } while (0)
1779
+
1780
+ #define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
1781
+ CHECK_VM_STACK_OVERFLOW0((cfp), (cfp)->sp, (margin))
1782
+
1783
+ VALUE rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr);
1784
+
1785
+ rb_execution_context_t *rb_vm_main_ractor_ec(rb_vm_t *vm); // ractor.c
1786
+
1787
+ /* for thread */
1788
+
1789
+ #if RUBY_VM_THREAD_MODEL == 2
1790
+
1791
+ RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor; // ractor.c
1792
+ RUBY_EXTERN rb_vm_t *ruby_current_vm_ptr;
1793
+ RUBY_EXTERN rb_event_flag_t ruby_vm_event_flags;
1794
+ RUBY_EXTERN rb_event_flag_t ruby_vm_event_enabled_global_flags;
1795
+ RUBY_EXTERN unsigned int ruby_vm_event_local_num;
1796
+
1797
+ #define GET_VM() rb_current_vm()
1798
+ #define GET_RACTOR() rb_current_ractor()
1799
+ #define GET_THREAD() rb_current_thread()
1800
+ #define GET_EC() rb_current_execution_context(true)
1801
+
1802
+ static inline rb_thread_t *
1803
+ rb_ec_thread_ptr(const rb_execution_context_t *ec)
1804
+ {
1805
+ return ec->thread_ptr;
1806
+ }
1807
+
1808
+ static inline rb_ractor_t *
1809
+ rb_ec_ractor_ptr(const rb_execution_context_t *ec)
1810
+ {
1811
+ const rb_thread_t *th = rb_ec_thread_ptr(ec);
1812
+ if (th) {
1813
+ VM_ASSERT(th->ractor != NULL);
1814
+ return th->ractor;
1815
+ }
1816
+ else {
1817
+ return NULL;
1818
+ }
1819
+ }
1820
+
1821
+ static inline rb_vm_t *
1822
+ rb_ec_vm_ptr(const rb_execution_context_t *ec)
1823
+ {
1824
+ const rb_thread_t *th = rb_ec_thread_ptr(ec);
1825
+ if (th) {
1826
+ return th->vm;
1827
+ }
1828
+ else {
1829
+ return NULL;
1830
+ }
1831
+ }
1832
+
1833
+ static inline rb_execution_context_t *
1834
+ rb_current_execution_context(bool expect_ec)
1835
+ {
1836
+ #ifdef RB_THREAD_LOCAL_SPECIFIER
1837
+ #ifdef __APPLE__
1838
+ rb_execution_context_t *ec = rb_current_ec();
1839
+ #else
1840
+ rb_execution_context_t *ec = ruby_current_ec;
1841
+ #endif
1842
+ #else
1843
+ rb_execution_context_t *ec = native_tls_get(ruby_current_ec_key);
1844
+ #endif
1845
+ VM_ASSERT(!expect_ec || ec != NULL);
1846
+ return ec;
1847
+ }
1848
+
1849
+ static inline rb_thread_t *
1850
+ rb_current_thread(void)
1851
+ {
1852
+ const rb_execution_context_t *ec = GET_EC();
1853
+ return rb_ec_thread_ptr(ec);
1854
+ }
1855
+
1856
+ static inline rb_ractor_t *
1857
+ rb_current_ractor_raw(bool expect)
1858
+ {
1859
+ if (ruby_single_main_ractor) {
1860
+ return ruby_single_main_ractor;
1861
+ }
1862
+ else {
1863
+ const rb_execution_context_t *ec = rb_current_execution_context(expect);
1864
+ return (expect || ec) ? rb_ec_ractor_ptr(ec) : NULL;
1865
+ }
1866
+ }
1867
+
1868
+ static inline rb_ractor_t *
1869
+ rb_current_ractor(void)
1870
+ {
1871
+ return rb_current_ractor_raw(true);
1872
+ }
1873
+
1874
+ static inline rb_vm_t *
1875
+ rb_current_vm(void)
1876
+ {
1877
+ #if 0 // TODO: reconsider the assertions
1878
+ VM_ASSERT(ruby_current_vm_ptr == NULL ||
1879
+ ruby_current_execution_context_ptr == NULL ||
1880
+ rb_ec_thread_ptr(GET_EC()) == NULL ||
1881
+ rb_ec_thread_ptr(GET_EC())->status == THREAD_KILLED ||
1882
+ rb_ec_vm_ptr(GET_EC()) == ruby_current_vm_ptr);
1883
+ #endif
1884
+
1885
+ return ruby_current_vm_ptr;
1886
+ }
1887
+
1888
+ void rb_ec_vm_lock_rec_release(const rb_execution_context_t *ec,
1889
+ unsigned int recorded_lock_rec,
1890
+ unsigned int current_lock_rec);
1891
+
1892
+ static inline unsigned int
1893
+ rb_ec_vm_lock_rec(const rb_execution_context_t *ec)
1894
+ {
1895
+ rb_vm_t *vm = rb_ec_vm_ptr(ec);
1896
+
1897
+ if (vm->ractor.sync.lock_owner != rb_ec_ractor_ptr(ec)) {
1898
+ return 0;
1899
+ }
1900
+ else {
1901
+ return vm->ractor.sync.lock_rec;
1902
+ }
1903
+ }
1904
+
1905
+ #else
1906
+ #error "unsupported thread model"
1907
+ #endif
1908
+
1909
+ enum {
1910
+ TIMER_INTERRUPT_MASK = 0x01,
1911
+ PENDING_INTERRUPT_MASK = 0x02,
1912
+ POSTPONED_JOB_INTERRUPT_MASK = 0x04,
1913
+ TRAP_INTERRUPT_MASK = 0x08,
1914
+ TERMINATE_INTERRUPT_MASK = 0x10,
1915
+ VM_BARRIER_INTERRUPT_MASK = 0x20,
1916
+ };
1917
+
1918
+ #define RUBY_VM_SET_TIMER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TIMER_INTERRUPT_MASK)
1919
+ #define RUBY_VM_SET_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, PENDING_INTERRUPT_MASK)
1920
+ #define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
1921
+ #define RUBY_VM_SET_TRAP_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TRAP_INTERRUPT_MASK)
1922
+ #define RUBY_VM_SET_TERMINATE_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TERMINATE_INTERRUPT_MASK)
1923
+ #define RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, VM_BARRIER_INTERRUPT_MASK)
1924
+ #define RUBY_VM_INTERRUPTED(ec) ((ec)->interrupt_flag & ~(ec)->interrupt_mask & \
1925
+ (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
1926
+
1927
+ static inline bool
1928
+ RUBY_VM_INTERRUPTED_ANY(rb_execution_context_t *ec)
1929
+ {
1930
+ #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
1931
+ uint32_t current_clock = rb_ec_vm_ptr(ec)->clock;
1932
+
1933
+ if (current_clock != ec->checked_clock) {
1934
+ ec->checked_clock = current_clock;
1935
+ RUBY_VM_SET_TIMER_INTERRUPT(ec);
1936
+ }
1937
+ #endif
1938
+ return ec->interrupt_flag & ~(ec)->interrupt_mask;
1939
+ }
1940
+
1941
+ VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt);
1942
+ int rb_signal_buff_size(void);
1943
+ int rb_signal_exec(rb_thread_t *th, int sig);
1944
+ void rb_threadptr_check_signal(rb_thread_t *mth);
1945
+ void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
1946
+ void rb_threadptr_signal_exit(rb_thread_t *th);
1947
+ int rb_threadptr_execute_interrupts(rb_thread_t *, int);
1948
+ void rb_threadptr_interrupt(rb_thread_t *th);
1949
+ void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th);
1950
+ void rb_threadptr_pending_interrupt_clear(rb_thread_t *th);
1951
+ void rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v);
1952
+ VALUE rb_ec_get_errinfo(const rb_execution_context_t *ec);
1953
+ void rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo);
1954
+ void rb_execution_context_update(rb_execution_context_t *ec);
1955
+ void rb_execution_context_mark(const rb_execution_context_t *ec);
1956
+ void rb_fiber_close(rb_fiber_t *fib);
1957
+ void Init_native_thread(rb_thread_t *th);
1958
+ int rb_vm_check_ints_blocking(rb_execution_context_t *ec);
1959
+
1960
+ // vm_sync.h
1961
+ void rb_vm_cond_wait(rb_vm_t *vm, rb_nativethread_cond_t *cond);
1962
+ void rb_vm_cond_timedwait(rb_vm_t *vm, rb_nativethread_cond_t *cond, unsigned long msec);
1963
+
1964
+ #define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
1965
+ static inline void
1966
+ rb_vm_check_ints(rb_execution_context_t *ec)
1967
+ {
1968
+ #ifdef RUBY_ASSERT_CRITICAL_SECTION
1969
+ VM_ASSERT(ruby_assert_critical_section_entered == 0);
1970
+ #endif
1971
+
1972
+ VM_ASSERT(ec == GET_EC());
1973
+
1974
+ if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {
1975
+ rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);
1976
+ }
1977
+ }
1978
+
1979
+ /* tracer */
1980
+
1981
+ struct rb_trace_arg_struct {
1982
+ rb_event_flag_t event;
1983
+ rb_execution_context_t *ec;
1984
+ const rb_control_frame_t *cfp;
1985
+ VALUE self;
1986
+ ID id;
1987
+ ID called_id;
1988
+ VALUE klass;
1989
+ VALUE data;
1990
+
1991
+ int klass_solved;
1992
+
1993
+ /* calc from cfp */
1994
+ int lineno;
1995
+ VALUE path;
1996
+ };
1997
+
1998
+ void rb_hook_list_mark(rb_hook_list_t *hooks);
1999
+ void rb_hook_list_mark_and_update(rb_hook_list_t *hooks);
2000
+ void rb_hook_list_free(rb_hook_list_t *hooks);
2001
+ void rb_hook_list_connect_tracepoint(VALUE target, rb_hook_list_t *list, VALUE tpval, unsigned int target_line);
2002
+ void rb_hook_list_remove_tracepoint(rb_hook_list_t *list, VALUE tpval);
2003
+
2004
+ void rb_exec_event_hooks(struct rb_trace_arg_struct *trace_arg, rb_hook_list_t *hooks, int pop_p);
2005
+
2006
+ #define EXEC_EVENT_HOOK_ORIG(ec_, hooks_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
2007
+ const rb_event_flag_t flag_arg_ = (flag_); \
2008
+ rb_hook_list_t *hooks_arg_ = (hooks_); \
2009
+ if (UNLIKELY((hooks_arg_)->events & (flag_arg_))) { \
2010
+ /* defer evaluating the other arguments */ \
2011
+ rb_exec_event_hook_orig(ec_, hooks_arg_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
2012
+ } \
2013
+ } while (0)
2014
+
2015
+ static inline void
2016
+ rb_exec_event_hook_orig(rb_execution_context_t *ec, rb_hook_list_t *hooks, rb_event_flag_t flag,
2017
+ VALUE self, ID id, ID called_id, VALUE klass, VALUE data, int pop_p)
2018
+ {
2019
+ struct rb_trace_arg_struct trace_arg;
2020
+
2021
+ VM_ASSERT((hooks->events & flag) != 0);
2022
+
2023
+ trace_arg.event = flag;
2024
+ trace_arg.ec = ec;
2025
+ trace_arg.cfp = ec->cfp;
2026
+ trace_arg.self = self;
2027
+ trace_arg.id = id;
2028
+ trace_arg.called_id = called_id;
2029
+ trace_arg.klass = klass;
2030
+ trace_arg.data = data;
2031
+ trace_arg.path = Qundef;
2032
+ trace_arg.klass_solved = 0;
2033
+
2034
+ rb_exec_event_hooks(&trace_arg, hooks, pop_p);
2035
+ }
2036
+
2037
+ struct rb_ractor_pub {
2038
+ VALUE self;
2039
+ uint32_t id;
2040
+ rb_hook_list_t hooks;
2041
+ };
2042
+
2043
+ static inline rb_hook_list_t *
2044
+ rb_ec_ractor_hooks(const rb_execution_context_t *ec)
2045
+ {
2046
+ struct rb_ractor_pub *cr_pub = (struct rb_ractor_pub *)rb_ec_ractor_ptr(ec);
2047
+ return &cr_pub->hooks;
2048
+ }
2049
+
2050
+ #define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2051
+ EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 0)
2052
+
2053
+ #define EXEC_EVENT_HOOK_AND_POP_FRAME(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2054
+ EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 1)
2055
+
2056
+ static inline void
2057
+ rb_exec_event_hook_script_compiled(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE eval_script)
2058
+ {
2059
+ EXEC_EVENT_HOOK(ec, RUBY_EVENT_SCRIPT_COMPILED, ec->cfp->self, 0, 0, 0,
2060
+ NIL_P(eval_script) ? (VALUE)iseq :
2061
+ rb_ary_new_from_args(2, eval_script, (VALUE)iseq));
2062
+ }
2063
+
2064
+ void rb_vm_trap_exit(rb_vm_t *vm);
2065
+
2066
+ RUBY_SYMBOL_EXPORT_BEGIN
2067
+
2068
+ int rb_thread_check_trap_pending(void);
2069
+
2070
+ /* #define RUBY_EVENT_RESERVED_FOR_INTERNAL_USE 0x030000 */ /* from vm_core.h */
2071
+ #define RUBY_EVENT_COVERAGE_LINE 0x010000
2072
+ #define RUBY_EVENT_COVERAGE_BRANCH 0x020000
2073
+
2074
+ extern VALUE rb_get_coverages(void);
2075
+ extern void rb_set_coverages(VALUE, int, VALUE);
2076
+ extern void rb_clear_coverages(void);
2077
+ extern void rb_reset_coverages(void);
2078
+ extern void rb_resume_coverages(void);
2079
+ extern void rb_suspend_coverages(void);
2080
+
2081
+ void rb_postponed_job_flush(rb_vm_t *vm);
2082
+
2083
+ // ractor.c
2084
+ RUBY_EXTERN VALUE rb_eRactorUnsafeError;
2085
+ RUBY_EXTERN VALUE rb_eRactorIsolationError;
2086
+
2087
+ RUBY_SYMBOL_EXPORT_END
2088
+
2089
+ #endif /* RUBY_VM_CORE_H */