Ruby 3.5.0dev (2025-02-20 revision 34098b669c0cbc024cd08e686891f1dfe0a10aaf)
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#define IMEMO_MASK 0x0f
18
19/* FL_USER0 to FL_USER3 is for type */
20#define IMEMO_FL_USHIFT (FL_USHIFT + 4)
21#define IMEMO_FL_USER0 FL_USER4
22#define IMEMO_FL_USER1 FL_USER5
23#define IMEMO_FL_USER2 FL_USER6
24#define IMEMO_FL_USER3 FL_USER7
25#define IMEMO_FL_USER4 FL_USER8
26#define IMEMO_FL_USER5 FL_USER9
27
28enum imemo_type {
29 imemo_env = 0,
30 imemo_cref = 1,
31 imemo_svar = 2,
32 imemo_throw_data = 3,
33 imemo_ifunc = 4,
34 imemo_memo = 5,
35 imemo_ment = 6,
36 imemo_iseq = 7,
37 imemo_tmpbuf = 8,
38 imemo_ast = 9, // Obsolete due to the universal parser
39 imemo_parser_strterm = 10,
40 imemo_callinfo = 11,
41 imemo_callcache = 12,
42 imemo_constcache = 13,
43};
44
45/* CREF (Class REFerence) is defined in method.h */
46
48struct vm_svar {
49 VALUE flags;
51 const VALUE lastline;
52 const VALUE backref;
53 const VALUE others;
54};
55
58 VALUE flags;
59 VALUE reserved;
60 const VALUE throw_obj;
61 const struct rb_control_frame_struct *catch_frame;
62 int throw_state;
63};
64
65#define THROW_DATA_CONSUMED IMEMO_FL_USER0
66
67/* IFUNC (Internal FUNCtion) */
68
70#if SIZEOF_INT * 2 > SIZEOF_VALUE
71 signed int min: (SIZEOF_VALUE * CHAR_BIT) / 2;
72 signed int max: (SIZEOF_VALUE * CHAR_BIT) / 2;
73#else
74 int min, max;
75#endif
76};
77
84struct vm_ifunc {
85 VALUE flags;
86 VALUE *svar_lep;
88 const void *data;
89 struct vm_ifunc_argc argc;
90};
91#define IFUNC_YIELD_OPTIMIZABLE IMEMO_FL_USER0
92
94 VALUE flags;
95 VALUE reserved;
96 VALUE *ptr; /* malloc'ed buffer */
97 struct rb_imemo_tmpbuf_struct *next; /* next imemo */
98 size_t cnt; /* buffer size in VALUE */
99};
100
105struct MEMO {
106 VALUE flags;
107 VALUE reserved;
108 const VALUE v1;
109 const VALUE v2;
110 union {
111 long cnt;
112 long state;
113 const VALUE value;
114 void (*func)(void);
115 } u3;
116};
117
118#define IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T)))
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
135rb_imemo_tmpbuf_t *rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
136struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
137static inline enum imemo_type imemo_type(VALUE imemo);
138static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
139static inline bool imemo_throw_data_p(VALUE imemo);
140static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
141static inline VALUE rb_imemo_tmpbuf_auto_free_pointer(void);
142static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
143static inline void *rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr);
144static inline VALUE rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str);
145static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
146static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
147
148size_t rb_imemo_memsize(VALUE obj);
149void rb_cc_table_mark(VALUE klass);
150void rb_imemo_mark_and_move(VALUE obj, bool reference_updating);
151void rb_cc_table_free(VALUE klass);
152void rb_imemo_free(VALUE obj);
153
154RUBY_SYMBOL_EXPORT_BEGIN
155VALUE rb_imemo_new(enum imemo_type type, VALUE v0, size_t size);
156const char *rb_imemo_name(enum imemo_type type);
157RUBY_SYMBOL_EXPORT_END
158
159static inline struct MEMO *
160MEMO_NEW(VALUE a, VALUE b, VALUE c)
161{
162 struct MEMO *memo = IMEMO_NEW(struct MEMO, imemo_memo, 0);
163 *((VALUE *)&memo->v1) = a;
164 *((VALUE *)&memo->v2) = b;
165 *((VALUE *)&memo->u3.value) = c;
166
167 return memo;
168}
169
170static inline enum imemo_type
171imemo_type(VALUE imemo)
172{
173 return (RBASIC(imemo)->flags >> FL_USHIFT) & IMEMO_MASK;
174}
175
176static inline int
177imemo_type_p(VALUE imemo, enum imemo_type imemo_type)
178{
179 if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) {
180 /* fixed at compile time if imemo_type is given. */
181 const VALUE mask = (IMEMO_MASK << FL_USHIFT) | RUBY_T_MASK;
182 const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO;
183 /* fixed at runtime. */
184 return expected_type == (RBASIC(imemo)->flags & mask);
185 }
186 else {
187 return 0;
188 }
189}
190
191#define IMEMO_TYPE_P(v, t) imemo_type_p((VALUE)(v), t)
192
193static inline bool
194imemo_throw_data_p(VALUE imemo)
195{
196 return RB_TYPE_P(imemo, T_IMEMO);
197}
198
199static inline struct vm_ifunc *
200rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
201{
202 return rb_vm_ifunc_new(func, data, 0, UNLIMITED_ARGUMENTS);
203}
204
205static inline VALUE
206rb_imemo_tmpbuf_auto_free_pointer(void)
207{
208 return rb_imemo_new(imemo_tmpbuf, 0, sizeof(rb_imemo_tmpbuf_t));
209}
210
211static inline void *
212RB_IMEMO_TMPBUF_PTR(VALUE v)
213{
214 const struct rb_imemo_tmpbuf_struct *p = (const void *)v;
215 return p->ptr;
216}
217
218static inline void *
219rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr)
220{
221 return ((rb_imemo_tmpbuf_t *)v)->ptr = ptr;
222}
223
224static inline VALUE
225rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str)
226{
227 const void *src;
228 VALUE imemo;
229 rb_imemo_tmpbuf_t *tmpbuf;
230 void *dst;
231 size_t len;
232
233 StringValue(str);
234 /* create tmpbuf to keep the pointer before xmalloc */
235 imemo = rb_imemo_tmpbuf_auto_free_pointer();
236 tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
237 len = RSTRING_LEN(str);
238 src = RSTRING_PTR(str);
239 dst = ruby_xmalloc(len);
240 memcpy(dst, src, len);
241 tmpbuf->ptr = dst;
242 return imemo;
243}
244
245static inline void
246MEMO_V1_SET(struct MEMO *m, VALUE v)
247{
248 RB_OBJ_WRITE(m, &m->v1, v);
249}
250
251static inline void
252MEMO_V2_SET(struct MEMO *m, VALUE v)
253{
254 RB_OBJ_WRITE(m, &m->v2, v);
255}
256
257#endif /* INTERNAL_IMEMO_H */
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
Definition fl_type.h:69
#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 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:105
IFUNC (Internal FUNCtion)
Definition imemo.h:84
SVAR (Special VARiable)
Definition imemo.h:48
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition imemo.h:50
THROW_DATA.
Definition imemo.h:57
#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