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