-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfrom_call_method_to_tco.c
More file actions
287 lines (254 loc) · 11 KB
/
Copy pathfrom_call_method_to_tco.c
File metadata and controls
287 lines (254 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
static void
vm_search_method(rb_call_info_t *ci, VALUE recv)
{
VALUE klass = CLASS_OF(recv);
#if OPT_INLINE_METHOD_CACHE
if (LIKELY(GET_GLOBAL_METHOD_STATE() == ci->method_state && RCLASS_SERIAL(klass) == ci->class_serial)) {
/* cache hit! */
return;
}
#endif
ci->me = rb_method_entry(klass, ci->mid, &ci->defined_class);
ci->klass = klass;
ci->call = vm_call_general;
#if OPT_INLINE_METHOD_CACHE
ci->method_state = GET_GLOBAL_METHOD_STATE();
ci->class_serial = RCLASS_SERIAL(klass);
#endif
}
static VALUE
vm_call_general(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci)
{
return vm_call_method(th, reg_cfp, ci);
}
VALUE
vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
{
int enable_fastpath = 1;
rb_call_info_t ci_temp;
start_method_dispatch:
if (ci->me != 0) {
if ((ci->me->flag == 0)) {
VALUE klass;
normal_method_dispatch:
switch (ci->me->def->type) {
case VM_METHOD_TYPE_ISEQ:{
CI_SET_FASTPATH(ci, vm_call_iseq_setup, enable_fastpath);
return vm_call_iseq_setup(th, cfp, ci);
}
case VM_METHOD_TYPE_NOTIMPLEMENTED:
case VM_METHOD_TYPE_CFUNC:
CI_SET_FASTPATH(ci, vm_call_cfunc, enable_fastpath);
return vm_call_cfunc(th, cfp, ci);
case VM_METHOD_TYPE_ATTRSET:{
CALLER_SETUP_ARG(cfp, ci);
rb_check_arity(ci->argc, 1, 1);
ci->aux.index = 0;
CI_SET_FASTPATH(ci, vm_call_attrset, enable_fastpath && !(ci->flag & VM_CALL_ARGS_SPLAT));
return vm_call_attrset(th, cfp, ci);
}
case VM_METHOD_TYPE_IVAR:{
CALLER_SETUP_ARG(cfp, ci);
rb_check_arity(ci->argc, 0, 0);
ci->aux.index = 0;
CI_SET_FASTPATH(ci, vm_call_ivar, enable_fastpath && !(ci->flag & VM_CALL_ARGS_SPLAT));
return vm_call_ivar(th, cfp, ci);
}
case VM_METHOD_TYPE_MISSING:{
ci->aux.missing_reason = 0;
CI_SET_FASTPATH(ci, vm_call_method_missing, enable_fastpath);
return vm_call_method_missing(th, cfp, ci);
}
case VM_METHOD_TYPE_BMETHOD:{
CI_SET_FASTPATH(ci, vm_call_bmethod, enable_fastpath);
return vm_call_bmethod(th, cfp, ci);
}
case VM_METHOD_TYPE_ZSUPER:{
klass = ci->me->klass;
klass = RCLASS_ORIGIN(klass);
zsuper_method_dispatch:
klass = RCLASS_SUPER(klass);
if (!klass) {
ci->me = 0;
goto start_method_dispatch;
}
ci_temp = *ci;
ci = &ci_temp;
ci->me = rb_method_entry(klass, ci->mid, &ci->defined_class);
if (ci->me != 0) {
goto normal_method_dispatch;
}
else {
goto start_method_dispatch;
}
}
case VM_METHOD_TYPE_OPTIMIZED:{
switch (ci->me->def->body.optimize_type) {
case OPTIMIZED_METHOD_TYPE_SEND:
CI_SET_FASTPATH(ci, vm_call_opt_send, enable_fastpath);
return vm_call_opt_send(th, cfp, ci);
case OPTIMIZED_METHOD_TYPE_CALL:
CI_SET_FASTPATH(ci, vm_call_opt_call, enable_fastpath);
return vm_call_opt_call(th, cfp, ci);
default:
rb_bug("vm_call_method: unsupported optimized method type (%d)",
ci->me->def->body.optimize_type);
}
break;
}
case VM_METHOD_TYPE_UNDEF:
break;
case VM_METHOD_TYPE_REFINED:{
NODE *cref = rb_vm_get_cref(cfp->iseq, cfp->ep);
VALUE refinements = cref ? cref->nd_refinements : Qnil;
VALUE refinement, defined_class;
rb_method_entry_t *me;
refinement = find_refinement(refinements,
ci->defined_class);
if (NIL_P(refinement)) {
goto no_refinement_dispatch;
}
me = rb_method_entry(refinement, ci->mid, &defined_class);
if (me) {
if (ci->call == vm_call_super_method) {
rb_control_frame_t *top_cfp = current_method_entry(th, cfp);
if (top_cfp->me &&
rb_method_definition_eq(me->def, top_cfp->me->def)) {
goto no_refinement_dispatch;
}
}
ci->me = me;
ci->defined_class = defined_class;
if (me->def->type != VM_METHOD_TYPE_REFINED) {
goto start_method_dispatch;
}
}
no_refinement_dispatch:
if (ci->me->def->body.orig_me) {
ci->me = ci->me->def->body.orig_me;
if (UNDEFINED_METHOD_ENTRY_P(ci->me)) {
ci->me = 0;
}
goto start_method_dispatch;
}
else {
klass = ci->me->klass;
goto zsuper_method_dispatch;
}
}
}
rb_bug("vm_call_method: unsupported method type (%d)", ci->me->def->type);
}
else {
int noex_safe;
if (!(ci->flag & VM_CALL_FCALL) && (ci->me->flag & NOEX_MASK) & NOEX_PRIVATE) {
int stat = NOEX_PRIVATE;
if (ci->flag & VM_CALL_VCALL) {
stat |= NOEX_VCALL;
}
ci->aux.missing_reason = stat;
CI_SET_FASTPATH(ci, vm_call_method_missing, 1);
return vm_call_method_missing(th, cfp, ci);
}
else if (!(ci->flag & VM_CALL_OPT_SEND) && (ci->me->flag & NOEX_MASK) & NOEX_PROTECTED) {
enable_fastpath = 0;
if (!rb_obj_is_kind_of(cfp->self, ci->defined_class)) {
ci->aux.missing_reason = NOEX_PROTECTED;
return vm_call_method_missing(th, cfp, ci);
}
else {
goto normal_method_dispatch;
}
}
else if ((noex_safe = NOEX_SAFE(ci->me->flag)) > th->safe_level && (noex_safe > 2)) {
rb_raise(rb_eSecurityError, "calling insecure method: %"PRIsVALUE, rb_id2str(ci->mid));
}
else {
goto normal_method_dispatch;
}
}
}
else {
/* method missing */
int stat = 0;
if (ci->flag & VM_CALL_VCALL) {
stat |= NOEX_VCALL;
}
if (ci->flag & VM_CALL_SUPER) {
stat |= NOEX_SUPER;
}
if (ci->mid == idMethodMissing) {
rb_control_frame_t *reg_cfp = cfp;
VALUE *argv = STACK_ADDR_FROM_TOP(ci->argc);
rb_raise_method_missing(th, ci->argc, argv, ci->recv, stat);
}
else {
ci->aux.missing_reason = stat;
CI_SET_FASTPATH(ci, vm_call_method_missing, 1);
return vm_call_method_missing(th, cfp, ci);
}
}
rb_bug("vm_call_method: unreachable");
}
static VALUE
vm_call_iseq_setup(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
{
vm_callee_setup_arg(th, ci, ci->me->def->body.iseq, cfp->sp - ci->argc);
return vm_call_iseq_setup_2(th, cfp, ci);
}
static VALUE
vm_call_iseq_setup_2(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
{
if (LIKELY(!(ci->flag & VM_CALL_TAILCALL))) {
return vm_call_iseq_setup_normal(th, cfp, ci);
}
else {
return vm_call_iseq_setup_tailcall(th, cfp, ci);
}
}
static inline VALUE
vm_call_iseq_setup_normal(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
{
int i, local_size;
VALUE *argv = cfp->sp - ci->argc;
rb_iseq_t *iseq = ci->me->def->body.iseq;
VALUE *sp = argv + iseq->param.size;
/* clear local variables (arg_size...local_size) */
for (i = iseq->param.size, local_size = iseq->local_size; i < local_size; i++) {
*sp++ = Qnil;
}
vm_push_frame(th, iseq, VM_FRAME_MAGIC_METHOD, ci->recv, ci->defined_class,
VM_ENVVAL_BLOCK_PTR(ci->blockptr),
iseq->iseq_encoded + ci->aux.opt_pc, sp, 0, ci->me, iseq->stack_max);
cfp->sp = argv - 1 /* recv */;
return Qundef;
}
static inline VALUE
vm_call_iseq_setup_tailcall(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
{
int i;
VALUE *argv = cfp->sp - ci->argc;
rb_iseq_t *iseq = ci->me->def->body.iseq;
VALUE *src_argv = argv;
VALUE *sp_orig, *sp;
VALUE finish_flag = VM_FRAME_TYPE_FINISH_P(cfp) ? VM_FRAME_FLAG_FINISH : 0;
cfp = th->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp); /* pop cf */
RUBY_VM_CHECK_INTS(th);
sp_orig = sp = cfp->sp;
/* push self */
sp[0] = ci->recv;
sp++;
/* copy arguments */
for (i=0; i < iseq->param.size; i++) {
*sp++ = src_argv[i];
}
/* clear local variables */
for (i = 0; i < iseq->local_size - iseq->param.size; i++) {
*sp++ = Qnil;
}
vm_push_frame(th, iseq, VM_FRAME_MAGIC_METHOD | finish_flag,
ci->recv, ci->defined_class, VM_ENVVAL_BLOCK_PTR(ci->blockptr),
iseq->iseq_encoded + ci->aux.opt_pc, sp, 0, ci->me, iseq->stack_max);
cfp->sp = sp_orig;
return Qundef;
}