Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

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
15 RUBY_SYMBOL_EXPORT_BEGIN
16
17 RUBY_EXTERN VALUE ruby_vm_const_missing_count;
18 RUBY_EXTERN rb_serial_t ruby_vm_global_method_state;
19 RUBY_EXTERN rb_serial_t ruby_vm_global_constant_state;
20 RUBY_EXTERN rb_serial_t ruby_vm_class_serial;
21
22 RUBY_SYMBOL_EXPORT_END
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
59 enum 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 };
67 enum 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
123 static inline void
124 CC_SET_FASTPATH(CALL_CACHE cc, vm_call_handler func, bool enabled)
125 {
126 if (LIKELY(enabled)) {
127 cc->call = func;
128 }
129 }
130
131 static inline void
132 CC_SET_ME(CALL_CACHE cc, const rb_callable_method_entry_t *me)
133 {
134 cc->me = me;
135 cc->method_serial = me ? me->def->method_serial : 0;
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
194 static inline struct vm_throw_data *
195 THROW_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
202 static inline VALUE
203 THROW_DATA_VAL(const struct vm_throw_data *obj)
204 {
205 VM_ASSERT(THROW_DATA_P(obj));
206 return obj->throw_obj;
207 }
208
209 static inline const rb_control_frame_t *
210 THROW_DATA_CATCH_FRAME(const struct vm_throw_data *obj)
211 {
212 VM_ASSERT(THROW_DATA_P(obj));
213 return obj->catch_frame;
214 }
215
216 static inline int
217 THROW_DATA_STATE(const struct vm_throw_data *obj)
218 {
219 VM_ASSERT(THROW_DATA_P(obj));
220 return obj->throw_state;
221 }
222
223 static inline int
224 THROW_DATA_CONSUMED_P(const struct vm_throw_data *obj)
225 {
226 VM_ASSERT(THROW_DATA_P(obj));
227 return obj->flags & THROW_DATA_CONSUMED;
228 }
229
230 static inline void
231 THROW_DATA_CATCH_FRAME_SET(struct vm_throw_data *obj, const rb_control_frame_t *cfp)
232 {
233 VM_ASSERT(THROW_DATA_P(obj));
234 obj->catch_frame = cfp;
235 }
236
237 static inline void
238 THROW_DATA_STATE_SET(struct vm_throw_data *obj, int st)
239 {
240 VM_ASSERT(THROW_DATA_P(obj));
241 obj->throw_state = st;
242 }
243
244 static inline void
245 THROW_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. */
260 static bool
261 vm_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) &&
264 !(METHOD_ENTRY_VISI(cc->me) == METHOD_VISI_PROTECTED);
265 }
266
267 #endif /* RUBY_INSNHELPER_H */
RUBY_SYMBOL_EXPORT_END
#define RUBY_SYMBOL_EXPORT_END
Definition: missing.h:49
rb_method_definition_struct::method_serial
uintptr_t method_serial
Definition: method.h:180
rb_call_cache::me
const struct rb_callable_method_entry_struct * me
Definition: internal.h:2373
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_callable_method_entry_struct
Definition: method.h:59
rb_call_cache::call
VALUE(* call)(struct rb_execution_context_struct *ec, struct rb_control_frame_struct *cfp, struct rb_calling_info *calling, struct rb_call_data *cd)
Definition: internal.h:2376
ci
rb_control_frame_t struct rb_calling_info const struct rb_call_info * ci
Definition: rb_mjit_min_header-2.7.0.h:15146
THROW_DATA_P
#define THROW_DATA_P(err)
Definition: internal.h:1201
rb_call_cache::method_serial
uintptr_t method_serial
Definition: internal.h:2374
VM_ASSERT
#define VM_ASSERT(expr)
Definition: vm_core.h:56
imemo_throw_data
@ imemo_throw_data
Definition: internal.h:1136
rb_imemo_new
VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0)
Definition: gc.c:2306
cc
const struct rb_call_cache * cc
Definition: rb_mjit_min_header-2.7.0.h:13228
rb_call_cache
Definition: internal.h:2355
THROW_DATA_CONSUMED
#define THROW_DATA_CONSUMED
Definition: internal.h:1190
obj
const VALUE VALUE obj
Definition: rb_mjit_min_header-2.7.0.h:5742
ruby_vm_const_missing_count
RUBY_SYMBOL_EXPORT_BEGIN RUBY_EXTERN VALUE ruby_vm_const_missing_count
Definition: vm_insnhelper.h:17
LIKELY
#define LIKELY(x)
Definition: ffi_common.h:125
ruby_vm_class_serial
RUBY_EXTERN rb_serial_t ruby_vm_class_serial
Definition: vm_insnhelper.h:20
METHOD_ENTRY_VISI
#define METHOD_ENTRY_VISI(me)
Definition: method.h:67
rb_serial_t
unsigned long rb_serial_t
Definition: internal.h:1014
IS_ARGS_KEYWORD
#define IS_ARGS_KEYWORD(ci)
Definition: vm_insnhelper.h:254
vm_throw_data
THROW_DATA.
Definition: internal.h:1193
METHOD_VISI_PROTECTED
@ METHOD_VISI_PROTECTED
Definition: method.h:30
rb_control_frame_struct
Definition: vm_core.h:760
ruby_vm_global_constant_state
RUBY_EXTERN rb_serial_t ruby_vm_global_constant_state
Definition: vm_insnhelper.h:19
me
const rb_callable_method_entry_t * me
Definition: rb_mjit_min_header-2.7.0.h:13226
vm_call_handler
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)
Definition: vm_core.h:263
ruby_vm_global_method_state
RUBY_EXTERN rb_serial_t ruby_vm_global_method_state
Definition: vm_insnhelper.h:18
RUBY_SYMBOL_EXPORT_BEGIN
#define RUBY_SYMBOL_EXPORT_BEGIN
Definition: missing.h:48
st
enum ruby_tag_type st
Definition: rb_mjit_min_header-2.7.0.h:11111
RUBY_EXTERN
#define RUBY_EXTERN
Definition: missing.h:77
rb_callable_method_entry_struct::def
struct rb_method_definition_struct *const def
Definition: method.h:62
TAG_BREAK
#define TAG_BREAK
Definition: vm_core.h:199
rb_call_info
Definition: internal.h:2387
IS_ARGS_SPLAT
#define IS_ARGS_SPLAT(ci)
Definition: vm_insnhelper.h:253
cfp
rb_control_frame_t * cfp
Definition: rb_mjit_min_header-2.7.0.h:14544