Ruby 3.5.0dev (2025-11-03 revision 4a3d8346a6d0e068508631541f6bc43e8b154ea1)
imemo.h
1#ifndef INTERNAL_IMEMO_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_IMEMO_H
11#include "ruby/internal/config.h"
12#include <stddef.h> /* for size_t */
13#include "id_table.h"
14#include "internal/array.h" /* for rb_ary_hidden_new_fill */
15#include "ruby/internal/stdbool.h" /* for bool */
16#include "ruby/ruby.h" /* for rb_block_call_func_t */
17
18#define IMEMO_MASK 0x0f
19
20/* FL_USER0 to FL_USER3 is for type */
21#define IMEMO_FL_USHIFT (FL_USHIFT + 4)
22#define IMEMO_FL_USER0 FL_USER4
23#define IMEMO_FL_USER1 FL_USER5
24#define IMEMO_FL_USER2 FL_USER6
25#define IMEMO_FL_USER3 FL_USER7
26#define IMEMO_FL_USER4 FL_USER8
27#define IMEMO_FL_USER5 FL_USER9
28#define IMEMO_FL_USER6 FL_USER10
29
30enum imemo_type {
31 imemo_env = 0,
32 imemo_cref = 1,
33 imemo_svar = 2,
34 imemo_throw_data = 3,
35 imemo_ifunc = 4,
36 imemo_memo = 5,
37 imemo_ment = 6,
38 imemo_iseq = 7,
39 imemo_tmpbuf = 8,
40 imemo_callinfo = 10,
41 imemo_callcache = 11,
42 imemo_constcache = 12,
43 imemo_fields = 13,
44};
45
46/* CREF (Class REFerence) is defined in method.h */
47
49struct vm_svar {
50 VALUE flags;
52 const VALUE lastline;
53 const VALUE backref;
54 const VALUE others;
55};
56
59 VALUE flags;
60 VALUE reserved;
61 const VALUE throw_obj;
62 const struct rb_control_frame_struct *catch_frame;
63 int throw_state;
64};
65
66#define THROW_DATA_CONSUMED IMEMO_FL_USER0
67
68/* IFUNC (Internal FUNCtion) */
69
71#if SIZEOF_INT * 2 > SIZEOF_VALUE
72 signed int min: (SIZEOF_VALUE * CHAR_BIT) / 2;
73 signed int max: (SIZEOF_VALUE * CHAR_BIT) / 2;
74#else
75 int min, max;
76#endif
77};
78
85struct vm_ifunc {
86 VALUE flags;
87 VALUE *svar_lep;
89 const void *data;
90 struct vm_ifunc_argc argc;
91};
92#define IFUNC_YIELD_OPTIMIZABLE IMEMO_FL_USER0
93
95 VALUE flags;
96 VALUE *ptr; /* malloc'ed buffer */
97 size_t cnt; /* buffer size in VALUE */
98};
99
104struct MEMO {
105 VALUE flags;
106 VALUE reserved;
107 const VALUE v1;
108 const VALUE v2;
109 union {
110 long cnt;
111 long state;
112 const VALUE value;
113 void (*func)(void);
114 } u3;
115};
116
117#define IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T), false))
118#define SHAREABLE_IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T), true))
119
120/* ment is in method.h */
121
122#define THROW_DATA_P(err) imemo_throw_data_p((VALUE)err)
123#define MEMO_CAST(m) ((struct MEMO *)(m))
124#define MEMO_FOR(type, value) ((type *)RARRAY_PTR(value))
125#define NEW_MEMO_FOR(type, value) \
126 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), MEMO_FOR(type, value))
127#define NEW_PARTIAL_MEMO_FOR(type, value, member) \
128 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), \
129 rb_ary_set_len((value), offsetof(type, member) / sizeof(VALUE)), \
130 MEMO_FOR(type, value))
131
132#ifndef RUBY_RUBYPARSER_H
134#endif
135VALUE rb_imemo_new(enum imemo_type type, VALUE v0, size_t size, bool is_shareable);
136VALUE rb_imemo_tmpbuf_new(void);
137struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
138static inline enum imemo_type imemo_type(VALUE imemo);
139static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
140static inline bool imemo_throw_data_p(VALUE imemo);
141static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
142static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
143static inline void *rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr);
144static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
145static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
146
147size_t rb_imemo_memsize(VALUE obj);
148void rb_imemo_mark_and_move(VALUE obj, bool reference_updating);
149void rb_imemo_free(VALUE obj);
150
151RUBY_SYMBOL_EXPORT_BEGIN
152const char *rb_imemo_name(enum imemo_type type);
153RUBY_SYMBOL_EXPORT_END
154
155static inline struct MEMO *
156MEMO_NEW(VALUE a, VALUE b, VALUE c)
157{
158 struct MEMO *memo = IMEMO_NEW(struct MEMO, imemo_memo, 0);
159 *((VALUE *)&memo->v1) = a;
160 *((VALUE *)&memo->v2) = b;
161 *((VALUE *)&memo->u3.value) = c;
162
163 return memo;
164}
165
166static inline enum imemo_type
167imemo_type(VALUE imemo)
168{
169 return (RBASIC(imemo)->flags >> FL_USHIFT) & IMEMO_MASK;
170}
171
172static inline int
173imemo_type_p(VALUE imemo, enum imemo_type imemo_type)
174{
175 if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) {
176 /* fixed at compile time if imemo_type is given. */
177 const VALUE mask = (IMEMO_MASK << FL_USHIFT) | RUBY_T_MASK;
178 const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO;
179 /* fixed at runtime. */
180 return expected_type == (RBASIC(imemo)->flags & mask);
181 }
182 else {
183 return 0;
184 }
185}
186
187#define IMEMO_TYPE_P(v, t) imemo_type_p((VALUE)(v), t)
188
189static inline bool
190imemo_throw_data_p(VALUE imemo)
191{
192 return RB_TYPE_P(imemo, T_IMEMO);
193}
194
195static inline struct vm_ifunc *
196rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
197{
198 return rb_vm_ifunc_new(func, data, 0, UNLIMITED_ARGUMENTS);
199}
200
201static inline void *
202RB_IMEMO_TMPBUF_PTR(VALUE v)
203{
204 const struct rb_imemo_tmpbuf_struct *p = (const void *)v;
205 return p->ptr;
206}
207
208static inline void *
209rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr)
210{
211 return ((rb_imemo_tmpbuf_t *)v)->ptr = ptr;
212}
213
214static inline VALUE
215rb_imemo_tmpbuf_new_from_an_RString(VALUE str)
216{
217 const void *src;
218 VALUE imemo;
219 rb_imemo_tmpbuf_t *tmpbuf;
220 void *dst;
221 size_t len;
222
223 StringValue(str);
224 /* create tmpbuf to keep the pointer before xmalloc */
225 imemo = rb_imemo_tmpbuf_new();
226 tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
227 len = RSTRING_LEN(str);
228 src = RSTRING_PTR(str);
229 dst = ruby_xmalloc(len);
230 memcpy(dst, src, len);
231 tmpbuf->ptr = dst;
232 return imemo;
233}
234
235static inline void
236MEMO_V1_SET(struct MEMO *m, VALUE v)
237{
238 RB_OBJ_WRITE(m, &m->v1, v);
239}
240
241static inline void
242MEMO_V2_SET(struct MEMO *m, VALUE v)
243{
244 RB_OBJ_WRITE(m, &m->v2, v);
245}
246
247struct rb_fields {
248 struct RBasic basic;
249 union {
250 struct {
251 VALUE fields[1];
252 } embed;
253 struct {
254 VALUE *ptr;
255 } external;
256 struct {
257 // Note: the st_table could be embedded, but complex T_CLASS should be rare to
258 // non-existent, so not really worth the trouble.
259 st_table *table;
260 } complex;
261 } as;
262};
263
264// IMEMO/fields and T_OBJECT have exactly the same layout.
265// This is useful for JIT and common codepaths.
266#define OBJ_FIELD_HEAP ROBJECT_HEAP
267STATIC_ASSERT(imemo_fields_flags, OBJ_FIELD_HEAP == IMEMO_FL_USER0);
268STATIC_ASSERT(imemo_fields_embed_offset, offsetof(struct RObject, as.ary) == offsetof(struct rb_fields, as.embed.fields));
269STATIC_ASSERT(imemo_fields_embed_offset, offsetof(struct RObject, as.heap.fields) == offsetof(struct rb_fields, as.external.ptr));
270STATIC_ASSERT(imemo_fields_embed_offset, offsetof(struct RObject, as.heap.fields) == offsetof(struct rb_fields, as.complex.table));
271
272#define IMEMO_OBJ_FIELDS(fields) ((struct rb_fields *)fields)
273
274VALUE rb_imemo_fields_new(VALUE owner, size_t capa, bool shareable);
275VALUE rb_imemo_fields_new_complex(VALUE owner, size_t capa, bool shareable);
276VALUE rb_imemo_fields_new_complex_tbl(VALUE owner, st_table *tbl, bool shareable);
277VALUE rb_imemo_fields_clone(VALUE fields_obj);
278void rb_imemo_fields_clear(VALUE fields_obj);
279
280static inline VALUE
281rb_imemo_fields_owner(VALUE fields_obj)
282{
283 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
284
285 return CLASS_OF(fields_obj);
286}
287
288static inline VALUE *
289rb_imemo_fields_ptr(VALUE fields_obj)
290{
291 if (!fields_obj) {
292 return NULL;
293 }
294
295 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields) || RB_TYPE_P(fields_obj, T_OBJECT));
296
297 if (UNLIKELY(FL_TEST_RAW(fields_obj, OBJ_FIELD_HEAP))) {
298 return IMEMO_OBJ_FIELDS(fields_obj)->as.external.ptr;
299 }
300 else {
301 return IMEMO_OBJ_FIELDS(fields_obj)->as.embed.fields;
302 }
303}
304
305static inline st_table *
306rb_imemo_fields_complex_tbl(VALUE fields_obj)
307{
308 if (!fields_obj) {
309 return NULL;
310 }
311
312 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields) || RB_TYPE_P(fields_obj, T_OBJECT));
313 RUBY_ASSERT(FL_TEST_RAW(fields_obj, OBJ_FIELD_HEAP));
314
315 // Some codepaths unconditionally access the fields_ptr, and assume it can be used as st_table if the
316 // shape is too_complex.
317 RUBY_ASSERT((st_table *)rb_imemo_fields_ptr(fields_obj) == IMEMO_OBJ_FIELDS(fields_obj)->as.complex.table);
318
319 return IMEMO_OBJ_FIELDS(fields_obj)->as.complex.table;
320}
321
322#endif /* INTERNAL_IMEMO_H */
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:206
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:131
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
Definition fl_type.h:68
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
int capa
Designed capacity of the buffer.
Definition io.h:11
int len
Length of the buffer.
Definition io.h:8
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
Definition iterator.h:88
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
C99 shim for <stdbool.h>
MEMO.
Definition imemo.h:104
Ruby object's base components.
Definition rbasic.h:69
Ruby's ordinal objects.
Definition robject.h:85
VALUE * fields
Pointer to a C array that holds instance variables.
Definition robject.h:99
struct RObject::@48::@49 heap
Object that use separated memory region for instance variables use this pattern.
Definition st.h:79
IFUNC (Internal FUNCtion)
Definition imemo.h:85
SVAR (Special VARiable)
Definition imemo.h:49
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition imemo.h:51
THROW_DATA.
Definition imemo.h:58
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
Definition value.h:69
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376
@ RUBY_T_MASK
Bitmask of ruby_value_type.
Definition value_type.h:145