Ruby: internal/imemo.h Source File

Ruby 3.4.7p58 (2025-10-08 revision 7a5688e2a27668e48f8d6ff4af5b2208b98a2f5e)

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 "internal/array.h" /* for rb_ary_hidden_new_fill */
14#include "ruby/internal/stdbool.h" /* for bool */
15#include "ruby/ruby.h" /* for rb_block_call_func_t */
16
17#ifndef IMEMO_DEBUG
18# define IMEMO_DEBUG 0
19#endif
20
21#define IMEMO_MASK 0x0f
22
23/* FL_USER0 to FL_USER3 is for type */
24#define IMEMO_FL_USHIFT (FL_USHIFT + 4)
25#define IMEMO_FL_USER0 FL_USER4
26#define IMEMO_FL_USER1 FL_USER5
27#define IMEMO_FL_USER2 FL_USER6
28#define IMEMO_FL_USER3 FL_USER7
29#define IMEMO_FL_USER4 FL_USER8
30#define IMEMO_FL_USER5 FL_USER9
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_ast = 9, // Obsolete due to the universal parser
43 imemo_parser_strterm = 10,
44 imemo_callinfo = 11,
45 imemo_callcache = 12,
46 imemo_constcache = 13,
47};
48
49/* CREF (Class REFerence) is defined in method.h */
50
52 struct vm_svar {
53 VALUE flags;
54 const VALUE cref_or_me;
55 const VALUE lastline;
56 const VALUE backref;
57 const VALUE others;
58};
59
61 struct vm_throw_data {
62 VALUE flags;
63 VALUE reserved;
64 const VALUE throw_obj;
65 const struct rb_control_frame_struct *catch_frame;
66 int throw_state;
67};
68
69#define THROW_DATA_CONSUMED IMEMO_FL_USER0
70
71/* IFUNC (Internal FUNCtion) */
72
73 struct vm_ifunc_argc {
74#if SIZEOF_INT * 2 > SIZEOF_VALUE
75 signed int min: (SIZEOF_VALUE * CHAR_BIT) / 2;
76 signed int max: (SIZEOF_VALUE * CHAR_BIT) / 2;
77#else
78 int min, max;
79#endif
80};
81
88 struct vm_ifunc {
89 VALUE flags;
90 VALUE *svar_lep;
91 rb_block_call_func_t func;
92 const void *data;
93 struct vm_ifunc_argc argc;
94};
95#define IFUNC_YIELD_OPTIMIZABLE IMEMO_FL_USER0
96
97 struct rb_imemo_tmpbuf_struct {
98 VALUE flags;
99 VALUE reserved;
100 VALUE *ptr; /* malloc'ed buffer */
101 struct rb_imemo_tmpbuf_struct *next; /* next imemo */
102 size_t cnt; /* buffer size in VALUE */
103};
104
109 struct MEMO {
110 VALUE flags;
111 VALUE reserved;
112 const VALUE v1;
113 const VALUE v2;
114 union {
115 long cnt;
116 long state;
117 const VALUE value;
118 void (*func)(void);
119 } u3;
120};
121
122#define IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0)))
123
124/* ment is in method.h */
125
126#define THROW_DATA_P(err) imemo_throw_data_p((VALUE)err)
127#define MEMO_CAST(m) ((struct MEMO *)(m))
128#define MEMO_FOR(type, value) ((type *)RARRAY_PTR(value))
129#define NEW_MEMO_FOR(type, value) \
130 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), MEMO_FOR(type, value))
131#define NEW_PARTIAL_MEMO_FOR(type, value, member) \
132 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), \
133 rb_ary_set_len((value), offsetof(type, member) / sizeof(VALUE)), \
134 MEMO_FOR(type, value))
135
136#ifndef RUBY_RUBYPARSER_H
137typedef struct rb_imemo_tmpbuf_struct rb_imemo_tmpbuf_t;
138#endif
139rb_imemo_tmpbuf_t *rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
140struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
141static inline enum imemo_type imemo_type(VALUE imemo);
142static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
143static inline bool imemo_throw_data_p(VALUE imemo);
144static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
145static inline VALUE rb_imemo_tmpbuf_auto_free_pointer(void);
146static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
147static inline void *rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr);
148static inline VALUE rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str);
149static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
150static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
151
152size_t rb_imemo_memsize(VALUE obj);
153void rb_cc_table_mark(VALUE klass);
154void rb_imemo_mark_and_move(VALUE obj, bool reference_updating);
155void rb_cc_table_free(VALUE klass);
156void rb_imemo_free(VALUE obj);
157
158RUBY_SYMBOL_EXPORT_BEGIN
159#if IMEMO_DEBUG
160 VALUE rb_imemo_new_debug(enum imemo_type type, VALUE v0, const char *file, int line);
161#define rb_imemo_new(type, v1, v2, v3, v0) rb_imemo_new_debug(type, v1, v2, v3, v0, __FILE__, __LINE__)
162#else
163 VALUE rb_imemo_new(enum imemo_type type, VALUE v0);
164#endif
165const char *rb_imemo_name(enum imemo_type type);
166RUBY_SYMBOL_EXPORT_END
167
168static inline struct MEMO *
169MEMO_NEW(VALUE a, VALUE b, VALUE c)
170{
171 struct MEMO *memo = IMEMO_NEW(struct MEMO, imemo_memo, 0);
172 *((VALUE *)&memo->v1) = a;
173 *((VALUE *)&memo->v2) = b;
174 *((VALUE *)&memo->u3.value) = c;
175
176 return memo;
177}
178
179static inline enum imemo_type
180imemo_type(VALUE imemo)
181{
182 return (RBASIC(imemo)->flags >> FL_USHIFT) & IMEMO_MASK;
183}
184
185static inline int
186imemo_type_p(VALUE imemo, enum imemo_type imemo_type)
187{
188 if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) {
189 /* fixed at compile time if imemo_type is given. */
190 const VALUE mask = (IMEMO_MASK << FL_USHIFT) | RUBY_T_MASK;
191 const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO;
192 /* fixed at runtime. */
193 return expected_type == (RBASIC(imemo)->flags & mask);
194 }
195 else {
196 return 0;
197 }
198}
199
200#define IMEMO_TYPE_P(v, t) imemo_type_p((VALUE)(v), t)
201
202static inline bool
203imemo_throw_data_p(VALUE imemo)
204{
205 return RB_TYPE_P(imemo, T_IMEMO);
206}
207
208static inline struct vm_ifunc *
209rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
210{
211 return rb_vm_ifunc_new(func, data, 0, UNLIMITED_ARGUMENTS);
212}
213
214static inline VALUE
215rb_imemo_tmpbuf_auto_free_pointer(void)
216{
217 return rb_imemo_new(imemo_tmpbuf, 0);
218}
219
220static inline void *
221RB_IMEMO_TMPBUF_PTR(VALUE v)
222{
223 const struct rb_imemo_tmpbuf_struct *p = (const void *)v;
224 return p->ptr;
225}
226
227static inline void *
228rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr)
229{
230 return ((rb_imemo_tmpbuf_t *)v)->ptr = ptr;
231}
232
233static inline VALUE
234rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str)
235{
236 const void *src;
237 VALUE imemo;
238 rb_imemo_tmpbuf_t *tmpbuf;
239 void *dst;
240 size_t len;
241
242 StringValue(str);
243 /* create tmpbuf to keep the pointer before xmalloc */
244 imemo = rb_imemo_tmpbuf_auto_free_pointer();
245 tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
246 len = RSTRING_LEN(str);
247 src = RSTRING_PTR(str);
248 dst = ruby_xmalloc(len);
249 memcpy(dst, src, len);
250 tmpbuf->ptr = dst;
251 return imemo;
252}
253
254static inline void
255MEMO_V1_SET(struct MEMO *m, VALUE v)
256{
257 RB_OBJ_WRITE(m, &m->v1, v);
258}
259
260static inline void
261MEMO_V2_SET(struct MEMO *m, VALUE v)
262{
263 RB_OBJ_WRITE(m, &m->v2, v);
264}
265
266#endif /* INTERNAL_IMEMO_H */
T_IMEMO
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
FL_USHIFT
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
Definition fl_type.h:69
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
len
int len
Length of the buffer.
Definition io.h:8
rb_block_call_func_t
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
Definition iterator.h:88
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
StringValue
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
ruby.h
RB_SPECIAL_CONST_P
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
Definition special_consts.h:327
stdbool.h
C99 shim for <stdbool.h>
MEMO
MEMO.
Definition imemo.h:109
rb_control_frame_struct
Definition vm_core.h:900
rb_imemo_tmpbuf_struct
Definition imemo.h:97
vm_ifunc_argc
Definition imemo.h:73
vm_ifunc
IFUNC (Internal FUNCtion)
Definition imemo.h:88
vm_svar
SVAR (Special VARiable)
Definition imemo.h:52
vm_svar::cref_or_me
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition imemo.h:54
vm_throw_data
THROW_DATA.
Definition imemo.h:61
SIZEOF_VALUE
#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
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_TYPE_P
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
@ RUBY_T_MASK
Bitmask of ruby_value_type.
Definition value_type.h:145