debase-ruby_core_source 0.10.16 → 0.10.17

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