Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
vm_insnhelper.h
Go to the documentation of this file.
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_SYMBOL_EXPORT_BEGIN
15
19
20RUBY_SYMBOL_EXPORT_END
21
22#if VM_COLLECT_USAGE_DETAILS
23#define COLLECT_USAGE_INSN(insn) vm_collect_usage_insn(insn)
24#define COLLECT_USAGE_OPERAND(insn, n, op) vm_collect_usage_operand((insn), (n), ((VALUE)(op)))
25
26#define COLLECT_USAGE_REGISTER(reg, s) vm_collect_usage_register((reg), (s))
27#else
28#define COLLECT_USAGE_INSN(insn) /* none */
29#define COLLECT_USAGE_OPERAND(insn, n, op) /* none */
30#define COLLECT_USAGE_REGISTER(reg, s) /* none */
31#endif
32
33/**********************************************************/
34/* deal with stack */
35/**********************************************************/
36
37#define PUSH(x) (SET_SV(x), INC_SP(1))
38#define TOPN(n) (*(GET_SP()-(n)-1))
39#define POPN(n) (DEC_SP(n))
40#define POP() (DEC_SP(1))
41#define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n))
42
43/**********************************************************/
44/* deal with registers */
45/**********************************************************/
46
47#define VM_REG_CFP (reg_cfp)
48#define VM_REG_PC (VM_REG_CFP->pc)
49#define VM_REG_SP (VM_REG_CFP->sp)
50#define VM_REG_EP (VM_REG_CFP->ep)
51
52#define RESTORE_REGS() do { \
53 VM_REG_CFP = ec->cfp; \
54} while (0)
55
56#if VM_COLLECT_USAGE_DETAILS
57enum vm_regan_regtype {
58 VM_REGAN_PC = 0,
59 VM_REGAN_SP = 1,
60 VM_REGAN_EP = 2,
61 VM_REGAN_CFP = 3,
62 VM_REGAN_SELF = 4,
63 VM_REGAN_ISEQ = 5
64};
65enum vm_regan_acttype {
66 VM_REGAN_ACT_GET = 0,
67 VM_REGAN_ACT_SET = 1
68};
69
70#define COLLECT_USAGE_REGISTER_HELPER(a, b, v) \
71 (COLLECT_USAGE_REGISTER((VM_REGAN_##a), (VM_REGAN_ACT_##b)), (v))
72#else
73#define COLLECT_USAGE_REGISTER_HELPER(a, b, v) (v)
74#endif
75
76/* PC */
77#define GET_PC() (COLLECT_USAGE_REGISTER_HELPER(PC, GET, VM_REG_PC))
78#define SET_PC(x) (VM_REG_PC = (COLLECT_USAGE_REGISTER_HELPER(PC, SET, (x))))
79#define GET_CURRENT_INSN() (*GET_PC())
80#define GET_OPERAND(n) (GET_PC()[(n)])
81#define ADD_PC(n) (SET_PC(VM_REG_PC + (n)))
82#define JUMP(dst) (SET_PC(VM_REG_PC + (dst)))
83
84/* frame pointer, environment pointer */
85#define GET_CFP() (COLLECT_USAGE_REGISTER_HELPER(CFP, GET, VM_REG_CFP))
86#define GET_EP() (COLLECT_USAGE_REGISTER_HELPER(EP, GET, VM_REG_EP))
87#define SET_EP(x) (VM_REG_EP = (COLLECT_USAGE_REGISTER_HELPER(EP, SET, (x))))
88#define GET_LEP() (VM_EP_LEP(GET_EP()))
89
90/* SP */
91#define GET_SP() (COLLECT_USAGE_REGISTER_HELPER(SP, GET, VM_REG_SP))
92#define SET_SP(x) (VM_REG_SP = (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
93#define INC_SP(x) (VM_REG_SP += (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
94#define DEC_SP(x) (VM_REG_SP -= (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
95#define SET_SV(x) (*GET_SP() = rb_ractor_confirm_belonging(x))
96 /* set current stack value as x */
97
98/* instruction sequence C struct */
99#define GET_ISEQ() (GET_CFP()->iseq)
100
101/**********************************************************/
102/* deal with variables */
103/**********************************************************/
104
105#define GET_PREV_EP(ep) ((VALUE *)((ep)[VM_ENV_DATA_INDEX_SPECVAL] & ~0x03))
106
107/**********************************************************/
108/* deal with values */
109/**********************************************************/
110
111#define GET_SELF() (COLLECT_USAGE_REGISTER_HELPER(SELF, GET, GET_CFP()->self))
112
113/**********************************************************/
114/* deal with control flow 2: method/iterator */
115/**********************************************************/
116
117/* set fastpath when cached method is *NOT* protected
118 * because inline method cache does not care about receiver.
119 */
120
121static inline void
122CC_SET_FASTPATH(const struct rb_callcache *cc, vm_call_handler func, bool enabled)
123{
124 if (LIKELY(enabled)) {
125 vm_cc_call_set(cc, func);
126 }
127}
128
129#define GET_BLOCK_HANDLER() (GET_LEP()[VM_ENV_DATA_INDEX_SPECVAL])
130
131/**********************************************************/
132/* deal with control flow 3: exception */
133/**********************************************************/
134
135
136/**********************************************************/
137/* deal with stack canary */
138/**********************************************************/
139
140#if VM_CHECK_MODE > 0
141#define SETUP_CANARY(cond) \
142 VALUE *canary = 0; \
143 if (cond) { \
144 canary = GET_SP(); \
145 SET_SV(vm_stack_canary); \
146 } \
147 else {\
148 SET_SV(Qfalse); /* cleanup */ \
149 }
150#define CHECK_CANARY(cond, insn) \
151 if (cond) { \
152 if (*canary == vm_stack_canary) { \
153 *canary = Qfalse; /* cleanup */ \
154 } \
155 else { \
156 rb_vm_canary_is_found_dead(insn, *canary); \
157 } \
158 }
159#else
160#define SETUP_CANARY(cond) if (cond) {} else {}
161#define CHECK_CANARY(cond, insn) if (cond) {(void)(insn);}
162#endif
163
164/**********************************************************/
165/* others */
166/**********************************************************/
167
168#ifndef MJIT_HEADER
169#define CALL_SIMPLE_METHOD() do { \
170 rb_snum_t x = leaf ? INSN_ATTR(width) : 0; \
171 rb_snum_t y = attr_width_opt_send_without_block(0); \
172 rb_snum_t z = x - y; \
173 ADD_PC(z); \
174 DISPATCH_ORIGINAL_INSN(opt_send_without_block); \
175} while (0)
176#endif
177
178#define PREV_CLASS_SERIAL() (ruby_vm_class_serial)
179#define NEXT_CLASS_SERIAL() (++ruby_vm_class_serial)
180#define GET_GLOBAL_CONSTANT_STATE() (ruby_vm_global_constant_state)
181#define INC_GLOBAL_CONSTANT_STATE() (++ruby_vm_global_constant_state)
182
183static inline struct vm_throw_data *
184THROW_DATA_NEW(VALUE val, const rb_control_frame_t *cf, int st)
185{
186 struct vm_throw_data *obj = (struct vm_throw_data *)rb_imemo_new(imemo_throw_data, val, (VALUE)cf, 0, 0);
187 obj->throw_state = st;
188 return obj;
189}
190
191static inline VALUE
192THROW_DATA_VAL(const struct vm_throw_data *obj)
193{
195 return obj->throw_obj;
196}
197
198static inline const rb_control_frame_t *
199THROW_DATA_CATCH_FRAME(const struct vm_throw_data *obj)
200{
202 return obj->catch_frame;
203}
204
205static inline int
206THROW_DATA_STATE(const struct vm_throw_data *obj)
207{
209 return obj->throw_state;
210}
211
212static inline int
213THROW_DATA_CONSUMED_P(const struct vm_throw_data *obj)
214{
216 return obj->flags & THROW_DATA_CONSUMED;
217}
218
219static inline void
220THROW_DATA_CATCH_FRAME_SET(struct vm_throw_data *obj, const rb_control_frame_t *cfp)
221{
223 obj->catch_frame = cfp;
224}
225
226static inline void
227THROW_DATA_STATE_SET(struct vm_throw_data *obj, int st)
228{
230 obj->throw_state = st;
231}
232
233static inline void
234THROW_DATA_CONSUMED_SET(struct vm_throw_data *obj)
235{
236 if (THROW_DATA_P(obj) &&
237 THROW_DATA_STATE(obj) == TAG_BREAK) {
239 }
240}
241
242#define IS_ARGS_SPLAT(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT)
243#define IS_ARGS_KEYWORD(ci) (vm_ci_flag(ci) & VM_CALL_KWARG)
244#define IS_ARGS_KW_SPLAT(ci) (vm_ci_flag(ci) & VM_CALL_KW_SPLAT)
245#define IS_ARGS_KW_OR_KW_SPLAT(ci) (vm_ci_flag(ci) & (VM_CALL_KWARG | VM_CALL_KW_SPLAT))
246#define IS_ARGS_KW_SPLAT_MUT(ci) (vm_ci_flag(ci) & VM_CALL_KW_SPLAT_MUT)
247
248/* If this returns true, an optimized function returned by `vm_call_iseq_setup_func`
249 can be used as a fastpath. */
250static inline bool
251vm_call_iseq_optimizable_p(const struct rb_callinfo *ci, const struct rb_callcache *cc)
252{
253 return !IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) &&
254 METHOD_ENTRY_CACHEABLE(vm_cc_cme(cc));
255}
256
257#endif /* RUBY_INSNHELPER_H */
#define RUBY_EXTERN
Definition: dllexport.h:36
#define LIKELY(x)
Definition: ffi_common.h:125
VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0)
Definition: gc.c:2412
#define THROW_DATA_CONSUMED
Definition: imemo.h:71
#define THROW_DATA_P(err)
Definition: imemo.h:120
@ imemo_throw_data
Definition: imemo.h:38
#define METHOD_ENTRY_CACHEABLE(me)
Definition: method.h:78
unsigned LONG_LONG rb_serial_t
Definition: serial.h:19
THROW_DATA.
Definition: imemo.h:63
const struct rb_control_frame_struct * catch_frame
Definition: imemo.h:67
const VALUE throw_obj
Definition: imemo.h:66
int throw_state
Definition: imemo.h:68
VALUE flags
Definition: imemo.h:64
unsigned long VALUE
Definition: value.h:38
VALUE(* vm_call_handler)(struct rb_execution_context_struct *ec, struct rb_control_frame_struct *cfp, struct rb_calling_info *calling)
Definition: vm_callinfo.h:267
#define VM_ASSERT(expr)
Definition: vm_core.h:61
#define TAG_BREAK
Definition: vm_core.h:200
rb_serial_t ruby_vm_class_serial
Definition: vm.c:408
VALUE ruby_vm_const_missing_count
Definition: vm.c:379
#define IS_ARGS_SPLAT(ci)
rb_serial_t ruby_vm_global_constant_state
Definition: vm.c:407
#define IS_ARGS_KEYWORD(ci)