Ruby 4.1.0dev (2026-09-07 revision b57404b461ba8bf34e802d86b0db78388216e182)
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 (FL_USER0 | FL_USER1 | FL_USER2 | FL_USER3 | FL_USER4)
19
20/* FL_USER0 to FL_USER4 is for type */
21#define IMEMO_FL_USHIFT (FL_USHIFT + 5)
22#define IMEMO_FL_USER0 FL_USER5
23#define IMEMO_FL_USER1 FL_USER6
24#define IMEMO_FL_USER2 FL_USER7
25#define IMEMO_FL_USER3 FL_USER8
26#define IMEMO_FL_USER4 FL_USER9
27#define IMEMO_FL_USER5 FL_USER10
28#define IMEMO_FL_USER6 FL_USER11
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 imemo_cdhash = 15,
47};
48
49/* CREF (Class REFerence) is defined in method.h */
50
52struct vm_svar {
53 VALUE flags;
55 const VALUE lastline;
56 const VALUE backref;
57 const VALUE others;
58};
59
62 VALUE flags;
63 const VALUE throw_obj;
64 const struct rb_control_frame_struct *catch_frame;
65 int throw_state;
66};
67
68#define THROW_DATA_CONSUMED IMEMO_FL_USER0
69
70/* IFUNC (Internal FUNCtion) */
71
73#if SIZEOF_INT * 2 > SIZEOF_VALUE
74 signed int min: (SIZEOF_VALUE * CHAR_BIT) / 2;
75 signed int max: (SIZEOF_VALUE * CHAR_BIT) / 2;
76#else
77 int min, max;
78#endif
79};
80
87struct vm_ifunc {
88 VALUE flags;
89 VALUE *svar_lep;
91 const void *data;
92 struct vm_ifunc_argc argc;
93};
94#define IFUNC_YIELD_OPTIMIZABLE IMEMO_FL_USER0
95
97 VALUE flags;
98 VALUE *ptr; /* malloc'ed buffer */
99 size_t size; /* buffer size in bytes */
100 bool marked; /* whether the buffer may contain object references */
101};
102
104 VALUE flags;
105 st_table tbl;
106};
107
108/* Set on imemo_memo when u3 holds a VALUE that GC must mark.
109 * When unset, u3 is a non-VALUE (cnt/state). */
110#define MEMO_U3_IS_VALUE IMEMO_FL_USER0
111
116struct MEMO {
117 VALUE flags;
118 const VALUE v1;
119 const VALUE v2;
120 union {
121 long cnt;
122 long state;
123 const VALUE value;
124 } u3;
125};
126
127#define IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T), false))
128#define SHAREABLE_IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T), true))
129
130/* ment is in method.h */
131
132#define THROW_DATA_P(err) imemo_throw_data_p((VALUE)err)
133#define MEMO_CAST(m) ((struct MEMO *)(m))
134#define MEMO_FOR(type, value) ((type *)RARRAY_PTR(value))
135#define NEW_MEMO_FOR(type, value) \
136 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), MEMO_FOR(type, value))
137#define NEW_PARTIAL_MEMO_FOR(type, value, member) \
138 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), \
139 rb_ary_set_len((value), offsetof(type, member) / sizeof(VALUE)), \
140 MEMO_FOR(type, value))
141
142#ifndef RUBY_RUBYPARSER_H
144#endif
145VALUE rb_imemo_new(enum imemo_type type, VALUE v0, size_t size, bool is_shareable);
146struct MEMO *rb_imemo_memo_new(VALUE a, VALUE b, long c);
147struct MEMO *rb_imemo_memo_new_value(VALUE a, VALUE b, VALUE c);
148struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
149static inline enum imemo_type imemo_type(VALUE imemo);
150static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
151static inline bool imemo_throw_data_p(VALUE imemo);
152static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
153static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
154static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
155static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
156
157size_t rb_imemo_memsize(VALUE obj);
158void rb_imemo_mark_and_move(VALUE obj, bool reference_updating);
159void rb_imemo_free(VALUE obj);
160
161RUBY_SYMBOL_EXPORT_BEGIN
162const char *rb_imemo_name(enum imemo_type type);
163ID rb_imemo_callinfo_mid(VALUE obj);
165 VALUE klass;
166 ID called_id;
167};
168bool rb_imemo_callcache_get_data(VALUE obj, struct rb_imemo_callcache_data *data);
169RUBY_SYMBOL_EXPORT_END
170
171static inline enum imemo_type
172imemo_type(VALUE imemo)
173{
174 return (RBASIC(imemo)->flags & IMEMO_MASK) >> FL_USHIFT;
175}
176
177static inline int
178imemo_type_p(VALUE imemo, enum imemo_type imemo_type)
179{
180 if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) {
181 /* fixed at compile time if imemo_type is given. */
182 const VALUE mask = IMEMO_MASK | RUBY_T_MASK;
183 const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO;
184 /* fixed at runtime. */
185 return expected_type == (RBASIC(imemo)->flags & mask);
186 }
187 else {
188 return 0;
189 }
190}
191
192#define IMEMO_TYPE_P(v, t) imemo_type_p((VALUE)(v), t)
193
194static inline bool
195imemo_throw_data_p(VALUE imemo)
196{
197 return RB_TYPE_P(imemo, T_IMEMO);
198}
199
200static inline struct vm_ifunc *
201rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
202{
203 return rb_vm_ifunc_new(func, data, 0, UNLIMITED_ARGUMENTS);
204}
205
206static inline void *
207RB_IMEMO_TMPBUF_PTR(VALUE v)
208{
209 const struct rb_imemo_tmpbuf_struct *p = (const void *)v;
210 return p->ptr;
211}
212
213static inline VALUE
214rb_imemo_tmpbuf_new_from_an_RString(VALUE str)
215{
216 VALUE imemo;
217 size_t len;
218
219 StringValue(str);
220 len = RSTRING_LEN(str);
221 rb_alloc_tmp_buffer(&imemo, len, false);
222 memcpy(RB_IMEMO_TMPBUF_PTR(imemo), RSTRING_PTR(str), len);
223 return imemo;
224}
225
226static inline void
227MEMO_V1_SET(struct MEMO *m, VALUE v)
228{
229 RB_OBJ_WRITE(m, &m->v1, v);
230}
231
232static inline void
233MEMO_V2_SET(struct MEMO *m, VALUE v)
234{
235 RB_OBJ_WRITE(m, &m->v2, v);
236}
237
238VALUE rb_imemo_cdhash_new(size_t size, const struct st_hash_type *type);
239
240static inline st_table *
241rb_imemo_cdhash_tbl(VALUE obj)
242{
243 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_cdhash));
244 return &((struct rb_imemo_cdhash *)obj)->tbl;
245}
246
247struct rb_fields {
248 struct RBasic basic;
249 union {
250 struct {
251 VALUE fields[1];
252 } embed;
253 struct {
254 st_table table;
255 } complex;
256 } as;
257};
258
259// IMEMO/fields and T_OBJECT have exactly the same layout.
260// This is useful for JIT and common codepaths.
261STATIC_ASSERT(imemo_fields_embed_offset, offsetof(struct RObject, as.ary) == offsetof(struct rb_fields, as.embed.fields));
262
263#define IMEMO_OBJ_FIELDS(fields) ((struct rb_fields *)fields)
264
265#define IMEMO_SUBCLASSES_HEAP IMEMO_FL_USER0
266
268 VALUE flags;
269 uint32_t count;
270 uint32_t capacity;
271 union {
272 VALUE *external;
273 VALUE embed[1];
274 } as;
275};
276
277static inline VALUE *
278rb_imemo_subclasses_entries(VALUE v)
279{
280 struct rb_subclasses *s = (struct rb_subclasses *)v;
281 return FL_TEST_RAW(v, IMEMO_SUBCLASSES_HEAP) ? s->as.external : s->as.embed;
282}
283
284VALUE rb_imemo_fields_new(VALUE owner, /* shape_id_t */ uint32_t shape_id, bool shareable);
285VALUE rb_imemo_subclasses_new(uint32_t capacity);
286VALUE rb_imemo_fields_new_complex(VALUE owner, /* shape_id_t */ uint32_t shape_id, size_t capa, bool shareable);
287VALUE rb_imemo_fields_new_complex_empty(VALUE owner);
288VALUE rb_imemo_fields_clone(VALUE fields_obj);
289void rb_imemo_fields_clear(VALUE fields_obj);
290
291static inline VALUE
292rb_imemo_fields_owner(VALUE fields_obj)
293{
294 RUBY_ASSERT(IMEMO_TYPE_P(fields_obj, imemo_fields));
295
296 return CLASS_OF(fields_obj);
297}
298
299#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 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:116
Ruby object's base components.
Definition rbasic.h:69
Ruby's ordinal objects.
Definition robject.h:56
Definition st.h:79
IFUNC (Internal FUNCtion)
Definition imemo.h:87
SVAR (Special VARiable)
Definition imemo.h:52
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition imemo.h:54
THROW_DATA.
Definition imemo.h:61
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