debase-ruby_core_source 3.2.0 → 3.2.1

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