llrb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.gitmodules +4 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +5 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +56 -0
  8. data/README.md +311 -0
  9. data/Rakefile +30 -0
  10. data/bin/bm_app_fib +41 -0
  11. data/bin/bm_empty_method +33 -0
  12. data/bin/bm_loop_while +27 -0
  13. data/bin/bm_plus +33 -0
  14. data/bin/console +14 -0
  15. data/bin/loop_while.rb +5 -0
  16. data/bin/setup +8 -0
  17. data/ext/llrb/cfg.h +124 -0
  18. data/ext/llrb/compiler.c +987 -0
  19. data/ext/llrb/compiler/funcs.h +164 -0
  20. data/ext/llrb/compiler/stack.h +43 -0
  21. data/ext/llrb/cruby.h +42 -0
  22. data/ext/llrb/cruby/ccan/build_assert/build_assert.h +40 -0
  23. data/ext/llrb/cruby/ccan/check_type/check_type.h +63 -0
  24. data/ext/llrb/cruby/ccan/container_of/container_of.h +142 -0
  25. data/ext/llrb/cruby/ccan/list/list.h +773 -0
  26. data/ext/llrb/cruby/ccan/str/str.h +16 -0
  27. data/ext/llrb/cruby/internal.h +1774 -0
  28. data/ext/llrb/cruby/iseq.h +252 -0
  29. data/ext/llrb/cruby/method.h +213 -0
  30. data/ext/llrb/cruby/node.h +520 -0
  31. data/ext/llrb/cruby/probes_helper.h +43 -0
  32. data/ext/llrb/cruby/ruby_assert.h +54 -0
  33. data/ext/llrb/cruby/ruby_atomic.h +233 -0
  34. data/ext/llrb/cruby/thread_pthread.h +54 -0
  35. data/ext/llrb/cruby/vm_core.h +1646 -0
  36. data/ext/llrb/cruby/vm_debug.h +37 -0
  37. data/ext/llrb/cruby/vm_exec.h +182 -0
  38. data/ext/llrb/cruby/vm_opts.h +57 -0
  39. data/ext/llrb/cruby_extra/id.h +220 -0
  40. data/ext/llrb/cruby_extra/insns.inc +113 -0
  41. data/ext/llrb/cruby_extra/insns_info.inc +796 -0
  42. data/ext/llrb/cruby_extra/probes.h +80 -0
  43. data/ext/llrb/extconf.rb +102 -0
  44. data/ext/llrb/llrb.c +148 -0
  45. data/ext/llrb/optimizer.cc +118 -0
  46. data/ext/llrb/parser.c +191 -0
  47. data/ext/llrb/profiler.c +336 -0
  48. data/ext/llrb_insn_checkkeyword.c +20 -0
  49. data/ext/llrb_insn_checkmatch.c +28 -0
  50. data/ext/llrb_insn_concatarray.c +23 -0
  51. data/ext/llrb_insn_concatstrings.c +21 -0
  52. data/ext/llrb_insn_defined.c +9 -0
  53. data/ext/llrb_insn_getclassvariable.c +10 -0
  54. data/ext/llrb_insn_getinstancevariable.c +44 -0
  55. data/ext/llrb_insn_getlocal.c +14 -0
  56. data/ext/llrb_insn_getlocal_level0.c +8 -0
  57. data/ext/llrb_insn_getlocal_level1.c +8 -0
  58. data/ext/llrb_insn_getspecial.c +14 -0
  59. data/ext/llrb_insn_invokeblock.c +39 -0
  60. data/ext/llrb_insn_invokesuper.c +47 -0
  61. data/ext/llrb_insn_opt_aref.c +25 -0
  62. data/ext/llrb_insn_opt_aset.c +28 -0
  63. data/ext/llrb_insn_opt_div.c +32 -0
  64. data/ext/llrb_insn_opt_eq.c +57 -0
  65. data/ext/llrb_insn_opt_ge.c +28 -0
  66. data/ext/llrb_insn_opt_gt.c +38 -0
  67. data/ext/llrb_insn_opt_le.c +29 -0
  68. data/ext/llrb_insn_opt_lt.c +38 -0
  69. data/ext/llrb_insn_opt_ltlt.c +27 -0
  70. data/ext/llrb_insn_opt_minus.c +36 -0
  71. data/ext/llrb_insn_opt_mod.c +32 -0
  72. data/ext/llrb_insn_opt_mult.c +30 -0
  73. data/ext/llrb_insn_opt_neq.c +103 -0
  74. data/ext/llrb_insn_opt_plus.c +48 -0
  75. data/ext/llrb_insn_opt_send_without_block.c +45 -0
  76. data/ext/llrb_insn_opt_str_freeze.c +12 -0
  77. data/ext/llrb_insn_putspecialobject.c +23 -0
  78. data/ext/llrb_insn_send.c +49 -0
  79. data/ext/llrb_insn_setclassvariable.c +19 -0
  80. data/ext/llrb_insn_setconstant.c +23 -0
  81. data/ext/llrb_insn_setinstancevariable.c +48 -0
  82. data/ext/llrb_insn_setlocal.c +16 -0
  83. data/ext/llrb_insn_setlocal_level0.c +9 -0
  84. data/ext/llrb_insn_setlocal_level1.c +10 -0
  85. data/ext/llrb_insn_setspecial.c +15 -0
  86. data/ext/llrb_insn_splatarray.c +13 -0
  87. data/ext/llrb_insn_throw.c +11 -0
  88. data/ext/llrb_insn_trace.c +37 -0
  89. data/ext/llrb_push_result.c +14 -0
  90. data/ext/llrb_self_from_cfp.c +12 -0
  91. data/ext/llrb_set_pc.c +8 -0
  92. data/lib/llrb.rb +2 -0
  93. data/lib/llrb/jit.rb +76 -0
  94. data/lib/llrb/start.rb +2 -0
  95. data/lib/llrb/version.rb +3 -0
  96. data/llrb.gemspec +48 -0
  97. data/wercker.yml +31 -0
  98. metadata +227 -0
