Ruby 4.1.0dev (2026-09-12 revision dac86d0d354e33a8ea90376a7ef30e74872d7b46)
vm_insnhelper.h (dac86d0d354e33a8ea90376a7ef30e74872d7b46)
1#ifndef RUBY_INSNHELPER_H
2#define RUBY_INSNHELPER_H
3/**********************************************************************
4
5 insnhelper.h - helper macros to implement each instructions
6
7 $Author$
8 created at: 04/01/01 15:50:34 JST
9
10 Copyright (C) 2004-2007 Koichi Sasada
11
12**********************************************************************/
13
14RUBY_EXTERN VALUE ruby_vm_const_missing_count;
15RUBY_EXTERN rb_serial_t ruby_vm_constant_cache_invalidations;
16RUBY_EXTERN rb_serial_t ruby_vm_constant_cache_misses;
17RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state;
18
19#if YJIT_STATS || ZJIT_STATS // We want vm_insn_count only on stats builds.
20// Increment vm_insn_count for --yjit-stats. We increment this even when
21// --yjit or --yjit-stats is not used because branching to skip it is slower.
22// We also don't use ATOMIC_INC for performance, allowing inaccuracy on Ractors.
23#define JIT_COLLECT_USAGE_INSN(insn) rb_vm_insn_count++
24#else
25#define JIT_COLLECT_USAGE_INSN(insn) // none
26#endif
27
28#if VM_COLLECT_USAGE_DETAILS
29#define COLLECT_USAGE_INSN(insn) vm_collect_usage_insn(insn)
30#define COLLECT_USAGE_OPERAND(insn, n, op) vm_collect_usage_operand((insn), (n), ((VALUE)(op)))
31#define COLLECT_USAGE_REGISTER(reg, s) vm_collect_usage_register((reg), (s))
32#else
33#define COLLECT_USAGE_INSN(insn) JIT_COLLECT_USAGE_INSN(insn)
34#define COLLECT_USAGE_OPERAND(insn, n, op) // none
35#define COLLECT_USAGE_REGISTER(reg, s) // none
36#endif
37
38/**********************************************************/
39/* deal with stack */
40/**********************************************************/
41
42#define PUSH(x) (SET_SV(x), INC_SP(1))
43#define TOPN(n) (*(GET_SP()-(n)-1))
44#define POPN(n) (DEC_SP(n))
45#define POP() (DEC_SP(1))
46#define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n))
47
48/**********************************************************/
49/* deal with registers */
50/**********************************************************/
51
52#define VM_REG_CFP (reg_cfp)
53#define VM_REG_PC (VM_REG_CFP->pc)
54#define VM_REG_SP (VM_REG_CFP->sp)
55#define VM_REG_EP (VM_REG_CFP->ep)
56
57#define RESTORE_REGS() do { \
58 VM_REG_CFP = ec->cfp; \
59} while (0)
60
61typedef enum call_type {
62 CALL_PUBLIC,
63 CALL_FCALL,
64 CALL_VCALL,
65 CALL_PUBLIC_KW,
66 CALL_FCALL_KW
67} call_type;
68
70 struct rb_call_data cd;
71 CALL_INFO caller_ci;
72};
73
74#if VM_COLLECT_USAGE_DETAILS
75enum vm_regan_regtype {
76 VM_REGAN_PC = 0,
77 VM_REGAN_SP = 1,
78 VM_REGAN_EP = 2,
79 VM_REGAN_CFP = 3,
80 VM_REGAN_SELF = 4,
81 VM_REGAN_ISEQ = 5
82};
83enum vm_regan_acttype {
84 VM_REGAN_ACT_GET = 0,
85 VM_REGAN_ACT_SET = 1
86};
87
88#define COLLECT_USAGE_REGISTER_HELPER(a, b, v) \
89 (COLLECT_USAGE_REGISTER((VM_REGAN_##a), (VM_REGAN_ACT_##b)), (v))
90#else
91#define COLLECT_USAGE_REGISTER_HELPER(a, b, v) (v)
92#endif
93
94/* PC */
95#define GET_PC() (COLLECT_USAGE_REGISTER_HELPER(PC, GET, VM_REG_PC))
96#define SET_PC(x) (VM_REG_PC = (COLLECT_USAGE_REGISTER_HELPER(PC, SET, (x))))
97#define GET_CURRENT_INSN() (*GET_PC())
98#define GET_OPERAND(n) (GET_PC()[(n)])
99#define ADD_PC(n) (SET_PC(VM_REG_PC + (n)))
100#define JUMP(dst) (SET_PC(VM_REG_PC + (dst)))
101
102/* frame pointer, environment pointer */
103#define GET_CFP() (COLLECT_USAGE_REGISTER_HELPER(CFP, GET, VM_REG_CFP))
104#define GET_EP() (COLLECT_USAGE_REGISTER_HELPER(EP, GET, VM_REG_EP))
105#define SET_EP(x) (VM_REG_EP = (COLLECT_USAGE_REGISTER_HELPER(EP, SET, (x))))
106#define GET_LEP() (VM_EP_LEP(GET_EP()))
107
108/* SP */
109#define GET_SP() (COLLECT_USAGE_REGISTER_HELPER(SP, GET, VM_REG_SP))
110#define SET_SP(x) (VM_REG_SP = (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
111#define INC_SP(x) (VM_REG_SP += (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
112#define DEC_SP(x) (VM_REG_SP -= (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
113#define SET_SV(x) (*GET_SP() = rb_ractor_confirm_belonging(x))
114 /* set current stack value as x */
115
116// instruction sequence C struct
117// Uses cfp->_iseq directly because the interpreter always has a valid _iseq
118// field (it's written on exit from JIT code). Code in vm_insnhelper.c that
119// may be called as a ZJIT fallback should use CFP_ISEQ() instead.
120#define GET_ISEQ() (GET_CFP()->_iseq)
121
122/**********************************************************/
123/* deal with variables */
124/**********************************************************/
125
126#define GET_PREV_EP(ep) ((VALUE *)((ep)[VM_ENV_DATA_INDEX_SPECVAL] & ~0x03))
127
128/**********************************************************/
129/* deal with values */
130/**********************************************************/
131
132#define GET_SELF() (COLLECT_USAGE_REGISTER_HELPER(SELF, GET, GET_CFP()->self))
133
134/**********************************************************/
135/* deal with control flow 2: method/iterator */
136/**********************************************************/
137
138/* set fastpath when cached method is *NOT* protected
139 * because inline method cache does not care about receiver.
140 */
141
142static inline void
143CC_SET_FASTPATH(const struct rb_callcache *cc, vm_call_handler func, bool enabled)
144{
145 if (LIKELY(enabled)) {
146 vm_cc_call_set(cc, func);
147 }
148}
149
150#define GET_BLOCK_HANDLER() VM_CF_BLOCK_HANDLER(GET_CFP())
151
152/**********************************************************/
153/* deal with control flow 3: exception */
154/**********************************************************/
155
156
157/**********************************************************/
158/* deal with stack canary */
159/**********************************************************/
160
161#if VM_CHECK_MODE > 0
162#define SETUP_CANARY(cond) \
163 VALUE *canary = 0; \
164 if (cond) { \
165 canary = GET_SP(); \
166 SET_SV(vm_stack_canary); \
167 } \
168 else {\
169 SET_SV(Qfalse); /* cleanup */ \
170 }
171#define CHECK_CANARY(cond, insn) \
172 if (cond) { \
173 if (*canary == vm_stack_canary) { \
174 *canary = Qfalse; /* cleanup */ \
175 } \
176 else { \
177 rb_vm_canary_is_found_dead(insn, *canary); \
178 } \
179 }
180#else
181#define SETUP_CANARY(cond) if (cond) {} else {}
182#define CHECK_CANARY(cond, insn) if (cond) {(void)(insn);}
183#endif
184
185/**********************************************************/
186/* others */
187/**********************************************************/
188
189#define CALL_SIMPLE_METHOD() do { \
190 rb_snum_t insn_width = attr_width_opt_send_without_block(0); \
191 ADD_PC(-insn_width); \
192 DISPATCH_ORIGINAL_INSN(opt_send_without_block); \
193} while (0)
194
195#define GET_GLOBAL_CVAR_STATE() (ruby_vm_global_cvar_state)
196#define INC_GLOBAL_CVAR_STATE() (++ruby_vm_global_cvar_state)
197
198static inline struct vm_throw_data *
199THROW_DATA_NEW(VALUE val, const rb_control_frame_t *cf, int st)
200{
201 struct vm_throw_data *obj = IMEMO_NEW(struct vm_throw_data, imemo_throw_data, 0);
202 *((VALUE *)&obj->throw_obj) = val;
203 *((struct rb_control_frame_struct **)&obj->catch_frame) = (struct rb_control_frame_struct *)cf;
204 obj->throw_state = st;
205
206 return obj;
207}
208
209static inline VALUE
210THROW_DATA_VAL(const struct vm_throw_data *obj)
211{
212 VM_ASSERT(THROW_DATA_P(obj));
213 return obj->throw_obj;
214}
215
216static inline const rb_control_frame_t *
217THROW_DATA_CATCH_FRAME(const struct vm_throw_data *obj)
218{
219 VM_ASSERT(THROW_DATA_P(obj));
220 return obj->catch_frame;
221}
222
223static inline int
224THROW_DATA_STATE(const struct vm_throw_data *obj)
225{
226 VM_ASSERT(THROW_DATA_P(obj));
227 return obj->throw_state;
228}
229
230static inline int
231THROW_DATA_CONSUMED_P(const struct vm_throw_data *obj)
232{
233 VM_ASSERT(THROW_DATA_P(obj));
234 return obj->flags & THROW_DATA_CONSUMED;
235}
236
237static inline void
238THROW_DATA_CATCH_FRAME_SET(struct vm_throw_data *obj, const rb_control_frame_t *cfp)
239{
240 VM_ASSERT(THROW_DATA_P(obj));
241 obj->catch_frame = cfp;
242}
243
244static inline void
245THROW_DATA_STATE_SET(struct vm_throw_data *obj, int st)
246{
247 VM_ASSERT(THROW_DATA_P(obj));
248 obj->throw_state = st;
249}
250
251static inline void
252THROW_DATA_CONSUMED_SET(struct vm_throw_data *obj)
253{
254 if (THROW_DATA_P(obj) &&
255 THROW_DATA_STATE(obj) == TAG_BREAK) {
256 obj->flags |= THROW_DATA_CONSUMED;
257 }
258}
259
260#define IS_ARGS_SPLAT(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT)
261#define IS_ARGS_KEYWORD(ci) (vm_ci_flag(ci) & VM_CALL_KWARG)
262#define IS_ARGS_KW_SPLAT(ci) (vm_ci_flag(ci) & VM_CALL_KW_SPLAT)
263#define IS_ARGS_KW_OR_KW_SPLAT(ci) (vm_ci_flag(ci) & (VM_CALL_KWARG | VM_CALL_KW_SPLAT))
264#define IS_ARGS_KW_SPLAT_MUT(ci) (vm_ci_flag(ci) & VM_CALL_KW_SPLAT_MUT)
265
266static inline bool
267vm_call_cacheable(const struct rb_callinfo *ci, const struct rb_callcache *cc)
268{
269 return !(vm_ci_flag(ci) & VM_CALL_FORWARDING) && ((vm_ci_flag(ci) & VM_CALL_FCALL) ||
270 METHOD_ENTRY_VISI(vm_cc_cme(cc)) != METHOD_VISI_PROTECTED);
271}
272/* If this returns true, an optimized function returned by `vm_call_iseq_setup_func`
273 can be used as a fastpath. */
274static inline bool
275vm_call_iseq_optimizable_p(const struct rb_callinfo *ci, const struct rb_callcache *cc)
276{
277 return !IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) && vm_call_cacheable(ci, cc);
278}
279
280#endif /* RUBY_INSNHELPER_H */
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
THROW_DATA.
Definition imemo.h:61
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40