Ruby 4.1.0dev (2026-05-15 revision a8bcae043f931d9b79f1cb1fe2c021985d07b984)
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_cvar_entry = 9,
41 imemo_callinfo = 10,
42 imemo_callcache = 11,
43 imemo_constcache = 12,
44 imemo_fields = 13,
45 imemo_subclasses = 14,
46};
47
48/* CREF (Class REFerence) is defined in method.h */
49
51struct vm_svar {
52 VALUE flags;
54 const VALUE lastline;
55 const VALUE backref;
56 const VALUE others;
57};
58
61 VALUE flags;
62 const VALUE throw_obj;
63 const struct rb_control_frame_struct *catch_frame;
64 int throw_state;
65};
66
67#define THROW_DATA_CONSUMED IMEMO_FL_USER0
68
69/* IFUNC (Internal FUNCtion) */
70
72#if SIZEOF_INT * 2 > SIZEOF_VALUE
73 signed int min: (SIZEOF_VALUE * CHAR_BIT) / 2;
74 signed int max: (SIZEOF_VALUE * CHAR_BIT) / 2;
75#else
76 int min, max;
77#endif
78};
79
86struct vm_ifunc {
87 VALUE flags;
88 VALUE *svar_lep;
90 const void *data;
91 struct vm_ifunc_argc argc;
92};
93#define IFUNC_YIELD_OPTIMIZABLE IMEMO_FL_USER0
94
96 VALUE flags;
97 VALUE *ptr; /* malloc'ed buffer */
98 size_t size; /* buffer size in bytes */
99};
100
101/* Set on imemo_memo when u3 holds a VALUE that GC must mark.
102 * When unset, u3 is a non-VALUE (cnt/state). */
103#define MEMO_U3_IS_VALUE IMEMO_FL_USER0
104
109struct MEMO {
110 VALUE flags;
111 const VALUE v1;
112 const VALUE v2;
113 union {
114 long cnt;
115 long state;
116 const VALUE value;
117 } u3;
118};
119
120#define IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T), false))
121#define SHAREABLE_IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T), true))
122
123/* ment is in method.h */
124
125#define THROW_DATA_P(err) imemo_throw_data_p((VALUE)err)
126#define MEMO_CAST(m) ((struct MEMO *)(m))
127#define MEMO_FOR(type, value) ((type *)RARRAY_PTR(value))
128#define NEW_MEMO_FOR(type, value) \
129 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), MEMO_FOR(type, value))
130#define NEW_PARTIAL_MEMO_FOR(type, value, member) \
131 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), \
132 rb_ary_set_len((value), offsetof(type, member) / sizeof(VALUE)), \
133 MEMO_FOR(type, value))
134
135#ifndef RUBY_RUBYPARSER_H
137#endif
138VALUE rb_imemo_new(enum imemo_type type, VALUE v0, size_t size, bool is_shareable);
139VALUE rb_imemo_tmpbuf_new(void);
140struct MEMO *rb_imemo_memo_new(VALUE a, VALUE b, long c);
141struct MEMO *rb_imemo_memo_new_value(VALUE a, VALUE b, VALUE c);
142struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
143static inline enum imemo_type imemo_type(VALUE imemo);
144static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
145static inline bool imemo_throw_data_p(VALUE imemo);
146static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
147static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
148static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
149static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
150
151size_t rb_imemo_memsize(VALUE obj);
152void rb_imemo_mark_and_move(VALUE obj, bool reference_updating);
153void rb_imemo_free(VALUE obj);
154
155RUBY_SYMBOL_EXPORT_BEGIN
156const char *rb_imemo_name(enum imemo_type type);
157RUBY_SYMBOL_EXPORT_END
158
159static inline enum imemo_type
160imemo_type(VALUE imemo)
161{
162 return (RBASIC(imemo)->flags >> FL_USHIFT) & IMEMO_MASK;
163}
164
165static inline int
166imemo_type_p(VALUE imemo, enum imemo_type imemo_type)
167{
168 if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) {
169 /* fixed at compile time if imemo_type is given. */
170 const VALUE mask = (IMEMO_MASK << FL_USHIFT) | RUBY_T_MASK;
171 const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO;
172 /* fixed at runtime. */
173 return expected_type == (RBASIC(imemo)->flags & mask);
174 }
175 else {
176 return 0;
177 }
178}
179
180#define IMEMO_TYPE_P(v, t) imemo_type_p((VALUE)(v), t)
181
182static inline bool
183imemo_throw_data_p(VALUE imemo)
184{
185 return RB_TYPE_P(imemo, T_IMEMO);
186}
187
188static inline struct vm_ifunc *
189rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
190{
191 return rb_vm_ifunc_new(func, data, 0, UNLIMITED_ARGUMENTS);
192}
193
194static inline void *
195RB_IMEMO_TMPBUF_PTR(VALUE v)
196{
197 const struct rb_imemo_tmpbuf_struct *p = (const void *)v;
198 return p->ptr;
199}
200
201static inline VALUE
202rb_imemo_tmpbuf_new_from_an_RString(VALUE str)
203{
204 VALUE imemo;
205 size_t len;
206
207 StringValue(str);
208 len = RSTRING_LEN(str);
209 rb_alloc_tmp_buffer(&imemo, len);
210 memcpy(RB_IMEMO_TMPBUF_PTR(imemo), RSTRING_PTR(str), len);
211 return imemo;
212}
213
214static inline void
215MEMO_V1_SET(struct MEMO *m, VALUE v)
216{
217 RB_OBJ_WRITE(m, &m->v1, v);
218}
219
220static inline void
221MEMO_V2_SET(struct MEMO *m, VALUE v)
222{
223 RB_OBJ_WRITE(m, &m->v2, v);
224}
225
226struct rb_fields {
227 struct RBasic basic;
228 union {
229 struct {
230 VALUE fields[1];
231 } embed;
232 struct {
233 VALUE *ptr;
234 } external;
235 struct {
236 // Note: the st_table could be embedded, but complex T_CLASS should be rare to
237 // non-existent, so not really worth the trouble.
238 st_table *table;
239 } complex;
240 } as;
241};
242
243// IMEMO/fields and T_OBJECT have exactly the same layout.
244// This is useful for JIT and common codepaths.
245#define OBJ_FIELD_HEAP ROBJECT_HEAP
246STATIC_ASSERT(imemo_fields_flags, OBJ_FIELD_HEAP == IMEMO_FL_USER0);
247STATIC_ASSERT(imemo_fields_embed_offset, offsetof(struct RObject, as.ary) == offsetof(struct rb_fields, as.embed.fields));
248STATIC_ASSERT(imemo_fields_external_offset, offsetof(struct RObject, as.heap.fields) == offsetof(struct rb_fields, as.external.ptr));
249STATIC_ASSERT(imemo_fields_complex_offset, offsetof(struct RObject, as.heap.fields) == offsetof(struct rb_fields, as.complex.table));
250
251#define IMEMO_OBJ_FIELDS(fields) ((struct rb_fields *)fields)
252
253#define IMEMO_SUBCLASSES_HEAP IMEMO_FL_USER0
254
256 VALUE flags;
257 uint32_t count;
258 uint32_t capacity;
259 union {
260 VALUE *external;
261 VALUE embed[1];
262 } as;
263};
264
265static inline VALUE *
266rb_imemo_subclasses_entries(VALUE v)
267{
268 struct rb_subclasses *s = (struct rb_subclasses *)v;
269 return FL_TEST_RAW(v, IMEMO_SUBCLASSES_HEAP) ? s->as.external : s->as.embed;
270}
271
272VALUE rb_imemo_fields_new(VALUE owner, /* shape_id_t */ uint32_t shape_id, bool shareable);
273VALUE rb_imemo_subclasses_new(uint32_t capacity);
274VALUE rb_imemo_fields_new_complex(VALUE owner, /* shape_id_t */ uint32_t shape_id, size_t capa, bool shareable);
275VALUE rb_imemo_fields_new_complex_tbl(VALUE owner, /* shape_id_t */ uint32_t shape_id, st_table *tbl, bool shareable);
276VALUE rb_imemo_fields_clone(VALUE fields_obj);
277void rb_imemo_fields_clear(VALUE fields_obj);
278
279static inline VALUE
280rb_imemo_fields_owner(VALUE fields_obj)
281{
282 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
283
284 return CLASS_OF(fields_obj);
285}
286
287static inline VALUE *
288rb_imemo_fields_ptr(VALUE fields_obj)
289{
290 if (!fields_obj) {
291 return NULL;
292 }
293
294 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields) || RB_TYPE_P(fields_obj, T_OBJECT));
295
296 if (UNLIKELY(FL_TEST_RAW(fields_obj, OBJ_FIELD_HEAP))) {
297 return IMEMO_OBJ_FIELDS(fields_obj)->as.external.ptr;
298 }
299 else {
300 return IMEMO_OBJ_FIELDS(fields_obj)->as.embed.fields;
301 }
302}
303
304static inline st_table *
305rb_imemo_fields_complex_tbl(VALUE fields_obj)
306{
307 if (!fields_obj) {
308 return NULL;
309 }
310
311 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields) || RB_TYPE_P(fields_obj, T_OBJECT));
312 RUBY_ASSERT(FL_TEST_RAW(fields_obj, OBJ_FIELD_HEAP));
313
314 // Some codepaths unconditionally access the fields_ptr, and assume it can be used as st_table if the
315 // shape is complex.
316 RUBY_ASSERT((st_table *)rb_imemo_fields_ptr(fields_obj) == IMEMO_OBJ_FIELDS(fields_obj)->as.complex.table);
317
318 return IMEMO_OBJ_FIELDS(fields_obj)->as.complex.table;
319}
320
321#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:205
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#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:67
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:456
#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:109
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::@56::@57 heap
Object that use separated memory region for instance variables use this pattern.
Definition st.h:79
IFUNC (Internal FUNCtion)
Definition imemo.h:86
SVAR (Special VARiable)
Definition imemo.h:51
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition imemo.h:53
THROW_DATA.
Definition imemo.h:60
#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