|
Ruby
2.0.0p451(2014-02-24revision45167)
|
00001 /********************************************************************** 00002 00003 vm_insnhelper.c - instruction helper functions. 00004 00005 $Author: nagachika $ 00006 00007 Copyright (C) 2007 Koichi Sasada 00008 00009 **********************************************************************/ 00010 00011 /* finish iseq array */ 00012 #include "insns.inc" 00013 #include <math.h> 00014 #include "constant.h" 00015 #include "internal.h" 00016 #include "probes.h" 00017 #include "probes_helper.h" 00018 00019 /* control stack frame */ 00020 00021 #ifndef INLINE 00022 #define INLINE inline 00023 #endif 00024 00025 static rb_control_frame_t *vm_get_ruby_level_caller_cfp(rb_thread_t *th, rb_control_frame_t *cfp); 00026 00027 static void 00028 vm_stackoverflow(void) 00029 { 00030 rb_exc_raise(sysstack_error); 00031 } 00032 00033 static inline rb_control_frame_t * 00034 vm_push_frame(rb_thread_t *th, 00035 const rb_iseq_t *iseq, 00036 VALUE type, 00037 VALUE self, 00038 VALUE klass, 00039 VALUE specval, 00040 const VALUE *pc, 00041 VALUE *sp, 00042 int local_size, 00043 const rb_method_entry_t *me) 00044 { 00045 rb_control_frame_t *const cfp = th->cfp - 1; 00046 int i; 00047 00048 /* check stack overflow */ 00049 if ((void *)(sp + local_size) >= (void *)cfp) { 00050 vm_stackoverflow(); 00051 } 00052 th->cfp = cfp; 00053 00054 /* setup vm value stack */ 00055 00056 /* initialize local variables */ 00057 for (i=0; i < local_size; i++) { 00058 *sp++ = Qnil; 00059 } 00060 00061 /* set special val */ 00062 *sp = specval; 00063 00064 /* setup vm control frame stack */ 00065 00066 cfp->pc = (VALUE *)pc; 00067 cfp->sp = sp + 1; 00068 #if VM_DEBUG_BP_CHECK 00069 cfp->bp_check = sp + 1; 00070 #endif 00071 cfp->ep = sp; 00072 cfp->iseq = (rb_iseq_t *) iseq; 00073 cfp->flag = type; 00074 cfp->self = self; 00075 cfp->block_iseq = 0; 00076 cfp->proc = 0; 00077 cfp->me = me; 00078 if (klass) { 00079 cfp->klass = klass; 00080 } 00081 else { 00082 rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); 00083 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, prev_cfp)) { 00084 cfp->klass = Qnil; 00085 } 00086 else { 00087 cfp->klass = prev_cfp->klass; 00088 } 00089 } 00090 00091 if (VMDEBUG == 2) { 00092 SDR(); 00093 } 00094 00095 return cfp; 00096 } 00097 00098 static inline void 00099 vm_pop_frame(rb_thread_t *th) 00100 { 00101 th->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp); 00102 00103 if (VMDEBUG == 2) { 00104 SDR(); 00105 } 00106 } 00107 00108 /* method dispatch */ 00109 static inline VALUE 00110 rb_arg_error_new(int argc, int min, int max) 00111 { 00112 VALUE err_mess = 0; 00113 if (min == max) { 00114 err_mess = rb_sprintf("wrong number of arguments (%d for %d)", argc, min); 00115 } 00116 else if (max == UNLIMITED_ARGUMENTS) { 00117 err_mess = rb_sprintf("wrong number of arguments (%d for %d+)", argc, min); 00118 } 00119 else { 00120 err_mess = rb_sprintf("wrong number of arguments (%d for %d..%d)", argc, min, max); 00121 } 00122 return rb_exc_new3(rb_eArgError, err_mess); 00123 } 00124 00125 NORETURN(static void argument_error(const rb_iseq_t *iseq, int miss_argc, int min_argc, int max_argc)); 00126 static void 00127 argument_error(const rb_iseq_t *iseq, int miss_argc, int min_argc, int max_argc) 00128 { 00129 VALUE exc = rb_arg_error_new(miss_argc, min_argc, max_argc); 00130 VALUE bt = rb_make_backtrace(); 00131 VALUE err_line = 0; 00132 00133 if (iseq) { 00134 int line_no = rb_iseq_first_lineno(iseq); 00135 00136 err_line = rb_sprintf("%s:%d:in `%s'", 00137 RSTRING_PTR(iseq->location.path), 00138 line_no, RSTRING_PTR(iseq->location.label)); 00139 rb_funcall(bt, rb_intern("unshift"), 1, err_line); 00140 } 00141 00142 rb_funcall(exc, rb_intern("set_backtrace"), 1, bt); 00143 rb_exc_raise(exc); 00144 } 00145 00146 NORETURN(static void unknown_keyword_error(const rb_iseq_t *iseq, VALUE hash)); 00147 static void 00148 unknown_keyword_error(const rb_iseq_t *iseq, VALUE hash) 00149 { 00150 VALUE sep = rb_usascii_str_new2(", "), keys; 00151 const char *msg; 00152 int i; 00153 for (i = 0; i < iseq->arg_keywords; i++) { 00154 rb_hash_delete(hash, ID2SYM(iseq->arg_keyword_table[i])); 00155 } 00156 keys = rb_funcall(hash, rb_intern("keys"), 0, 0); 00157 if (!RB_TYPE_P(keys, T_ARRAY)) rb_raise(rb_eArgError, "unknown keyword"); 00158 msg = RARRAY_LEN(keys) == 1 ? "" : "s"; 00159 keys = rb_funcall(keys, rb_intern("join"), 1, sep); 00160 rb_raise(rb_eArgError, "unknown keyword%s: %"PRIsVALUE, msg, keys); 00161 } 00162 00163 void 00164 rb_error_arity(int argc, int min, int max) 00165 { 00166 rb_exc_raise(rb_arg_error_new(argc, min, max)); 00167 } 00168 00169 /* svar */ 00170 00171 static inline NODE * 00172 lep_svar_place(rb_thread_t *th, VALUE *lep) 00173 { 00174 VALUE *svar; 00175 00176 if (lep && th->root_lep != lep) { 00177 svar = &lep[-1]; 00178 } 00179 else { 00180 svar = &th->root_svar; 00181 } 00182 if (NIL_P(*svar)) { 00183 *svar = (VALUE)NEW_IF(Qnil, Qnil, Qnil); 00184 } 00185 return (NODE *)*svar; 00186 } 00187 00188 static VALUE 00189 lep_svar_get(rb_thread_t *th, VALUE *lep, rb_num_t key) 00190 { 00191 NODE *svar = lep_svar_place(th, lep); 00192 00193 switch (key) { 00194 case 0: 00195 return svar->u1.value; 00196 case 1: 00197 return svar->u2.value; 00198 default: { 00199 const VALUE ary = svar->u3.value; 00200 00201 if (NIL_P(ary)) { 00202 return Qnil; 00203 } 00204 else { 00205 return rb_ary_entry(ary, key - DEFAULT_SPECIAL_VAR_COUNT); 00206 } 00207 } 00208 } 00209 } 00210 00211 static void 00212 lep_svar_set(rb_thread_t *th, VALUE *lep, rb_num_t key, VALUE val) 00213 { 00214 NODE *svar = lep_svar_place(th, lep); 00215 00216 switch (key) { 00217 case 0: 00218 svar->u1.value = val; 00219 return; 00220 case 1: 00221 svar->u2.value = val; 00222 return; 00223 default: { 00224 VALUE ary = svar->u3.value; 00225 00226 if (NIL_P(ary)) { 00227 svar->u3.value = ary = rb_ary_new(); 00228 } 00229 rb_ary_store(ary, key - DEFAULT_SPECIAL_VAR_COUNT, val); 00230 } 00231 } 00232 } 00233 00234 static inline VALUE 00235 vm_getspecial(rb_thread_t *th, VALUE *lep, rb_num_t key, rb_num_t type) 00236 { 00237 VALUE val; 00238 00239 if (type == 0) { 00240 val = lep_svar_get(th, lep, key); 00241 } 00242 else { 00243 VALUE backref = lep_svar_get(th, lep, 1); 00244 00245 if (type & 0x01) { 00246 switch (type >> 1) { 00247 case '&': 00248 val = rb_reg_last_match(backref); 00249 break; 00250 case '`': 00251 val = rb_reg_match_pre(backref); 00252 break; 00253 case '\'': 00254 val = rb_reg_match_post(backref); 00255 break; 00256 case '+': 00257 val = rb_reg_match_last(backref); 00258 break; 00259 default: 00260 rb_bug("unexpected back-ref"); 00261 } 00262 } 00263 else { 00264 val = rb_reg_nth_match((int)(type >> 1), backref); 00265 } 00266 } 00267 return val; 00268 } 00269 00270 static NODE * 00271 vm_get_cref0(const rb_iseq_t *iseq, const VALUE *ep) 00272 { 00273 while (1) { 00274 if (VM_EP_LEP_P(ep)) { 00275 if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) return NULL; 00276 return iseq->cref_stack; 00277 } 00278 else if (ep[-1] != Qnil) { 00279 return (NODE *)ep[-1]; 00280 } 00281 ep = VM_EP_PREV_EP(ep); 00282 } 00283 } 00284 00285 NODE * 00286 rb_vm_get_cref(const rb_iseq_t *iseq, const VALUE *ep) 00287 { 00288 NODE *cref = vm_get_cref0(iseq, ep); 00289 00290 if (cref == 0) { 00291 rb_bug("rb_vm_get_cref: unreachable"); 00292 } 00293 return cref; 00294 } 00295 00296 static NODE * 00297 vm_cref_push(rb_thread_t *th, VALUE klass, int noex, rb_block_t *blockptr) 00298 { 00299 rb_control_frame_t *cfp = vm_get_ruby_level_caller_cfp(th, th->cfp); 00300 NODE *cref = NEW_CREF(klass); 00301 cref->nd_refinements = Qnil; 00302 cref->nd_visi = noex; 00303 00304 if (blockptr) { 00305 cref->nd_next = vm_get_cref0(blockptr->iseq, blockptr->ep); 00306 } 00307 else if (cfp) { 00308 cref->nd_next = vm_get_cref0(cfp->iseq, cfp->ep); 00309 } 00310 /* TODO: why cref->nd_next is 1? */ 00311 if (cref->nd_next && cref->nd_next != (void *) 1 && 00312 !NIL_P(cref->nd_next->nd_refinements)) { 00313 COPY_CREF_OMOD(cref, cref->nd_next); 00314 } 00315 00316 return cref; 00317 } 00318 00319 static inline VALUE 00320 vm_get_cbase(const rb_iseq_t *iseq, const VALUE *ep) 00321 { 00322 NODE *cref = rb_vm_get_cref(iseq, ep); 00323 VALUE klass = Qundef; 00324 00325 while (cref) { 00326 if ((klass = cref->nd_clss) != 0) { 00327 break; 00328 } 00329 cref = cref->nd_next; 00330 } 00331 00332 return klass; 00333 } 00334 00335 static inline VALUE 00336 vm_get_const_base(const rb_iseq_t *iseq, const VALUE *ep) 00337 { 00338 NODE *cref = rb_vm_get_cref(iseq, ep); 00339 VALUE klass = Qundef; 00340 00341 while (cref) { 00342 if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) && 00343 (klass = cref->nd_clss) != 0) { 00344 break; 00345 } 00346 cref = cref->nd_next; 00347 } 00348 00349 return klass; 00350 } 00351 00352 static inline void 00353 vm_check_if_namespace(VALUE klass) 00354 { 00355 VALUE str; 00356 if (!RB_TYPE_P(klass, T_CLASS) && !RB_TYPE_P(klass, T_MODULE)) { 00357 str = rb_inspect(klass); 00358 rb_raise(rb_eTypeError, "%s is not a class/module", 00359 StringValuePtr(str)); 00360 } 00361 } 00362 00363 static inline VALUE 00364 vm_get_iclass(rb_control_frame_t *cfp, VALUE klass) 00365 { 00366 if (RB_TYPE_P(klass, T_MODULE) && 00367 FL_TEST(klass, RMODULE_IS_OVERLAID) && 00368 RB_TYPE_P(cfp->klass, T_ICLASS) && 00369 RBASIC(cfp->klass)->klass == klass) { 00370 return cfp->klass; 00371 } 00372 else { 00373 return klass; 00374 } 00375 } 00376 00377 static inline VALUE 00378 vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq, 00379 VALUE orig_klass, ID id, int is_defined) 00380 { 00381 VALUE val; 00382 00383 if (orig_klass == Qnil) { 00384 /* in current lexical scope */ 00385 const NODE *root_cref = rb_vm_get_cref(iseq, th->cfp->ep); 00386 const NODE *cref; 00387 VALUE klass = orig_klass; 00388 00389 while (root_cref && root_cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) { 00390 root_cref = root_cref->nd_next; 00391 } 00392 cref = root_cref; 00393 while (cref && cref->nd_next) { 00394 if (cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) { 00395 klass = Qnil; 00396 } 00397 else { 00398 klass = cref->nd_clss; 00399 } 00400 cref = cref->nd_next; 00401 00402 if (!NIL_P(klass)) { 00403 VALUE av, am = 0; 00404 st_data_t data; 00405 search_continue: 00406 if (RCLASS_CONST_TBL(klass) && 00407 st_lookup(RCLASS_CONST_TBL(klass), id, &data)) { 00408 val = ((rb_const_entry_t*)data)->value; 00409 if (val == Qundef) { 00410 if (am == klass) break; 00411 am = klass; 00412 if (is_defined) return 1; 00413 if (rb_autoloading_value(klass, id, &av)) return av; 00414 rb_autoload_load(klass, id); 00415 goto search_continue; 00416 } 00417 else { 00418 if (is_defined) { 00419 return 1; 00420 } 00421 else { 00422 return val; 00423 } 00424 } 00425 } 00426 } 00427 } 00428 00429 /* search self */ 00430 if (root_cref && !NIL_P(root_cref->nd_clss)) { 00431 klass = vm_get_iclass(th->cfp, root_cref->nd_clss); 00432 } 00433 else { 00434 klass = CLASS_OF(th->cfp->self); 00435 } 00436 00437 if (is_defined) { 00438 return rb_const_defined(klass, id); 00439 } 00440 else { 00441 return rb_const_get(klass, id); 00442 } 00443 } 00444 else { 00445 vm_check_if_namespace(orig_klass); 00446 if (is_defined) { 00447 return rb_public_const_defined_from(orig_klass, id); 00448 } 00449 else { 00450 return rb_public_const_get_from(orig_klass, id); 00451 } 00452 } 00453 } 00454 00455 static inline VALUE 00456 vm_get_cvar_base(NODE *cref, rb_control_frame_t *cfp) 00457 { 00458 VALUE klass; 00459 00460 if (!cref) { 00461 rb_bug("vm_get_cvar_base: no cref"); 00462 } 00463 00464 while (cref->nd_next && 00465 (NIL_P(cref->nd_clss) || FL_TEST(cref->nd_clss, FL_SINGLETON) || 00466 (cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL))) { 00467 cref = cref->nd_next; 00468 } 00469 if (!cref->nd_next) { 00470 rb_warn("class variable access from toplevel"); 00471 } 00472 00473 klass = vm_get_iclass(cfp, cref->nd_clss); 00474 00475 if (NIL_P(klass)) { 00476 rb_raise(rb_eTypeError, "no class variables available"); 00477 } 00478 return klass; 00479 } 00480 00481 static VALUE 00482 vm_search_const_defined_class(const VALUE cbase, ID id) 00483 { 00484 if (rb_const_defined_at(cbase, id)) return cbase; 00485 if (cbase == rb_cObject) { 00486 VALUE tmp = RCLASS_SUPER(cbase); 00487 while (tmp) { 00488 if (rb_const_defined_at(tmp, id)) return tmp; 00489 tmp = RCLASS_SUPER(tmp); 00490 } 00491 } 00492 return 0; 00493 } 00494 00495 #ifndef USE_IC_FOR_IVAR 00496 #define USE_IC_FOR_IVAR 1 00497 #endif 00498 00499 static inline VALUE 00500 vm_getivar(VALUE obj, ID id, IC ic, rb_call_info_t *ci, int is_attr) 00501 { 00502 #if USE_IC_FOR_IVAR 00503 if (RB_TYPE_P(obj, T_OBJECT)) { 00504 VALUE val = Qundef; 00505 VALUE klass = RBASIC(obj)->klass; 00506 00507 if (LIKELY((!is_attr && (ic->ic_class == klass && ic->ic_vmstat == GET_VM_STATE_VERSION())) || 00508 (is_attr && ci->aux.index > 0))) { 00509 long index = !is_attr ? ic->ic_value.index : ci->aux.index - 1; 00510 long len = ROBJECT_NUMIV(obj); 00511 VALUE *ptr = ROBJECT_IVPTR(obj); 00512 00513 if (index < len) { 00514 val = ptr[index]; 00515 } 00516 } 00517 else { 00518 st_data_t index; 00519 long len = ROBJECT_NUMIV(obj); 00520 VALUE *ptr = ROBJECT_IVPTR(obj); 00521 struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj); 00522 00523 if (iv_index_tbl) { 00524 if (st_lookup(iv_index_tbl, id, &index)) { 00525 if ((long)index < len) { 00526 val = ptr[index]; 00527 } 00528 if (!is_attr) { 00529 ic->ic_class = klass; 00530 ic->ic_value.index = index; 00531 ic->ic_vmstat = GET_VM_STATE_VERSION(); 00532 } 00533 else { /* call_info */ 00534 ci->aux.index = index + 1; 00535 } 00536 } 00537 } 00538 } 00539 00540 if (UNLIKELY(val == Qundef)) { 00541 if (!is_attr) rb_warning("instance variable %s not initialized", rb_id2name(id)); 00542 val = Qnil; 00543 } 00544 return val; 00545 } 00546 #endif /* USE_IC_FOR_IVAR */ 00547 if (is_attr) 00548 return rb_attr_get(obj, id); 00549 return rb_ivar_get(obj, id); 00550 } 00551 00552 static inline VALUE 00553 vm_setivar(VALUE obj, ID id, VALUE val, IC ic, rb_call_info_t *ci, int is_attr) 00554 { 00555 #if USE_IC_FOR_IVAR 00556 if (!OBJ_UNTRUSTED(obj) && rb_safe_level() >= 4) { 00557 rb_raise(rb_eSecurityError, "Insecure: can't modify instance variable"); 00558 } 00559 00560 rb_check_frozen(obj); 00561 00562 if (RB_TYPE_P(obj, T_OBJECT)) { 00563 VALUE klass = RBASIC(obj)->klass; 00564 st_data_t index; 00565 00566 if (LIKELY( 00567 (!is_attr && ic->ic_class == klass && ic->ic_vmstat == GET_VM_STATE_VERSION()) || 00568 (is_attr && ci->aux.index > 0))) { 00569 long index = !is_attr ? ic->ic_value.index : ci->aux.index-1; 00570 long len = ROBJECT_NUMIV(obj); 00571 VALUE *ptr = ROBJECT_IVPTR(obj); 00572 00573 if (index < len) { 00574 ptr[index] = val; 00575 return val; /* inline cache hit */ 00576 } 00577 } 00578 else { 00579 struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj); 00580 00581 if (iv_index_tbl && st_lookup(iv_index_tbl, (st_data_t)id, &index)) { 00582 if (!is_attr) { 00583 ic->ic_class = klass; 00584 ic->ic_value.index = index; 00585 ic->ic_vmstat = GET_VM_STATE_VERSION(); 00586 } 00587 else { 00588 ci->aux.index = index + 1; 00589 } 00590 } 00591 /* fall through */ 00592 } 00593 } 00594 #endif /* USE_IC_FOR_IVAR */ 00595 return rb_ivar_set(obj, id, val); 00596 } 00597 00598 static VALUE 00599 vm_getinstancevariable(VALUE obj, ID id, IC ic) 00600 { 00601 return vm_getivar(obj, id, ic, 0, 0); 00602 } 00603 00604 static void 00605 vm_setinstancevariable(VALUE obj, ID id, VALUE val, IC ic) 00606 { 00607 vm_setivar(obj, id, val, ic, 0, 0); 00608 } 00609 00610 static VALUE 00611 vm_throw(rb_thread_t *th, rb_control_frame_t *reg_cfp, 00612 rb_num_t throw_state, VALUE throwobj) 00613 { 00614 int state = (int)(throw_state & 0xff); 00615 int flag = (int)(throw_state & 0x8000); 00616 rb_num_t level = throw_state >> 16; 00617 00618 if (state != 0) { 00619 VALUE *pt = 0; 00620 if (flag != 0) { 00621 pt = (void *) 1; 00622 } 00623 else { 00624 if (state == TAG_BREAK) { 00625 rb_control_frame_t *cfp = GET_CFP(); 00626 VALUE *ep = GET_EP(); 00627 int is_orphan = 1; 00628 rb_iseq_t *base_iseq = GET_ISEQ(); 00629 00630 search_parent: 00631 if (cfp->iseq->type != ISEQ_TYPE_BLOCK) { 00632 if (cfp->iseq->type == ISEQ_TYPE_CLASS) { 00633 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); 00634 ep = cfp->ep; 00635 goto search_parent; 00636 } 00637 ep = VM_EP_PREV_EP(ep); 00638 base_iseq = base_iseq->parent_iseq; 00639 00640 while ((VALUE *) cfp < th->stack + th->stack_size) { 00641 if (cfp->ep == ep) { 00642 goto search_parent; 00643 } 00644 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); 00645 } 00646 rb_bug("VM (throw): can't find break base."); 00647 } 00648 00649 if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_LAMBDA) { 00650 /* lambda{... break ...} */ 00651 is_orphan = 0; 00652 pt = cfp->ep; 00653 state = TAG_RETURN; 00654 } 00655 else { 00656 ep = VM_EP_PREV_EP(ep); 00657 00658 while ((VALUE *)cfp < th->stack + th->stack_size) { 00659 if (cfp->ep == ep) { 00660 VALUE epc = cfp->pc - cfp->iseq->iseq_encoded; 00661 rb_iseq_t *iseq = cfp->iseq; 00662 int i; 00663 00664 for (i=0; i<iseq->catch_table_size; i++) { 00665 struct iseq_catch_table_entry *entry = &iseq->catch_table[i]; 00666 00667 if (entry->type == CATCH_TYPE_BREAK && 00668 entry->start < epc && entry->end >= epc) { 00669 if (entry->cont == epc) { 00670 goto found; 00671 } 00672 else { 00673 break; 00674 } 00675 } 00676 } 00677 break; 00678 00679 found: 00680 pt = ep; 00681 is_orphan = 0; 00682 break; 00683 } 00684 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); 00685 } 00686 } 00687 00688 if (is_orphan) { 00689 rb_vm_localjump_error("break from proc-closure", throwobj, TAG_BREAK); 00690 } 00691 } 00692 else if (state == TAG_RETRY) { 00693 rb_num_t i; 00694 pt = VM_EP_PREV_EP(GET_EP()); 00695 for (i = 0; i < level; i++) { 00696 pt = GC_GUARDED_PTR_REF((VALUE *) * pt); 00697 } 00698 } 00699 else if (state == TAG_RETURN) { 00700 rb_control_frame_t *cfp = GET_CFP(); 00701 VALUE *ep = GET_EP(); 00702 VALUE *target_lep = VM_CF_LEP(cfp); 00703 int in_class_frame = 0; 00704 00705 /* check orphan and get dfp */ 00706 while ((VALUE *) cfp < th->stack + th->stack_size) { 00707 VALUE *lep = VM_CF_LEP(cfp); 00708 00709 if (!target_lep) { 00710 target_lep = lep; 00711 } 00712 00713 if (lep == target_lep && cfp->iseq->type == ISEQ_TYPE_CLASS) { 00714 in_class_frame = 1; 00715 target_lep = 0; 00716 } 00717 00718 if (lep == target_lep) { 00719 if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_LAMBDA) { 00720 VALUE *tep = ep; 00721 00722 if (in_class_frame) { 00723 /* lambda {class A; ... return ...; end} */ 00724 ep = cfp->ep; 00725 goto valid_return; 00726 } 00727 00728 while (target_lep != tep) { 00729 if (cfp->ep == tep) { 00730 /* in lambda */ 00731 ep = cfp->ep; 00732 goto valid_return; 00733 } 00734 tep = VM_EP_PREV_EP(tep); 00735 } 00736 } 00737 } 00738 00739 if (cfp->ep == target_lep && cfp->iseq->type == ISEQ_TYPE_METHOD) { 00740 ep = target_lep; 00741 goto valid_return; 00742 } 00743 00744 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); 00745 } 00746 00747 rb_vm_localjump_error("unexpected return", throwobj, TAG_RETURN); 00748 00749 valid_return: 00750 pt = ep; 00751 } 00752 else { 00753 rb_bug("isns(throw): unsupport throw type"); 00754 } 00755 } 00756 th->state = state; 00757 return (VALUE)NEW_THROW_OBJECT(throwobj, (VALUE) pt, state); 00758 } 00759 else { 00760 /* continue throw */ 00761 VALUE err = throwobj; 00762 00763 if (FIXNUM_P(err)) { 00764 th->state = FIX2INT(err); 00765 } 00766 else if (SYMBOL_P(err)) { 00767 th->state = TAG_THROW; 00768 } 00769 else if (BUILTIN_TYPE(err) == T_NODE) { 00770 th->state = GET_THROWOBJ_STATE(err); 00771 } 00772 else { 00773 th->state = TAG_RAISE; 00774 /*th->state = FIX2INT(rb_ivar_get(err, idThrowState));*/ 00775 } 00776 return err; 00777 } 00778 } 00779 00780 static inline void 00781 vm_expandarray(rb_control_frame_t *cfp, VALUE ary, rb_num_t num, int flag) 00782 { 00783 int is_splat = flag & 0x01; 00784 rb_num_t space_size = num + is_splat; 00785 VALUE *base = cfp->sp, *ptr; 00786 rb_num_t len; 00787 00788 if (!RB_TYPE_P(ary, T_ARRAY)) { 00789 ary = rb_ary_to_ary(ary); 00790 } 00791 00792 cfp->sp += space_size; 00793 00794 ptr = RARRAY_PTR(ary); 00795 len = (rb_num_t)RARRAY_LEN(ary); 00796 00797 if (flag & 0x02) { 00798 /* post: ..., nil ,ary[-1], ..., ary[0..-num] # top */ 00799 rb_num_t i = 0, j; 00800 00801 if (len < num) { 00802 for (i=0; i<num-len; i++) { 00803 *base++ = Qnil; 00804 } 00805 } 00806 for (j=0; i<num; i++, j++) { 00807 VALUE v = ptr[len - j - 1]; 00808 *base++ = v; 00809 } 00810 if (is_splat) { 00811 *base = rb_ary_new4(len - j, ptr); 00812 } 00813 } 00814 else { 00815 /* normal: ary[num..-1], ary[num-2], ary[num-3], ..., ary[0] # top */ 00816 rb_num_t i; 00817 VALUE *bptr = &base[space_size - 1]; 00818 00819 for (i=0; i<num; i++) { 00820 if (len <= i) { 00821 for (; i<num; i++) { 00822 *bptr-- = Qnil; 00823 } 00824 break; 00825 } 00826 *bptr-- = ptr[i]; 00827 } 00828 if (is_splat) { 00829 if (num > len) { 00830 *bptr = rb_ary_new(); 00831 } 00832 else { 00833 *bptr = rb_ary_new4(len - num, ptr + num); 00834 } 00835 } 00836 } 00837 RB_GC_GUARD(ary); 00838 } 00839 00840 static VALUE vm_call_general(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci); 00841 00842 static void 00843 vm_search_method(rb_call_info_t *ci, VALUE recv) 00844 { 00845 VALUE klass = CLASS_OF(recv); 00846 00847 #if OPT_INLINE_METHOD_CACHE 00848 if (LIKELY(GET_VM_STATE_VERSION() == ci->vmstat && klass == ci->klass)) { 00849 /* cache hit! */ 00850 } 00851 else { 00852 ci->me = rb_method_entry(klass, ci->mid, &ci->defined_class); 00853 ci->klass = klass; 00854 ci->vmstat = GET_VM_STATE_VERSION(); 00855 ci->call = vm_call_general; 00856 } 00857 #else 00858 ci->me = rb_method_entry(klass, ci->mid, &ci->defined_class); 00859 ci->call = vm_call_general; 00860 ci->klass = klass; 00861 #endif 00862 } 00863 00864 static inline int 00865 check_cfunc(const rb_method_entry_t *me, VALUE (*func)()) 00866 { 00867 if (me && me->def->type == VM_METHOD_TYPE_CFUNC && 00868 me->def->body.cfunc.func == func) { 00869 return 1; 00870 } 00871 else { 00872 return 0; 00873 } 00874 } 00875 00876 static 00877 #ifndef NO_BIG_INLINE 00878 inline 00879 #endif 00880 VALUE 00881 opt_eq_func(VALUE recv, VALUE obj, CALL_INFO ci) 00882 { 00883 if (FIXNUM_2_P(recv, obj) && 00884 BASIC_OP_UNREDEFINED_P(BOP_EQ, FIXNUM_REDEFINED_OP_FLAG)) { 00885 return (recv == obj) ? Qtrue : Qfalse; 00886 } 00887 else if (FLONUM_2_P(recv, obj) && 00888 BASIC_OP_UNREDEFINED_P(BOP_EQ, FLOAT_REDEFINED_OP_FLAG)) { 00889 return (recv == obj) ? Qtrue : Qfalse; 00890 } 00891 else if (!SPECIAL_CONST_P(recv) && !SPECIAL_CONST_P(obj)) { 00892 if (HEAP_CLASS_OF(recv) == rb_cFloat && 00893 HEAP_CLASS_OF(obj) == rb_cFloat && 00894 BASIC_OP_UNREDEFINED_P(BOP_EQ, FLOAT_REDEFINED_OP_FLAG)) { 00895 double a = RFLOAT_VALUE(recv); 00896 double b = RFLOAT_VALUE(obj); 00897 00898 if (isnan(a) || isnan(b)) { 00899 return Qfalse; 00900 } 00901 return (a == b) ? Qtrue : Qfalse; 00902 } 00903 else if (HEAP_CLASS_OF(recv) == rb_cString && 00904 HEAP_CLASS_OF(obj) == rb_cString && 00905 BASIC_OP_UNREDEFINED_P(BOP_EQ, STRING_REDEFINED_OP_FLAG)) { 00906 return rb_str_equal(recv, obj); 00907 } 00908 } 00909 00910 { 00911 vm_search_method(ci, recv); 00912 00913 if (check_cfunc(ci->me, rb_obj_equal)) { 00914 return recv == obj ? Qtrue : Qfalse; 00915 } 00916 } 00917 00918 return Qundef; 00919 } 00920 00921 static VALUE 00922 vm_call0(rb_thread_t*, VALUE, ID, int, const VALUE*, const rb_method_entry_t*, VALUE); 00923 00924 static VALUE 00925 check_match(VALUE pattern, VALUE target, enum vm_check_match_type type) 00926 { 00927 switch (type) { 00928 case VM_CHECKMATCH_TYPE_WHEN: 00929 return pattern; 00930 case VM_CHECKMATCH_TYPE_RESCUE: 00931 if (!rb_obj_is_kind_of(pattern, rb_cModule)) { 00932 rb_raise(rb_eTypeError, "class or module required for rescue clause"); 00933 } 00934 /* fall through */ 00935 case VM_CHECKMATCH_TYPE_CASE: { 00936 VALUE defined_class; 00937 rb_method_entry_t *me = rb_method_entry_with_refinements(CLASS_OF(pattern), idEqq, &defined_class); 00938 if (me) { 00939 return vm_call0(GET_THREAD(), pattern, idEqq, 1, &target, me, defined_class); 00940 } 00941 else { 00942 /* fallback to funcall (e.g. method_missing) */ 00943 return rb_funcall2(pattern, idEqq, 1, &target); 00944 } 00945 } 00946 default: 00947 rb_bug("check_match: unreachable"); 00948 } 00949 } 00950 00951 00952 #if defined(_MSC_VER) && _MSC_VER < 1300 00953 #define CHECK_CMP_NAN(a, b) if (isnan(a) || isnan(b)) return Qfalse; 00954 #else 00955 #define CHECK_CMP_NAN(a, b) /* do nothing */ 00956 #endif 00957 00958 static inline VALUE 00959 double_cmp_lt(double a, double b) 00960 { 00961 CHECK_CMP_NAN(a, b); 00962 return a < b ? Qtrue : Qfalse; 00963 } 00964 00965 static inline VALUE 00966 double_cmp_le(double a, double b) 00967 { 00968 CHECK_CMP_NAN(a, b); 00969 return a <= b ? Qtrue : Qfalse; 00970 } 00971 00972 static inline VALUE 00973 double_cmp_gt(double a, double b) 00974 { 00975 CHECK_CMP_NAN(a, b); 00976 return a > b ? Qtrue : Qfalse; 00977 } 00978 00979 static inline VALUE 00980 double_cmp_ge(double a, double b) 00981 { 00982 CHECK_CMP_NAN(a, b); 00983 return a >= b ? Qtrue : Qfalse; 00984 } 00985 00986 static VALUE * 00987 vm_base_ptr(rb_control_frame_t *cfp) 00988 { 00989 rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); 00990 VALUE *bp = prev_cfp->sp + cfp->iseq->local_size + 1; 00991 00992 if (cfp->iseq->type == ISEQ_TYPE_METHOD) { 00993 /* adjust `self' */ 00994 bp += 1; 00995 } 00996 00997 #if VM_DEBUG_BP_CHECK 00998 if (bp != cfp->bp_check) { 00999 fprintf(stderr, "bp_check: %ld, bp: %ld\n", 01000 (long)(cfp->bp_check - GET_THREAD()->stack), 01001 (long)(bp - GET_THREAD()->stack)); 01002 rb_bug("vm_base_ptr: unreachable"); 01003 } 01004 #endif 01005 01006 return bp; 01007 } 01008 01009 /* method call processes with call_info */ 01010 01011 static void 01012 vm_caller_setup_args(const rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01013 { 01014 #define SAVE_RESTORE_CI(expr, ci) do { \ 01015 int saved_argc = (ci)->argc; rb_block_t *saved_blockptr = (ci)->blockptr; /* save */ \ 01016 expr; \ 01017 (ci)->argc = saved_argc; (ci)->blockptr = saved_blockptr; /* restore */ \ 01018 } while (0) 01019 01020 if (UNLIKELY(ci->flag & VM_CALL_ARGS_BLOCKARG)) { 01021 rb_proc_t *po; 01022 VALUE proc; 01023 01024 proc = *(--cfp->sp); 01025 01026 if (proc != Qnil) { 01027 if (!rb_obj_is_proc(proc)) { 01028 VALUE b; 01029 01030 SAVE_RESTORE_CI(b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc"), ci); 01031 01032 if (NIL_P(b) || !rb_obj_is_proc(b)) { 01033 rb_raise(rb_eTypeError, 01034 "wrong argument type %s (expected Proc)", 01035 rb_obj_classname(proc)); 01036 } 01037 proc = b; 01038 } 01039 GetProcPtr(proc, po); 01040 ci->blockptr = &po->block; 01041 RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp)->proc = proc; 01042 } 01043 } 01044 else if (ci->blockiseq != 0) { /* likely */ 01045 ci->blockptr = RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp); 01046 ci->blockptr->iseq = ci->blockiseq; 01047 ci->blockptr->proc = 0; 01048 } 01049 01050 /* expand top of stack? */ 01051 01052 if (UNLIKELY(ci->flag & VM_CALL_ARGS_SPLAT)) { 01053 VALUE ary = *(cfp->sp - 1); 01054 VALUE *ptr; 01055 int i; 01056 VALUE tmp; 01057 01058 SAVE_RESTORE_CI(tmp = rb_check_convert_type(ary, T_ARRAY, "Array", "to_a"), ci); 01059 01060 if (NIL_P(tmp)) { 01061 /* do nothing */ 01062 } 01063 else { 01064 long len = RARRAY_LEN(tmp); 01065 ptr = RARRAY_PTR(tmp); 01066 cfp->sp -= 1; 01067 01068 CHECK_VM_STACK_OVERFLOW(cfp, len); 01069 01070 for (i = 0; i < len; i++) { 01071 *cfp->sp++ = ptr[i]; 01072 } 01073 ci->argc += i-1; 01074 } 01075 } 01076 } 01077 01078 static int 01079 separate_symbol(st_data_t key, st_data_t value, st_data_t arg) 01080 { 01081 VALUE *kwdhash = (VALUE *)arg; 01082 01083 if (!SYMBOL_P(key)) kwdhash++; 01084 if (!*kwdhash) *kwdhash = rb_hash_new(); 01085 rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value); 01086 return ST_CONTINUE; 01087 } 01088 01089 static VALUE 01090 extract_keywords(VALUE *orighash) 01091 { 01092 VALUE parthash[2] = {0, 0}; 01093 VALUE hash = *orighash; 01094 01095 if (RHASH_EMPTY_P(hash)) { 01096 *orighash = 0; 01097 return hash; 01098 } 01099 st_foreach(RHASH_TBL(hash), separate_symbol, (st_data_t)&parthash); 01100 *orighash = parthash[1]; 01101 return parthash[0]; 01102 } 01103 01104 static inline int 01105 vm_callee_setup_keyword_arg(const rb_iseq_t *iseq, int argc, int m, VALUE *orig_argv, VALUE *kwd) 01106 { 01107 VALUE keyword_hash, orig_hash; 01108 int i, j; 01109 01110 if (argc > m && 01111 !NIL_P(orig_hash = rb_check_hash_type(orig_argv[argc-1])) && 01112 (keyword_hash = extract_keywords(&orig_hash)) != 0) { 01113 if (!orig_hash) { 01114 argc--; 01115 } 01116 else { 01117 orig_argv[argc-1] = orig_hash; 01118 } 01119 if (iseq->arg_keyword_check) { 01120 for (i = j = 0; i < iseq->arg_keywords; i++) { 01121 if (st_lookup(RHASH_TBL(keyword_hash), ID2SYM(iseq->arg_keyword_table[i]), 0)) j++; 01122 } 01123 if (RHASH_TBL(keyword_hash)->num_entries > (unsigned int) j) { 01124 unknown_keyword_error(iseq, keyword_hash); 01125 } 01126 } 01127 } 01128 else { 01129 keyword_hash = rb_hash_new(); 01130 } 01131 01132 *kwd = keyword_hash; 01133 01134 return argc; 01135 } 01136 01137 static inline int 01138 vm_callee_setup_arg_complex(rb_thread_t *th, rb_call_info_t *ci, const rb_iseq_t *iseq, VALUE *orig_argv) 01139 { 01140 const int m = iseq->argc; 01141 const int opts = iseq->arg_opts - (iseq->arg_opts > 0); 01142 const int min = m + iseq->arg_post_len; 01143 const int max = (iseq->arg_rest == -1) ? m + opts + iseq->arg_post_len : UNLIMITED_ARGUMENTS; 01144 const int orig_argc = ci->argc; 01145 int argc = orig_argc; 01146 VALUE *argv = orig_argv; 01147 VALUE keyword_hash = Qnil; 01148 rb_num_t opt_pc = 0; 01149 01150 th->mark_stack_len = argc + iseq->arg_size; 01151 01152 /* keyword argument */ 01153 if (iseq->arg_keyword != -1) { 01154 argc = vm_callee_setup_keyword_arg(iseq, argc, m, orig_argv, &keyword_hash); 01155 } 01156 01157 /* mandatory */ 01158 if ((argc < min) || (argc > max && max != UNLIMITED_ARGUMENTS)) { 01159 argument_error(iseq, argc, min, max); 01160 } 01161 01162 argv += m; 01163 argc -= m; 01164 01165 /* post arguments */ 01166 if (iseq->arg_post_len) { 01167 if (!(orig_argc < iseq->arg_post_start)) { 01168 VALUE *new_argv = ALLOCA_N(VALUE, argc); 01169 MEMCPY(new_argv, argv, VALUE, argc); 01170 argv = new_argv; 01171 } 01172 01173 MEMCPY(&orig_argv[iseq->arg_post_start], &argv[argc -= iseq->arg_post_len], 01174 VALUE, iseq->arg_post_len); 01175 } 01176 01177 /* opt arguments */ 01178 if (iseq->arg_opts) { 01179 if (argc > opts) { 01180 argc -= opts; 01181 argv += opts; 01182 opt_pc = iseq->arg_opt_table[opts]; /* no opt */ 01183 } 01184 else { 01185 int i; 01186 for (i = argc; i<opts; i++) { 01187 orig_argv[i + m] = Qnil; 01188 } 01189 opt_pc = iseq->arg_opt_table[argc]; 01190 argc = 0; 01191 } 01192 } 01193 01194 /* rest arguments */ 01195 if (iseq->arg_rest != -1) { 01196 orig_argv[iseq->arg_rest] = rb_ary_new4(argc, argv); 01197 argc = 0; 01198 } 01199 01200 /* keyword argument */ 01201 if (iseq->arg_keyword != -1) { 01202 orig_argv[iseq->arg_keyword] = keyword_hash; 01203 } 01204 01205 /* block arguments */ 01206 if (iseq->arg_block != -1) { 01207 VALUE blockval = Qnil; 01208 const rb_block_t *blockptr = ci->blockptr; 01209 01210 if (blockptr) { 01211 /* make Proc object */ 01212 if (blockptr->proc == 0) { 01213 rb_proc_t *proc; 01214 blockval = rb_vm_make_proc(th, blockptr, rb_cProc); 01215 GetProcPtr(blockval, proc); 01216 ci->blockptr = &proc->block; 01217 } 01218 else { 01219 blockval = blockptr->proc; 01220 } 01221 } 01222 01223 orig_argv[iseq->arg_block] = blockval; /* Proc or nil */ 01224 } 01225 01226 th->mark_stack_len = 0; 01227 return (int)opt_pc; 01228 } 01229 01230 static VALUE vm_call_iseq_setup_2(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci); 01231 static inline VALUE vm_call_iseq_setup_normal(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci); 01232 static inline VALUE vm_call_iseq_setup_tailcall(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci); 01233 01234 #define VM_CALLEE_SETUP_ARG(th, ci, iseq, argv, is_lambda) \ 01235 if (LIKELY((iseq)->arg_simple & 0x01)) { \ 01236 /* simple check */ \ 01237 if ((ci)->argc != (iseq)->argc) { \ 01238 argument_error((iseq), ((ci)->argc), (iseq)->argc, (iseq)->argc); \ 01239 } \ 01240 (ci)->aux.opt_pc = 0; \ 01241 CI_SET_FASTPATH((ci), UNLIKELY((ci)->flag & VM_CALL_TAILCALL) ? vm_call_iseq_setup_tailcall : vm_call_iseq_setup_normal, !(is_lambda) && !((ci)->me->flag & NOEX_PROTECTED)); \ 01242 } \ 01243 else { \ 01244 (ci)->aux.opt_pc = vm_callee_setup_arg_complex((th), (ci), (iseq), (argv)); \ 01245 } 01246 01247 static VALUE 01248 vm_call_iseq_setup(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01249 { 01250 VM_CALLEE_SETUP_ARG(th, ci, ci->me->def->body.iseq, cfp->sp - ci->argc, 0); 01251 return vm_call_iseq_setup_2(th, cfp, ci); 01252 } 01253 01254 static VALUE 01255 vm_call_iseq_setup_2(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01256 { 01257 if (LIKELY(!(ci->flag & VM_CALL_TAILCALL))) { 01258 return vm_call_iseq_setup_normal(th, cfp, ci); 01259 } 01260 else { 01261 return vm_call_iseq_setup_tailcall(th, cfp, ci); 01262 } 01263 } 01264 01265 static inline VALUE 01266 vm_call_iseq_setup_normal(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01267 { 01268 int i; 01269 VALUE *argv = cfp->sp - ci->argc; 01270 rb_iseq_t *iseq = ci->me->def->body.iseq; 01271 VALUE *sp = argv + iseq->arg_size; 01272 01273 CHECK_VM_STACK_OVERFLOW(cfp, iseq->stack_max); 01274 01275 /* clear local variables */ 01276 for (i = 0; i < iseq->local_size - iseq->arg_size; i++) { 01277 *sp++ = Qnil; 01278 } 01279 01280 vm_push_frame(th, iseq, VM_FRAME_MAGIC_METHOD, ci->recv, ci->defined_class, 01281 VM_ENVVAL_BLOCK_PTR(ci->blockptr), 01282 iseq->iseq_encoded + ci->aux.opt_pc, sp, 0, ci->me); 01283 01284 cfp->sp = argv - 1 /* recv */; 01285 return Qundef; 01286 } 01287 01288 static inline VALUE 01289 vm_call_iseq_setup_tailcall(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01290 { 01291 int i; 01292 VALUE *argv = cfp->sp - ci->argc; 01293 rb_iseq_t *iseq = ci->me->def->body.iseq; 01294 VALUE *src_argv = argv; 01295 VALUE *sp_orig, *sp; 01296 VALUE finish_flag = VM_FRAME_TYPE_FINISH_P(cfp) ? VM_FRAME_FLAG_FINISH : 0; 01297 01298 cfp = th->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp); /* pop cf */ 01299 01300 CHECK_VM_STACK_OVERFLOW(cfp, iseq->stack_max); 01301 RUBY_VM_CHECK_INTS(th); 01302 01303 sp_orig = sp = cfp->sp; 01304 01305 /* push self */ 01306 sp[0] = ci->recv; 01307 sp++; 01308 01309 /* copy arguments */ 01310 for (i=0; i < iseq->arg_size; i++) { 01311 *sp++ = src_argv[i]; 01312 } 01313 01314 /* clear local variables */ 01315 for (i = 0; i < iseq->local_size - iseq->arg_size; i++) { 01316 *sp++ = Qnil; 01317 } 01318 01319 vm_push_frame(th, iseq, VM_FRAME_MAGIC_METHOD | finish_flag, 01320 ci->recv, ci->defined_class, VM_ENVVAL_BLOCK_PTR(ci->blockptr), 01321 iseq->iseq_encoded + ci->aux.opt_pc, sp, 0, ci->me); 01322 01323 cfp->sp = sp_orig; 01324 return Qundef; 01325 } 01326 01327 static VALUE 01328 call_cfunc_m2(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01329 { 01330 return (*func)(recv, rb_ary_new4(argc, argv)); 01331 } 01332 01333 static VALUE 01334 call_cfunc_m1(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01335 { 01336 return (*func)(argc, argv, recv); 01337 } 01338 01339 static VALUE 01340 call_cfunc_0(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01341 { 01342 return (*func)(recv); 01343 } 01344 01345 static VALUE 01346 call_cfunc_1(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01347 { 01348 return (*func)(recv, argv[0]); 01349 } 01350 01351 static VALUE 01352 call_cfunc_2(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01353 { 01354 return (*func)(recv, argv[0], argv[1]); 01355 } 01356 01357 static VALUE 01358 call_cfunc_3(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01359 { 01360 return (*func)(recv, argv[0], argv[1], argv[2]); 01361 } 01362 01363 static VALUE 01364 call_cfunc_4(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01365 { 01366 return (*func)(recv, argv[0], argv[1], argv[2], argv[3]); 01367 } 01368 01369 static VALUE 01370 call_cfunc_5(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01371 { 01372 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4]); 01373 } 01374 01375 static VALUE 01376 call_cfunc_6(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01377 { 01378 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); 01379 } 01380 01381 static VALUE 01382 call_cfunc_7(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01383 { 01384 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]); 01385 } 01386 01387 static VALUE 01388 call_cfunc_8(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01389 { 01390 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]); 01391 } 01392 01393 static VALUE 01394 call_cfunc_9(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01395 { 01396 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]); 01397 } 01398 01399 static VALUE 01400 call_cfunc_10(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01401 { 01402 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]); 01403 } 01404 01405 static VALUE 01406 call_cfunc_11(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01407 { 01408 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]); 01409 } 01410 01411 static VALUE 01412 call_cfunc_12(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01413 { 01414 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]); 01415 } 01416 01417 static VALUE 01418 call_cfunc_13(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01419 { 01420 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12]); 01421 } 01422 01423 static VALUE 01424 call_cfunc_14(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01425 { 01426 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13]); 01427 } 01428 01429 static VALUE 01430 call_cfunc_15(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv) 01431 { 01432 return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14]); 01433 } 01434 01435 #ifndef VM_PROFILE 01436 #define VM_PROFILE 0 01437 #endif 01438 01439 #if VM_PROFILE 01440 static int vm_profile_counter[4]; 01441 #define VM_PROFILE_UP(x) (vm_profile_counter[x]++) 01442 #define VM_PROFILE_ATEXIT() atexit(vm_profile_show_result) 01443 static void vm_profile_show_result(void) 01444 { 01445 fprintf(stderr, "VM Profile results: \n"); 01446 fprintf(stderr, "r->c call: %d\n", vm_profile_counter[0]); 01447 fprintf(stderr, "r->c popf: %d\n", vm_profile_counter[1]); 01448 fprintf(stderr, "c->c call: %d\n", vm_profile_counter[2]); 01449 fprintf(stderr, "r->c popf: %d\n", vm_profile_counter[3]); 01450 } 01451 #else 01452 #define VM_PROFILE_UP(x) 01453 #define VM_PROFILE_ATEXIT() 01454 #endif 01455 01456 static VALUE 01457 vm_call_cfunc_with_frame(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 01458 { 01459 VALUE val; 01460 const rb_method_entry_t *me = ci->me; 01461 const rb_method_cfunc_t *cfunc = &me->def->body.cfunc; 01462 int len = cfunc->argc; 01463 01464 /* don't use `ci' after EXEC_EVENT_HOOK because ci can be override */ 01465 VALUE recv = ci->recv; 01466 VALUE defined_class = ci->defined_class; 01467 rb_block_t *blockptr = ci->blockptr; 01468 int argc = ci->argc; 01469 01470 RUBY_DTRACE_CMETHOD_ENTRY_HOOK(th, me->klass, me->called_id); 01471 EXEC_EVENT_HOOK(th, RUBY_EVENT_C_CALL, recv, me->called_id, me->klass, Qundef); 01472 01473 vm_push_frame(th, 0, VM_FRAME_MAGIC_CFUNC, recv, defined_class, 01474 VM_ENVVAL_BLOCK_PTR(blockptr), 0, th->cfp->sp, 1, me); 01475 01476 if (len >= 0) rb_check_arity(argc, len, len); 01477 01478 reg_cfp->sp -= argc + 1; 01479 VM_PROFILE_UP(0); 01480 val = (*cfunc->invoker)(cfunc->func, recv, argc, reg_cfp->sp + 1); 01481 01482 if (reg_cfp != th->cfp + 1) { 01483 rb_bug("vm_call_cfunc - cfp consistency error"); 01484 } 01485 01486 vm_pop_frame(th); 01487 01488 EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, recv, me->called_id, me->klass, val); 01489 RUBY_DTRACE_CMETHOD_RETURN_HOOK(th, me->klass, me->called_id); 01490 01491 return val; 01492 } 01493 01494 #if OPT_CALL_CFUNC_WITHOUT_FRAME 01495 static VALUE 01496 vm_call_cfunc_latter(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 01497 { 01498 VALUE val; 01499 int argc = ci->argc; 01500 VALUE *argv = STACK_ADDR_FROM_TOP(argc); 01501 const rb_method_cfunc_t *cfunc = &ci->me->def->body.cfunc; 01502 01503 th->passed_ci = ci; 01504 reg_cfp->sp -= argc + 1; 01505 ci->aux.inc_sp = argc + 1; 01506 VM_PROFILE_UP(0); 01507 val = (*cfunc->invoker)(cfunc->func, ci, argv); 01508 01509 /* check */ 01510 if (reg_cfp == th->cfp) { /* no frame push */ 01511 if (UNLIKELY(th->passed_ci != ci)) { 01512 rb_bug("vm_call_cfunc_latter: passed_ci error (ci: %p, passed_ci: %p)", ci, th->passed_ci); 01513 } 01514 th->passed_ci = 0; 01515 } 01516 else { 01517 if (UNLIKELY(reg_cfp != RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp))) { 01518 rb_bug("vm_call_cfunc_latter: cfp consistency error (%p, %p)", reg_cfp, th->cfp+1); 01519 } 01520 vm_pop_frame(th); 01521 VM_PROFILE_UP(1); 01522 } 01523 01524 return val; 01525 } 01526 01527 static VALUE 01528 vm_call_cfunc(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 01529 { 01530 VALUE val; 01531 const rb_method_entry_t *me = ci->me; 01532 int len = me->def->body.cfunc.argc; 01533 VALUE recv = ci->recv; 01534 01535 if (len >= 0) rb_check_arity(ci->argc, len, len); 01536 01537 RUBY_DTRACE_CMETHOD_ENTRY_HOOK(th, me->klass, me->called_id); 01538 EXEC_EVENT_HOOK(th, RUBY_EVENT_C_CALL, recv, me->called_id, me->klass, Qnil); 01539 01540 if (!(ci->me->flag & NOEX_PROTECTED) && 01541 !(ci->flag & VM_CALL_ARGS_SPLAT)) { 01542 CI_SET_FASTPATH(ci, vm_call_cfunc_latter, 1); 01543 } 01544 val = vm_call_cfunc_latter(th, reg_cfp, ci); 01545 01546 EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, recv, me->called_id, me->klass, val); 01547 RUBY_DTRACE_CMETHOD_RETURN_HOOK(th, me->klass, me->called_id); 01548 01549 return val; 01550 } 01551 01552 void 01553 vm_call_cfunc_push_frame(rb_thread_t *th) 01554 { 01555 rb_call_info_t *ci = th->passed_ci; 01556 const rb_method_entry_t *me = ci->me; 01557 th->passed_ci = 0; 01558 01559 vm_push_frame(th, 0, VM_FRAME_MAGIC_CFUNC, ci->recv, ci->defined_class, 01560 VM_ENVVAL_BLOCK_PTR(ci->blockptr), 0, th->cfp->sp + ci->aux.inc_sp, 1, me); 01561 01562 if (ci->call != vm_call_general) { 01563 ci->call = vm_call_cfunc_with_frame; 01564 } 01565 } 01566 #else /* OPT_CALL_CFUNC_WITHOUT_FRAME */ 01567 static VALUE 01568 vm_call_cfunc(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 01569 { 01570 return vm_call_cfunc_with_frame(th, reg_cfp, ci); 01571 } 01572 #endif 01573 01574 static VALUE 01575 vm_call_ivar(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01576 { 01577 VALUE val = vm_getivar(ci->recv, ci->me->def->body.attr.id, 0, ci, 1); 01578 cfp->sp -= 1; 01579 return val; 01580 } 01581 01582 static VALUE 01583 vm_call_attrset(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01584 { 01585 VALUE val = vm_setivar(ci->recv, ci->me->def->body.attr.id, *(cfp->sp - 1), 0, ci, 1); 01586 cfp->sp -= 2; 01587 return val; 01588 } 01589 01590 static inline VALUE 01591 vm_call_bmethod_body(rb_thread_t *th, rb_call_info_t *ci, const VALUE *argv) 01592 { 01593 rb_proc_t *proc; 01594 VALUE val; 01595 01596 RUBY_DTRACE_METHOD_ENTRY_HOOK(th, ci->me->klass, ci->me->called_id); 01597 EXEC_EVENT_HOOK(th, RUBY_EVENT_CALL, ci->recv, ci->me->called_id, ci->me->klass, Qnil); 01598 01599 /* control block frame */ 01600 th->passed_me = ci->me; 01601 GetProcPtr(ci->me->def->body.proc, proc); 01602 val = vm_invoke_proc(th, proc, ci->recv, ci->defined_class, ci->argc, argv, ci->blockptr); 01603 01604 EXEC_EVENT_HOOK(th, RUBY_EVENT_RETURN, ci->recv, ci->me->called_id, ci->me->klass, val); 01605 RUBY_DTRACE_METHOD_RETURN_HOOK(th, ci->me->klass, ci->me->called_id); 01606 01607 return val; 01608 } 01609 01610 static VALUE 01611 vm_call_bmethod(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01612 { 01613 VALUE *argv = ALLOCA_N(VALUE, ci->argc); 01614 MEMCPY(argv, cfp->sp - ci->argc, VALUE, ci->argc); 01615 cfp->sp += - ci->argc - 1; 01616 01617 return vm_call_bmethod_body(th, ci, argv); 01618 } 01619 01620 static 01621 #ifdef _MSC_VER 01622 __forceinline 01623 #else 01624 inline 01625 #endif 01626 VALUE vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci); 01627 01628 static VALUE 01629 vm_call_opt_send(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 01630 { 01631 int i = ci->argc - 1; 01632 VALUE sym; 01633 rb_call_info_t ci_entry; 01634 01635 if (ci->argc == 0) { 01636 rb_raise(rb_eArgError, "no method name given"); 01637 } 01638 01639 ci_entry = *ci; /* copy ci entry */ 01640 ci = &ci_entry; 01641 01642 sym = TOPN(i); 01643 01644 if (SYMBOL_P(sym)) { 01645 ci->mid = SYM2ID(sym); 01646 } 01647 else if (!(ci->mid = rb_check_id(&sym))) { 01648 if (rb_method_basic_definition_p(CLASS_OF(ci->recv), idMethodMissing)) { 01649 VALUE exc = make_no_method_exception(rb_eNoMethodError, NULL, ci->recv, rb_long2int(ci->argc), &TOPN(i)); 01650 rb_exc_raise(exc); 01651 } 01652 ci->mid = rb_to_id(sym); 01653 } 01654 01655 /* shift arguments */ 01656 if (i > 0) { 01657 MEMMOVE(&TOPN(i), &TOPN(i-1), VALUE, i); 01658 } 01659 ci->me = 01660 rb_method_entry_without_refinements(CLASS_OF(ci->recv), 01661 ci->mid, &ci->defined_class); 01662 ci->argc -= 1; 01663 DEC_SP(1); 01664 01665 ci->flag = VM_CALL_FCALL | VM_CALL_OPT_SEND; 01666 01667 return vm_call_method(th, reg_cfp, ci); 01668 } 01669 01670 static VALUE 01671 vm_call_opt_call(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01672 { 01673 rb_proc_t *proc; 01674 int argc = ci->argc; 01675 VALUE *argv = ALLOCA_N(VALUE, argc); 01676 GetProcPtr(ci->recv, proc); 01677 MEMCPY(argv, cfp->sp - argc, VALUE, argc); 01678 cfp->sp -= argc + 1; 01679 01680 return rb_vm_invoke_proc(th, proc, argc, argv, ci->blockptr); 01681 } 01682 01683 static VALUE 01684 vm_call_method_missing(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 01685 { 01686 VALUE *argv = STACK_ADDR_FROM_TOP(ci->argc); 01687 rb_call_info_t ci_entry; 01688 01689 ci_entry.flag = VM_CALL_FCALL | VM_CALL_OPT_SEND; 01690 ci_entry.argc = ci->argc+1; 01691 ci_entry.mid = idMethodMissing; 01692 ci_entry.blockptr = ci->blockptr; 01693 ci_entry.recv = ci->recv; 01694 ci_entry.me = rb_method_entry(CLASS_OF(ci_entry.recv), idMethodMissing, &ci_entry.defined_class); 01695 01696 /* shift arguments: m(a, b, c) #=> method_missing(:m, a, b, c) */ 01697 CHECK_VM_STACK_OVERFLOW(reg_cfp, 1); 01698 if (ci->argc > 0) { 01699 MEMMOVE(argv+1, argv, VALUE, ci->argc); 01700 } 01701 argv[0] = ID2SYM(ci->mid); 01702 INC_SP(1); 01703 01704 th->method_missing_reason = ci->aux.missing_reason; 01705 return vm_call_method(th, reg_cfp, &ci_entry); 01706 } 01707 01708 static inline VALUE 01709 find_refinement(VALUE refinements, VALUE klass) 01710 { 01711 if (NIL_P(refinements)) { 01712 return Qnil; 01713 } 01714 return rb_hash_lookup(refinements, klass); 01715 } 01716 01717 static int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2); 01718 static VALUE vm_call_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci); 01719 01720 static rb_control_frame_t * 01721 current_method_entry(rb_thread_t *th, rb_control_frame_t *cfp) 01722 { 01723 rb_control_frame_t *top_cfp = cfp; 01724 01725 if (cfp->iseq && cfp->iseq->type == ISEQ_TYPE_BLOCK) { 01726 rb_iseq_t *local_iseq = cfp->iseq->local_iseq; 01727 do { 01728 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); 01729 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)) { 01730 /* TODO: orphan block */ 01731 return top_cfp; 01732 } 01733 } while (cfp->iseq != local_iseq); 01734 } 01735 return cfp; 01736 } 01737 01738 static 01739 #ifdef _MSC_VER 01740 __forceinline 01741 #else 01742 inline 01743 #endif 01744 VALUE 01745 vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) 01746 { 01747 int enable_fastpath = 1; 01748 rb_call_info_t ci_temp; 01749 01750 start_method_dispatch: 01751 if (ci->me != 0) { 01752 if ((ci->me->flag == 0)) { 01753 VALUE klass; 01754 01755 normal_method_dispatch: 01756 switch (ci->me->def->type) { 01757 case VM_METHOD_TYPE_ISEQ:{ 01758 CI_SET_FASTPATH(ci, vm_call_iseq_setup, enable_fastpath); 01759 return vm_call_iseq_setup(th, cfp, ci); 01760 } 01761 case VM_METHOD_TYPE_NOTIMPLEMENTED: 01762 case VM_METHOD_TYPE_CFUNC: 01763 CI_SET_FASTPATH(ci, vm_call_cfunc, enable_fastpath); 01764 return vm_call_cfunc(th, cfp, ci); 01765 case VM_METHOD_TYPE_ATTRSET:{ 01766 rb_check_arity(ci->argc, 1, 1); 01767 ci->aux.index = 0; 01768 CI_SET_FASTPATH(ci, vm_call_attrset, enable_fastpath && !(ci->flag & VM_CALL_ARGS_SPLAT)); 01769 return vm_call_attrset(th, cfp, ci); 01770 } 01771 case VM_METHOD_TYPE_IVAR:{ 01772 rb_check_arity(ci->argc, 0, 0); 01773 ci->aux.index = 0; 01774 CI_SET_FASTPATH(ci, vm_call_ivar, enable_fastpath && !(ci->flag & VM_CALL_ARGS_SPLAT)); 01775 return vm_call_ivar(th, cfp, ci); 01776 } 01777 case VM_METHOD_TYPE_MISSING:{ 01778 ci->aux.missing_reason = 0; 01779 CI_SET_FASTPATH(ci, vm_call_method_missing, enable_fastpath); 01780 return vm_call_method_missing(th, cfp, ci); 01781 } 01782 case VM_METHOD_TYPE_BMETHOD:{ 01783 CI_SET_FASTPATH(ci, vm_call_bmethod, enable_fastpath); 01784 return vm_call_bmethod(th, cfp, ci); 01785 } 01786 case VM_METHOD_TYPE_ZSUPER:{ 01787 klass = ci->me->klass; 01788 klass = RCLASS_ORIGIN(klass); 01789 zsuper_method_dispatch: 01790 klass = RCLASS_SUPER(klass); 01791 ci_temp = *ci; 01792 ci = &ci_temp; 01793 01794 ci->me = rb_method_entry(klass, ci->mid, &ci->defined_class); 01795 01796 if (ci->me != 0) { 01797 goto normal_method_dispatch; 01798 } 01799 else { 01800 goto start_method_dispatch; 01801 } 01802 } 01803 case VM_METHOD_TYPE_OPTIMIZED:{ 01804 switch (ci->me->def->body.optimize_type) { 01805 case OPTIMIZED_METHOD_TYPE_SEND: 01806 CI_SET_FASTPATH(ci, vm_call_opt_send, enable_fastpath); 01807 return vm_call_opt_send(th, cfp, ci); 01808 case OPTIMIZED_METHOD_TYPE_CALL: 01809 CI_SET_FASTPATH(ci, vm_call_opt_call, enable_fastpath); 01810 return vm_call_opt_call(th, cfp, ci); 01811 default: 01812 rb_bug("vm_call_method: unsupported optimized method type (%d)", 01813 ci->me->def->body.optimize_type); 01814 } 01815 break; 01816 } 01817 case VM_METHOD_TYPE_UNDEF: 01818 break; 01819 case VM_METHOD_TYPE_REFINED:{ 01820 NODE *cref = rb_vm_get_cref(cfp->iseq, cfp->ep); 01821 VALUE refinements = cref ? cref->nd_refinements : Qnil; 01822 VALUE refinement, defined_class; 01823 rb_method_entry_t *me; 01824 01825 refinement = find_refinement(refinements, 01826 ci->defined_class); 01827 if (NIL_P(refinement)) { 01828 goto no_refinement_dispatch; 01829 } 01830 me = rb_method_entry(refinement, ci->mid, &defined_class); 01831 if (me) { 01832 if (ci->call == vm_call_super_method) { 01833 rb_control_frame_t *top_cfp = current_method_entry(th, cfp); 01834 if (top_cfp->me && 01835 rb_method_definition_eq(me->def, top_cfp->me->def)) { 01836 goto no_refinement_dispatch; 01837 } 01838 } 01839 ci->me = me; 01840 ci->defined_class = defined_class; 01841 if (me->def->type != VM_METHOD_TYPE_REFINED) { 01842 goto start_method_dispatch; 01843 } 01844 } 01845 01846 no_refinement_dispatch: 01847 if (ci->me->def->body.orig_me) { 01848 ci->me = ci->me->def->body.orig_me; 01849 if (UNDEFINED_METHOD_ENTRY_P(ci->me)) { 01850 ci->me = 0; 01851 } 01852 goto start_method_dispatch; 01853 } 01854 else { 01855 klass = ci->me->klass; 01856 goto zsuper_method_dispatch; 01857 } 01858 } 01859 } 01860 rb_bug("vm_call_method: unsupported method type (%d)", ci->me->def->type); 01861 } 01862 else { 01863 int noex_safe; 01864 if (!(ci->flag & VM_CALL_FCALL) && (ci->me->flag & NOEX_MASK) & NOEX_PRIVATE) { 01865 int stat = NOEX_PRIVATE; 01866 01867 if (ci->flag & VM_CALL_VCALL) { 01868 stat |= NOEX_VCALL; 01869 } 01870 ci->aux.missing_reason = stat; 01871 CI_SET_FASTPATH(ci, vm_call_method_missing, 1); 01872 return vm_call_method_missing(th, cfp, ci); 01873 } 01874 else if (!(ci->flag & VM_CALL_OPT_SEND) && (ci->me->flag & NOEX_MASK) & NOEX_PROTECTED) { 01875 enable_fastpath = 0; 01876 if (!rb_obj_is_kind_of(cfp->self, ci->defined_class)) { 01877 ci->aux.missing_reason = NOEX_PROTECTED; 01878 return vm_call_method_missing(th, cfp, ci); 01879 } 01880 else { 01881 goto normal_method_dispatch; 01882 } 01883 } 01884 else if ((noex_safe = NOEX_SAFE(ci->me->flag)) > th->safe_level && (noex_safe > 2)) { 01885 rb_raise(rb_eSecurityError, "calling insecure method: %s", rb_id2name(ci->mid)); 01886 } 01887 else { 01888 goto normal_method_dispatch; 01889 } 01890 } 01891 } 01892 else { 01893 /* method missing */ 01894 int stat = 0; 01895 if (ci->flag & VM_CALL_VCALL) { 01896 stat |= NOEX_VCALL; 01897 } 01898 if (ci->flag & VM_CALL_SUPER) { 01899 stat |= NOEX_SUPER; 01900 } 01901 if (ci->mid == idMethodMissing) { 01902 rb_control_frame_t *reg_cfp = cfp; 01903 VALUE *argv = STACK_ADDR_FROM_TOP(ci->argc); 01904 rb_raise_method_missing(th, ci->argc, argv, ci->recv, stat); 01905 } 01906 else { 01907 ci->aux.missing_reason = stat; 01908 CI_SET_FASTPATH(ci, vm_call_method_missing, 1); 01909 return vm_call_method_missing(th, cfp, ci); 01910 } 01911 } 01912 01913 rb_bug("vm_call_method: unreachable"); 01914 } 01915 01916 static VALUE 01917 vm_call_general(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 01918 { 01919 return vm_call_method(th, reg_cfp, ci); 01920 } 01921 01922 static VALUE 01923 vm_call_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 01924 { 01925 return vm_call_method(th, reg_cfp, ci); 01926 } 01927 01928 /* super */ 01929 01930 static inline VALUE 01931 vm_search_normal_superclass(VALUE klass) 01932 { 01933 if (BUILTIN_TYPE(klass) == T_ICLASS && 01934 FL_TEST(RBASIC(klass)->klass, RMODULE_IS_REFINEMENT)) { 01935 klass = RBASIC(klass)->klass; 01936 } 01937 klass = RCLASS_ORIGIN(klass); 01938 return RCLASS_SUPER(klass); 01939 } 01940 01941 static void 01942 vm_super_outside(void) 01943 { 01944 rb_raise(rb_eNoMethodError, "super called outside of method"); 01945 } 01946 01947 static int 01948 vm_search_superclass(rb_control_frame_t *reg_cfp, rb_iseq_t *iseq, VALUE sigval, rb_call_info_t *ci) 01949 { 01950 while (iseq && !iseq->klass) { 01951 iseq = iseq->parent_iseq; 01952 } 01953 01954 if (iseq == 0) { 01955 return -1; 01956 } 01957 01958 ci->mid = iseq->defined_method_id; 01959 01960 if (iseq != iseq->local_iseq) { 01961 /* defined by Module#define_method() */ 01962 rb_control_frame_t *lcfp = GET_CFP(); 01963 01964 if (!sigval) { 01965 /* zsuper */ 01966 return -2; 01967 } 01968 01969 while (lcfp->iseq != iseq) { 01970 rb_thread_t *th = GET_THREAD(); 01971 VALUE *tep = VM_EP_PREV_EP(lcfp->ep); 01972 while (1) { 01973 lcfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(lcfp); 01974 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, lcfp)) { 01975 return -1; 01976 } 01977 if (lcfp->ep == tep) { 01978 break; 01979 } 01980 } 01981 } 01982 01983 /* temporary measure for [Bug #2420] [Bug #3136] */ 01984 if (!lcfp->me) { 01985 return -1; 01986 } 01987 01988 ci->mid = lcfp->me->def->original_id; 01989 ci->klass = vm_search_normal_superclass(lcfp->klass); 01990 } 01991 else { 01992 ci->klass = vm_search_normal_superclass(reg_cfp->klass); 01993 } 01994 01995 return 0; 01996 } 01997 01998 static void 01999 vm_search_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 02000 { 02001 VALUE current_defined_class; 02002 rb_iseq_t *iseq = GET_ISEQ(); 02003 VALUE sigval = TOPN(ci->argc); 02004 02005 current_defined_class = GET_CFP()->klass; 02006 if (NIL_P(current_defined_class)) { 02007 vm_super_outside(); 02008 } 02009 02010 if (!NIL_P(RCLASS_REFINED_CLASS(current_defined_class))) { 02011 current_defined_class = RCLASS_REFINED_CLASS(current_defined_class); 02012 } 02013 02014 if (!FL_TEST(current_defined_class, RMODULE_INCLUDED_INTO_REFINEMENT) && 02015 !rb_obj_is_kind_of(ci->recv, current_defined_class)) { 02016 VALUE m = RB_TYPE_P(current_defined_class, T_ICLASS) ? 02017 RBASIC(current_defined_class)->klass : current_defined_class; 02018 02019 rb_raise(rb_eTypeError, 02020 "self has wrong type to call super in this context: " 02021 "%s (expected %s)", 02022 rb_obj_classname(ci->recv), rb_class2name(m)); 02023 } 02024 02025 switch (vm_search_superclass(GET_CFP(), iseq, sigval, ci)) { 02026 case -1: 02027 vm_super_outside(); 02028 case -2: 02029 rb_raise(rb_eRuntimeError, 02030 "implicit argument passing of super from method defined" 02031 " by define_method() is not supported." 02032 " Specify all arguments explicitly."); 02033 } 02034 if (!ci->klass) { 02035 /* bound instance method of module */ 02036 ci->aux.missing_reason = NOEX_SUPER; 02037 CI_SET_FASTPATH(ci, vm_call_method_missing, 1); 02038 return; 02039 } 02040 02041 /* TODO: use inline cache */ 02042 ci->me = rb_method_entry(ci->klass, ci->mid, &ci->defined_class); 02043 ci->call = vm_call_super_method; 02044 02045 while (iseq && !iseq->klass) { 02046 iseq = iseq->parent_iseq; 02047 } 02048 02049 if (ci->me && ci->me->def->type == VM_METHOD_TYPE_ISEQ && ci->me->def->body.iseq == iseq) { 02050 ci->klass = RCLASS_SUPER(ci->defined_class); 02051 ci->me = rb_method_entry(ci->klass, ci->mid, &ci->defined_class); 02052 } 02053 } 02054 02055 /* yield */ 02056 02057 static inline int 02058 block_proc_is_lambda(const VALUE procval) 02059 { 02060 rb_proc_t *proc; 02061 02062 if (procval) { 02063 GetProcPtr(procval, proc); 02064 return proc->is_lambda; 02065 } 02066 else { 02067 return 0; 02068 } 02069 } 02070 02071 static inline VALUE 02072 vm_yield_with_cfunc(rb_thread_t *th, const rb_block_t *block, 02073 VALUE self, int argc, const VALUE *argv, 02074 const rb_block_t *blockargptr) 02075 { 02076 NODE *ifunc = (NODE *) block->iseq; 02077 VALUE val, arg, blockarg; 02078 int lambda = block_proc_is_lambda(block->proc); 02079 02080 if (lambda) { 02081 arg = rb_ary_new4(argc, argv); 02082 } 02083 else if (argc == 0) { 02084 arg = Qnil; 02085 } 02086 else { 02087 arg = argv[0]; 02088 } 02089 02090 if (blockargptr) { 02091 if (blockargptr->proc) { 02092 blockarg = blockargptr->proc; 02093 } 02094 else { 02095 blockarg = rb_vm_make_proc(th, blockargptr, rb_cProc); 02096 } 02097 } 02098 else { 02099 blockarg = Qnil; 02100 } 02101 02102 vm_push_frame(th, (rb_iseq_t *)ifunc, VM_FRAME_MAGIC_IFUNC, self, 02103 0, VM_ENVVAL_PREV_EP_PTR(block->ep), 0, 02104 th->cfp->sp, 1, 0); 02105 02106 val = (*ifunc->nd_cfnc) (arg, ifunc->nd_tval, argc, argv, blockarg); 02107 02108 th->cfp++; 02109 return val; 02110 } 02111 02112 02113 /*-- 02114 * @brief on supplied all of optional, rest and post parameters. 02115 * @pre iseq is block style (not lambda style) 02116 */ 02117 static inline int 02118 vm_yield_setup_block_args_complex(rb_thread_t *th, const rb_iseq_t *iseq, 02119 int argc, VALUE *argv) 02120 { 02121 rb_num_t opt_pc = 0; 02122 int i; 02123 const int m = iseq->argc; 02124 const int r = iseq->arg_rest; 02125 int len = iseq->arg_post_len; 02126 int start = iseq->arg_post_start; 02127 int rsize = argc > m ? argc - m : 0; /* # of arguments which did not consumed yet */ 02128 int psize = rsize > len ? len : rsize; /* # of post arguments */ 02129 int osize = 0; /* # of opt arguments */ 02130 VALUE ary; 02131 02132 /* reserves arguments for post parameters */ 02133 rsize -= psize; 02134 02135 if (iseq->arg_opts) { 02136 const int opts = iseq->arg_opts - 1; 02137 if (rsize > opts) { 02138 osize = opts; 02139 opt_pc = iseq->arg_opt_table[opts]; 02140 } 02141 else { 02142 osize = rsize; 02143 opt_pc = iseq->arg_opt_table[rsize]; 02144 } 02145 } 02146 rsize -= osize; 02147 02148 if (0) { 02149 printf(" argc: %d\n", argc); 02150 printf(" len: %d\n", len); 02151 printf("start: %d\n", start); 02152 printf("rsize: %d\n", rsize); 02153 } 02154 02155 if (r == -1) { 02156 /* copy post argument */ 02157 MEMMOVE(&argv[start], &argv[m+osize], VALUE, psize); 02158 } 02159 else { 02160 ary = rb_ary_new4(rsize, &argv[r]); 02161 02162 /* copy post argument */ 02163 MEMMOVE(&argv[start], &argv[m+rsize+osize], VALUE, psize); 02164 argv[r] = ary; 02165 } 02166 02167 for (i=psize; i<len; i++) { 02168 argv[start + i] = Qnil; 02169 } 02170 02171 return (int)opt_pc; 02172 } 02173 02174 static inline int 02175 vm_yield_setup_block_args(rb_thread_t *th, const rb_iseq_t * iseq, 02176 int orig_argc, VALUE *argv, 02177 const rb_block_t *blockptr) 02178 { 02179 int i; 02180 int argc = orig_argc; 02181 const int m = iseq->argc; 02182 VALUE ary, arg0; 02183 VALUE keyword_hash = Qnil; 02184 int opt_pc = 0; 02185 02186 th->mark_stack_len = argc; 02187 02188 /* 02189 * yield [1, 2] 02190 * => {|a|} => a = [1, 2] 02191 * => {|a, b|} => a, b = [1, 2] 02192 */ 02193 arg0 = argv[0]; 02194 if (!(iseq->arg_simple & 0x02) && /* exclude {|a|} */ 02195 ((m + iseq->arg_post_len) > 0 || /* positional arguments exist */ 02196 iseq->arg_opts > 2 || /* multiple optional arguments exist */ 02197 iseq->arg_keyword != -1 || /* any keyword arguments */ 02198 0) && 02199 argc == 1 && !NIL_P(ary = rb_check_array_type(arg0))) { /* rhs is only an array */ 02200 th->mark_stack_len = argc = RARRAY_LENINT(ary); 02201 02202 CHECK_VM_STACK_OVERFLOW(th->cfp, argc); 02203 02204 MEMCPY(argv, RARRAY_PTR(ary), VALUE, argc); 02205 } 02206 else { 02207 /* vm_push_frame current argv is at the top of sp because vm_invoke_block 02208 * set sp at the first element of argv. 02209 * Therefore when rb_check_array_type(arg0) called to_ary and called to_ary 02210 * or method_missing run vm_push_frame, it initializes local variables. 02211 * see also https://bugs.ruby-lang.org/issues/8484 02212 */ 02213 argv[0] = arg0; 02214 } 02215 02216 /* keyword argument */ 02217 if (iseq->arg_keyword != -1) { 02218 argc = vm_callee_setup_keyword_arg(iseq, argc, m, argv, &keyword_hash); 02219 } 02220 02221 for (i=argc; i<m; i++) { 02222 argv[i] = Qnil; 02223 } 02224 02225 if (iseq->arg_rest == -1 && iseq->arg_opts == 0) { 02226 const int arg_size = iseq->arg_size; 02227 if (arg_size < argc) { 02228 /* 02229 * yield 1, 2 02230 * => {|a|} # truncate 02231 */ 02232 th->mark_stack_len = argc = arg_size; 02233 } 02234 } 02235 else { 02236 int r = iseq->arg_rest; 02237 02238 if (iseq->arg_post_len || 02239 iseq->arg_opts) { /* TODO: implement simple version for (iseq->arg_post_len==0 && iseq->arg_opts > 0) */ 02240 opt_pc = vm_yield_setup_block_args_complex(th, iseq, argc, argv); 02241 } 02242 else { 02243 if (argc < r) { 02244 /* yield 1 02245 * => {|a, b, *r|} 02246 */ 02247 for (i=argc; i<r; i++) { 02248 argv[i] = Qnil; 02249 } 02250 argv[r] = rb_ary_new(); 02251 } 02252 else { 02253 argv[r] = rb_ary_new4(argc-r, &argv[r]); 02254 } 02255 } 02256 02257 th->mark_stack_len = iseq->arg_size; 02258 } 02259 02260 /* keyword argument */ 02261 if (iseq->arg_keyword != -1) { 02262 argv[iseq->arg_keyword] = keyword_hash; 02263 } 02264 02265 /* {|&b|} */ 02266 if (iseq->arg_block != -1) { 02267 VALUE procval = Qnil; 02268 02269 if (blockptr) { 02270 if (blockptr->proc == 0) { 02271 procval = rb_vm_make_proc(th, blockptr, rb_cProc); 02272 } 02273 else { 02274 procval = blockptr->proc; 02275 } 02276 } 02277 02278 argv[iseq->arg_block] = procval; 02279 } 02280 02281 th->mark_stack_len = 0; 02282 return opt_pc; 02283 } 02284 02285 static inline int 02286 vm_yield_setup_args(rb_thread_t * const th, const rb_iseq_t *iseq, 02287 int argc, VALUE *argv, const rb_block_t *blockptr, int lambda) 02288 { 02289 if (0) { /* for debug */ 02290 printf(" argc: %d\n", argc); 02291 printf("iseq argc: %d\n", iseq->argc); 02292 printf("iseq opts: %d\n", iseq->arg_opts); 02293 printf("iseq rest: %d\n", iseq->arg_rest); 02294 printf("iseq post: %d\n", iseq->arg_post_len); 02295 printf("iseq blck: %d\n", iseq->arg_block); 02296 printf("iseq smpl: %d\n", iseq->arg_simple); 02297 printf(" lambda: %s\n", lambda ? "true" : "false"); 02298 } 02299 02300 if (lambda) { 02301 /* call as method */ 02302 rb_call_info_t ci_entry; 02303 ci_entry.flag = 0; 02304 ci_entry.argc = argc; 02305 ci_entry.blockptr = (rb_block_t *)blockptr; 02306 VM_CALLEE_SETUP_ARG(th, &ci_entry, iseq, argv, 1); 02307 return ci_entry.aux.opt_pc; 02308 } 02309 else { 02310 return vm_yield_setup_block_args(th, iseq, argc, argv, blockptr); 02311 } 02312 } 02313 02314 static VALUE 02315 vm_invoke_block(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci) 02316 { 02317 const rb_block_t *block = VM_CF_BLOCK_PTR(reg_cfp); 02318 rb_iseq_t *iseq; 02319 VALUE type = GET_ISEQ()->local_iseq->type; 02320 02321 if ((type != ISEQ_TYPE_METHOD && type != ISEQ_TYPE_CLASS) || block == 0) { 02322 rb_vm_localjump_error("no block given (yield)", Qnil, 0); 02323 } 02324 iseq = block->iseq; 02325 02326 if (UNLIKELY(ci->flag & VM_CALL_ARGS_SPLAT)) { 02327 vm_caller_setup_args(th, GET_CFP(), ci); 02328 } 02329 02330 if (BUILTIN_TYPE(iseq) != T_NODE) { 02331 int opt_pc; 02332 const int arg_size = iseq->arg_size; 02333 VALUE * const rsp = GET_SP() - ci->argc; 02334 SET_SP(rsp); 02335 02336 CHECK_VM_STACK_OVERFLOW(GET_CFP(), iseq->stack_max); 02337 opt_pc = vm_yield_setup_args(th, iseq, ci->argc, rsp, 0, block_proc_is_lambda(block->proc)); 02338 02339 vm_push_frame(th, iseq, VM_FRAME_MAGIC_BLOCK, block->self, 02340 block->klass, 02341 VM_ENVVAL_PREV_EP_PTR(block->ep), 02342 iseq->iseq_encoded + opt_pc, 02343 rsp + arg_size, 02344 iseq->local_size - arg_size, 0); 02345 02346 return Qundef; 02347 } 02348 else { 02349 VALUE val = vm_yield_with_cfunc(th, block, block->self, ci->argc, STACK_ADDR_FROM_TOP(ci->argc), 0); 02350 POPN(ci->argc); /* TODO: should put before C/yield? */ 02351 return val; 02352 } 02353 } 02354
1.7.6.1