Ruby 4.1.0dev (2026-12-06 revision 6b501f5444cf675f4a654bda46c812e2c1df9fb8)
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
18struct rb_gc_mark_ctx; /* internal/gc.h */
19
20#define IMEMO_MASK (FL_USER0 | FL_USER1 | FL_USER2 | FL_USER3 | FL_USER4)
21
22/* FL_USER0 to FL_USER4 is for type */
23#define IMEMO_FL_USHIFT (FL_USHIFT + 5)
24#define IMEMO_FL_USER0 FL_USER5
25#define IMEMO_FL_USER1 FL_USER6
26#define IMEMO_FL_USER2 FL_USER7
27#define IMEMO_FL_USER3 FL_USER8
28#define IMEMO_FL_USER4 FL_USER9
29#define IMEMO_FL_USER5 FL_USER10
30#define IMEMO_FL_USER6 FL_USER11
31
32enum imemo_type {
33 imemo_env = 0,
34 imemo_cref = 1,
35 imemo_svar = 2,
36 imemo_throw_data = 3,
37 imemo_ifunc = 4,
38 imemo_memo = 5,
39 imemo_ment = 6,
40 imemo_iseq = 7,
41 imemo_tmpbuf = 8,
42 imemo_cvar_entry = 9,
43 imemo_callinfo = 10,
44 imemo_callcache = 11,
45 imemo_constcache = 12,
46 imemo_fields = 13,
47 imemo_subclasses = 14,
48 imemo_cdhash = 15,
49 imemo_once_storage = 16,
50};
51
52/* CREF (Class REFerence) is defined in method.h */
53
55struct vm_svar {
56 VALUE flags;
58 const VALUE lastline;
59 const VALUE backref;
60 const VALUE others;
61};
62
65 VALUE flags;
66 const VALUE throw_obj;
67 const struct rb_control_frame_struct *catch_frame;
68 int throw_state;
69};
70
71#define THROW_DATA_CONSUMED IMEMO_FL_USER0
72
73/* IFUNC (Internal FUNCtion) */
74
76#if SIZEOF_INT * 2 > SIZEOF_VALUE
77 signed int min: (SIZEOF_VALUE * CHAR_BIT) / 2;
78 signed int max: (SIZEOF_VALUE * CHAR_BIT) / 2;
79#else
80 int min, max;
81#endif
82};
83
90struct vm_ifunc {
91 VALUE flags;
92 VALUE *svar_lep;
94 const void *data;
95 struct vm_ifunc_argc argc;
96};
97#define IFUNC_YIELD_OPTIMIZABLE IMEMO_FL_USER0
98
100 VALUE flags;
101 VALUE *ptr; /* malloc'ed buffer */
102 size_t size; /* buffer size in bytes */
103 bool marked; /* whether the buffer may contain object references */
104};
105
107 VALUE flags;
108 st_table tbl;
109};
110
113 VALUE flags;
114 const VALUE value;
115 struct rb_thread_struct *running_thread;
116};
117
118/* Set on imemo_memo when u3 holds a VALUE that GC must mark.
119 * When unset, u3 is a non-VALUE (cnt/ucnt/state). */
120#define MEMO_U3_IS_VALUE IMEMO_FL_USER0
121
126struct MEMO {
127 VALUE flags;
128 const VALUE v1;
129 const VALUE v2;
130 union {
131 rb_len_t cnt;
132 rb_ulen_t ucnt;
133 long state;
134 const VALUE value;
135 } u3;
136};
137
138#define IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T), false))
139#define SHAREABLE_IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T), true))
140
141/* ment is in method.h */
142
143#define THROW_DATA_P(err) imemo_throw_data_p((VALUE)err)
144#define MEMO_CAST(m) ((struct MEMO *)(m))
145#define MEMO_FOR(type, value) ((type *)RARRAY_PTR(value))
146#define NEW_MEMO_FOR(type, value) \
147 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), MEMO_FOR(type, value))
148#define NEW_PARTIAL_MEMO_FOR(type, value, member) \
149 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), \
150 rb_ary_set_len((value), offsetof(type, member) / sizeof(VALUE)), \
151 MEMO_FOR(type, value))
152
153#ifndef RUBY_RUBYPARSER_H
154typedef struct rb_imemo_tmpbuf_struct rb_imemo_tmpbuf_t;
155#endif
156VALUE rb_imemo_new(enum imemo_type type, VALUE v0, size_t size, bool is_shareable);
157struct MEMO *rb_imemo_memo_new(VALUE a, VALUE b, rb_len_t c);
158struct MEMO *rb_imemo_memo_new_value(VALUE a, VALUE b, VALUE c);
159struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
160static inline enum imemo_type imemo_type(VALUE imemo);
161static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
162static inline bool imemo_throw_data_p(VALUE imemo);
163static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
164static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
165static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
166static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
167
168size_t rb_imemo_memsize(VALUE obj);
169void rb_imemo_mark_and_move(const struct rb_gc_mark_ctx *ctx, VALUE obj, bool reference_updating);
170void rb_imemo_free(VALUE obj);
171
172RUBY_SYMBOL_EXPORT_BEGIN
173const char *rb_imemo_name(enum imemo_type type);
174ID rb_imemo_callinfo_mid(VALUE obj);
176 VALUE klass;
177 ID called_id;
178};
179bool rb_imemo_callcache_get_data(VALUE obj, struct rb_imemo_callcache_data *data);
180RUBY_SYMBOL_EXPORT_END
181
182static inline enum imemo_type
183imemo_type(VALUE imemo)
184{
185 return (RBASIC(imemo)->flags & IMEMO_MASK) >> FL_USHIFT;
186}
187
188static inline int
189imemo_type_p(VALUE imemo, enum imemo_type imemo_type)
190{
191 if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) {
192 /* fixed at compile time if imemo_type is given. */
193 const VALUE mask = IMEMO_MASK | RUBY_T_MASK;
194 const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO;
195 /* fixed at runtime. */
196 return expected_type == (RBASIC(imemo)->flags & mask);
197 }
198 else {
199 return 0;
200 }
201}
202
203#define IMEMO_TYPE_P(v, t) imemo_type_p((VALUE)(v), t)
204
205static inline bool
206imemo_throw_data_p(VALUE imemo)
207{
208 return RB_TYPE_P(imemo, T_IMEMO);
209}
210
211static inline struct vm_ifunc *
212rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
213{
214 return rb_vm_ifunc_new(func, data, 0, UNLIMITED_ARGUMENTS);
215}
216
217static inline void *
218RB_IMEMO_TMPBUF_PTR(VALUE v)
219{
220 const struct rb_imemo_tmpbuf_struct *p = (const void *)v;
221 return p->ptr;
222}
223
224static inline VALUE
225rb_imemo_tmpbuf_new_from_an_RString(VALUE str)
226{
227 VALUE imemo;
228 size_t len;
229
230 StringValue(str);
231 len = RSTRING_LEN(str);
232 rb_alloc_tmp_buffer(&imemo, len, false);
233 memcpy(RB_IMEMO_TMPBUF_PTR(imemo), RSTRING_PTR(str), len);
234 return imemo;
235}
236
237static inline void
238MEMO_V1_SET(struct MEMO *m, VALUE v)
239{
240 RB_OBJ_WRITE(m, &m->v1, v);
241}
242
243static inline void
244MEMO_V2_SET(struct MEMO *m, VALUE v)
245{
246 RB_OBJ_WRITE(m, &m->v2, v);
247}
248
249VALUE rb_imemo_cdhash_new(size_t size, const struct st_hash_type *type);
250VALUE rb_imemo_once_storage_new(void);
251
252static inline st_table *
253rb_imemo_cdhash_tbl(VALUE obj)
254{
255 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_cdhash));
256 return &((struct rb_imemo_cdhash *)obj)->tbl;
257}
258
259struct rb_fields {
260 struct RBasic basic;
261 union {
262 struct {
263 VALUE fields[1];
264 } embed;
265 struct {
266 st_table table;
267 } complex;
268 } as;
269};
270
271// IMEMO/fields and T_OBJECT have exactly the same layout.
272// This is useful for JIT and common codepaths.
273STATIC_ASSERT(imemo_fields_embed_offset, offsetof(struct RObject, as.ary) == offsetof(struct rb_fields, as.embed.fields));
274
275#define IMEMO_OBJ_FIELDS(fields) ((struct rb_fields *)fields)
276
277#define IMEMO_SUBCLASSES_HEAP IMEMO_FL_USER0
278
280 VALUE flags;
281 uint32_t count;
282 uint32_t capacity;
283 union {
284 VALUE *external;
285 VALUE embed[1];
286 } as;
287};
288
289static inline VALUE *
290rb_imemo_subclasses_entries(VALUE v)
291{
292 struct rb_subclasses *s = (struct rb_subclasses *)v;
293 return FL_TEST_RAW(v, IMEMO_SUBCLASSES_HEAP) ? s->as.external : s->as.embed;
294}
295
296VALUE rb_imemo_fields_new(VALUE owner, /* shape_id_t */ uint32_t shape_id, bool shareable);
297VALUE rb_imemo_subclasses_new(uint32_t capacity);
298VALUE rb_imemo_fields_new_complex(VALUE owner, /* shape_id_t */ uint32_t shape_id, size_t capa, bool shareable);
299VALUE rb_imemo_fields_new_complex_empty(VALUE owner);
300VALUE rb_imemo_fields_clone(VALUE fields_obj);
301void rb_imemo_fields_clear(VALUE fields_obj);
302
303static inline VALUE
304rb_imemo_fields_owner(VALUE fields_obj)
305{
306 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
307
308 return CLASS_OF(fields_obj);
309}
310
311#endif /* INTERNAL_IMEMO_H */
#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 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:492
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#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:126
Ruby object's base components.
Definition rbasic.h:69
Ruby's ordinal objects.
Definition robject.h:51
Storage for the once instruction (see vm_once_dispatch).
Definition imemo.h:112
Definition st.h:79
IFUNC (Internal FUNCtion).
Definition imemo.h:90
SVAR (Special VARiable).
Definition imemo.h:55
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition imemo.h:57
THROW_DATA.
Definition imemo.h:64
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
unsigned long rb_ulen_t
An unsigned integer type that has the same width with rb_len_t.
Definition value.h:145
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
#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