@@ -0,0 +1,43 @@
1
+ #ifndef RUBY_PROBES_HELPER_H
2
+ #define RUBY_PROBES_HELPER_H
3
+
4
+ #include "ruby/ruby.h"
5
+ #include "probes.h"
6
+
7
+ struct ruby_dtrace_method_hook_args {
8
+ const char *classname;
9
+ const char *methodname;
10
+ const char *filename;
11
+ int line_no;
12
+ volatile VALUE klass;
13
+ volatile VALUE name;
14
+ };
15
+
16
+ NOINLINE(int ruby_th_dtrace_setup(rb_thread_t *, VALUE, ID, struct ruby_dtrace_method_hook_args *));
17
+
18
+ #define RUBY_DTRACE_METHOD_HOOK(name, th, klazz, id) \
19
+ do { \
20
+ if (UNLIKELY(RUBY_DTRACE_##name##_ENABLED())) { \
21
+ struct ruby_dtrace_method_hook_args args; \
22
+ if (ruby_th_dtrace_setup(th, klazz, id, &args)) { \
23
+ RUBY_DTRACE_##name(args.classname, \
24
+ args.methodname, \
25
+ args.filename, \
26
+ args.line_no); \
27
+ } \
28
+ } \
29
+ } while (0)
30
+
31
+ #define RUBY_DTRACE_METHOD_ENTRY_HOOK(th, klass, id) \
32
+ RUBY_DTRACE_METHOD_HOOK(METHOD_ENTRY, th, klass, id)
33
+
34
+ #define RUBY_DTRACE_METHOD_RETURN_HOOK(th, klass, id) \
35
+ RUBY_DTRACE_METHOD_HOOK(METHOD_RETURN, th, klass, id)
36
+
37
+ #define RUBY_DTRACE_CMETHOD_ENTRY_HOOK(th, klass, id) \
38
+ RUBY_DTRACE_METHOD_HOOK(CMETHOD_ENTRY, th, klass, id)
39
+
40
+ #define RUBY_DTRACE_CMETHOD_RETURN_HOOK(th, klass, id) \
41
+ RUBY_DTRACE_METHOD_HOOK(CMETHOD_RETURN, th, klass, id)
42
+
43
+ #endif /* RUBY_PROBES_HELPER_H */
@@ -0,0 +1,54 @@
1
+ #ifndef RUBY_ASSERT_H
2
+ #define RUBY_ASSERT_H
3
+
4
+ #include "ruby/ruby.h"
5
+
6
+ #if defined(__cplusplus)
7
+ extern "C" {
8
+ #if 0
9
+ } /* satisfy cc-mode */
10
+ #endif
11
+ #endif
12
+
13
+ NORETURN(void rb_assert_failure(const char *, int, const char *, const char *));
14
+ #ifdef RUBY_FUNCTION_NAME_STRING
15
+ # define RUBY_ASSERT_FAIL(expr) \
16
+ rb_assert_failure(__FILE__, __LINE__, RUBY_FUNCTION_NAME_STRING, expr)
17
+ #else
18
+ # define RUBY_ASSERT_FAIL(expr) \
19
+ rb_assert_failure(__FILE__, __LINE__, NULL, expr)
20
+ #endif
21
+ #define RUBY_ASSERT_MESG(expr, mesg) \
22
+ ((expr) ? (void)0 : RUBY_ASSERT_FAIL(mesg))
23
+ #ifdef HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P
24
+ # define RUBY_ASSERT_MESG_WHEN(cond, expr, mesg) \
25
+ __builtin_choose_expr( \
26
+ __builtin_constant_p(cond), \
27
+ __builtin_choose_expr(cond, RUBY_ASSERT_MESG(expr, mesg), (void)0), \
28
+ RUBY_ASSERT_MESG(!(cond) || (expr), mesg))
29
+ #else
30
+ # define RUBY_ASSERT_MESG_WHEN(cond, expr, mesg) \
31
+ RUBY_ASSERT_MESG(!(cond) || (expr), mesg)
32
+ #endif
33
+ #define RUBY_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(!RUBY_NDEBUG+0, expr, #expr)
34
+ #define RUBY_ASSERT_WHEN(cond, expr) RUBY_ASSERT_MESG_WHEN(cond, expr, #expr)
35
+
36
+ #undef assert
37
+ #define assert RUBY_ASSERT
38
+
39
+ #ifndef RUBY_NDEBUG
40
+ # ifdef NDEBUG
41
+ # define RUBY_NDEBUG 1
42
+ # else
43
+ # define RUBY_NDEBUG 0
44
+ # endif
45
+ #endif
46
+
47
+ #if defined(__cplusplus)
48
+ #if 0
49
+ { /* satisfy cc-mode */
50
+ #endif
51
+ } /* extern "C" { */
52
+ #endif
53
+
54
+ #endif
@@ -0,0 +1,233 @@
1
+ #ifndef RUBY_ATOMIC_H
2
+ #define RUBY_ATOMIC_H
3
+
4
+ #if 0
5
+ #elif defined HAVE_GCC_ATOMIC_BUILTINS
6
+ typedef unsigned int rb_atomic_t;
7
+ # define ATOMIC_SET(var, val) (void)__atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST)
8
+ # define ATOMIC_INC(var) __atomic_fetch_add(&(var), 1, __ATOMIC_SEQ_CST)
9
+ # define ATOMIC_DEC(var) __atomic_fetch_sub(&(var), 1, __ATOMIC_SEQ_CST)
10
+ # define ATOMIC_OR(var, val) __atomic_fetch_or(&(var), (val), __ATOMIC_SEQ_CST)
11
+ # define ATOMIC_EXCHANGE(var, val) __atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST)
12
+ # define ATOMIC_CAS(var, oldval, newval) \
13
+ ({ __typeof__(var) oldvaldup = (oldval); /* oldval should not be modified */ \
14
+ __atomic_compare_exchange_n(&(var), &oldvaldup, (newval), 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
15
+ oldvaldup; })
16
+
17
+ # define ATOMIC_SIZE_ADD(var, val) __atomic_fetch_add(&(var), (val), __ATOMIC_SEQ_CST)
18
+ # define ATOMIC_SIZE_SUB(var, val) __atomic_fetch_sub(&(var), (val), __ATOMIC_SEQ_CST)
19
+
20
+ # define RUBY_ATOMIC_GENERIC_MACRO 1
21
+
22
+ #elif defined HAVE_GCC_SYNC_BUILTINS
23
+ /* @shyouhei hack to support atomic operations in case of gcc. Gcc
24
+ * has its own pseudo-insns to support them. See info, or
25
+ * http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html */
26
+
27
+ typedef unsigned int rb_atomic_t; /* Anything OK */
28
+ # define ATOMIC_SET(var, val) (void)__sync_lock_test_and_set(&(var), (val))
29
+ # define ATOMIC_INC(var) __sync_fetch_and_add(&(var), 1)
30
+ # define ATOMIC_DEC(var) __sync_fetch_and_sub(&(var), 1)
31
+ # define ATOMIC_OR(var, val) __sync_fetch_and_or(&(var), (val))
32
+ # define ATOMIC_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
33
+ # define ATOMIC_CAS(var, oldval, newval) __sync_val_compare_and_swap(&(var), (oldval), (newval))
34
+
35
+ # define ATOMIC_SIZE_ADD(var, val) __sync_fetch_and_add(&(var), (val))
36
+ # define ATOMIC_SIZE_SUB(var, val) __sync_fetch_and_sub(&(var), (val))
37
+
38
+ # define RUBY_ATOMIC_GENERIC_MACRO 1
39
+
40
+ #elif defined _WIN32
41
+ #if defined _MSC_VER && _MSC_VER > 1200
42
+ #pragma intrinsic(_InterlockedOr)
43
+ #endif
44
+ typedef LONG rb_atomic_t;
45
+
46
+ # define ATOMIC_SET(var, val) InterlockedExchange(&(var), (val))
47
+ # define ATOMIC_INC(var) InterlockedIncrement(&(var))
48
+ # define ATOMIC_DEC(var) InterlockedDecrement(&(var))
49
+ #if defined __GNUC__
50
+ # define ATOMIC_OR(var, val) __asm__("lock\n\t" "orl\t%1, %0" : "=m"(var) : "Ir"(val))
51
+ #elif defined _MSC_VER && _MSC_VER <= 1200
52
+ # define ATOMIC_OR(var, val) rb_w32_atomic_or(&(var), (val))
53
+ static inline void
54
+ rb_w32_atomic_or(volatile rb_atomic_t *var, rb_atomic_t val)
55
+ {
56
+ #ifdef _M_IX86
57
+ __asm mov eax, var;
58
+ __asm mov ecx, val;
59
+ __asm lock or [eax], ecx;
60
+ #else
61
+ #error unsupported architecture
62
+ #endif
63
+ }
64
+ #else
65
+ # define ATOMIC_OR(var, val) _InterlockedOr(&(var), (val))
66
+ #endif
67
+ # define ATOMIC_EXCHANGE(var, val) InterlockedExchange(&(var), (val))
68
+ # define ATOMIC_CAS(var, oldval, newval) InterlockedCompareExchange(&(var), (newval), (oldval))
69
+ # if defined _MSC_VER && _MSC_VER <= 1200
70
+ static inline rb_atomic_t
71
+ rb_w32_atomic_cas(volatile rb_atomic_t *var, rb_atomic_t oldval, rb_atomic_t newval)
72
+ {
73
+ return (rb_atomic_t)InterlockedCompareExchange((PVOID *)var, (PVOID)newval, (PVOID)oldval);
74
+ }
75
+ # undef ATOMIC_CAS
76
+ # define ATOMIC_CAS(var, oldval, newval) rb_w32_atomic_cas(&(var), (oldval), (newval))
77
+ # endif
78
+ # ifdef _M_AMD64
79
+ # define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd64((LONG_LONG *)&(var), (val))
80
+ # define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd64((LONG_LONG *)&(var), -(LONG)(val))
81
+ # define ATOMIC_SIZE_INC(var) InterlockedIncrement64(&(var))
82
+ # define ATOMIC_SIZE_DEC(var) InterlockedDecrement64(&(var))
83
+ # define ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange64(&(var), (val))
84
+ # define ATOMIC_SIZE_CAS(var, oldval, newval) InterlockedCompareExchange64(&(var), (newval), (oldval))
85
+ # else
86
+ # define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd((LONG *)&(var), (val))
87
+ # define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd((LONG *)&(var), -(LONG)(val))
88
+ # define ATOMIC_SIZE_INC(var) InterlockedIncrement((LONG *)&(var))
89
+ # define ATOMIC_SIZE_DEC(var) InterlockedDecrement((LONG *)&(var))
90
+ # define ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange((LONG *)&(var), (val))
91
+ # endif
92
+
93
+ #elif defined(__sun) && defined(HAVE_ATOMIC_H)
94
+ #include <atomic.h>
95
+ typedef unsigned int rb_atomic_t;
96
+
97
+ # define ATOMIC_SET(var, val) (void)atomic_swap_uint(&(var), (val))
98
+ # define ATOMIC_INC(var) atomic_inc_uint(&(var))
99
+ # define ATOMIC_DEC(var) atomic_dec_uint(&(var))
100
+ # define ATOMIC_OR(var, val) atomic_or_uint(&(var), (val))
101
+ # define ATOMIC_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
102
+ # define ATOMIC_CAS(var, oldval, newval) atomic_cas_uint(&(var), (oldval), (newval))
103
+
104
+ # if SIZEOF_SIZE_T == SIZEOF_LONG
105
+ # define ATOMIC_SIZE_ADD(var, val) atomic_add_long(&(var), (val))
106
+ # define ATOMIC_SIZE_SUB(var, val) atomic_add_long(&(var), -(val))
107
+ # define ATOMIC_SIZE_INC(var) atomic_inc_ulong(&(var))
108
+ # define ATOMIC_SIZE_DEC(var) atomic_dec_ulong(&(var))
109
+ # define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_ulong(&(var), (val))
110
+ # define ATOMIC_SIZE_CAS(var, oldval, val) atomic_cas_ulong(&(var), (oldval), (val))
111
+ # else
112
+ # define ATOMIC_SIZE_ADD(var, val) atomic_add_int(&(var), (val))
113
+ # define ATOMIC_SIZE_SUB(var, val) atomic_add_int(&(var), -(val))
114
+ # define ATOMIC_SIZE_INC(var) atomic_inc_uint(&(var))
115
+ # define ATOMIC_SIZE_DEC(var) atomic_dec_uint(&(var))
116
+ # define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
117
+ # endif
118
+
119
+ #else
120
+ typedef int rb_atomic_t;
121
+ #define NEED_RUBY_ATOMIC_OPS
122
+ extern rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val);
123
+ extern rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr,
124
+ rb_atomic_t cmp,
125
+ rb_atomic_t newval);
126
+
127
+ # define ATOMIC_SET(var, val) (void)((var) = (val))
128
+ # define ATOMIC_INC(var) ((var)++)
129
+ # define ATOMIC_DEC(var) ((var)--)
130
+ # define ATOMIC_OR(var, val) ((var) |= (val))
131
+ # define ATOMIC_EXCHANGE(var, val) ruby_atomic_exchange(&(var), (val))
132
+ # define ATOMIC_CAS(var, oldval, newval) ruby_atomic_compare_and_swap(&(var), (oldval), (newval))
133
+
134
+ # define ATOMIC_SIZE_ADD(var, val) (void)((var) += (val))
135
+ # define ATOMIC_SIZE_SUB(var, val) (void)((var) -= (val))
136
+ # define ATOMIC_SIZE_EXCHANGE(var, val) ruby_atomic_size_exchange(&(var), (val))
137
+ static inline size_t
138
+ ruby_atomic_size_exchange(size_t *ptr, size_t val)
139
+ {
140
+ size_t old = *ptr;
141
+ *ptr = val;
142
+ return old;
143
+ }
144
+ #endif
145
+
146
+ #ifndef ATOMIC_SIZE_INC
147
+ # define ATOMIC_SIZE_INC(var) ATOMIC_INC(var)
148
+ #endif
149
+ #ifndef ATOMIC_SIZE_DEC
150
+ # define ATOMIC_SIZE_DEC(var) ATOMIC_DEC(var)
151
+ #endif
152
+ #ifndef ATOMIC_SIZE_EXCHANGE
153
+ # define ATOMIC_SIZE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
154
+ #endif
155
+ #ifndef ATOMIC_SIZE_CAS
156
+ # define ATOMIC_SIZE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
157
+ #endif
158
+
159
+ #if RUBY_ATOMIC_GENERIC_MACRO
160
+ # ifndef ATOMIC_PTR_EXCHANGE
161
+ # define ATOMIC_PTR_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
162
+ # endif
163
+ # ifndef ATOMIC_PTR_CAS
164
+ # define ATOMIC_PTR_CAS(var, oldval, newval) ATOMIC_CAS(var, oldval, newval)
165
+ # endif
166
+
167
+ # ifndef ATOMIC_VALUE_EXCHANGE
168
+ # define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
169
+ # endif
170
+ # ifndef ATOMIC_VALUE_CAS
171
+ # define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
172
+ # endif
173
+ #endif
174
+
175
+ #ifndef ATOMIC_PTR_EXCHANGE
176
+ # if SIZEOF_VOIDP == SIZEOF_SIZE_T
177
+ # define ATOMIC_PTR_EXCHANGE(var, val) (void *)ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
178
+ # else
179
+ # define ATOMIC_PTR_EXCHANGE(var, val) ruby_atomic_ptr_exchange((const void **)&(var), (val))
180
+ static inline void *
181
+ ruby_atomic_ptr_exchange(const void **ptr, const void *val)
182
+ {
183
+ const void *const old = *ptr;
184
+ *ptr = val;
185
+ return (void *)old;
186
+ }
187
+ # endif
188
+ #endif
189
+ #ifndef ATOMIC_PTR_CAS
190
+ # if SIZEOF_VOIDP == SIZEOF_SIZE_T
191
+ # define ATOMIC_PTR_CAS(var, oldval, val) (void *)ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
192
+ # else
193
+ # define ATOMIC_PTR_CAS(var, oldval, val) ruby_atomic_ptr_cas(&(var), (oldval), (val))
194
+ static inline void *
195
+ ruby_atomic_ptr_cas(const void **ptr, const void *oldval, const void *val)
196
+ {
197
+ const void *const old = *ptr;
198
+ if (old == oldval) *ptr = val;
199
+ return (void *)old;
200
+ }
201
+ # endif
202
+ #endif
203
+
204
+ #ifndef ATOMIC_VALUE_EXCHANGE
205
+ # if SIZEOF_VALUE == SIZEOF_SIZE_T
206
+ # define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
207
+ # else
208
+ # define ATOMIC_VALUE_EXCHANGE(var, val) ruby_atomic_value_exchange(&(var), (val))
209
+ static inline VALUE
210
+ ruby_atomic_value_exchange(VALUE *ptr, VALUE val)
211
+ {
212
+ const VALUE old = *ptr;
213
+ *ptr = val;
214
+ return old;
215
+ }
216
+ # endif
217
+ #endif
218
+ #ifndef ATOMIC_VALUE_CAS
219
+ # if SIZEOF_VALUE == SIZEOF_SIZE_T
220
+ # define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
221
+ # else
222
+ # define ATOMIC_VALUE_CAS(var, oldval, val) ruby_atomic_value_cas(&(var), (oldval), (val))
223
+ static inline VALUE
224
+ ruby_atomic_value_cas(VALUE *ptr, VALUE oldval, VALUE val)
225
+ {
226
+ const VALUE old = *ptr;
227
+ if (old == oldval) *ptr = val;
228
+ return old;
229
+ }
230
+ # endif
231
+ #endif
232
+
233
+ #endif /* RUBY_ATOMIC_H */
@@ -0,0 +1,54 @@
1
+ /**********************************************************************
2
+
3
+ thread_pthread.h -
4
+
5
+ $Author$
6
+
7
+ Copyright (C) 2004-2007 Koichi Sasada
8
+
9
+ **********************************************************************/
10
+
11
+ #ifndef RUBY_THREAD_PTHREAD_H
12
+ #define RUBY_THREAD_PTHREAD_H
13
+
14
+ #ifdef HAVE_PTHREAD_NP_H
15
+ #include <pthread_np.h>
16
+ #endif
17
+
18
+ #define RB_NATIVETHREAD_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
19
+ #define RB_NATIVETHREAD_COND_INIT { PTHREAD_COND_INITIALIZER, }
20
+
21
+ typedef struct rb_thread_cond_struct {
22
+ pthread_cond_t cond;
23
+ #ifdef HAVE_CLOCKID_T
24
+ clockid_t clockid;
25
+ #endif
26
+ } rb_nativethread_cond_t;
27
+
28
+ typedef struct native_thread_data_struct {
29
+ struct list_node ubf_list;
30
+ rb_nativethread_cond_t sleep_cond;
31
+ } native_thread_data_t;
32
+
33
+ #undef except
34
+ #undef try
35
+ #undef leave
36
+ #undef finally
37
+
38
+ typedef struct rb_global_vm_lock_struct {
39
+ /* fast path */
40
+ unsigned long acquired;
41
+ rb_nativethread_lock_t lock;
42
+
43
+ /* slow path */
44
+ volatile unsigned long waiting;
45
+ rb_nativethread_cond_t cond;
46
+
47
+ /* yield */
48
+ rb_nativethread_cond_t switch_cond;
49
+ rb_nativethread_cond_t switch_wait_cond;
50
+ int need_yield;
51
+ int wait_yield;
52
+ } rb_global_vm_lock_t;
53
+
54
+ #endif /* RUBY_THREAD_PTHREAD_H */
@@ -0,0 +1,1646 @@
1
+ /**********************************************************************
2
+
3
+ vm_core.h -
4
+
5
+ $Author$
6
+ created at: 04/01/01 19:41:38 JST
7
+
8
+ Copyright (C) 2004-2007 Koichi Sasada
9
+
10
+ **********************************************************************/
11
+
12
+ #ifndef RUBY_VM_CORE_H
13
+ #define RUBY_VM_CORE_H
14
+
15
+ /*
16
+ * Enable check mode.
17
+ * 1: enable local assertions.
18
+ */
19
+ #ifndef VM_CHECK_MODE
20
+ #define VM_CHECK_MODE 0
21
+ #endif
22
+
23
+ /**
24
+ * VM Debug Level
25
+ *
26
+ * debug level:
27
+ * 0: no debug output
28
+ * 1: show instruction name
29
+ * 2: show stack frame when control stack frame is changed
30
+ * 3: show stack status
31
+ * 4: show register
32
+ * 5:
33
+ * 10: gc check
34
+ */
35
+
36
+ #ifndef VMDEBUG
37
+ #define VMDEBUG 0
38
+ #endif
39
+
40
+ #if 0
41
+ #undef VMDEBUG
42
+ #define VMDEBUG 3
43
+ #endif
44
+
45
+ #include "ruby_assert.h"
46
+
47
+ #if VM_CHECK_MODE > 0
48
+ #define VM_ASSERT(expr) ( \
49
+ RUBY_ASSERT_MESG_WHEN(VM_CHECK_MODE > 0, expr, #expr))
50
+
51
+ #define VM_UNREACHABLE(func) rb_bug(#func ": unreachable")
52
+
53
+ #else
54
+ #define VM_ASSERT(expr) ((void)0)
55
+ #define VM_UNREACHABLE(func) ((void)0)
56
+ #endif
57
+
58
+ #define RUBY_VM_THREAD_MODEL 2
59
+
60
+ #include "ruby/ruby.h"
61
+ #include "ruby/st.h"
62
+
63
+ #include "node.h"
64
+ #include "vm_debug.h"
65
+ #include "vm_opts.h"
66
+ #include "id.h"
67
+ #include "method.h"
68
+ #include "ruby_atomic.h"
69
+ #include "ccan/list/list.h"
70
+
71
+ #include "ruby/thread_native.h"
72
+ #if defined(_WIN32)
73
+ #include "thread_win32.h"
74
+ #elif defined(HAVE_PTHREAD_H)
75
+ #include "thread_pthread.h"
76
+ #endif
77
+
78
+ #ifndef ENABLE_VM_OBJSPACE
79
+ #ifdef _WIN32
80
+ /*
81
+ * TODO: object space independent st_table.
82
+ * socklist and conlist will be freed exit_handler(), after object
83
+ * space destruction.
84
+ */
85
+ #define ENABLE_VM_OBJSPACE 0
86
+ #else
87
+ #define ENABLE_VM_OBJSPACE 1
88
+ #endif
89
+ #endif
90
+
91
+ #include <setjmp.h>
92
+ #include <signal.h>
93
+
94
+ #ifndef NSIG
95
+ # define NSIG (_SIGMAX + 1) /* For QNX */
96
+ #endif
97
+
98
+ #define RUBY_NSIG NSIG
99
+
100
+ #ifdef HAVE_STDARG_PROTOTYPES
101
+ #include <stdarg.h>
102
+ #define va_init_list(a,b) va_start((a),(b))
103
+ #else
104
+ #include <varargs.h>
105
+ #define va_init_list(a,b) va_start((a))
106
+ #endif
107
+
108
+ #if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
109
+ #define USE_SIGALTSTACK
110
+ #endif
111
+
112
+ /*****************/
113
+ /* configuration */
114
+ /*****************/
115
+
116
+ /* gcc ver. check */
117
+ #if defined(__GNUC__) && __GNUC__ >= 2
118
+
119
+ #if OPT_TOKEN_THREADED_CODE
120
+ #if OPT_DIRECT_THREADED_CODE
121
+ #undef OPT_DIRECT_THREADED_CODE
122
+ #endif
123
+ #endif
124
+
125
+ #else /* defined(__GNUC__) && __GNUC__ >= 2 */
126
+
127
+ /* disable threaded code options */
128
+ #if OPT_DIRECT_THREADED_CODE
129
+ #undef OPT_DIRECT_THREADED_CODE
130
+ #endif
131
+ #if OPT_TOKEN_THREADED_CODE
132
+ #undef OPT_TOKEN_THREADED_CODE
133
+ #endif
134
+ #endif
135
+
136
+ #ifdef __native_client__
137
+ #undef OPT_DIRECT_THREADED_CODE
138
+ #endif
139
+
140
+ /* call threaded code */
141
+ #if OPT_CALL_THREADED_CODE
142
+ #if OPT_DIRECT_THREADED_CODE
143
+ #undef OPT_DIRECT_THREADED_CODE
144
+ #endif /* OPT_DIRECT_THREADED_CODE */
145
+ #if OPT_STACK_CACHING
146
+ #undef OPT_STACK_CACHING
147
+ #endif /* OPT_STACK_CACHING */
148
+ #endif /* OPT_CALL_THREADED_CODE */
149
+
150
+ typedef unsigned long rb_num_t;
151
+
152
+ enum ruby_tag_type {
153
+ RUBY_TAG_RETURN = 0x1,
154
+ RUBY_TAG_BREAK = 0x2,
155
+ RUBY_TAG_NEXT = 0x3,
156
+ RUBY_TAG_RETRY = 0x4,
157
+ RUBY_TAG_REDO = 0x5,
158
+ RUBY_TAG_RAISE = 0x6,
159
+ RUBY_TAG_THROW = 0x7,
160
+ RUBY_TAG_FATAL = 0x8,
161
+ RUBY_TAG_MASK = 0xf
162
+ };
163
+ #define TAG_RETURN RUBY_TAG_RETURN
164
+ #define TAG_BREAK RUBY_TAG_BREAK
165
+ #define TAG_NEXT RUBY_TAG_NEXT
166
+ #define TAG_RETRY RUBY_TAG_RETRY
167
+ #define TAG_REDO RUBY_TAG_REDO
168
+ #define TAG_RAISE RUBY_TAG_RAISE
169
+ #define TAG_THROW RUBY_TAG_THROW
170
+ #define TAG_FATAL RUBY_TAG_FATAL
171
+ #define TAG_MASK RUBY_TAG_MASK
172
+
173
+ enum ruby_vm_throw_flags {
174
+ VM_THROW_NO_ESCAPE_FLAG = 0x8000,
175
+ VM_THROW_LEVEL_SHIFT = 16,
176
+ VM_THROW_STATE_MASK = 0xff
177
+ };
178
+
179
+ /* forward declarations */
180
+ struct rb_thread_struct;
181
+ struct rb_control_frame_struct;
182
+
183
+ /* iseq data type */
184
+ typedef struct rb_compile_option_struct rb_compile_option_t;
185
+
186
+ struct iseq_inline_cache_entry {
187
+ rb_serial_t ic_serial;
188
+ const rb_cref_t *ic_cref;
189
+ union {
190
+ size_t index;
191
+ VALUE value;
192
+ } ic_value;
193
+ };
194
+
195
+ union iseq_inline_storage_entry {
196
+ struct {
197
+ struct rb_thread_struct *running_thread;
198
+ VALUE value;
199
+ } once;
200
+ struct iseq_inline_cache_entry cache;
201
+ };
202
+
203
+ enum method_missing_reason {
204
+ MISSING_NOENTRY = 0x00,
205
+ MISSING_PRIVATE = 0x01,
206
+ MISSING_PROTECTED = 0x02,
207
+ MISSING_FCALL = 0x04,
208
+ MISSING_VCALL = 0x08,
209
+ MISSING_SUPER = 0x10,
210
+ MISSING_MISSING = 0x20,
211
+ MISSING_NONE = 0x40
212
+ };
213
+
214
+ struct rb_call_info {
215
+ /* fixed at compile time */
216
+ ID mid;
217
+ unsigned int flag;
218
+ int orig_argc;
219
+ };
220
+
221
+ struct rb_call_info_kw_arg {
222
+ int keyword_len;
223
+ VALUE keywords[1];
224
+ };
225
+
226
+ struct rb_call_info_with_kwarg {
227
+ struct rb_call_info ci;
228
+ struct rb_call_info_kw_arg *kw_arg;
229
+ };
230
+
231
+ struct rb_calling_info {
232
+ VALUE block_handler;
233
+ VALUE recv;
234
+ int argc;
235
+ };
236
+
237
+ struct rb_call_cache;
238
+ typedef VALUE (*vm_call_handler)(struct rb_thread_struct *th, struct rb_control_frame_struct *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc);
239
+
240
+ struct rb_call_cache {
241
+ /* inline cache: keys */
242
+ rb_serial_t method_state;
243
+ rb_serial_t class_serial;
244
+
245
+ /* inline cache: values */
246
+ const rb_callable_method_entry_t *me;
247
+
248
+ vm_call_handler call;
249
+
250
+ union {
251
+ unsigned int index; /* used by ivar */
252
+ enum method_missing_reason method_missing_reason; /* used by method_missing */
253
+ int inc_sp; /* used by cfunc */
254
+ } aux;
255
+ };
256
+
257
+ #if 1
258
+ #define CoreDataFromValue(obj, type) (type*)DATA_PTR(obj)
259
+ #else
260
+ #define CoreDataFromValue(obj, type) (type*)rb_data_object_get(obj)
261
+ #endif
262
+ #define GetCoreDataFromValue(obj, type, ptr) ((ptr) = CoreDataFromValue((obj), type))
263
+
264
+ typedef struct rb_iseq_location_struct {
265
+ VALUE path;
266
+ VALUE absolute_path;
267
+ VALUE base_label;
268
+ VALUE label;
269
+ VALUE first_lineno; /* TODO: may be unsigned short */
270
+ } rb_iseq_location_t;
271
+
272
+ struct rb_iseq_constant_body {
273
+ enum iseq_type {
274
+ ISEQ_TYPE_TOP,
275
+ ISEQ_TYPE_METHOD,
276
+ ISEQ_TYPE_BLOCK,
277
+ ISEQ_TYPE_CLASS,
278
+ ISEQ_TYPE_RESCUE,
279
+ ISEQ_TYPE_ENSURE,
280
+ ISEQ_TYPE_EVAL,
281
+ ISEQ_TYPE_MAIN,
282
+ ISEQ_TYPE_DEFINED_GUARD
283
+ } type; /* instruction sequence type */
284
+
285
+ unsigned int iseq_size;
286
+ const VALUE *iseq_encoded; /* encoded iseq (insn addr and operands) */
287
+
288
+ /**
289
+ * parameter information
290
+ *
291
+ * def m(a1, a2, ..., aM, # mandatory
292
+ * b1=(...), b2=(...), ..., bN=(...), # optional
293
+ * *c, # rest
294
+ * d1, d2, ..., dO, # post
295
+ * e1:(...), e2:(...), ..., eK:(...), # keyword
296
+ * **f, # keyword_rest
297
+ * &g) # block
298
+ * =>
299
+ *
300
+ * lead_num = M
301
+ * opt_num = N
302
+ * rest_start = M+N
303
+ * post_start = M+N+(*1)
304
+ * post_num = O
305
+ * keyword_num = K
306
+ * block_start = M+N+(*1)+O+K
307
+ * keyword_bits = M+N+(*1)+O+K+(&1)
308
+ * size = M+N+O+(*1)+K+(&1)+(**1) // parameter size.
309
+ */
310
+
311
+ struct {
312
+ struct {
313
+ unsigned int has_lead : 1;
314
+ unsigned int has_opt : 1;
315
+ unsigned int has_rest : 1;
316
+ unsigned int has_post : 1;
317
+ unsigned int has_kw : 1;
318
+ unsigned int has_kwrest : 1;
319
+ unsigned int has_block : 1;
320
+
321
+ unsigned int ambiguous_param0 : 1; /* {|a|} */
322
+ } flags;
323
+
324
+ unsigned int size;
325
+
326
+ int lead_num;
327
+ int opt_num;
328
+ int rest_start;
329
+ int post_start;
330
+ int post_num;
331
+ int block_start;
332
+
333
+ const VALUE *opt_table; /* (opt_num + 1) entries. */
334
+ /* opt_num and opt_table:
335
+ *
336
+ * def foo o1=e1, o2=e2, ..., oN=eN
337
+ * #=>
338
+ * # prologue code
339
+ * A1: e1
340
+ * A2: e2
341
+ * ...
342
+ * AN: eN
343
+ * AL: body
344
+ * opt_num = N
345
+ * opt_table = [A1, A2, ..., AN, AL]
346
+ */
347
+
348
+ const struct rb_iseq_param_keyword {
349
+ int num;
350
+ int required_num;
351
+ int bits_start;
352
+ int rest_start;
353
+ const ID *table;
354
+ const VALUE *default_values;
355
+ } *keyword;
356
+ } param;
357
+
358
+ rb_iseq_location_t location;
359
+
360
+ /* insn info, must be freed */
361
+ const struct iseq_line_info_entry *line_info_table;
362
+
363
+ const ID *local_table; /* must free */
364
+
365
+ /* catch table */
366
+ const struct iseq_catch_table *catch_table;
367
+
368
+ /* for child iseq */
369
+ const struct rb_iseq_struct *parent_iseq;
370
+ struct rb_iseq_struct *local_iseq; /* local_iseq->flip_cnt can be modified */
371
+
372
+ union iseq_inline_storage_entry *is_entries;
373
+ struct rb_call_info *ci_entries; /* struct rb_call_info ci_entries[ci_size];
374
+ * struct rb_call_info_with_kwarg cikw_entries[ci_kw_size];
375
+ * So that:
376
+ * struct rb_call_info_with_kwarg *cikw_entries = &body->ci_entries[ci_size];
377
+ */
378
+ struct rb_call_cache *cc_entries; /* size is ci_size = ci_kw_size */
379
+
380
+ VALUE mark_ary; /* Array: includes operands which should be GC marked */
381
+
382
+ unsigned int local_table_size;
383
+ unsigned int is_size;
384
+ unsigned int ci_size;
385
+ unsigned int ci_kw_size;
386
+ unsigned int line_info_size;
387
+ unsigned int stack_max; /* for stack overflow check */
388
+ };
389
+
390
+ /* T_IMEMO/iseq */
391
+ /* typedef rb_iseq_t is in method.h */
392
+ struct rb_iseq_struct {
393
+ VALUE flags;
394
+ VALUE reserved1;
395
+ struct rb_iseq_constant_body *body;
396
+
397
+ union { /* 4, 5 words */
398
+ struct iseq_compile_data *compile_data; /* used at compile time */
399
+
400
+ struct {
401
+ VALUE obj;
402
+ int index;
403
+ } loader;
404
+ } aux;
405
+ };
406
+
407
+ #ifndef USE_LAZY_LOAD
408
+ #define USE_LAZY_LOAD 0
409
+ #endif
410
+
411
+ #if USE_LAZY_LOAD
412
+ const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq);
413
+ #endif
414
+
415
+ static inline const rb_iseq_t *
416
+ rb_iseq_check(const rb_iseq_t *iseq)
417
+ {
418
+ #if USE_LAZY_LOAD
419
+ if (iseq->body == NULL) {
420
+ rb_iseq_complete((rb_iseq_t *)iseq);
421
+ }
422
+ #endif
423
+ return iseq;
424
+ }
425
+
426
+ enum ruby_special_exceptions {
427
+ ruby_error_reenter,
428
+ ruby_error_nomemory,
429
+ ruby_error_sysstack,
430
+ ruby_error_closed_stream,
431
+ ruby_special_error_count
432
+ };
433
+
434
+ enum ruby_basic_operators {
435
+ BOP_PLUS,
436
+ BOP_MINUS,
437
+ BOP_MULT,
438
+ BOP_DIV,
439
+ BOP_MOD,
440
+ BOP_EQ,
441
+ BOP_EQQ,
442
+ BOP_LT,
443
+ BOP_LE,
444
+ BOP_LTLT,
445
+ BOP_AREF,
446
+ BOP_ASET,
447
+ BOP_LENGTH,
448
+ BOP_SIZE,
449
+ BOP_EMPTY_P,
450
+ BOP_SUCC,
451
+ BOP_GT,
452
+ BOP_GE,
453
+ BOP_NOT,
454
+ BOP_NEQ,
455
+ BOP_MATCH,
456
+ BOP_FREEZE,
457
+ BOP_MAX,
458
+ BOP_MIN,
459
+
460
+ BOP_LAST_
461
+ };
462
+
463
+ #define GetVMPtr(obj, ptr) \
464
+ GetCoreDataFromValue((obj), rb_vm_t, (ptr))
465
+
466
+ struct rb_vm_struct;
467
+ typedef void rb_vm_at_exit_func(struct rb_vm_struct*);
468
+
469
+ typedef struct rb_at_exit_list {
470
+ rb_vm_at_exit_func *func;
471
+ struct rb_at_exit_list *next;
472
+ } rb_at_exit_list;
473
+
474
+ struct rb_objspace;
475
+ struct rb_objspace *rb_objspace_alloc(void);
476
+ void rb_objspace_free(struct rb_objspace *);
477
+
478
+ typedef struct rb_hook_list_struct {
479
+ struct rb_event_hook_struct *hooks;
480
+ rb_event_flag_t events;
481
+ int need_clean;
482
+ } rb_hook_list_t;
483
+
484
+ typedef struct rb_vm_struct {
485
+ VALUE self;
486
+
487
+ rb_global_vm_lock_t gvl;
488
+ rb_nativethread_lock_t thread_destruct_lock;
489
+
490
+ struct rb_thread_struct *main_thread;
491
+ struct rb_thread_struct *running_thread;
492
+
493
+ struct list_head living_threads;
494
+ size_t living_thread_num;
495
+ VALUE thgroup_default;
496
+
497
+ unsigned int running: 1;
498
+ unsigned int thread_abort_on_exception: 1;
499
+ unsigned int thread_report_on_exception: 1;
500
+ int trace_running;
501
+ volatile int sleeper;
502
+
503
+ /* object management */
504
+ VALUE mark_object_ary;
505
+ const VALUE special_exceptions[ruby_special_error_count];
506
+
507
+ /* load */
508
+ VALUE top_self;
509
+ VALUE load_path;
510
+ VALUE load_path_snapshot;
511
+ VALUE load_path_check_cache;
512
+ VALUE expanded_load_path;
513
+ VALUE loaded_features;
514
+ VALUE loaded_features_snapshot;
515
+ struct st_table *loaded_features_index;
516
+ struct st_table *loading_table;
517
+
518
+ /* signal */
519
+ struct {
520
+ VALUE cmd;
521
+ int safe;
522
+ } trap_list[RUBY_NSIG];
523
+
524
+ /* hook */
525
+ rb_hook_list_t event_hooks;
526
+
527
+ /* relation table of ensure - rollback for callcc */
528
+ struct st_table *ensure_rollback_table;
529
+
530
+ /* postponed_job */
531
+ struct rb_postponed_job_struct *postponed_job_buffer;
532
+ int postponed_job_index;
533
+
534
+ int src_encoding_index;
535
+
536
+ VALUE verbose, debug, orig_progname, progname;
537
+ VALUE coverages;
538
+
539
+ VALUE defined_module_hash;
540
+
541
+ struct rb_objspace *objspace;
542
+
543
+ rb_at_exit_list *at_exit;
544
+
545
+ VALUE *defined_strings;
546
+ st_table *frozen_strings;
547
+
548
+ /* params */
549
+ struct { /* size in byte */
550
+ size_t thread_vm_stack_size;
551
+ size_t thread_machine_stack_size;
552
+ size_t fiber_vm_stack_size;
553
+ size_t fiber_machine_stack_size;
554
+ } default_params;
555
+
556
+ short redefined_flag[BOP_LAST_];
557
+ } rb_vm_t;
558
+
559
+ /* default values */
560
+
561
+ #define RUBY_VM_SIZE_ALIGN 4096
562
+
563
+ #define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
564
+ #define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
565
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
566
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
567
+
568
+ #define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
569
+ #define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
570
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE)) /* 256 KB or 512 KB */
571
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
572
+
573
+ /* optimize insn */
574
+ #define INTEGER_REDEFINED_OP_FLAG (1 << 0)
575
+ #define FLOAT_REDEFINED_OP_FLAG (1 << 1)
576
+ #define STRING_REDEFINED_OP_FLAG (1 << 2)
577
+ #define ARRAY_REDEFINED_OP_FLAG (1 << 3)
578
+ #define HASH_REDEFINED_OP_FLAG (1 << 4)
579
+ /* #define BIGNUM_REDEFINED_OP_FLAG (1 << 5) */
580
+ #define SYMBOL_REDEFINED_OP_FLAG (1 << 6)
581
+ #define TIME_REDEFINED_OP_FLAG (1 << 7)
582
+ #define REGEXP_REDEFINED_OP_FLAG (1 << 8)
583
+ #define NIL_REDEFINED_OP_FLAG (1 << 9)
584
+ #define TRUE_REDEFINED_OP_FLAG (1 << 10)
585
+ #define FALSE_REDEFINED_OP_FLAG (1 << 11)
586
+
587
+ #define BASIC_OP_UNREDEFINED_P(op, klass) (LIKELY((GET_VM()->redefined_flag[(op)]&(klass)) == 0))
588
+
589
+ #ifndef VM_DEBUG_BP_CHECK
590
+ #define VM_DEBUG_BP_CHECK 0
591
+ #endif
592
+
593
+ #ifndef VM_DEBUG_VERIFY_METHOD_CACHE
594
+ #define VM_DEBUG_VERIFY_METHOD_CACHE (VM_DEBUG_MODE != 0)
595
+ #endif
596
+
597
+ struct rb_captured_block {
598
+ VALUE self;
599
+ const VALUE *ep;
600
+ union {
601
+ const rb_iseq_t *iseq;
602
+ const struct vm_ifunc *ifunc;
603
+ VALUE val;
604
+ } code;
605
+ };
606
+
607
+ enum rb_block_handler_type {
608
+ block_handler_type_iseq,
609
+ block_handler_type_ifunc,
610
+ block_handler_type_symbol,
611
+ block_handler_type_proc
612
+ };
613
+
614
+ enum rb_block_type {
615
+ block_type_iseq,
616
+ block_type_ifunc,
617
+ block_type_symbol,
618
+ block_type_proc
619
+ };
620
+
621
+ struct rb_block {
622
+ union {
623
+ struct rb_captured_block captured;
624
+ VALUE symbol;
625
+ VALUE proc;
626
+ } as;
627
+ enum rb_block_type type;
628
+ };
629
+
630
+ typedef struct rb_control_frame_struct {
631
+ const VALUE *pc; /* cfp[0] */
632
+ VALUE *sp; /* cfp[1] */
633
+ const rb_iseq_t *iseq; /* cfp[2] */
634
+ VALUE self; /* cfp[3] / block[0] */
635
+ const VALUE *ep; /* cfp[4] / block[1] */
636
+ const void *block_code; /* cfp[5] / block[2] */ /* iseq or ifunc */
637
+
638
+ #if VM_DEBUG_BP_CHECK
639
+ VALUE *bp_check; /* cfp[6] */
640
+ #endif
641
+ } rb_control_frame_t;
642
+
643
+ extern const rb_data_type_t ruby_threadptr_data_type;
644
+
645
+ #define GetThreadPtr(obj, ptr) \
646
+ TypedData_Get_Struct((obj), rb_thread_t, &ruby_threadptr_data_type, (ptr))
647
+
648
+ enum rb_thread_status {
649
+ THREAD_RUNNABLE,
650
+ THREAD_STOPPED,
651
+ THREAD_STOPPED_FOREVER,
652
+ THREAD_KILLED
653
+ };
654
+
655
+ typedef RUBY_JMP_BUF rb_jmpbuf_t;
656
+
657
+ /*
658
+ the members which are written in TH_PUSH_TAG() should be placed at
659
+ the beginning and the end, so that entire region is accessible.
660
+ */
661
+ struct rb_vm_tag {
662
+ VALUE tag;
663
+ VALUE retval;
664
+ rb_jmpbuf_t buf;
665
+ struct rb_vm_tag *prev;
666
+ };
667
+
668
+ struct rb_vm_protect_tag {
669
+ struct rb_vm_protect_tag *prev;
670
+ };
671
+
672
+ struct rb_unblock_callback {
673
+ rb_unblock_function_t *func;
674
+ void *arg;
675
+ };
676
+
677
+ struct rb_mutex_struct;
678
+
679
+ typedef struct rb_thread_list_struct{
680
+ struct rb_thread_list_struct *next;
681
+ struct rb_thread_struct *th;
682
+ } rb_thread_list_t;
683
+
684
+ typedef struct rb_ensure_entry {
685
+ VALUE marker;
686
+ VALUE (*e_proc)(ANYARGS);
687
+ VALUE data2;
688
+ } rb_ensure_entry_t;
689
+
690
+ typedef struct rb_ensure_list {
691
+ struct rb_ensure_list *next;
692
+ struct rb_ensure_entry entry;
693
+ } rb_ensure_list_t;
694
+
695
+ typedef char rb_thread_id_string_t[sizeof(rb_nativethread_id_t) * 2 + 3];
696
+
697
+ typedef struct rb_fiber_struct rb_fiber_t;
698
+
699
+ typedef struct rb_thread_struct {
700
+ struct list_node vmlt_node;
701
+ VALUE self;
702
+ rb_vm_t *vm;
703
+
704
+ /* execution information */
705
+ VALUE *stack; /* must free, must mark */
706
+ size_t stack_size; /* size in word (byte size / sizeof(VALUE)) */
707
+ rb_control_frame_t *cfp;
708
+ int safe_level;
709
+ int raised_flag;
710
+ VALUE last_status; /* $? */
711
+
712
+ /* passing state */
713
+ int state;
714
+
715
+ int waiting_fd;
716
+
717
+ /* for rb_iterate */
718
+ VALUE passed_block_handler;
719
+
720
+ /* for bmethod */
721
+ const rb_callable_method_entry_t *passed_bmethod_me;
722
+
723
+ /* for cfunc */
724
+ struct rb_calling_info *calling;
725
+
726
+ /* for load(true) */
727
+ VALUE top_self;
728
+ VALUE top_wrapper;
729
+
730
+ /* eval env */
731
+ const VALUE *root_lep;
732
+ VALUE root_svar;
733
+
734
+ /* thread control */
735
+ rb_nativethread_id_t thread_id;
736
+ #ifdef NON_SCALAR_THREAD_ID
737
+ rb_thread_id_string_t thread_id_string;
738
+ #endif
739
+ enum rb_thread_status status;
740
+ int to_kill;
741
+ int priority;
742
+
743
+ native_thread_data_t native_thread_data;
744
+ void *blocking_region_buffer;
745
+
746
+ VALUE thgroup;
747
+ VALUE value;
748
+
749
+ /* temporary place of errinfo */
750
+ VALUE errinfo;
751
+
752
+ /* temporary place of retval on OPT_CALL_THREADED_CODE */
753
+ #if OPT_CALL_THREADED_CODE
754
+ VALUE retval;
755
+ #endif
756
+
757
+ /* async errinfo queue */
758
+ VALUE pending_interrupt_queue;
759
+ VALUE pending_interrupt_mask_stack;
760
+ int pending_interrupt_queue_checked;
761
+
762
+ rb_atomic_t interrupt_flag;
763
+ unsigned long interrupt_mask;
764
+ rb_nativethread_lock_t interrupt_lock;
765
+ rb_nativethread_cond_t interrupt_cond;
766
+ struct rb_unblock_callback unblock;
767
+ VALUE locking_mutex;
768
+ struct rb_mutex_struct *keeping_mutexes;
769
+
770
+ struct rb_vm_tag *tag;
771
+ struct rb_vm_protect_tag *protect_tag;
772
+
773
+ /* storage */
774
+ st_table *local_storage;
775
+ VALUE local_storage_recursive_hash;
776
+ VALUE local_storage_recursive_hash_for_trace;
777
+
778
+ rb_thread_list_t *join_list;
779
+
780
+ VALUE first_proc;
781
+ VALUE first_args;
782
+ VALUE (*first_func)(ANYARGS);
783
+
784
+ /* for GC */
785
+ struct {
786
+ VALUE *stack_start;
787
+ VALUE *stack_end;
788
+ size_t stack_maxsize;
789
+ #ifdef __ia64
790
+ VALUE *register_stack_start;
791
+ VALUE *register_stack_end;
792
+ size_t register_stack_maxsize;
793
+ #endif
794
+ jmp_buf regs;
795
+ } machine;
796
+
797
+ /* statistics data for profiler */
798
+ VALUE stat_insn_usage;
799
+
800
+ /* tracer */
801
+ rb_hook_list_t event_hooks;
802
+ struct rb_trace_arg_struct *trace_arg; /* trace information */
803
+
804
+ /* fiber */
805
+ rb_fiber_t *fiber;
806
+ rb_fiber_t *root_fiber;
807
+ rb_jmpbuf_t root_jmpbuf;
808
+
809
+ /* ensure & callcc */
810
+ rb_ensure_list_t *ensure_list;
811
+
812
+ /* misc */
813
+ enum method_missing_reason method_missing_reason: 8;
814
+ unsigned int abort_on_exception: 1;
815
+ unsigned int report_on_exception: 1;
816
+ #ifdef USE_SIGALTSTACK
817
+ void *altstack;
818
+ #endif
819
+ unsigned long running_time_us;
820
+ VALUE name;
821
+ } rb_thread_t;
822
+
823
+ typedef enum {
824
+ VM_DEFINECLASS_TYPE_CLASS = 0x00,
825
+ VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
826
+ VM_DEFINECLASS_TYPE_MODULE = 0x02,
827
+ /* 0x03..0x06 is reserved */
828
+ VM_DEFINECLASS_TYPE_MASK = 0x07
829
+ } rb_vm_defineclass_type_t;
830
+
831
+ #define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
832
+ #define VM_DEFINECLASS_FLAG_SCOPED 0x08
833
+ #define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
834
+ #define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
835
+ #define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
836
+ ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
837
+
838
+ /* iseq.c */
839
+ RUBY_SYMBOL_EXPORT_BEGIN
840
+
841
+ /* node -> iseq */
842
+ rb_iseq_t *rb_iseq_new(NODE*, VALUE, VALUE, VALUE, const rb_iseq_t *parent, enum iseq_type);
843
+ rb_iseq_t *rb_iseq_new_top(NODE *node, VALUE name, VALUE path, VALUE absolute_path, const rb_iseq_t *parent);
844
+ rb_iseq_t *rb_iseq_new_main(NODE *node, VALUE path, VALUE absolute_path, const rb_iseq_t *parent);
845
+ rb_iseq_t *rb_iseq_new_with_bopt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE, enum iseq_type, VALUE);
846
+ rb_iseq_t *rb_iseq_new_with_opt(NODE*, VALUE, VALUE, VALUE, VALUE, const rb_iseq_t *parent, enum iseq_type, const rb_compile_option_t*);
847
+
848
+ /* src -> iseq */
849
+ rb_iseq_t *rb_iseq_compile(VALUE src, VALUE file, VALUE line);
850
+ rb_iseq_t *rb_iseq_compile_on_base(VALUE src, VALUE file, VALUE line, const struct rb_block *base_block);
851
+ rb_iseq_t *rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE absolute_path, VALUE line, const struct rb_block *base_block, VALUE opt);
852
+
853
+ VALUE rb_iseq_disasm(const rb_iseq_t *iseq);
854
+ int rb_iseq_disasm_insn(VALUE str, const VALUE *iseqval, size_t pos, const rb_iseq_t *iseq, VALUE child);
855
+ const char *ruby_node_name(int node);
856
+
857
+ VALUE rb_iseq_coverage(const rb_iseq_t *iseq);
858
+
859
+ RUBY_EXTERN VALUE rb_cISeq;
860
+ RUBY_EXTERN VALUE rb_cRubyVM;
861
+ RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
862
+ RUBY_SYMBOL_EXPORT_END
863
+
864
+ #define GetProcPtr(obj, ptr) \
865
+ GetCoreDataFromValue((obj), rb_proc_t, (ptr))
866
+
867
+ typedef struct {
868
+ const struct rb_block block;
869
+ int8_t safe_level; /* 0..1 */
870
+ int8_t is_from_method; /* bool */
871
+ int8_t is_lambda; /* bool */
872
+ } rb_proc_t;
873
+
874
+ typedef struct {
875
+ VALUE flags; /* imemo header */
876
+ const rb_iseq_t *iseq;
877
+ const VALUE *ep;
878
+ const VALUE *env;
879
+ unsigned int env_size;
880
+ } rb_env_t;
881
+
882
+ extern const rb_data_type_t ruby_binding_data_type;
883
+
884
+ #define GetBindingPtr(obj, ptr) \
885
+ GetCoreDataFromValue((obj), rb_binding_t, (ptr))
886
+
887
+ typedef struct {
888
+ struct rb_block block;
889
+ VALUE path;
890
+ unsigned short first_lineno;
891
+ } rb_binding_t;
892
+
893
+ /* used by compile time and send insn */
894
+
895
+ enum vm_check_match_type {
896
+ VM_CHECKMATCH_TYPE_WHEN = 1,
897
+ VM_CHECKMATCH_TYPE_CASE = 2,
898
+ VM_CHECKMATCH_TYPE_RESCUE = 3
899
+ };
900
+
901
+ #define VM_CHECKMATCH_TYPE_MASK 0x03
902
+ #define VM_CHECKMATCH_ARRAY 0x04
903
+
904
+ #define VM_CALL_ARGS_SPLAT (0x01 << 0) /* m(*args) */
905
+ #define VM_CALL_ARGS_BLOCKARG (0x01 << 1) /* m(&block) */
906
+ #define VM_CALL_FCALL (0x01 << 2) /* m(...) */
907
+ #define VM_CALL_VCALL (0x01 << 3) /* m */
908
+ #define VM_CALL_ARGS_SIMPLE (0x01 << 4) /* (ci->flag & (SPLAT|BLOCKARG)) && blockiseq == NULL && ci->kw_arg == NULL */
909
+ #define VM_CALL_BLOCKISEQ (0x01 << 5) /* has blockiseq */
910
+ #define VM_CALL_KWARG (0x01 << 6) /* has kwarg */
911
+ #define VM_CALL_TAILCALL (0x01 << 7) /* located at tail position */
912
+ #define VM_CALL_SUPER (0x01 << 8) /* super */
913
+ #define VM_CALL_OPT_SEND (0x01 << 9) /* internal flag */
914
+
915
+ enum vm_special_object_type {
916
+ VM_SPECIAL_OBJECT_VMCORE = 1,
917
+ VM_SPECIAL_OBJECT_CBASE,
918
+ VM_SPECIAL_OBJECT_CONST_BASE
919
+ };
920
+
921
+ enum vm_svar_index {
922
+ VM_SVAR_LASTLINE = 0, /* $_ */
923
+ VM_SVAR_BACKREF = 1, /* $~ */
924
+
925
+ VM_SVAR_EXTRA_START = 2,
926
+ VM_SVAR_FLIPFLOP_START = 2 /* flipflop */
927
+ };
928
+
929
+ /* inline cache */
930
+ typedef struct iseq_inline_cache_entry *IC;
931
+ typedef struct rb_call_info *CALL_INFO;
932
+ typedef struct rb_call_cache *CALL_CACHE;
933
+
934
+ void rb_vm_change_state(void);
935
+
936
+ typedef VALUE CDHASH;
937
+
938
+ #ifndef FUNC_FASTCALL
939
+ #define FUNC_FASTCALL(x) x
940
+ #endif
941
+
942
+ typedef rb_control_frame_t *
943
+ (FUNC_FASTCALL(*rb_insn_func_t))(rb_thread_t *, rb_control_frame_t *);
944
+
945
+ #define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
946
+ #define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
947
+
948
+ #define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
949
+ #define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
950
+ #define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
951
+
952
+ enum {
953
+ /* Frame/Environment flag bits:
954
+ * MMMM MMMM MMMM MMMM ____ ____ FFFF EEEX (LSB)
955
+ *
956
+ * X : tag for GC marking (It seems as Fixnum)
957
+ * EEE : 3 bits Env flags
958
+ * FFFF: 4 bits Frame flags
959
+ * MMMM: 16 bits frame magic (to check frame corruption)
960
+ */
961
+
962
+ /* frame types */
963
+ VM_FRAME_MAGIC_METHOD = 0x11110001,
964
+ VM_FRAME_MAGIC_BLOCK = 0x22220001,
965
+ VM_FRAME_MAGIC_CLASS = 0x33330001,
966
+ VM_FRAME_MAGIC_TOP = 0x44440001,
967
+ VM_FRAME_MAGIC_CFUNC = 0x55550001,
968
+ VM_FRAME_MAGIC_PROC = 0x66660001,
969
+ VM_FRAME_MAGIC_IFUNC = 0x77770001,
970
+ VM_FRAME_MAGIC_EVAL = 0x88880001,
971
+ VM_FRAME_MAGIC_LAMBDA = 0x99990001,
972
+ VM_FRAME_MAGIC_RESCUE = 0xaaaa0001,
973
+ VM_FRAME_MAGIC_DUMMY = 0xbbbb0001,
974
+
975
+ VM_FRAME_MAGIC_MASK = 0xffff0001,
976
+
977
+ /* frame flag */
978
+ VM_FRAME_FLAG_PASSED = 0x0010,
979
+ VM_FRAME_FLAG_FINISH = 0x0020,
980
+ VM_FRAME_FLAG_BMETHOD = 0x0040,
981
+ VM_FRAME_FLAG_CFRAME = 0x0080,
982
+
983
+ /* env flag */
984
+ VM_ENV_FLAG_LOCAL = 0x0002,
985
+ VM_ENV_FLAG_ESCAPED = 0x0004,
986
+ VM_ENV_FLAG_WB_REQUIRED = 0x0008
987
+ };
988
+
989
+ #define VM_ENV_DATA_SIZE ( 3)
990
+
991
+ #define VM_ENV_DATA_INDEX_ME_CREF (-2) /* ep[-2] */
992
+ #define VM_ENV_DATA_INDEX_SPECVAL (-1) /* ep[-1] */
993
+ #define VM_ENV_DATA_INDEX_FLAGS ( 0) /* ep[ 0] */
994
+ #define VM_ENV_DATA_INDEX_ENV ( 1) /* ep[ 1] */
995
+ #define VM_ENV_DATA_INDEX_ENV_PROC ( 2) /* ep[ 2] */
996
+
997
+ #define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
998
+
999
+ static inline void VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value);
1000
+
1001
+ static inline void
1002
+ VM_ENV_FLAGS_SET(const VALUE *ep, VALUE flag)
1003
+ {
1004
+ VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1005
+ VM_ASSERT(FIXNUM_P(flags));
1006
+ VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1007
+ }
1008
+
1009
+ static inline void
1010
+ VM_ENV_FLAGS_UNSET(const VALUE *ep, VALUE flag)
1011
+ {
1012
+ VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1013
+ VM_ASSERT(FIXNUM_P(flags));
1014
+ VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1015
+ }
1016
+
1017
+ static inline unsigned long
1018
+ VM_ENV_FLAGS(const VALUE *ep, long flag)
1019
+ {
1020
+ VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1021
+ VM_ASSERT(FIXNUM_P(flags));
1022
+ return flags & flag;
1023
+ }
1024
+
1025
+ static inline unsigned long
1026
+ VM_FRAME_TYPE(const rb_control_frame_t *cfp)
1027
+ {
1028
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1029
+ }
1030
+
1031
+ static inline int
1032
+ VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
1033
+ {
1034
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH ) != 0;
1035
+ }
1036
+
1037
+ static inline int
1038
+ VM_FRAME_BMETHOD_P(const rb_control_frame_t *cfp)
1039
+ {
1040
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1041
+ }
1042
+
1043
+ static inline int
1044
+ rb_obj_is_iseq(VALUE iseq)
1045
+ {
1046
+ return RB_TYPE_P(iseq, T_IMEMO) && imemo_type(iseq) == imemo_iseq;
1047
+ }
1048
+
1049
+ #if VM_CHECK_MODE > 0
1050
+ #define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1051
+ #endif
1052
+
1053
+ static inline int
1054
+ VM_FRAME_CFRAME_P(const rb_control_frame_t *cfp)
1055
+ {
1056
+ int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1057
+ VM_ASSERT(RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) != cframe_p);
1058
+ return cframe_p;
1059
+ }
1060
+
1061
+ static inline int
1062
+ VM_FRAME_RUBYFRAME_P(const rb_control_frame_t *cfp)
1063
+ {
1064
+ return !VM_FRAME_CFRAME_P(cfp);
1065
+ }
1066
+
1067
+ #define RUBYVM_CFUNC_FRAME_P(cfp) \
1068
+ (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1069
+
1070
+ #define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1071
+ #define VM_BLOCK_HANDLER_NONE 0
1072
+
1073
+ static inline int
1074
+ VM_ENV_LOCAL_P(const VALUE *ep)
1075
+ {
1076
+ return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1077
+ }
1078
+
1079
+ static inline const VALUE *
1080
+ VM_ENV_PREV_EP(const VALUE *ep)
1081
+ {
1082
+ VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1083
+ return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1084
+ }
1085
+
1086
+ static inline VALUE
1087
+ VM_ENV_BLOCK_HANDLER(const VALUE *ep)
1088
+ {
1089
+ VM_ASSERT(VM_ENV_LOCAL_P(ep));
1090
+ return ep[VM_ENV_DATA_INDEX_SPECVAL];
1091
+ }
1092
+
1093
+ #if VM_CHECK_MODE > 0
1094
+ int rb_vm_ep_in_heap_p(const VALUE *ep);
1095
+ #endif
1096
+
1097
+ static inline int
1098
+ VM_ENV_ESCAPED_P(const VALUE *ep)
1099
+ {
1100
+ VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1101
+ return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1102
+ }
1103
+
1104
+ #if VM_CHECK_MODE > 0
1105
+ static inline int
1106
+ vm_assert_env(VALUE obj)
1107
+ {
1108
+ VM_ASSERT(RB_TYPE_P(obj, T_IMEMO));
1109
+ VM_ASSERT(imemo_type(obj) == imemo_env);
1110
+ return 1;
1111
+ }
1112
+ #endif
1113
+
1114
+ static inline VALUE
1115
+ VM_ENV_ENVVAL(const VALUE *ep)
1116
+ {
1117
+ VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1118
+ VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1119
+ VM_ASSERT(vm_assert_env(envval));
1120
+ return envval;
1121
+ }
1122
+
1123
+ static inline const rb_env_t *
1124
+ VM_ENV_ENVVAL_PTR(const VALUE *ep)
1125
+ {
1126
+ return (const rb_env_t *)VM_ENV_ENVVAL(ep);
1127
+ }
1128
+
1129
+ static inline VALUE
1130
+ VM_ENV_PROCVAL(const VALUE *ep)
1131
+ {
1132
+ VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1133
+ VM_ASSERT(VM_ENV_LOCAL_P(ep));
1134
+ VM_ASSERT(VM_ENV_BLOCK_HANDLER(ep) != VM_BLOCK_HANDLER_NONE);
1135
+
1136
+ return ep[VM_ENV_DATA_INDEX_ENV_PROC];
1137
+ }
1138
+
1139
+ static inline const rb_env_t *
1140
+ vm_env_new(VALUE *env_ep, VALUE *env_body, unsigned int env_size, const rb_iseq_t *iseq)
1141
+ {
1142
+ rb_env_t *env = (rb_env_t *)rb_imemo_new(imemo_env, (VALUE)env_ep, (VALUE)env_body, 0, (VALUE)iseq);
1143
+ env->env_size = env_size;
1144
+ env_ep[VM_ENV_DATA_INDEX_ENV] = (VALUE)env;
1145
+ return env;
1146
+ }
1147
+
1148
+ static inline void
1149
+ VM_FORCE_WRITE(const VALUE *ptr, VALUE v)
1150
+ {
1151
+ *((VALUE *)ptr) = v;
1152
+ }
1153
+
1154
+ static inline void
1155
+ VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value)
1156
+ {
1157
+ VM_ASSERT(RB_SPECIAL_CONST_P(special_const_value));
1158
+ VM_FORCE_WRITE(ptr, special_const_value);
1159
+ }
1160
+
1161
+ static inline void
1162
+ VM_STACK_ENV_WRITE(const VALUE *ep, int index, VALUE v)
1163
+ {
1164
+ VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1165
+ VM_FORCE_WRITE(&ep[index], v);
1166
+ }
1167
+
1168
+ const VALUE *rb_vm_ep_local_ep(const VALUE *ep);
1169
+ const VALUE *rb_vm_proc_local_ep(VALUE proc);
1170
+
1171
+ VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp);
1172
+
1173
+ #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1174
+ #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1175
+ #define RUBY_VM_END_CONTROL_FRAME(th) \
1176
+ ((rb_control_frame_t *)((th)->stack + (th)->stack_size))
1177
+ #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1178
+ ((void *)(ecfp) > (void *)(cfp))
1179
+ #define RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp) \
1180
+ (!RUBY_VM_VALID_CONTROL_FRAME_P((cfp), RUBY_VM_END_CONTROL_FRAME(th)))
1181
+
1182
+ static inline int
1183
+ VM_BH_ISEQ_BLOCK_P(VALUE block_handler)
1184
+ {
1185
+ if ((block_handler & 0x03) == 0x01) {
1186
+ #if VM_CHECK_MODE > 0
1187
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1188
+ VM_ASSERT(RB_TYPE_P(captured->code.val, T_IMEMO));
1189
+ VM_ASSERT(imemo_type(captured->code.val) == imemo_iseq);
1190
+ #endif
1191
+ return 1;
1192
+ }
1193
+ else {
1194
+ return 0;
1195
+ }
1196
+ }
1197
+
1198
+ static inline VALUE
1199
+ VM_BH_FROM_ISEQ_BLOCK(const struct rb_captured_block *captured)
1200
+ {
1201
+ VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1202
+ VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1203
+ return block_handler;
1204
+ }
1205
+
1206
+ static inline const struct rb_captured_block *
1207
+ VM_BH_TO_ISEQ_BLOCK(VALUE block_handler)
1208
+ {
1209
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1210
+ VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1211
+ return captured;
1212
+ }
1213
+
1214
+ static inline int
1215
+ VM_BH_IFUNC_P(VALUE block_handler)
1216
+ {
1217
+ if ((block_handler & 0x03) == 0x03) {
1218
+ #if VM_CHECK_MODE > 0
1219
+ struct rb_captured_block *captured = (void *)(block_handler & ~0x03);
1220
+ VM_ASSERT(RB_TYPE_P(captured->code.val, T_IMEMO));
1221
+ VM_ASSERT(imemo_type(captured->code.val) == imemo_ifunc);
1222
+ #endif
1223
+ return 1;
1224
+ }
1225
+ else {
1226
+ return 0;
1227
+ }
1228
+ }
1229
+
1230
+ static inline VALUE
1231
+ VM_BH_FROM_IFUNC_BLOCK(const struct rb_captured_block *captured)
1232
+ {
1233
+ VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1234
+ VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1235
+ return block_handler;
1236
+ }
1237
+
1238
+ static inline const struct rb_captured_block *
1239
+ VM_BH_TO_IFUNC_BLOCK(VALUE block_handler)
1240
+ {
1241
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1242
+ VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1243
+ return captured;
1244
+ }
1245
+
1246
+ static inline const struct rb_captured_block *
1247
+ VM_BH_TO_CAPT_BLOCK(VALUE block_handler)
1248
+ {
1249
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1250
+ VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1251
+ return captured;
1252
+ }
1253
+
1254
+ static inline enum rb_block_handler_type
1255
+ vm_block_handler_type(VALUE block_handler)
1256
+ {
1257
+ if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1258
+ return block_handler_type_iseq;
1259
+ }
1260
+ else if (VM_BH_IFUNC_P(block_handler)) {
1261
+ return block_handler_type_ifunc;
1262
+ }
1263
+ else if (SYMBOL_P(block_handler)) {
1264
+ return block_handler_type_symbol;
1265
+ }
1266
+ else {
1267
+ VM_ASSERT(rb_obj_is_proc(block_handler));
1268
+ return block_handler_type_proc;
1269
+ }
1270
+ }
1271
+
1272
+ static inline int
1273
+ vm_block_handler_verify(VALUE block_handler)
1274
+ {
1275
+ VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1276
+ vm_block_handler_type(block_handler) >= 0);
1277
+ return 1;
1278
+ }
1279
+
1280
+ static inline enum rb_block_type
1281
+ vm_block_type(const struct rb_block *block)
1282
+ {
1283
+ #if VM_CHECK_MODE > 0
1284
+ switch (block->type) {
1285
+ case block_type_iseq:
1286
+ VM_ASSERT(RB_TYPE_P(block->as.captured.code.val, T_IMEMO));
1287
+ VM_ASSERT(imemo_type(block->as.captured.code.val) == imemo_iseq);
1288
+ break;
1289
+ case block_type_ifunc:
1290
+ VM_ASSERT(RB_TYPE_P(block->as.captured.code.val, T_IMEMO));
1291
+ VM_ASSERT(imemo_type(block->as.captured.code.val) == imemo_ifunc);
1292
+ break;
1293
+ case block_type_symbol:
1294
+ VM_ASSERT(SYMBOL_P(block->as.symbol));
1295
+ break;
1296
+ case block_type_proc:
1297
+ VM_ASSERT(rb_obj_is_proc(block->as.proc));
1298
+ break;
1299
+ }
1300
+ #endif
1301
+ return block->type;
1302
+ }
1303
+
1304
+ static inline void
1305
+ vm_block_type_set(const struct rb_block *block, enum rb_block_type type)
1306
+ {
1307
+ struct rb_block *mb = (struct rb_block *)block;
1308
+ mb->type = type;
1309
+ }
1310
+
1311
+ static inline const struct rb_block *
1312
+ vm_proc_block(VALUE procval)
1313
+ {
1314
+ rb_proc_t *proc = RTYPEDDATA_DATA(procval);
1315
+ VM_ASSERT(rb_obj_is_proc(procval));
1316
+ return &proc->block;
1317
+ }
1318
+
1319
+ static inline const rb_iseq_t *vm_block_iseq(const struct rb_block *block);
1320
+ static inline const VALUE *vm_block_ep(const struct rb_block *block);
1321
+
1322
+ static inline const rb_iseq_t *
1323
+ vm_proc_iseq(VALUE procval)
1324
+ {
1325
+ VM_ASSERT(rb_obj_is_proc(procval));
1326
+ return vm_block_iseq(vm_proc_block(procval));
1327
+ }
1328
+
1329
+ static inline const VALUE *
1330
+ vm_proc_ep(VALUE procval)
1331
+ {
1332
+ return vm_block_ep(vm_proc_block(procval));
1333
+ }
1334
+
1335
+ static inline const rb_iseq_t *
1336
+ vm_block_iseq(const struct rb_block *block)
1337
+ {
1338
+ switch (vm_block_type(block)) {
1339
+ case block_type_iseq: return block->as.captured.code.iseq;
1340
+ case block_type_proc: return vm_proc_iseq(block->as.proc);
1341
+ case block_type_ifunc:
1342
+ case block_type_symbol: return NULL;
1343
+ }
1344
+ VM_UNREACHABLE(vm_block_iseq);
1345
+ return NULL;
1346
+ }
1347
+
1348
+ static inline const VALUE *
1349
+ vm_block_ep(const struct rb_block *block)
1350
+ {
1351
+ switch (vm_block_type(block)) {
1352
+ case block_type_iseq:
1353
+ case block_type_ifunc: return block->as.captured.ep;
1354
+ case block_type_proc: return vm_proc_ep(block->as.proc);
1355
+ case block_type_symbol: return NULL;
1356
+ }
1357
+ VM_UNREACHABLE(vm_block_ep);
1358
+ return NULL;
1359
+ }
1360
+
1361
+ static inline VALUE
1362
+ vm_block_self(const struct rb_block *block)
1363
+ {
1364
+ switch (vm_block_type(block)) {
1365
+ case block_type_iseq:
1366
+ case block_type_ifunc:
1367
+ return block->as.captured.self;
1368
+ case block_type_proc:
1369
+ return vm_block_self(vm_proc_block(block->as.proc));
1370
+ case block_type_symbol:
1371
+ return Qundef;
1372
+ }
1373
+ VM_UNREACHABLE(vm_block_self);
1374
+ return Qundef;
1375
+ }
1376
+
1377
+ static inline VALUE
1378
+ VM_BH_TO_SYMBOL(VALUE block_handler)
1379
+ {
1380
+ VM_ASSERT(SYMBOL_P(block_handler));
1381
+ return block_handler;
1382
+ }
1383
+
1384
+ static inline VALUE
1385
+ VM_BH_FROM_SYMBOL(VALUE symbol)
1386
+ {
1387
+ VM_ASSERT(SYMBOL_P(symbol));
1388
+ return symbol;
1389
+ }
1390
+
1391
+ static inline VALUE
1392
+ VM_BH_TO_PROC(VALUE block_handler)
1393
+ {
1394
+ VM_ASSERT(rb_obj_is_proc(block_handler));
1395
+ return block_handler;
1396
+ }
1397
+
1398
+ static inline VALUE
1399
+ VM_BH_FROM_PROC(VALUE procval)
1400
+ {
1401
+ VM_ASSERT(rb_obj_is_proc(procval));
1402
+ return procval;
1403
+ }
1404
+
1405
+ /* VM related object allocate functions */
1406
+ VALUE rb_thread_alloc(VALUE klass);
1407
+ VALUE rb_proc_alloc(VALUE klass);
1408
+ VALUE rb_binding_alloc(VALUE klass);
1409
+
1410
+ /* for debug */
1411
+ extern void rb_vmdebug_stack_dump_raw(rb_thread_t *, rb_control_frame_t *);
1412
+ extern void rb_vmdebug_debug_print_pre(rb_thread_t *th, rb_control_frame_t *cfp, const VALUE *_pc);
1413
+ extern void rb_vmdebug_debug_print_post(rb_thread_t *th, rb_control_frame_t *cfp);
1414
+
1415
+ #define SDR() rb_vmdebug_stack_dump_raw(GET_THREAD(), GET_THREAD()->cfp)
1416
+ #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_THREAD(), (cfp))
1417
+ void rb_vm_bugreport(const void *);
1418
+ NORETURN(void rb_bug_context(const void *, const char *fmt, ...));
1419
+
1420
+ /* functions about thread/vm execution */
1421
+ RUBY_SYMBOL_EXPORT_BEGIN
1422
+ VALUE rb_iseq_eval(const rb_iseq_t *iseq);
1423
+ VALUE rb_iseq_eval_main(const rb_iseq_t *iseq);
1424
+ RUBY_SYMBOL_EXPORT_END
1425
+ int rb_thread_method_id_and_class(rb_thread_t *th, ID *idp, ID *called_idp, VALUE *klassp);
1426
+
1427
+ VALUE rb_vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, int argc, const VALUE *argv, VALUE block_handler);
1428
+ VALUE rb_vm_make_proc_lambda(rb_thread_t *th, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda);
1429
+ VALUE rb_vm_make_proc(rb_thread_t *th, const struct rb_captured_block *captured, VALUE klass);
1430
+ VALUE rb_vm_make_binding(rb_thread_t *th, const rb_control_frame_t *src_cfp);
1431
+ VALUE rb_vm_env_local_variables(const rb_env_t *env);
1432
+ const rb_env_t *rb_vm_env_prev_env(const rb_env_t *env);
1433
+ const VALUE *rb_binding_add_dynavars(rb_binding_t *bind, int dyncount, const ID *dynvars);
1434
+ void rb_vm_inc_const_missing_count(void);
1435
+ void rb_vm_gvl_destroy(rb_vm_t *vm);
1436
+ VALUE rb_vm_call(rb_thread_t *th, VALUE recv, VALUE id, int argc,
1437
+ const VALUE *argv, const rb_callable_method_entry_t *me);
1438
+ void rb_vm_pop_frame(rb_thread_t *th);
1439
+
1440
+ void rb_thread_start_timer_thread(void);
1441
+ void rb_thread_stop_timer_thread(void);
1442
+ void rb_thread_reset_timer_thread(void);
1443
+ void rb_thread_wakeup_timer_thread(void);
1444
+
1445
+ static inline void
1446
+ rb_vm_living_threads_init(rb_vm_t *vm)
1447
+ {
1448
+ list_head_init(&vm->living_threads);
1449
+ vm->living_thread_num = 0;
1450
+ }
1451
+
1452
+ static inline void
1453
+ rb_vm_living_threads_insert(rb_vm_t *vm, rb_thread_t *th)
1454
+ {
1455
+ list_add_tail(&vm->living_threads, &th->vmlt_node);
1456
+ vm->living_thread_num++;
1457
+ }
1458
+
1459
+ static inline void
1460
+ rb_vm_living_threads_remove(rb_vm_t *vm, rb_thread_t *th)
1461
+ {
1462
+ list_del(&th->vmlt_node);
1463
+ vm->living_thread_num--;
1464
+ }
1465
+
1466
+ typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
1467
+ rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(const rb_thread_t *th, const rb_control_frame_t *cfp);
1468
+ rb_control_frame_t *rb_vm_get_binding_creatable_next_cfp(const rb_thread_t *th, const rb_control_frame_t *cfp);
1469
+ int rb_vm_get_sourceline(const rb_control_frame_t *);
1470
+ VALUE rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method);
1471
+ void rb_vm_stack_to_heap(rb_thread_t *th);
1472
+ void ruby_thread_init_stack(rb_thread_t *th);
1473
+ int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp);
1474
+ void rb_vm_rewind_cfp(rb_thread_t *th, rb_control_frame_t *cfp);
1475
+
1476
+ void rb_vm_register_special_exception(enum ruby_special_exceptions sp, VALUE exception_class, const char *mesg);
1477
+
1478
+ void rb_gc_mark_machine_stack(rb_thread_t *th);
1479
+
1480
+ int rb_autoloading_value(VALUE mod, ID id, VALUE* value);
1481
+
1482
+ void rb_vm_rewrite_cref(rb_cref_t *node, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr);
1483
+
1484
+ const rb_callable_method_entry_t *rb_vm_frame_method_entry(const rb_control_frame_t *cfp);
1485
+
1486
+ #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
1487
+
1488
+ #define RUBY_CONST_ASSERT(expr) (1/!!(expr)) /* expr must be a compile-time constant */
1489
+ #define VM_STACK_OVERFLOWED_P(cfp, sp, margin) \
1490
+ (!RUBY_CONST_ASSERT(sizeof(*(sp)) == sizeof(VALUE)) || \
1491
+ !RUBY_CONST_ASSERT(sizeof(*(cfp)) == sizeof(rb_control_frame_t)) || \
1492
+ ((rb_control_frame_t *)((sp) + (margin)) + 1) >= (cfp))
1493
+ #define WHEN_VM_STACK_OVERFLOWED(cfp, sp, margin) \
1494
+ if (LIKELY(!VM_STACK_OVERFLOWED_P(cfp, sp, margin))) {(void)0;} else /* overflowed */
1495
+ #define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) \
1496
+ WHEN_VM_STACK_OVERFLOWED(cfp, sp, margin) vm_stackoverflow()
1497
+ #define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
1498
+ WHEN_VM_STACK_OVERFLOWED(cfp, (cfp)->sp, margin) vm_stackoverflow()
1499
+
1500
+ /* for thread */
1501
+
1502
+ #if RUBY_VM_THREAD_MODEL == 2
1503
+ extern rb_thread_t *ruby_current_thread;
1504
+ extern rb_vm_t *ruby_current_vm;
1505
+ extern rb_event_flag_t ruby_vm_event_flags;
1506
+
1507
+ #define GET_VM() ruby_current_vm
1508
+
1509
+ #ifndef OPT_CALL_CFUNC_WITHOUT_FRAME
1510
+ #define OPT_CALL_CFUNC_WITHOUT_FRAME 0
1511
+ #endif
1512
+
1513
+ #define GET_THREAD() vm_thread_with_frame(ruby_current_thread)
1514
+ #if OPT_CALL_CFUNC_WITHOUT_FRAME
1515
+ static inline rb_thread_t *
1516
+ vm_thread_with_frame(rb_thread_t *th)
1517
+ {
1518
+ if (UNLIKELY(th->passed_ci != 0)) {
1519
+ void rb_vm_call_cfunc_push_frame(rb_thread_t *th);
1520
+ rb_vm_call_cfunc_push_frame(th);
1521
+ }
1522
+ return th;
1523
+ }
1524
+ #else
1525
+ #define vm_thread_with_frame(th) (th)
1526
+ #endif
1527
+
1528
+ #define rb_thread_set_current_raw(th) (void)(ruby_current_thread = (th))
1529
+ #define rb_thread_set_current(th) do { \
1530
+ if ((th)->vm->running_thread != (th)) { \
1531
+ (th)->running_time_us = 0; \
1532
+ } \
1533
+ rb_thread_set_current_raw(th); \
1534
+ (th)->vm->running_thread = (th); \
1535
+ } while (0)
1536
+
1537
+ #else
1538
+ #error "unsupported thread model"
1539
+ #endif
1540
+
1541
+ enum {
1542
+ TIMER_INTERRUPT_MASK = 0x01,
1543
+ PENDING_INTERRUPT_MASK = 0x02,
1544
+ POSTPONED_JOB_INTERRUPT_MASK = 0x04,
1545
+ TRAP_INTERRUPT_MASK = 0x08
1546
+ };
1547
+
1548
+ #define RUBY_VM_SET_TIMER_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, TIMER_INTERRUPT_MASK)
1549
+ #define RUBY_VM_SET_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, PENDING_INTERRUPT_MASK)
1550
+ #define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
1551
+ #define RUBY_VM_SET_TRAP_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, TRAP_INTERRUPT_MASK)
1552
+ #define RUBY_VM_INTERRUPTED(th) ((th)->interrupt_flag & ~(th)->interrupt_mask & (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
1553
+ #define RUBY_VM_INTERRUPTED_ANY(th) ((th)->interrupt_flag & ~(th)->interrupt_mask)
1554
+
1555
+ VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt);
1556
+ int rb_signal_buff_size(void);
1557
+ void rb_signal_exec(rb_thread_t *th, int sig);
1558
+ void rb_threadptr_check_signal(rb_thread_t *mth);
1559
+ void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
1560
+ void rb_threadptr_signal_exit(rb_thread_t *th);
1561
+ void rb_threadptr_execute_interrupts(rb_thread_t *, int);
1562
+ void rb_threadptr_interrupt(rb_thread_t *th);
1563
+ void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th);
1564
+ void rb_threadptr_pending_interrupt_clear(rb_thread_t *th);
1565
+ void rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v);
1566
+ int rb_threadptr_pending_interrupt_active_p(rb_thread_t *th);
1567
+ void rb_threadptr_error_print(rb_thread_t *volatile th, volatile VALUE errinfo);
1568
+
1569
+ #define RUBY_VM_CHECK_INTS(th) ruby_vm_check_ints(th)
1570
+ static inline void
1571
+ ruby_vm_check_ints(rb_thread_t *th)
1572
+ {
1573
+ if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(th))) {
1574
+ rb_threadptr_execute_interrupts(th, 0);
1575
+ }
1576
+ }
1577
+
1578
+ /* tracer */
1579
+ struct rb_trace_arg_struct {
1580
+ rb_event_flag_t event;
1581
+ rb_thread_t *th;
1582
+ rb_control_frame_t *cfp;
1583
+ VALUE self;
1584
+ ID id;
1585
+ ID called_id;
1586
+ VALUE klass;
1587
+ VALUE data;
1588
+
1589
+ int klass_solved;
1590
+
1591
+ /* calc from cfp */
1592
+ int lineno;
1593
+ VALUE path;
1594
+ };
1595
+
1596
+ void rb_threadptr_exec_event_hooks(struct rb_trace_arg_struct *trace_arg);
1597
+ void rb_threadptr_exec_event_hooks_and_pop_frame(struct rb_trace_arg_struct *trace_arg);
1598
+
1599
+ #define EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
1600
+ const rb_event_flag_t flag_arg_ = (flag_); \
1601
+ if (UNLIKELY(ruby_vm_event_flags & (flag_arg_))) { \
1602
+ /* defer evaluating the other arguments */ \
1603
+ ruby_exec_event_hook_orig(th_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
1604
+ } \
1605
+ } while (0)
1606
+
1607
+ static inline void
1608
+ ruby_exec_event_hook_orig(rb_thread_t *const th, const rb_event_flag_t flag,
1609
+ VALUE self, ID id, ID called_id, VALUE klass, VALUE data, int pop_p)
1610
+ {
1611
+ if ((th->event_hooks.events | th->vm->event_hooks.events) & flag) {
1612
+ struct rb_trace_arg_struct trace_arg;
1613
+ trace_arg.event = flag;
1614
+ trace_arg.th = th;
1615
+ trace_arg.cfp = th->cfp;
1616
+ trace_arg.self = self;
1617
+ trace_arg.id = id;
1618
+ trace_arg.called_id = called_id;
1619
+ trace_arg.klass = klass;
1620
+ trace_arg.data = data;
1621
+ trace_arg.path = Qundef;
1622
+ trace_arg.klass_solved = 0;
1623
+ if (pop_p) rb_threadptr_exec_event_hooks_and_pop_frame(&trace_arg);
1624
+ else rb_threadptr_exec_event_hooks(&trace_arg);
1625
+ }
1626
+ }
1627
+
1628
+ #define EXEC_EVENT_HOOK(th_, flag_, self_, id_, called_id_, klass_, data_) \
1629
+ EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, called_id_, klass_, data_, 0)
1630
+
1631
+ #define EXEC_EVENT_HOOK_AND_POP_FRAME(th_, flag_, self_, id_, called_id_, klass_, data_) \
1632
+ EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, called_id_, klass_, data_, 1)
1633
+
1634
+ RUBY_SYMBOL_EXPORT_BEGIN
1635
+
1636
+ int rb_thread_check_trap_pending(void);
1637
+
1638
+ extern VALUE rb_get_coverages(void);
1639
+ extern void rb_set_coverages(VALUE);
1640
+ extern void rb_reset_coverages(void);
1641
+
1642
+ void rb_postponed_job_flush(rb_vm_t *vm);
1643
+
1644
+ RUBY_SYMBOL_EXPORT_END
1645
+
1646
+ #endif /* RUBY_VM_CORE_H */