Ruby 3.2.4p170 (2024-04-23 revision af471c0e0127eea0cafa6f308c0425bbfab0acf5)
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 "internal/gc.h" /* for RB_OBJ_WRITE */
15#include "ruby/internal/stdbool.h" /* for bool */
16#include "ruby/ruby.h" /* for rb_block_call_func_t */
17
18#ifndef IMEMO_DEBUG
19# define IMEMO_DEBUG 0
20#endif
21
22#define IMEMO_MASK 0x0f
23
24/* FL_USER0 to FL_USER3 is for type */
25#define IMEMO_FL_USHIFT (FL_USHIFT + 4)
26#define IMEMO_FL_USER0 FL_USER4
27#define IMEMO_FL_USER1 FL_USER5
28#define IMEMO_FL_USER2 FL_USER6
29#define IMEMO_FL_USER3 FL_USER7
30#define IMEMO_FL_USER4 FL_USER8
31#define IMEMO_FL_USER5 FL_USER9
32
33enum imemo_type {
34 imemo_env = 0,
35 imemo_cref = 1,
36 imemo_svar = 2,
37 imemo_throw_data = 3,
38 imemo_ifunc = 4,
39 imemo_memo = 5,
40 imemo_ment = 6,
41 imemo_iseq = 7,
42 imemo_tmpbuf = 8,
43 imemo_ast = 9,
44 imemo_parser_strterm = 10,
45 imemo_callinfo = 11,
46 imemo_callcache = 12,
47 imemo_constcache = 13,
48};
49
50/* CREF (Class REFerence) is defined in method.h */
51
53struct vm_svar {
54 VALUE flags;
56 const VALUE lastline;
57 const VALUE backref;
58 const VALUE others;
59};
60
63 VALUE flags;
64 VALUE reserved;
65 const VALUE throw_obj;
66 const struct rb_control_frame_struct *catch_frame;
67 int throw_state;
68};
69
70#define THROW_DATA_CONSUMED IMEMO_FL_USER0
71
72/* IFUNC (Internal FUNCtion) */
73
75#if SIZEOF_INT * 2 > SIZEOF_VALUE
76 signed int min: (SIZEOF_VALUE * CHAR_BIT) / 2;
77 signed int max: (SIZEOF_VALUE * CHAR_BIT) / 2;
78#else
79 int min, max;
80#endif
81};
82
84struct vm_ifunc {
85 VALUE flags;
86 VALUE reserved;
88 const void *data;
89 struct vm_ifunc_argc argc;
90};
91
93 VALUE flags;
94 VALUE reserved;
95 VALUE *ptr; /* malloc'ed buffer */
96 struct rb_imemo_tmpbuf_struct *next; /* next imemo */
97 size_t cnt; /* buffer size in VALUE */
98};
99
104struct MEMO {
105 VALUE flags;
106 VALUE reserved;
107 const VALUE v1;
108 const VALUE v2;
109 union {
110 long cnt;
111 long state;
112 const VALUE value;
113 void (*func)(void);
114 } u3;
115};
116
117/* ment is in method.h */
118
119#define THROW_DATA_P(err) imemo_throw_data_p((VALUE)err)
120#define MEMO_CAST(m) ((struct MEMO *)(m))
121#define MEMO_NEW(a, b, c) ((struct MEMO *)rb_imemo_new(imemo_memo, (VALUE)(a), (VALUE)(b), (VALUE)(c), 0))
122#define MEMO_FOR(type, value) ((type *)RARRAY_PTR(value))
123#define NEW_MEMO_FOR(type, value) \
124 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), MEMO_FOR(type, value))
125#define NEW_PARTIAL_MEMO_FOR(type, value, member) \
126 ((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), \
127 rb_ary_set_len((value), offsetof(type, member) / sizeof(VALUE)), \
128 MEMO_FOR(type, value))
129
131rb_imemo_tmpbuf_t *rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
132struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
133void rb_strterm_mark(VALUE obj);
134static inline enum imemo_type imemo_type(VALUE imemo);
135static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
136static inline bool imemo_throw_data_p(VALUE imemo);
137static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
138static inline VALUE rb_imemo_tmpbuf_auto_free_pointer(void);
139static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
140static inline void *rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr);
141static inline VALUE rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str);
142static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
143static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
144
145RUBY_SYMBOL_EXPORT_BEGIN
146#if IMEMO_DEBUG
147VALUE rb_imemo_new_debug(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0, const char *file, int line);
148#define rb_imemo_new(type, v1, v2, v3, v0) rb_imemo_new_debug(type, v1, v2, v3, v0, __FILE__, __LINE__)
149#else
150VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0);
151#endif
152const char *rb_imemo_name(enum imemo_type type);
153RUBY_SYMBOL_EXPORT_END
154
155static inline enum imemo_type
156imemo_type(VALUE imemo)
157{
158 return (RBASIC(imemo)->flags >> FL_USHIFT) & IMEMO_MASK;
159}
160
161static inline int
162imemo_type_p(VALUE imemo, enum imemo_type imemo_type)
163{
164 if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) {
165 /* fixed at compile time if imemo_type is given. */
166 const VALUE mask = (IMEMO_MASK << FL_USHIFT) | RUBY_T_MASK;
167 const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO;
168 /* fixed at runtime. */
169 return expected_type == (RBASIC(imemo)->flags & mask);
170 }
171 else {
172 return 0;
173 }
174}
175
176#define IMEMO_TYPE_P(v, t) imemo_type_p((VALUE)v, t)
177
178static inline bool
179imemo_throw_data_p(VALUE imemo)
180{
181 return RB_TYPE_P(imemo, T_IMEMO);
182}
183
184static inline struct vm_ifunc *
185rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
186{
187 return rb_vm_ifunc_new(func, data, 0, UNLIMITED_ARGUMENTS);
188}
189
190static inline VALUE
191rb_imemo_tmpbuf_auto_free_pointer(void)
192{
193 return rb_imemo_new(imemo_tmpbuf, 0, 0, 0, 0);
194}
195
196static inline void *
197RB_IMEMO_TMPBUF_PTR(VALUE v)
198{
199 const struct rb_imemo_tmpbuf_struct *p = (const void *)v;
200 return p->ptr;
201}
202
203static inline void *
204rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr)
205{
206 return ((rb_imemo_tmpbuf_t *)v)->ptr = ptr;
207}
208
209static inline VALUE
210rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str)
211{
212 const void *src;
213 VALUE imemo;
214 rb_imemo_tmpbuf_t *tmpbuf;
215 void *dst;
216 size_t len;
217
218 SafeStringValue(str);
219 /* create tmpbuf to keep the pointer before xmalloc */
220 imemo = rb_imemo_tmpbuf_auto_free_pointer();
221 tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
222 len = RSTRING_LEN(str);
223 src = RSTRING_PTR(str);
224 dst = ruby_xmalloc(len);
225 memcpy(dst, src, len);
226 tmpbuf->ptr = dst;
227 return imemo;
228}
229
230static inline void
231MEMO_V1_SET(struct MEMO *m, VALUE v)
232{
233 RB_OBJ_WRITE(m, &m->v1, v);
234}
235
236static inline void
237MEMO_V2_SET(struct MEMO *m, VALUE v)
238{
239 RB_OBJ_WRITE(m, &m->v2, v);
240}
241
242#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:70
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition rgengc.h:220
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
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 SafeStringValue(v)
Definition rstring.h:104
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition rstring.h:484
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition rstring.h:498
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:104
IFUNC (Internal FUNCtion)
Definition imemo.h:84
SVAR (Special VARiable)
Definition imemo.h:53
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition imemo.h:55
THROW_DATA.
Definition imemo.h:62
#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:375
@ RUBY_T_MASK
Bitmask of ruby_value_type.
Definition value_type.h:144