1 #ifndef RUBY_INSNHELPER_H
2 #define RUBY_INSNHELPER_H
3 /**********************************************************************
5 insnhelper.h - helper macros to implement each instructions
8 created at: 04/01/01 15:50:34 JST
10 Copyright (C) 2004-2007 Koichi Sasada
12 **********************************************************************/
14 MJIT_SYMBOL_EXPORT_BEGIN
16 RUBY_EXTERN VALUE ruby_vm_const_missing_count
;
17 RUBY_EXTERN rb_serial_t ruby_vm_global_constant_state
;
18 RUBY_EXTERN rb_serial_t ruby_vm_class_serial
;
19 RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state
;
21 MJIT_SYMBOL_EXPORT_END
23 #if VM_COLLECT_USAGE_DETAILS
24 #define COLLECT_USAGE_INSN(insn) vm_collect_usage_insn(insn)
25 #define COLLECT_USAGE_OPERAND(insn, n, op) vm_collect_usage_operand((insn), (n), ((VALUE)(op)))
27 #define COLLECT_USAGE_REGISTER(reg, s) vm_collect_usage_register((reg), (s))
29 /* for --yjit-stats */
30 #define COLLECT_USAGE_INSN(insn) rb_yjit_collect_vm_usage_insn(insn)
31 #define COLLECT_USAGE_OPERAND(insn, n, op) /* none */
32 #define COLLECT_USAGE_REGISTER(reg, s) /* none */
34 #define COLLECT_USAGE_INSN(insn) /* none */
35 #define COLLECT_USAGE_OPERAND(insn, n, op) /* none */
36 #define COLLECT_USAGE_REGISTER(reg, s) /* none */
39 /**********************************************************/
41 /**********************************************************/
43 #define PUSH(x) (SET_SV(x), INC_SP(1))
44 #define TOPN(n) (*(GET_SP()-(n)-1))
45 #define POPN(n) (DEC_SP(n))
46 #define POP() (DEC_SP(1))
47 #define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n))
49 /**********************************************************/
50 /* deal with registers */
51 /**********************************************************/
53 #define VM_REG_CFP (reg_cfp)
54 #define VM_REG_PC (VM_REG_CFP->pc)
55 #define VM_REG_SP (VM_REG_CFP->sp)
56 #define VM_REG_EP (VM_REG_CFP->ep)
58 #define RESTORE_REGS() do { \
59 VM_REG_CFP = ec->cfp; \
62 #if VM_COLLECT_USAGE_DETAILS
63 enum vm_regan_regtype
{
71 enum vm_regan_acttype
{
76 #define COLLECT_USAGE_REGISTER_HELPER(a, b, v) \
77 (COLLECT_USAGE_REGISTER((VM_REGAN_##a), (VM_REGAN_ACT_##b)), (v))
79 #define COLLECT_USAGE_REGISTER_HELPER(a, b, v) (v)
83 #define GET_PC() (COLLECT_USAGE_REGISTER_HELPER(PC, GET, VM_REG_PC))
84 #define SET_PC(x) (VM_REG_PC = (COLLECT_USAGE_REGISTER_HELPER(PC, SET, (x))))
85 #define GET_CURRENT_INSN() (*GET_PC())
86 #define GET_OPERAND(n) (GET_PC()[(n)])
87 #define ADD_PC(n) (SET_PC(VM_REG_PC + (n)))
88 #define JUMP(dst) (SET_PC(VM_REG_PC + (dst)))
90 /* frame pointer, environment pointer */
91 #define GET_CFP() (COLLECT_USAGE_REGISTER_HELPER(CFP, GET, VM_REG_CFP))
92 #define GET_EP() (COLLECT_USAGE_REGISTER_HELPER(EP, GET, VM_REG_EP))
93 #define SET_EP(x) (VM_REG_EP = (COLLECT_USAGE_REGISTER_HELPER(EP, SET, (x))))
94 #define GET_LEP() (VM_EP_LEP(GET_EP()))
97 #define GET_SP() (COLLECT_USAGE_REGISTER_HELPER(SP, GET, VM_REG_SP))
98 #define SET_SP(x) (VM_REG_SP = (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
99 #define INC_SP(x) (VM_REG_SP += (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
100 #define DEC_SP(x) (VM_REG_SP -= (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
101 #define SET_SV(x) (*GET_SP() = rb_ractor_confirm_belonging(x))
102 /* set current stack value as x */
104 /* instruction sequence C struct */
105 #define GET_ISEQ() (GET_CFP()->iseq)
107 /**********************************************************/
108 /* deal with variables */
109 /**********************************************************/
111 #define GET_PREV_EP(ep) ((VALUE *)((ep)[VM_ENV_DATA_INDEX_SPECVAL] & ~0x03))
113 /**********************************************************/
114 /* deal with values */
115 /**********************************************************/
117 #define GET_SELF() (COLLECT_USAGE_REGISTER_HELPER(SELF, GET, GET_CFP()->self))
119 /**********************************************************/
120 /* deal with control flow 2: method/iterator */
121 /**********************************************************/
123 /* set fastpath when cached method is *NOT* protected
124 * because inline method cache does not care about receiver.
128 CC_SET_FASTPATH(const struct rb_callcache
*cc
, vm_call_handler func
, bool enabled
)
130 if (LIKELY(enabled
)) {
131 vm_cc_call_set(cc
, func
);
135 #define GET_BLOCK_HANDLER() (GET_LEP()[VM_ENV_DATA_INDEX_SPECVAL])
137 /**********************************************************/
138 /* deal with control flow 3: exception */
139 /**********************************************************/
142 /**********************************************************/
143 /* deal with stack canary */
144 /**********************************************************/
146 #if VM_CHECK_MODE > 0
147 #define SETUP_CANARY(cond) \
151 SET_SV(vm_stack_canary); \
154 SET_SV(Qfalse); /* cleanup */ \
156 #define CHECK_CANARY(cond, insn) \
158 if (*canary == vm_stack_canary) { \
159 *canary = Qfalse; /* cleanup */ \
162 rb_vm_canary_is_found_dead(insn, *canary); \
166 #define SETUP_CANARY(cond) if (cond) {} else {}
167 #define CHECK_CANARY(cond, insn) if (cond) {(void)(insn);}
170 /**********************************************************/
172 /**********************************************************/
175 #define CALL_SIMPLE_METHOD() do { \
176 rb_snum_t x = leaf ? INSN_ATTR(width) : 0; \
177 rb_snum_t y = attr_width_opt_send_without_block(0); \
178 rb_snum_t z = x - y; \
180 DISPATCH_ORIGINAL_INSN(opt_send_without_block); \
184 #define PREV_CLASS_SERIAL() (ruby_vm_class_serial)
185 #define NEXT_CLASS_SERIAL() (++ruby_vm_class_serial)
186 #define GET_GLOBAL_CONSTANT_STATE() (ruby_vm_global_constant_state)
187 #define INC_GLOBAL_CONSTANT_STATE() (++ruby_vm_global_constant_state)
188 #define GET_GLOBAL_CVAR_STATE() (ruby_vm_global_cvar_state)
189 #define INC_GLOBAL_CVAR_STATE() (++ruby_vm_global_cvar_state)
191 static inline struct vm_throw_data
*
192 THROW_DATA_NEW(VALUE val
, const rb_control_frame_t
*cf
, int st
)
194 struct vm_throw_data
*obj
= (struct vm_throw_data
*)rb_imemo_new(imemo_throw_data
, val
, (VALUE
)cf
, 0, 0);
195 obj
->throw_state
= st
;
200 THROW_DATA_VAL(const struct vm_throw_data
*obj
)
202 VM_ASSERT(THROW_DATA_P(obj
));
203 return obj
->throw_obj
;
206 static inline const rb_control_frame_t
*
207 THROW_DATA_CATCH_FRAME(const struct vm_throw_data
*obj
)
209 VM_ASSERT(THROW_DATA_P(obj
));
210 return obj
->catch_frame
;
214 THROW_DATA_STATE(const struct vm_throw_data
*obj
)
216 VM_ASSERT(THROW_DATA_P(obj
));
217 return obj
->throw_state
;
221 THROW_DATA_CONSUMED_P(const struct vm_throw_data
*obj
)
223 VM_ASSERT(THROW_DATA_P(obj
));
224 return obj
->flags
& THROW_DATA_CONSUMED
;
228 THROW_DATA_CATCH_FRAME_SET(struct vm_throw_data
*obj
, const rb_control_frame_t
*cfp
)
230 VM_ASSERT(THROW_DATA_P(obj
));
231 obj
->catch_frame
= cfp
;
235 THROW_DATA_STATE_SET(struct vm_throw_data
*obj
, int st
)
237 VM_ASSERT(THROW_DATA_P(obj
));
238 obj
->throw_state
= st
;
242 THROW_DATA_CONSUMED_SET(struct vm_throw_data
*obj
)
244 if (THROW_DATA_P(obj
) &&
245 THROW_DATA_STATE(obj
) == TAG_BREAK
) {
246 obj
->flags
|= THROW_DATA_CONSUMED
;
250 #define IS_ARGS_SPLAT(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT)
251 #define IS_ARGS_KEYWORD(ci) (vm_ci_flag(ci) & VM_CALL_KWARG)
252 #define IS_ARGS_KW_SPLAT(ci) (vm_ci_flag(ci) & VM_CALL_KW_SPLAT)
253 #define IS_ARGS_KW_OR_KW_SPLAT(ci) (vm_ci_flag(ci) & (VM_CALL_KWARG | VM_CALL_KW_SPLAT))
254 #define IS_ARGS_KW_SPLAT_MUT(ci) (vm_ci_flag(ci) & VM_CALL_KW_SPLAT_MUT)
256 /* If this returns true, an optimized function returned by `vm_call_iseq_setup_func`
257 can be used as a fastpath. */
259 vm_call_iseq_optimizable_p(const struct rb_callinfo
*ci
, const struct rb_callcache
*cc
)
261 return !IS_ARGS_SPLAT(ci
) && !IS_ARGS_KEYWORD(ci
) &&
262 METHOD_ENTRY_CACHEABLE(vm_cc_cme(cc
));
265 #endif /* RUBY_INSNHELPER_H */