Ruby  2.4.2p198(2017-09-14revision59899)
vm_insnhelper.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  vm_insnhelper.c - instruction helper functions.
4 
5  $Author: nagachika $
6 
7  Copyright (C) 2007 Koichi Sasada
8 
9 **********************************************************************/
10 
11 /* finish iseq array */
12 #include "insns.inc"
13 #include <math.h>
14 #include "constant.h"
15 #include "internal.h"
16 #include "probes.h"
17 #include "probes_helper.h"
18 #include "ruby/config.h"
19 
20 /* control stack frame */
21 
23 
24 VALUE
26 {
28  rb_obj_copy_ivar(e, exc);
29  return e;
30 }
31 
32 static void
34 {
36 }
37 
38 #if VM_CHECK_MODE > 0
39 static int
40 callable_class_p(VALUE klass)
41 {
42 #if VM_CHECK_MODE >= 2
43  if (!klass) return FALSE;
44  switch (RB_BUILTIN_TYPE(klass)) {
45  case T_ICLASS:
46  if (!RB_TYPE_P(RCLASS_SUPER(klass), T_MODULE)) break;
47  case T_MODULE:
48  return TRUE;
49  }
50  while (klass) {
51  if (klass == rb_cBasicObject) {
52  return TRUE;
53  }
54  klass = RCLASS_SUPER(klass);
55  }
56  return FALSE;
57 #else
58  return klass != 0;
59 #endif
60 }
61 
62 static int
63 callable_method_entry_p(const rb_callable_method_entry_t *me)
64 {
65  if (me == NULL || callable_class_p(me->defined_class)) {
66  return TRUE;
67  }
68  else {
69  return FALSE;
70  }
71 }
72 
73 static void
74 vm_check_frame_detail(VALUE type, int req_block, int req_me, int req_cref, VALUE specval, VALUE cref_or_me, int is_cframe, const rb_iseq_t *iseq)
75 {
76  unsigned int magic = (unsigned int)(type & VM_FRAME_MAGIC_MASK);
77  enum imemo_type cref_or_me_type = imemo_env; /* impossible value */
78 
79  if (RB_TYPE_P(cref_or_me, T_IMEMO)) {
80  cref_or_me_type = imemo_type(cref_or_me);
81  }
82  if (type & VM_FRAME_FLAG_BMETHOD) {
83  req_me = TRUE;
84  }
85 
86  if (req_block && (type & VM_ENV_FLAG_LOCAL) == 0) {
87  rb_bug("vm_push_frame: specval (%p) should be a block_ptr on %x frame", (void *)specval, magic);
88  }
89  if (!req_block && (type & VM_ENV_FLAG_LOCAL) != 0) {
90  rb_bug("vm_push_frame: specval (%p) should not be a block_ptr on %x frame", (void *)specval, magic);
91  }
92 
93  if (req_me) {
94  if (cref_or_me_type != imemo_ment) {
95  rb_bug("vm_push_frame: (%s) should be method entry on %x frame", rb_obj_info(cref_or_me), magic);
96  }
97  }
98  else {
99  if (req_cref && cref_or_me_type != imemo_cref) {
100  rb_bug("vm_push_frame: (%s) should be CREF on %x frame", rb_obj_info(cref_or_me), magic);
101  }
102  else { /* cref or Qfalse */
103  if (cref_or_me != Qfalse && cref_or_me_type != imemo_cref) {
104  if ((magic == VM_FRAME_MAGIC_LAMBDA || magic == VM_FRAME_MAGIC_IFUNC) && (cref_or_me_type == imemo_ment)) {
105  /* ignore */
106  }
107  else {
108  rb_bug("vm_push_frame: (%s) should be false or cref on %x frame", rb_obj_info(cref_or_me), magic);
109  }
110  }
111  }
112  }
113 
114  if (cref_or_me_type == imemo_ment) {
115  const rb_callable_method_entry_t *me = (const rb_callable_method_entry_t *)cref_or_me;
116 
117  if (!callable_method_entry_p(me)) {
118  rb_bug("vm_push_frame: ment (%s) should be callable on %x frame.", rb_obj_info(cref_or_me), magic);
119  }
120  }
121 
122  if ((type & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY) {
123  VM_ASSERT(iseq == NULL ||
124  RUBY_VM_NORMAL_ISEQ_P(iseq) /* argument error. it shold be fixed */);
125  }
126  else {
127  VM_ASSERT(is_cframe == !RUBY_VM_NORMAL_ISEQ_P(iseq));
128  }
129 }
130 
131 static void
132 vm_check_frame(VALUE type,
133  VALUE specval,
134  VALUE cref_or_me,
135  const rb_iseq_t *iseq)
136 {
137  VALUE given_magic = type & VM_FRAME_MAGIC_MASK;
138  VM_ASSERT(FIXNUM_P(type));
139 
140 #define CHECK(magic, req_block, req_me, req_cref, is_cframe) \
141  case magic: \
142  vm_check_frame_detail(type, req_block, req_me, req_cref, \
143  specval, cref_or_me, is_cframe, iseq); \
144  break
145  switch (given_magic) {
146  /* BLK ME CREF CFRAME */
158  default:
159  rb_bug("vm_push_frame: unknown type (%x)", (unsigned int)given_magic);
160  }
161 #undef CHECK
162 }
163 #else
164 #define vm_check_frame(a, b, c, d)
165 #endif /* VM_CHECK_MODE > 0 */
166 
167 static inline rb_control_frame_t *
169  const rb_iseq_t *iseq,
170  VALUE type,
171  VALUE self,
172  VALUE specval,
173  VALUE cref_or_me,
174  const VALUE *pc,
175  VALUE *sp,
176  int local_size,
177  int stack_max)
178 {
179  rb_control_frame_t *const cfp = th->cfp - 1;
180  int i;
181 
182  vm_check_frame(type, specval, cref_or_me, iseq);
183  VM_ASSERT(local_size >= 0);
184 
185  /* check stack overflow */
186  CHECK_VM_STACK_OVERFLOW0(cfp, sp, local_size + stack_max);
187 
188  th->cfp = cfp;
189 
190  /* setup new frame */
191  cfp->pc = (VALUE *)pc;
192  cfp->iseq = (rb_iseq_t *)iseq;
193  cfp->self = self;
194  cfp->block_code = NULL;
195 
196  /* setup vm value stack */
197 
198  /* initialize local variables */
199  for (i=0; i < local_size; i++) {
200  *sp++ = Qnil;
201  }
202 
203  /* setup ep with managing data */
207  *sp++ = cref_or_me; /* ep[-2] / Qnil or T_IMEMO(cref) or T_IMEMO(ment) */
208  *sp++ = specval /* ep[-1] / block handler or prev env ptr */;
209  *sp = type; /* ep[-0] / ENV_FLAGS */
210 
211  cfp->ep = sp;
212  cfp->sp = sp + 1;
213 
214 #if VM_DEBUG_BP_CHECK
215  cfp->bp_check = sp + 1;
216 #endif
217 
218  if (VMDEBUG == 2) {
219  SDR();
220  }
221 
222  return cfp;
223 }
224 
227  const rb_iseq_t *iseq,
228  VALUE type,
229  VALUE self,
230  VALUE specval,
231  VALUE cref_or_me,
232  const VALUE *pc,
233  VALUE *sp,
234  int local_size,
235  int stack_max)
236 {
237  return vm_push_frame(th, iseq, type, self, specval, cref_or_me, pc, sp, local_size, stack_max);
238 }
239 
240 /* return TRUE if the frame is finished */
241 static inline int
243 {
244  VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
245 
247  if (VMDEBUG == 2) SDR();
248 
250 
251  return flags & VM_FRAME_FLAG_FINISH;
252 }
253 
254 void
256 {
257  vm_pop_frame(th, th->cfp, th->cfp->ep);
258 }
259 
260 /* method dispatch */
261 static inline VALUE
262 rb_arity_error_new(int argc, int min, int max)
263 {
264  VALUE err_mess = 0;
265  if (min == max) {
266  err_mess = rb_sprintf("wrong number of arguments (given %d, expected %d)", argc, min);
267  }
268  else if (max == UNLIMITED_ARGUMENTS) {
269  err_mess = rb_sprintf("wrong number of arguments (given %d, expected %d+)", argc, min);
270  }
271  else {
272  err_mess = rb_sprintf("wrong number of arguments (given %d, expected %d..%d)", argc, min, max);
273  }
274  return rb_exc_new3(rb_eArgError, err_mess);
275 }
276 
277 void
278 rb_error_arity(int argc, int min, int max)
279 {
280  rb_exc_raise(rb_arity_error_new(argc, min, max));
281 }
282 
283 /* lvar */
284 
285 NOINLINE(static void vm_env_write_slowpath(const VALUE *ep, int index, VALUE v));
286 
287 static void
288 vm_env_write_slowpath(const VALUE *ep, int index, VALUE v)
289 {
290  /* remember env value forcely */
292  VM_FORCE_WRITE(&ep[index], v);
294 }
295 
296 static inline void
297 vm_env_write(const VALUE *ep, int index, VALUE v)
298 {
299  VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
300  if (LIKELY((flags & VM_ENV_FLAG_WB_REQUIRED) == 0)) {
301  VM_STACK_ENV_WRITE(ep, index, v);
302  }
303  else {
304  vm_env_write_slowpath(ep, index, v);
305  }
306 }
307 
308 void
309 rb_vm_env_write(const VALUE *ep, int index, VALUE v)
310 {
311  vm_env_write(ep, index, v);
312 }
313 
314 
315 /* svar */
316 
317 #if VM_CHECK_MODE > 0
318 static int
319 vm_svar_valid_p(VALUE svar)
320 {
321  if (RB_TYPE_P((VALUE)svar, T_IMEMO)) {
322  switch (imemo_type(svar)) {
323  case imemo_svar:
324  case imemo_cref:
325  case imemo_ment:
326  return TRUE;
327  default:
328  break;
329  }
330  }
331  rb_bug("vm_svar_valid_p: unknown type: %s", rb_obj_info(svar));
332  return FALSE;
333 }
334 #endif
335 
336 static inline struct vm_svar *
337 lep_svar(rb_thread_t *th, const VALUE *lep)
338 {
339  VALUE svar;
340 
341  if (lep && (th == NULL || th->root_lep != lep)) {
342  svar = lep[VM_ENV_DATA_INDEX_ME_CREF];
343  }
344  else {
345  svar = th->root_svar;
346  }
347 
348  VM_ASSERT(svar == Qfalse || vm_svar_valid_p(svar));
349 
350  return (struct vm_svar *)svar;
351 }
352 
353 static inline void
354 lep_svar_write(rb_thread_t *th, const VALUE *lep, const struct vm_svar *svar)
355 {
356  VM_ASSERT(vm_svar_valid_p((VALUE)svar));
357 
358  if (lep && (th == NULL || th->root_lep != lep)) {
360  }
361  else {
362  RB_OBJ_WRITE(th->self, &th->root_svar, svar);
363  }
364 }
365 
366 static VALUE
368 {
369  const struct vm_svar *svar = lep_svar(th, lep);
370 
371  if ((VALUE)svar == Qfalse || imemo_type((VALUE)svar) != imemo_svar) return Qnil;
372 
373  switch (key) {
374  case VM_SVAR_LASTLINE:
375  return svar->lastline;
376  case VM_SVAR_BACKREF:
377  return svar->backref;
378  default: {
379  const VALUE ary = svar->others;
380 
381  if (NIL_P(ary)) {
382  return Qnil;
383  }
384  else {
385  return rb_ary_entry(ary, key - VM_SVAR_EXTRA_START);
386  }
387  }
388  }
389 }
390 
391 static struct vm_svar *
393 {
394  return (struct vm_svar *)rb_imemo_new(imemo_svar, Qnil, Qnil, Qnil, obj);
395 }
396 
397 static void
399 {
400  struct vm_svar *svar = lep_svar(th, lep);
401 
402  if ((VALUE)svar == Qfalse || imemo_type((VALUE)svar) != imemo_svar) {
403  lep_svar_write(th, lep, svar = svar_new((VALUE)svar));
404  }
405 
406  switch (key) {
407  case VM_SVAR_LASTLINE:
408  RB_OBJ_WRITE(svar, &svar->lastline, val);
409  return;
410  case VM_SVAR_BACKREF:
411  RB_OBJ_WRITE(svar, &svar->backref, val);
412  return;
413  default: {
414  VALUE ary = svar->others;
415 
416  if (NIL_P(ary)) {
417  RB_OBJ_WRITE(svar, &svar->others, ary = rb_ary_new());
418  }
419  rb_ary_store(ary, key - VM_SVAR_EXTRA_START, val);
420  }
421  }
422 }
423 
424 static inline VALUE
426 {
427  VALUE val;
428 
429  if (type == 0) {
430  val = lep_svar_get(th, lep, key);
431  }
432  else {
434 
435  if (type & 0x01) {
436  switch (type >> 1) {
437  case '&':
438  val = rb_reg_last_match(backref);
439  break;
440  case '`':
441  val = rb_reg_match_pre(backref);
442  break;
443  case '\'':
444  val = rb_reg_match_post(backref);
445  break;
446  case '+':
447  val = rb_reg_match_last(backref);
448  break;
449  default:
450  rb_bug("unexpected back-ref");
451  }
452  }
453  else {
454  val = rb_reg_nth_match((int)(type >> 1), backref);
455  }
456  }
457  return val;
458 }
459 
460 PUREFUNC(static rb_callable_method_entry_t *check_method_entry(VALUE obj, int can_be_svar));
462 check_method_entry(VALUE obj, int can_be_svar)
463 {
464  if (obj == Qfalse) return NULL;
465 
466 #if VM_CHECK_MODE > 0
467  if (!RB_TYPE_P(obj, T_IMEMO)) rb_bug("check_method_entry: unknown type: %s", rb_obj_info(obj));
468 #endif
469 
470  switch (imemo_type(obj)) {
471  case imemo_ment:
472  return (rb_callable_method_entry_t *)obj;
473  case imemo_cref:
474  return NULL;
475  case imemo_svar:
476  if (can_be_svar) {
477  return check_method_entry(((struct vm_svar *)obj)->cref_or_me, FALSE);
478  }
479  default:
480 #if VM_CHECK_MODE > 0
481  rb_bug("check_method_entry: svar should not be there:");
482 #endif
483  return NULL;
484  }
485 }
486 
489 {
490  const VALUE *ep = cfp->ep;
492 
493  while (!VM_ENV_LOCAL_P(ep)) {
494  if ((me = check_method_entry(ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE)) != NULL) return me;
495  ep = VM_ENV_PREV_EP(ep);
496  }
497 
499 }
500 
501 static rb_cref_t *
503 {
504  switch (me->def->type) {
505  case VM_METHOD_TYPE_ISEQ:
506  return me->def->body.iseq.cref;
507  default:
508  return NULL;
509  }
510 }
511 
512 #if VM_CHECK_MODE == 0
513 PUREFUNC(static rb_cref_t *check_cref(VALUE, int));
514 #endif
515 static rb_cref_t *
516 check_cref(VALUE obj, int can_be_svar)
517 {
518  if (obj == Qfalse) return NULL;
519 
520 #if VM_CHECK_MODE > 0
521  if (!RB_TYPE_P(obj, T_IMEMO)) rb_bug("check_cref: unknown type: %s", rb_obj_info(obj));
522 #endif
523 
524  switch (imemo_type(obj)) {
525  case imemo_ment:
527  case imemo_cref:
528  return (rb_cref_t *)obj;
529  case imemo_svar:
530  if (can_be_svar) {
531  return check_cref(((struct vm_svar *)obj)->cref_or_me, FALSE);
532  }
533  default:
534 #if VM_CHECK_MODE > 0
535  rb_bug("check_method_entry: svar should not be there:");
536 #endif
537  return NULL;
538  }
539 }
540 
541 static inline rb_cref_t *
542 vm_env_cref(const VALUE *ep)
543 {
544  rb_cref_t *cref;
545 
546  while (!VM_ENV_LOCAL_P(ep)) {
547  if ((cref = check_cref(ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE)) != NULL) return cref;
548  ep = VM_ENV_PREV_EP(ep);
549  }
550 
552 }
553 
554 static int
555 is_cref(const VALUE v, int can_be_svar)
556 {
557  if (RB_TYPE_P(v, T_IMEMO)) {
558  switch (imemo_type(v)) {
559  case imemo_cref:
560  return TRUE;
561  case imemo_svar:
562  if (can_be_svar) return is_cref(((struct vm_svar *)v)->cref_or_me, FALSE);
563  default:
564  break;
565  }
566  }
567  return FALSE;
568 }
569 
570 static int
572 {
573  while (!VM_ENV_LOCAL_P(ep)) {
574  if (is_cref(ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE)) return TRUE;
575  ep = VM_ENV_PREV_EP(ep);
576  }
578 }
579 
580 static rb_cref_t *
581 cref_replace_with_duplicated_cref_each_frame(const VALUE *vptr, int can_be_svar, VALUE parent)
582 {
583  const VALUE v = *vptr;
584  rb_cref_t *cref, *new_cref;
585 
586  if (RB_TYPE_P(v, T_IMEMO)) {
587  switch (imemo_type(v)) {
588  case imemo_cref:
589  cref = (rb_cref_t *)v;
590  new_cref = vm_cref_dup(cref);
591  if (parent) {
592  RB_OBJ_WRITE(parent, vptr, new_cref);
593  }
594  else {
595  VM_FORCE_WRITE(vptr, (VALUE)new_cref);
596  }
597  return (rb_cref_t *)new_cref;
598  case imemo_svar:
599  if (can_be_svar) {
600  return cref_replace_with_duplicated_cref_each_frame((const VALUE *)&((struct vm_svar *)v)->cref_or_me, FALSE, v);
601  }
602  case imemo_ment:
603  rb_bug("cref_replace_with_duplicated_cref_each_frame: unreachable");
604  default:
605  break;
606  }
607  }
608  return FALSE;
609 }
610 
611 static rb_cref_t *
613 {
614  if (vm_env_cref_by_cref(ep)) {
615  rb_cref_t *cref;
616  VALUE envval;
617 
618  while (!VM_ENV_LOCAL_P(ep)) {
619  envval = VM_ENV_ESCAPED_P(ep) ? VM_ENV_ENVVAL(ep) : Qfalse;
621  return cref;
622  }
623  ep = VM_ENV_PREV_EP(ep);
624  }
625  envval = VM_ENV_ESCAPED_P(ep) ? VM_ENV_ENVVAL(ep) : Qfalse;
627  }
628  else {
629  rb_bug("vm_cref_dup: unreachable");
630  }
631 }
632 
633 
634 static rb_cref_t *
636 {
637  rb_cref_t *cref = vm_env_cref(ep);
638 
639  if (cref != NULL) {
640  return cref;
641  }
642  else {
643  rb_bug("rb_vm_get_cref: unreachable");
644  }
645 }
646 
647 static const rb_cref_t *
649 {
650  const rb_cref_t *cref = rb_vm_get_cref(ep);
651  const rb_cref_t *key_cref = cref;
652 
653  while (cref) {
654  if (FL_TEST(CREF_CLASS(cref), FL_SINGLETON)) {
655  return key_cref;
656  }
657  cref = CREF_NEXT(cref);
658  }
659 
660  /* does not include singleton class */
661  return NULL;
662 }
663 
664 void
665 rb_vm_rewrite_cref(rb_cref_t *cref, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr)
666 {
667  rb_cref_t *new_cref;
668 
669  while (cref) {
670  if (CREF_CLASS(cref) == old_klass) {
671  new_cref = vm_cref_new_use_prev(new_klass, METHOD_VISI_UNDEF, FALSE, cref, FALSE);
672  *new_cref_ptr = new_cref;
673  return;
674  }
675  new_cref = vm_cref_new_use_prev(CREF_CLASS(cref), METHOD_VISI_UNDEF, FALSE, cref, FALSE);
676  cref = CREF_NEXT(cref);
677  *new_cref_ptr = new_cref;
678  new_cref_ptr = (rb_cref_t **)&new_cref->next;
679  }
680  *new_cref_ptr = NULL;
681 }
682 
683 static rb_cref_t *
684 vm_cref_push(rb_thread_t *th, VALUE klass, const VALUE *ep, int pushed_by_eval)
685 {
686  rb_cref_t *prev_cref = NULL;
687 
688  if (ep) {
689  prev_cref = vm_env_cref(ep);
690  }
691  else {
693 
694  if (cfp) {
695  prev_cref = vm_env_cref(cfp->ep);
696  }
697  }
698 
699  return vm_cref_new(klass, METHOD_VISI_PUBLIC, FALSE, prev_cref, pushed_by_eval);
700 }
701 
702 static inline VALUE
703 vm_get_cbase(const VALUE *ep)
704 {
705  const rb_cref_t *cref = rb_vm_get_cref(ep);
706  VALUE klass = Qundef;
707 
708  while (cref) {
709  if ((klass = CREF_CLASS(cref)) != 0) {
710  break;
711  }
712  cref = CREF_NEXT(cref);
713  }
714 
715  return klass;
716 }
717 
718 static inline VALUE
720 {
721  const rb_cref_t *cref = rb_vm_get_cref(ep);
722  VALUE klass = Qundef;
723 
724  while (cref) {
725  if (!CREF_PUSHED_BY_EVAL(cref) &&
726  (klass = CREF_CLASS(cref)) != 0) {
727  break;
728  }
729  cref = CREF_NEXT(cref);
730  }
731 
732  return klass;
733 }
734 
735 static inline void
737 {
738  if (!RB_TYPE_P(klass, T_CLASS) && !RB_TYPE_P(klass, T_MODULE)) {
739  rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a class/module", klass);
740  }
741 }
742 
743 static inline void
745 {
746  if (RB_TYPE_P(self, T_MODULE) && FL_TEST(self, RMODULE_IS_REFINEMENT)) {
747  rb_warn("not defined at the refinement, but at the outer class/module");
748  }
749 }
750 
751 static inline VALUE
753 {
754  return klass;
755 }
756 
757 static inline VALUE
758 vm_get_ev_const(rb_thread_t *th, VALUE orig_klass, ID id, int is_defined)
759 {
760  void rb_const_warn_if_deprecated(const rb_const_entry_t *ce, VALUE klass, ID id);
761  VALUE val;
762 
763  if (orig_klass == Qnil) {
764  /* in current lexical scope */
765  const rb_cref_t *root_cref = rb_vm_get_cref(th->cfp->ep);
766  const rb_cref_t *cref;
767  VALUE klass = Qnil;
768 
769  while (root_cref && CREF_PUSHED_BY_EVAL(root_cref)) {
770  root_cref = CREF_NEXT(root_cref);
771  }
772  cref = root_cref;
773  while (cref && CREF_NEXT(cref)) {
774  if (CREF_PUSHED_BY_EVAL(cref)) {
775  klass = Qnil;
776  }
777  else {
778  klass = CREF_CLASS(cref);
779  }
780  cref = CREF_NEXT(cref);
781 
782  if (!NIL_P(klass)) {
783  VALUE av, am = 0;
784  rb_const_entry_t *ce;
785  search_continue:
786  if ((ce = rb_const_lookup(klass, id))) {
787  rb_const_warn_if_deprecated(ce, klass, id);
788  val = ce->value;
789  if (val == Qundef) {
790  if (am == klass) break;
791  am = klass;
792  if (is_defined) return 1;
793  if (rb_autoloading_value(klass, id, &av)) return av;
794  rb_autoload_load(klass, id);
795  goto search_continue;
796  }
797  else {
798  if (is_defined) {
799  return 1;
800  }
801  else {
802  return val;
803  }
804  }
805  }
806  }
807  }
808 
809  /* search self */
810  if (root_cref && !NIL_P(CREF_CLASS(root_cref))) {
811  klass = vm_get_iclass(th->cfp, CREF_CLASS(root_cref));
812  }
813  else {
814  klass = CLASS_OF(th->cfp->self);
815  }
816 
817  if (is_defined) {
818  return rb_const_defined(klass, id);
819  }
820  else {
821  return rb_const_get(klass, id);
822  }
823  }
824  else {
825  vm_check_if_namespace(orig_klass);
826  if (is_defined) {
827  return rb_public_const_defined_from(orig_klass, id);
828  }
829  else {
830  return rb_public_const_get_from(orig_klass, id);
831  }
832  }
833 }
834 
835 static inline VALUE
837 {
838  VALUE klass;
839 
840  if (!cref) {
841  rb_bug("vm_get_cvar_base: no cref");
842  }
843 
844  while (CREF_NEXT(cref) &&
845  (NIL_P(CREF_CLASS(cref)) || FL_TEST(CREF_CLASS(cref), FL_SINGLETON) ||
846  CREF_PUSHED_BY_EVAL(cref))) {
847  cref = CREF_NEXT(cref);
848  }
849  if (!CREF_NEXT(cref)) {
850  rb_warn("class variable access from toplevel");
851  }
852 
853  klass = vm_get_iclass(cfp, CREF_CLASS(cref));
854 
855  if (NIL_P(klass)) {
856  rb_raise(rb_eTypeError, "no class variables available");
857  }
858  return klass;
859 }
860 
861 static VALUE
863 {
864  if (rb_const_defined_at(cbase, id)) return cbase;
865  if (cbase == rb_cObject) {
866  VALUE tmp = RCLASS_SUPER(cbase);
867  while (tmp) {
868  if (rb_const_defined_at(tmp, id)) return tmp;
869  tmp = RCLASS_SUPER(tmp);
870  }
871  }
872  return 0;
873 }
874 
875 #ifndef USE_IC_FOR_IVAR
876 #define USE_IC_FOR_IVAR 1
877 #endif
878 
879 ALWAYS_INLINE(static VALUE vm_getivar(VALUE, ID, IC, struct rb_call_cache *, int));
880 static inline VALUE
881 vm_getivar(VALUE obj, ID id, IC ic, struct rb_call_cache *cc, int is_attr)
882 {
883 #if USE_IC_FOR_IVAR
884  if (LIKELY(RB_TYPE_P(obj, T_OBJECT))) {
885  VALUE val = Qundef;
886  if (LIKELY(is_attr ? cc->aux.index > 0 : ic->ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass))) {
887  st_index_t index = !is_attr ? ic->ic_value.index : (cc->aux.index - 1);
888  if (LIKELY(index < ROBJECT_NUMIV(obj))) {
889  val = ROBJECT_IVPTR(obj)[index];
890  }
891  undef_check:
892  if (UNLIKELY(val == Qundef)) {
893  if (!is_attr && RTEST(ruby_verbose))
894  rb_warning("instance variable %"PRIsVALUE" not initialized", QUOTE_ID(id));
895  val = Qnil;
896  }
897  return val;
898  }
899  else {
900  st_data_t index;
901  struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
902 
903  if (iv_index_tbl) {
904  if (st_lookup(iv_index_tbl, id, &index)) {
905  if (index < ROBJECT_NUMIV(obj)) {
906  val = ROBJECT_IVPTR(obj)[index];
907  }
908  if (!is_attr) {
909  ic->ic_value.index = index;
910  ic->ic_serial = RCLASS_SERIAL(RBASIC(obj)->klass);
911  }
912  else { /* call_info */
913  cc->aux.index = (int)index + 1;
914  }
915  }
916  }
917  goto undef_check;
918  }
919  }
920 #endif /* USE_IC_FOR_IVAR */
921  if (is_attr)
922  return rb_attr_get(obj, id);
923  return rb_ivar_get(obj, id);
924 }
925 
926 static inline VALUE
927 vm_setivar(VALUE obj, ID id, VALUE val, IC ic, struct rb_call_cache *cc, int is_attr)
928 {
929 #if USE_IC_FOR_IVAR
930  rb_check_frozen(obj);
931 
932  if (LIKELY(RB_TYPE_P(obj, T_OBJECT))) {
933  VALUE klass = RBASIC(obj)->klass;
934  st_data_t index;
935 
936  if (LIKELY(
937  (!is_attr && ic->ic_serial == RCLASS_SERIAL(klass)) ||
938  (is_attr && cc->aux.index > 0))) {
939  VALUE *ptr = ROBJECT_IVPTR(obj);
940  index = !is_attr ? ic->ic_value.index : cc->aux.index-1;
941 
942  if (index < ROBJECT_NUMIV(obj)) {
943  RB_OBJ_WRITE(obj, &ptr[index], val);
944  return val; /* inline cache hit */
945  }
946  }
947  else {
948  struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
949 
950  if (iv_index_tbl && st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
951  if (!is_attr) {
952  ic->ic_value.index = index;
953  ic->ic_serial = RCLASS_SERIAL(klass);
954  }
955  else if (index >= INT_MAX) {
956  rb_raise(rb_eArgError, "too many instance variables");
957  }
958  else {
959  cc->aux.index = (int)(index + 1);
960  }
961  }
962  /* fall through */
963  }
964  }
965 #endif /* USE_IC_FOR_IVAR */
966  return rb_ivar_set(obj, id, val);
967 }
968 
969 static inline VALUE
971 {
972  return vm_getivar(obj, id, ic, 0, 0);
973 }
974 
975 static inline void
977 {
978  vm_setivar(obj, id, val, ic, 0, 0);
979 }
980 
981 static VALUE
983 {
984  /* continue throw */
985 
986  if (FIXNUM_P(err)) {
987  th->state = FIX2INT(err);
988  }
989  else if (SYMBOL_P(err)) {
990  th->state = TAG_THROW;
991  }
992  else if (THROW_DATA_P(err)) {
993  th->state = THROW_DATA_STATE((struct vm_throw_data *)err);
994  }
995  else {
996  th->state = TAG_RAISE;
997  /*th->state = FIX2INT(rb_ivar_get(err, idThrowState));*/
998  }
999  return err;
1000 }
1001 
1002 static VALUE
1003 vm_throw_start(rb_thread_t *const th, rb_control_frame_t *const reg_cfp, enum ruby_tag_type state,
1004  const int flag, const rb_num_t level, const VALUE throwobj)
1005 {
1006  const rb_control_frame_t *escape_cfp = NULL;
1007  const rb_control_frame_t * const eocfp = RUBY_VM_END_CONTROL_FRAME(th); /* end of control frame pointer */
1008 
1009  if (flag != 0) {
1010  /* do nothing */
1011  }
1012  else if (state == TAG_BREAK) {
1013  int is_orphan = 1;
1014  const VALUE *ep = GET_EP();
1015  const rb_iseq_t *base_iseq = GET_ISEQ();
1016  escape_cfp = reg_cfp;
1017 
1018  while (base_iseq->body->type != ISEQ_TYPE_BLOCK) {
1019  if (escape_cfp->iseq->body->type == ISEQ_TYPE_CLASS) {
1020  escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
1021  ep = escape_cfp->ep;
1022  base_iseq = escape_cfp->iseq;
1023  }
1024  else {
1025  ep = VM_ENV_PREV_EP(ep);
1026  base_iseq = base_iseq->body->parent_iseq;
1027  escape_cfp = rb_vm_search_cf_from_ep(th, escape_cfp, ep);
1028  VM_ASSERT(escape_cfp->iseq == base_iseq);
1029  }
1030  }
1031 
1032  if (VM_FRAME_TYPE(escape_cfp) == VM_FRAME_MAGIC_LAMBDA) {
1033  /* lambda{... break ...} */
1034  is_orphan = 0;
1035  state = TAG_RETURN;
1036  }
1037  else {
1038  ep = VM_ENV_PREV_EP(ep);
1039 
1040  while (escape_cfp < eocfp) {
1041  if (escape_cfp->ep == ep) {
1042  const rb_iseq_t *const iseq = escape_cfp->iseq;
1043  const VALUE epc = escape_cfp->pc - iseq->body->iseq_encoded;
1044  const struct iseq_catch_table *const ct = iseq->body->catch_table;
1045  unsigned int i;
1046 
1047  if (!ct) break;
1048  for (i=0; i < ct->size; i++) {
1049  const struct iseq_catch_table_entry * const entry = &ct->entries[i];
1050 
1051  if (entry->type == CATCH_TYPE_BREAK && entry->start < epc && entry->end >= epc) {
1052  if (entry->cont == epc) { /* found! */
1053  is_orphan = 0;
1054  }
1055  break;
1056  }
1057  }
1058  break;
1059  }
1060 
1061  escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
1062  }
1063  }
1064 
1065  if (is_orphan) {
1066  rb_vm_localjump_error("break from proc-closure", throwobj, TAG_BREAK);
1067  }
1068  }
1069  else if (state == TAG_RETRY) {
1070  rb_num_t i;
1071  const VALUE *ep = VM_ENV_PREV_EP(GET_EP());
1072 
1073  for (i = 0; i < level; i++) {
1074  ep = VM_ENV_PREV_EP(ep);
1075  }
1076 
1077  escape_cfp = rb_vm_search_cf_from_ep(th, reg_cfp, ep);
1078  }
1079  else if (state == TAG_RETURN) {
1080  const VALUE *current_ep = GET_EP();
1081  const VALUE *target_lep = VM_EP_LEP(current_ep);
1082  int in_class_frame = 0;
1083  int toplevel = 1;
1084  escape_cfp = reg_cfp;
1085 
1086  while (escape_cfp < eocfp) {
1087  const VALUE *lep = VM_CF_LEP(escape_cfp);
1088 
1089  if (!target_lep) {
1090  target_lep = lep;
1091  }
1092 
1093  if (lep == target_lep &&
1094  VM_FRAME_RUBYFRAME_P(escape_cfp) &&
1095  escape_cfp->iseq->body->type == ISEQ_TYPE_CLASS) {
1096  in_class_frame = 1;
1097  target_lep = 0;
1098  }
1099 
1100  if (lep == target_lep) {
1101  if (VM_FRAME_TYPE(escape_cfp) == VM_FRAME_MAGIC_LAMBDA) {
1102  toplevel = 0;
1103  if (in_class_frame) {
1104  /* lambda {class A; ... return ...; end} */
1105  goto valid_return;
1106  }
1107  else {
1108  const VALUE *tep = current_ep;
1109 
1110  while (target_lep != tep) {
1111  if (escape_cfp->ep == tep) {
1112  /* in lambda */
1113  goto valid_return;
1114  }
1115  tep = VM_ENV_PREV_EP(tep);
1116  }
1117  }
1118  }
1119  else if (VM_FRAME_RUBYFRAME_P(escape_cfp)) {
1120  switch (escape_cfp->iseq->body->type) {
1121  case ISEQ_TYPE_TOP:
1122  case ISEQ_TYPE_MAIN:
1123  if (toplevel) goto valid_return;
1124  break;
1125  case ISEQ_TYPE_EVAL:
1126  case ISEQ_TYPE_CLASS:
1127  toplevel = 0;
1128  break;
1129  default:
1130  break;
1131  }
1132  }
1133  }
1134 
1135  if (escape_cfp->ep == target_lep && escape_cfp->iseq->body->type == ISEQ_TYPE_METHOD) {
1136  goto valid_return;
1137  }
1138 
1139  escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
1140  }
1141  rb_vm_localjump_error("unexpected return", throwobj, TAG_RETURN);
1142 
1143  valid_return:;
1144  /* do nothing */
1145  }
1146  else {
1147  rb_bug("isns(throw): unsupport throw type");
1148  }
1149 
1150  th->state = state;
1151  return (VALUE)THROW_DATA_NEW(throwobj, escape_cfp, state);
1152 }
1153 
1154 static VALUE
1156  rb_num_t throw_state, VALUE throwobj)
1157 {
1158  const int state = (int)(throw_state & VM_THROW_STATE_MASK);
1159  const int flag = (int)(throw_state & VM_THROW_NO_ESCAPE_FLAG);
1160  const rb_num_t level = throw_state >> VM_THROW_LEVEL_SHIFT;
1161 
1162  if (state != 0) {
1163  return vm_throw_start(th, reg_cfp, state, flag, level, throwobj);
1164  }
1165  else {
1166  return vm_throw_continue(th, throwobj);
1167  }
1168 }
1169 
1170 static inline void
1172 {
1173  int is_splat = flag & 0x01;
1174  rb_num_t space_size = num + is_splat;
1175  VALUE *base = cfp->sp;
1176  const VALUE *ptr;
1177  rb_num_t len;
1178 
1179  if (!RB_TYPE_P(ary, T_ARRAY)) {
1180  ary = rb_ary_to_ary(ary);
1181  }
1182 
1183  cfp->sp += space_size;
1184 
1185  ptr = RARRAY_CONST_PTR(ary);
1186  len = (rb_num_t)RARRAY_LEN(ary);
1187 
1188  if (flag & 0x02) {
1189  /* post: ..., nil ,ary[-1], ..., ary[0..-num] # top */
1190  rb_num_t i = 0, j;
1191 
1192  if (len < num) {
1193  for (i=0; i<num-len; i++) {
1194  *base++ = Qnil;
1195  }
1196  }
1197  for (j=0; i<num; i++, j++) {
1198  VALUE v = ptr[len - j - 1];
1199  *base++ = v;
1200  }
1201  if (is_splat) {
1202  *base = rb_ary_new4(len - j, ptr);
1203  }
1204  }
1205  else {
1206  /* normal: ary[num..-1], ary[num-2], ary[num-3], ..., ary[0] # top */
1207  rb_num_t i;
1208  VALUE *bptr = &base[space_size - 1];
1209 
1210  for (i=0; i<num; i++) {
1211  if (len <= i) {
1212  for (; i<num; i++) {
1213  *bptr-- = Qnil;
1214  }
1215  break;
1216  }
1217  *bptr-- = ptr[i];
1218  }
1219  if (is_splat) {
1220  if (num > len) {
1221  *bptr = rb_ary_new();
1222  }
1223  else {
1224  *bptr = rb_ary_new4(len - num, ptr + num);
1225  }
1226  }
1227  }
1228  RB_GC_GUARD(ary);
1229 }
1230 
1231 static VALUE vm_call_general(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc);
1232 
1233 static void
1234 vm_search_method(const struct rb_call_info *ci, struct rb_call_cache *cc, VALUE recv)
1235 {
1236  VALUE klass = CLASS_OF(recv);
1237 
1238 #if OPT_INLINE_METHOD_CACHE
1239  if (LIKELY(GET_GLOBAL_METHOD_STATE() == cc->method_state && RCLASS_SERIAL(klass) == cc->class_serial)) {
1240  /* cache hit! */
1241  VM_ASSERT(cc->call != NULL);
1242  return;
1243  }
1244 #endif
1245 
1246  cc->me = rb_callable_method_entry(klass, ci->mid);
1247  VM_ASSERT(callable_method_entry_p(cc->me));
1248  cc->call = vm_call_general;
1249 #if OPT_INLINE_METHOD_CACHE
1251  cc->class_serial = RCLASS_SERIAL(klass);
1252 #endif
1253 }
1254 
1255 static inline int
1257 {
1258  if (me && me->def->type == VM_METHOD_TYPE_CFUNC &&
1259  me->def->body.cfunc.func == func) {
1260  return 1;
1261  }
1262  else {
1263  return 0;
1264  }
1265 }
1266 
1267 static
1268 #ifndef NO_BIG_INLINE
1269 inline
1270 #endif
1271 VALUE
1273 {
1274  if (FIXNUM_2_P(recv, obj) &&
1276  return (recv == obj) ? Qtrue : Qfalse;
1277  }
1278  else if (FLONUM_2_P(recv, obj) &&
1280  return (recv == obj) ? Qtrue : Qfalse;
1281  }
1282  else if (!SPECIAL_CONST_P(recv) && !SPECIAL_CONST_P(obj)) {
1283  if (RBASIC_CLASS(recv) == rb_cFloat &&
1284  RBASIC_CLASS(obj) == rb_cFloat &&
1286  double a = RFLOAT_VALUE(recv);
1287  double b = RFLOAT_VALUE(obj);
1288 
1289  if (isnan(a) || isnan(b)) {
1290  return Qfalse;
1291  }
1292  return (a == b) ? Qtrue : Qfalse;
1293  }
1294  else if (RBASIC_CLASS(recv) == rb_cString &&
1295  RBASIC_CLASS(obj) == rb_cString &&
1297  return rb_str_equal(recv, obj);
1298  }
1299  }
1300 
1301  {
1302  vm_search_method(ci, cc, recv);
1303 
1304  if (check_cfunc(cc->me, rb_obj_equal)) {
1305  return recv == obj ? Qtrue : Qfalse;
1306  }
1307  }
1308 
1309  return Qundef;
1310 }
1311 
1312 VALUE
1314 {
1315  struct rb_call_info ci;
1316  struct rb_call_cache cc;
1317 
1318  ci.mid = idEq;
1319  cc.method_state = 0;
1320  cc.class_serial = 0;
1321  cc.me = NULL;
1322  return opt_eq_func(obj1, obj2, &ci, &cc);
1323 }
1324 
1325 static VALUE vm_call0(rb_thread_t*, VALUE, ID, int, const VALUE*, const rb_callable_method_entry_t *);
1326 
1327 static VALUE
1328 check_match(VALUE pattern, VALUE target, enum vm_check_match_type type)
1329 {
1330  switch (type) {
1332  return pattern;
1334  if (!rb_obj_is_kind_of(pattern, rb_cModule)) {
1335  rb_raise(rb_eTypeError, "class or module required for rescue clause");
1336  }
1337  /* fall through */
1338  case VM_CHECKMATCH_TYPE_CASE: {
1340  if (me) {
1341  return vm_call0(GET_THREAD(), pattern, idEqq, 1, &target, me);
1342  }
1343  else {
1344  /* fallback to funcall (e.g. method_missing) */
1345  return rb_funcallv(pattern, idEqq, 1, &target);
1346  }
1347  }
1348  default:
1349  rb_bug("check_match: unreachable");
1350  }
1351 }
1352 
1353 
1354 #if defined(_MSC_VER) && _MSC_VER < 1300
1355 #define CHECK_CMP_NAN(a, b) if (isnan(a) || isnan(b)) return Qfalse;
1356 #else
1357 #define CHECK_CMP_NAN(a, b) /* do nothing */
1358 #endif
1359 
1360 static inline VALUE
1361 double_cmp_lt(double a, double b)
1362 {
1363  CHECK_CMP_NAN(a, b);
1364  return a < b ? Qtrue : Qfalse;
1365 }
1366 
1367 static inline VALUE
1368 double_cmp_le(double a, double b)
1369 {
1370  CHECK_CMP_NAN(a, b);
1371  return a <= b ? Qtrue : Qfalse;
1372 }
1373 
1374 static inline VALUE
1375 double_cmp_gt(double a, double b)
1376 {
1377  CHECK_CMP_NAN(a, b);
1378  return a > b ? Qtrue : Qfalse;
1379 }
1380 
1381 static inline VALUE
1382 double_cmp_ge(double a, double b)
1383 {
1384  CHECK_CMP_NAN(a, b);
1385  return a >= b ? Qtrue : Qfalse;
1386 }
1387 
1388 static VALUE *
1390 {
1391  const rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1392 
1393  if (cfp->iseq && VM_FRAME_RUBYFRAME_P(cfp)) {
1394  VALUE *bp = prev_cfp->sp + cfp->iseq->body->local_table_size + VM_ENV_DATA_SIZE;
1395  if (cfp->iseq->body->type == ISEQ_TYPE_METHOD) {
1396  /* adjust `self' */
1397  bp += 1;
1398  }
1399 #if VM_DEBUG_BP_CHECK
1400  if (bp != cfp->bp_check) {
1401  fprintf(stderr, "bp_check: %ld, bp: %ld\n",
1402  (long)(cfp->bp_check - GET_THREAD()->stack),
1403  (long)(bp - GET_THREAD()->stack));
1404  rb_bug("vm_base_ptr: unreachable");
1405  }
1406 #endif
1407  return bp;
1408  }
1409  else {
1410  return NULL;
1411  }
1412 }
1413 
1414 /* method call processes with call_info */
1415 
1416 #include "vm_args.c"
1417 
1418 static inline VALUE vm_call_iseq_setup_2(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, int opt_pc, int param_size, int local_size);
1419 static inline VALUE vm_call_iseq_setup_normal(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, int opt_pc, int param_size, int local_size);
1420 static inline VALUE vm_call_iseq_setup_tailcall(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, int opt_pc);
1421 static VALUE vm_call_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc);
1422 static VALUE vm_call_method_nome(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc);
1423 static VALUE vm_call_method_each_type(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc);
1424 static inline VALUE vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc);
1425 
1426 static vm_call_handler vm_call_iseq_setup_func(const struct rb_call_info *ci, const int param_size, const int local_size);
1427 
1429 static void method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *def, void *opts);
1431 
1432 static const rb_iseq_t *
1434 {
1435 #if VM_CHECK_MODE > 0
1436  if (def->type != VM_METHOD_TYPE_ISEQ) rb_bug("def_iseq_ptr: not iseq (%d)", def->type);
1437 #endif
1438  return rb_iseq_check(def->body.iseq.iseqptr);
1439 }
1440 
1441 static VALUE
1443 {
1444  return vm_call_iseq_setup_tailcall(th, cfp, calling, ci, cc, 0);
1445 }
1446 
1447 static VALUE
1449 {
1450  const rb_iseq_t *iseq = def_iseq_ptr(cc->me->def);
1451  int param = iseq->body->param.size;
1452  int local = iseq->body->local_table_size;
1453  return vm_call_iseq_setup_normal(th, cfp, calling, ci, cc, 0, param, local);
1454 }
1455 
1456 static inline int
1458 {
1459  return iseq->body->param.flags.has_opt == FALSE &&
1460  iseq->body->param.flags.has_rest == FALSE &&
1461  iseq->body->param.flags.has_post == FALSE &&
1462  iseq->body->param.flags.has_kw == FALSE &&
1463  iseq->body->param.flags.has_kwrest == FALSE &&
1464  iseq->body->param.flags.has_block == FALSE;
1465 }
1466 
1467 static inline int
1468 vm_callee_setup_arg(rb_thread_t *th, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc,
1469  const rb_iseq_t *iseq, VALUE *argv, int param_size, int local_size)
1470 {
1471  if (LIKELY(simple_iseq_p(iseq))) {
1472  rb_control_frame_t *cfp = th->cfp;
1473 
1474  CALLER_SETUP_ARG(cfp, calling, ci); /* splat arg */
1475 
1476  if (calling->argc != iseq->body->param.lead_num) {
1477  argument_arity_error(th, iseq, calling->argc, iseq->body->param.lead_num, iseq->body->param.lead_num);
1478  }
1479 
1480  CI_SET_FASTPATH(cc, vm_call_iseq_setup_func(ci, param_size, local_size),
1481  (!IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) &&
1483  return 0;
1484  }
1485  else {
1486  return setup_parameters_complex(th, iseq, calling, ci, argv, arg_setup_method);
1487  }
1488 }
1489 
1490 static VALUE
1491 vm_call_iseq_setup(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
1492 {
1493  const rb_iseq_t *iseq = def_iseq_ptr(cc->me->def);
1494  const int param_size = iseq->body->param.size;
1495  const int local_size = iseq->body->local_table_size;
1496  const int opt_pc = vm_callee_setup_arg(th, calling, ci, cc, def_iseq_ptr(cc->me->def), cfp->sp - calling->argc, param_size, local_size);
1497  return vm_call_iseq_setup_2(th, cfp, calling, ci, cc, opt_pc, param_size, local_size);
1498 }
1499 
1500 static inline VALUE
1501 vm_call_iseq_setup_2(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc,
1502  int opt_pc, int param_size, int local_size)
1503 {
1504  if (LIKELY(!(ci->flag & VM_CALL_TAILCALL))) {
1505  return vm_call_iseq_setup_normal(th, cfp, calling, ci, cc, opt_pc, param_size, local_size);
1506  }
1507  else {
1508  return vm_call_iseq_setup_tailcall(th, cfp, calling, ci, cc, opt_pc);
1509  }
1510 }
1511 
1512 static inline VALUE
1514  int opt_pc, int param_size, int local_size)
1515 {
1516  const rb_callable_method_entry_t *me = cc->me;
1517  const rb_iseq_t *iseq = def_iseq_ptr(me->def);
1518  VALUE *argv = cfp->sp - calling->argc;
1519  VALUE *sp = argv + param_size;
1520  cfp->sp = argv - 1 /* recv */;
1521 
1523  calling->block_handler, (VALUE)me,
1524  iseq->body->iseq_encoded + opt_pc, sp,
1525  local_size - param_size,
1526  iseq->body->stack_max);
1527  return Qundef;
1528 }
1529 
1530 static inline VALUE
1532  int opt_pc)
1533 {
1534  unsigned int i;
1535  VALUE *argv = cfp->sp - calling->argc;
1536  const rb_callable_method_entry_t *me = cc->me;
1537  const rb_iseq_t *iseq = def_iseq_ptr(me->def);
1538  VALUE *src_argv = argv;
1539  VALUE *sp_orig, *sp;
1540  VALUE finish_flag = VM_FRAME_FINISHED_P(cfp) ? VM_FRAME_FLAG_FINISH : 0;
1541 
1542  if (VM_BH_FROM_CFP_P(calling->block_handler, cfp)) {
1544  const struct rb_captured_block *src_captured = VM_BH_TO_CAPT_BLOCK(calling->block_handler);
1545  dst_captured->code.val = src_captured->code.val;
1546  if (VM_BH_ISEQ_BLOCK_P(calling->block_handler)) {
1547  calling->block_handler = VM_BH_FROM_ISEQ_BLOCK(dst_captured);
1548  }
1549  else {
1550  calling->block_handler = VM_BH_FROM_IFUNC_BLOCK(dst_captured);
1551  }
1552  }
1553 
1554  vm_pop_frame(th, cfp, cfp->ep);
1555  cfp = th->cfp;
1556 
1557  sp_orig = sp = cfp->sp;
1558 
1559  /* push self */
1560  sp[0] = calling->recv;
1561  sp++;
1562 
1563  /* copy arguments */
1564  for (i=0; i < iseq->body->param.size; i++) {
1565  *sp++ = src_argv[i];
1566  }
1567 
1568  vm_push_frame(th, iseq, VM_FRAME_MAGIC_METHOD | VM_ENV_FLAG_LOCAL | finish_flag,
1569  calling->recv, calling->block_handler, (VALUE)me,
1570  iseq->body->iseq_encoded + opt_pc, sp,
1571  iseq->body->local_table_size - iseq->body->param.size,
1572  iseq->body->stack_max);
1573 
1574  cfp->sp = sp_orig;
1575  RUBY_VM_CHECK_INTS(th);
1576 
1577  return Qundef;
1578 }
1579 
1580 static VALUE
1582 {
1583  return (*func)(recv, rb_ary_new4(argc, argv));
1584 }
1585 
1586 static VALUE
1588 {
1589  return (*func)(argc, argv, recv);
1590 }
1591 
1592 static VALUE
1594 {
1595  return (*func)(recv);
1596 }
1597 
1598 static VALUE
1600 {
1601  return (*func)(recv, argv[0]);
1602 }
1603 
1604 static VALUE
1606 {
1607  return (*func)(recv, argv[0], argv[1]);
1608 }
1609 
1610 static VALUE
1612 {
1613  return (*func)(recv, argv[0], argv[1], argv[2]);
1614 }
1615 
1616 static VALUE
1618 {
1619  return (*func)(recv, argv[0], argv[1], argv[2], argv[3]);
1620 }
1621 
1622 static VALUE
1624 {
1625  return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4]);
1626 }
1627 
1628 static VALUE
1630 {
1631  return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
1632 }
1633 
1634 static VALUE
1636 {
1637  return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
1638 }
1639 
1640 static VALUE
1642 {
1643  return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
1644 }
1645 
1646 static VALUE
1648 {
1649  return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
1650 }
1651 
1652 static VALUE
1654 {
1655  return (*func)(recv, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
1656 }
1657 
1658 static VALUE
1660 {
1661  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]);
1662 }
1663 
1664 static VALUE
1666 {
1667  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]);
1668 }
1669 
1670 static VALUE
1672 {
1673  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]);
1674 }
1675 
1676 static VALUE
1678 {
1679  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]);
1680 }
1681 
1682 static VALUE
1684 {
1685  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]);
1686 }
1687 
1688 #ifndef VM_PROFILE
1689 #define VM_PROFILE 0
1690 #endif
1691 
1692 #if VM_PROFILE
1693 enum {
1694  VM_PROFILE_R2C_CALL,
1695  VM_PROFILE_R2C_POPF,
1696  VM_PROFILE_C2C_CALL,
1697  VM_PROFILE_C2C_POPF,
1698  VM_PROFILE_COUNT
1699 };
1700 static int vm_profile_counter[VM_PROFILE_COUNT];
1701 #define VM_PROFILE_UP(x) (vm_profile_counter[VM_PROFILE_##x]++)
1702 #define VM_PROFILE_ATEXIT() atexit(vm_profile_show_result)
1703 static void
1704 vm_profile_show_result(void)
1705 {
1706  fprintf(stderr, "VM Profile results: \n");
1707  fprintf(stderr, "r->c call: %d\n", vm_profile_counter[VM_PROFILE_R2C_CALL]);
1708  fprintf(stderr, "r->c popf: %d\n", vm_profile_counter[VM_PROFILE_R2C_POPF]);
1709  fprintf(stderr, "c->c call: %d\n", vm_profile_counter[VM_PROFILE_C2C_CALL]);
1710  fprintf(stderr, "c->c popf: %d\n", vm_profile_counter[VM_PROFILE_C2C_POPF]);
1711 }
1712 #else
1713 #define VM_PROFILE_UP(x)
1714 #define VM_PROFILE_ATEXIT()
1715 #endif
1716 
1717 static inline
1718 const rb_method_cfunc_t *
1720 {
1721 #if VM_DEBUG_VERIFY_METHOD_CACHE
1722  switch (me->def->type) {
1723  case VM_METHOD_TYPE_CFUNC:
1725  break;
1726 # define METHOD_BUG(t) case VM_METHOD_TYPE_##t: rb_bug("wrong method type: " #t)
1727  METHOD_BUG(ISEQ);
1728  METHOD_BUG(ATTRSET);
1729  METHOD_BUG(IVAR);
1730  METHOD_BUG(BMETHOD);
1731  METHOD_BUG(ZSUPER);
1732  METHOD_BUG(UNDEF);
1733  METHOD_BUG(OPTIMIZED);
1734  METHOD_BUG(MISSING);
1735  METHOD_BUG(REFINED);
1736  METHOD_BUG(ALIAS);
1737 # undef METHOD_BUG
1738  default:
1739  rb_bug("wrong method type: %d", me->def->type);
1740  }
1741 #endif
1742  return &me->def->body.cfunc;
1743 }
1744 
1745 static VALUE
1746 vm_call_cfunc_with_frame(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
1747 {
1748  VALUE val;
1749  const rb_callable_method_entry_t *me = cc->me;
1750  const rb_method_cfunc_t *cfunc = vm_method_cfunc_entry(me);
1751  int len = cfunc->argc;
1752 
1753  VALUE recv = calling->recv;
1754  VALUE block_handler = calling->block_handler;
1755  int argc = calling->argc;
1756 
1758  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_CALL, recv, me->def->original_id, ci->mid, me->owner, Qundef);
1759 
1761  block_handler, (VALUE)me,
1762  0, th->cfp->sp, 0, 0);
1763 
1764  if (len >= 0) rb_check_arity(argc, len, len);
1765 
1766  reg_cfp->sp -= argc + 1;
1767  VM_PROFILE_UP(R2C_CALL);
1768  val = (*cfunc->invoker)(cfunc->func, recv, argc, reg_cfp->sp + 1);
1769 
1770  if (reg_cfp != th->cfp + 1) {
1771  rb_bug("vm_call_cfunc - cfp consistency error");
1772  }
1773 
1774  rb_vm_pop_frame(th);
1775 
1776  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, recv, me->def->original_id, ci->mid, me->owner, val);
1777  RUBY_DTRACE_CMETHOD_RETURN_HOOK(th, me->owner, me->def->original_id);
1778 
1779  return val;
1780 }
1781 
1782 #if OPT_CALL_CFUNC_WITHOUT_FRAME
1783 static VALUE
1784 vm_call_cfunc_latter(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling)
1785 {
1786  VALUE val;
1787  int argc = calling->argc;
1788  VALUE *argv = STACK_ADDR_FROM_TOP(argc);
1789  VALUE recv = calling->recv;
1790  const rb_method_cfunc_t *cfunc = vm_method_cfunc_entry(cc->me);
1791 
1792  th->passed_calling = calling;
1793  reg_cfp->sp -= argc + 1;
1794  ci->aux.inc_sp = argc + 1;
1795  VM_PROFILE_UP(R2C_CALL);
1796  val = (*cfunc->invoker)(cfunc->func, recv, argc, argv);
1797 
1798  /* check */
1799  if (reg_cfp == th->cfp) { /* no frame push */
1800  if (UNLIKELY(th->passed_ci != ci)) {
1801  rb_bug("vm_call_cfunc_latter: passed_ci error (ci: %p, passed_ci: %p)", ci, th->passed_ci);
1802  }
1803  th->passed_ci = 0;
1804  }
1805  else {
1806  if (UNLIKELY(reg_cfp != RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp))) {
1807  rb_bug("vm_call_cfunc_latter: cfp consistency error (%p, %p)", reg_cfp, th->cfp+1);
1808  }
1809  vm_pop_frame(th, reg_cfp, reg_cfp->ep);
1810  VM_PROFILE_UP(R2C_POPF);
1811  }
1812 
1813  return val;
1814 }
1815 
1816 static VALUE
1817 vm_call_cfunc(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci)
1818 {
1819  VALUE val;
1820  const rb_callable_method_entry_t *me = cc->me;
1821  int len = vm_method_cfunc_entry(me)->argc;
1822  VALUE recv = calling->recv;
1823 
1824  CALLER_SETUP_ARG(reg_cfp, calling, ci);
1825  if (len >= 0) rb_check_arity(calling->argc, len, len);
1826 
1828  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_CALL, recv, me->called_id, me->owner, Qnil);
1829 
1830  if (!(cc->me->def->flag & METHOD_VISI_PROTECTED) &&
1831  !(ci->flag & VM_CALL_ARGS_SPLAT) &&
1832  !(ci->kw_arg != NULL)) {
1833  CI_SET_FASTPATH(cc, vm_call_cfunc_latter, 1);
1834  }
1835  val = vm_call_cfunc_latter(th, reg_cfp, calling);
1836 
1837  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, recv, me->called_id, me->owner, val);
1839 
1840  return val;
1841 }
1842 
1843 void
1844 rb_vm_call_cfunc_push_frame(rb_thread_t *th)
1845 {
1846  struct rb_calling_info *calling = th->passed_calling;
1847  const rb_callable_method_entry_t *me = calling->me;
1848  th->passed_ci = 0;
1849 
1851  calling->recv, calling->block_handler, (VALUE)me /* cref */,
1852  0, th->cfp->sp + cc->aux.inc_sp, 0, 0);
1853 
1854  if (calling->call != vm_call_general) {
1855  calling->call = vm_call_cfunc_with_frame;
1856  }
1857 }
1858 #else /* OPT_CALL_CFUNC_WITHOUT_FRAME */
1859 static VALUE
1860 vm_call_cfunc(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
1861 {
1862  CALLER_SETUP_ARG(reg_cfp, calling, ci);
1863  return vm_call_cfunc_with_frame(th, reg_cfp, calling, ci, cc);
1864 }
1865 #endif
1866 
1867 static VALUE
1868 vm_call_ivar(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
1869 {
1870  cfp->sp -= 1;
1871  return vm_getivar(calling->recv, cc->me->def->body.attr.id, NULL, cc, 1);
1872 }
1873 
1874 static VALUE
1875 vm_call_attrset(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
1876 {
1877  VALUE val = *(cfp->sp - 1);
1878  cfp->sp -= 2;
1879  return vm_setivar(calling->recv, cc->me->def->body.attr.id, val, NULL, cc, 1);
1880 }
1881 
1882 static inline VALUE
1883 vm_call_bmethod_body(rb_thread_t *th, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, const VALUE *argv)
1884 {
1885  rb_proc_t *proc;
1886  VALUE val;
1887 
1888  /* control block frame */
1889  th->passed_bmethod_me = cc->me;
1890  GetProcPtr(cc->me->def->body.proc, proc);
1891  val = vm_invoke_bmethod(th, proc, calling->recv, calling->argc, argv, calling->block_handler);
1892 
1893  return val;
1894 }
1895 
1896 static VALUE
1897 vm_call_bmethod(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
1898 {
1899  VALUE *argv;
1900  int argc;
1901 
1902  CALLER_SETUP_ARG(cfp, calling, ci);
1903  argc = calling->argc;
1904  argv = ALLOCA_N(VALUE, argc);
1905  MEMCPY(argv, cfp->sp - argc, VALUE, argc);
1906  cfp->sp += - argc - 1;
1907 
1908  return vm_call_bmethod_body(th, calling, ci, cc, argv);
1909 }
1910 
1911 static enum method_missing_reason
1913 {
1915  if (ci->flag & VM_CALL_VCALL) stat |= MISSING_VCALL;
1916  if (ci->flag & VM_CALL_FCALL) stat |= MISSING_FCALL;
1917  if (ci->flag & VM_CALL_SUPER) stat |= MISSING_SUPER;
1918  return stat;
1919 }
1920 
1921 static VALUE
1922 vm_call_opt_send(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *orig_ci, struct rb_call_cache *orig_cc)
1923 {
1924  int i;
1925  VALUE sym;
1926  struct rb_call_info *ci;
1927  struct rb_call_info_with_kwarg ci_entry;
1928  struct rb_call_cache cc_entry, *cc;
1929 
1930  CALLER_SETUP_ARG(reg_cfp, calling, orig_ci);
1931 
1932  i = calling->argc - 1;
1933 
1934  if (calling->argc == 0) {
1935  rb_raise(rb_eArgError, "no method name given");
1936  }
1937 
1938  /* setup new ci */
1939  if (orig_ci->flag & VM_CALL_KWARG) {
1940  ci = (struct rb_call_info *)&ci_entry;
1941  ci_entry = *(struct rb_call_info_with_kwarg *)orig_ci;
1942  }
1943  else {
1944  ci = &ci_entry.ci;
1945  ci_entry.ci = *orig_ci;
1946  }
1947  ci->flag = ci->flag & ~VM_CALL_KWARG; /* TODO: delegate kw_arg without making a Hash object */
1948 
1949  /* setup new cc */
1950  cc_entry = *orig_cc;
1951  cc = &cc_entry;
1952 
1953  sym = TOPN(i);
1954 
1955  if (!(ci->mid = rb_check_id(&sym))) {
1956  if (rb_method_basic_definition_p(CLASS_OF(calling->recv), idMethodMissing)) {
1958  rb_long2int(calling->argc), &TOPN(i),
1959  ci->flag & (VM_CALL_FCALL|VM_CALL_VCALL));
1960  rb_exc_raise(exc);
1961  }
1962  TOPN(i) = rb_str_intern(sym);
1963  ci->mid = idMethodMissing;
1965  }
1966  else {
1967  /* shift arguments */
1968  if (i > 0) {
1969  MEMMOVE(&TOPN(i), &TOPN(i-1), VALUE, i);
1970  }
1971  calling->argc -= 1;
1972  DEC_SP(1);
1973  }
1974 
1977  return vm_call_method(th, reg_cfp, calling, ci, cc);
1978 }
1979 
1980 static VALUE
1981 vm_call_opt_call(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
1982 {
1983  rb_proc_t *proc;
1984  int argc;
1985  VALUE *argv;
1986 
1987  CALLER_SETUP_ARG(cfp, calling, ci);
1988 
1989  argc = calling->argc;
1990  argv = ALLOCA_N(VALUE, argc);
1991  GetProcPtr(calling->recv, proc);
1992  MEMCPY(argv, cfp->sp - argc, VALUE, argc);
1993  cfp->sp -= argc + 1;
1994 
1995  return rb_vm_invoke_proc(th, proc, argc, argv, calling->block_handler);
1996 }
1997 
1998 static VALUE
1999 vm_call_method_missing(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *orig_ci, struct rb_call_cache *orig_cc)
2000 {
2001  VALUE *argv = STACK_ADDR_FROM_TOP(calling->argc);
2002  struct rb_call_info ci_entry;
2003  const struct rb_call_info *ci;
2004  struct rb_call_cache cc_entry, *cc;
2005  unsigned int argc;
2006 
2007  CALLER_SETUP_ARG(reg_cfp, calling, orig_ci);
2008  argc = calling->argc+1;
2009 
2010  ci_entry.flag = VM_CALL_FCALL | VM_CALL_OPT_SEND;
2011  ci_entry.mid = idMethodMissing;
2012  ci_entry.orig_argc = argc;
2013  ci = &ci_entry;
2014 
2015  cc_entry = *orig_cc;
2016  cc_entry.me =
2018  idMethodMissing);
2019  cc = &cc_entry;
2020 
2021  calling->argc = argc;
2022 
2023  /* shift arguments: m(a, b, c) #=> method_missing(:m, a, b, c) */
2024  CHECK_VM_STACK_OVERFLOW(reg_cfp, 1);
2025  if (argc > 1) {
2026  MEMMOVE(argv+1, argv, VALUE, argc-1);
2027  }
2028  argv[0] = ID2SYM(orig_ci->mid);
2029  INC_SP(1);
2030 
2032  return vm_call_method(th, reg_cfp, calling, ci, cc);
2033 }
2034 
2036 static VALUE
2037 vm_call_zsuper(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, VALUE klass)
2038 {
2039  klass = RCLASS_SUPER(klass);
2040  cc->me = klass ? rb_callable_method_entry(klass, ci->mid) : NULL;
2041 
2042  if (!cc->me) {
2043  return vm_call_method_nome(th, cfp, calling, ci, cc);
2044  }
2045  if (cc->me->def->type == VM_METHOD_TYPE_REFINED &&
2046  cc->me->def->body.refined.orig_me) {
2048  }
2049  return vm_call_method_each_type(th, cfp, calling, ci, cc);
2050 }
2051 
2052 static inline VALUE
2053 find_refinement(VALUE refinements, VALUE klass)
2054 {
2055  if (NIL_P(refinements)) {
2056  return Qnil;
2057  }
2058  return rb_hash_lookup(refinements, klass);
2059 }
2060 
2062 static rb_control_frame_t *
2064 {
2065  rb_control_frame_t *top_cfp = cfp;
2066 
2067  if (cfp->iseq && cfp->iseq->body->type == ISEQ_TYPE_BLOCK) {
2068  const rb_iseq_t *local_iseq = cfp->iseq->body->local_iseq;
2069 
2070  do {
2071  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2073  /* TODO: orphan block */
2074  return top_cfp;
2075  }
2076  } while (cfp->iseq != local_iseq);
2077  }
2078  return cfp;
2079 }
2080 
2081 static VALUE
2082 find_defined_class_by_owner(VALUE current_class, VALUE target_owner)
2083 {
2084  VALUE klass = current_class;
2085 
2086  /* for prepended Module, then start from cover class */
2087  if (RB_TYPE_P(klass, T_ICLASS) && FL_TEST(klass, RICLASS_IS_ORIGIN)) klass = RBASIC_CLASS(klass);
2088 
2089  while (RTEST(klass)) {
2090  VALUE owner = RB_TYPE_P(klass, T_ICLASS) ? RBASIC_CLASS(klass) : klass;
2091  if (owner == target_owner) {
2092  return klass;
2093  }
2094  klass = RCLASS_SUPER(klass);
2095  }
2096 
2097  return current_class; /* maybe module function */
2098 }
2099 
2100 static const rb_callable_method_entry_t *
2102 {
2103  const rb_method_entry_t *orig_me = me->def->body.alias.original_me;
2104  const rb_callable_method_entry_t *cme;
2105 
2106  if (orig_me->defined_class == 0) {
2107  VALUE defined_class = find_defined_class_by_owner(me->defined_class, orig_me->owner);
2108  VM_ASSERT(RB_TYPE_P(orig_me->owner, T_MODULE));
2109  cme = rb_method_entry_complement_defined_class(orig_me, me->called_id, defined_class);
2110 
2111  if (me->def->alias_count + me->def->complemented_count == 0) {
2112  RB_OBJ_WRITE(me, &me->def->body.alias.original_me, cme);
2113  }
2114  else {
2117  (void *)cme);
2118  }
2119  }
2120  else {
2121  cme = (const rb_callable_method_entry_t *)orig_me;
2122  }
2123 
2124  VM_ASSERT(callable_method_entry_p(cme));
2125  return cme;
2126 }
2127 
2128 static const rb_callable_method_entry_t *
2130 {
2131  const rb_method_entry_t *orig_me = me->def->body.refined.orig_me;
2132  const rb_callable_method_entry_t *cme;
2133 
2134  if (orig_me->defined_class == 0) {
2135  cme = NULL;
2136  rb_notimplement();
2137  }
2138  else {
2139  cme = (const rb_callable_method_entry_t *)orig_me;
2140  }
2141 
2142  VM_ASSERT(callable_method_entry_p(cme));
2143 
2144  if (UNDEFINED_METHOD_ENTRY_P(cme)) {
2145  cme = NULL;
2146  }
2147 
2148  return cme;
2149 }
2150 
2151 static VALUE
2153 {
2154  switch (cc->me->def->type) {
2155  case VM_METHOD_TYPE_ISEQ:
2157  return vm_call_iseq_setup(th, cfp, calling, ci, cc);
2158 
2160  case VM_METHOD_TYPE_CFUNC:
2162  return vm_call_cfunc(th, cfp, calling, ci, cc);
2163 
2165  CALLER_SETUP_ARG(cfp, calling, ci);
2166  rb_check_arity(calling->argc, 1, 1);
2167  cc->aux.index = 0;
2169  return vm_call_attrset(th, cfp, calling, ci, cc);
2170 
2171  case VM_METHOD_TYPE_IVAR:
2172  CALLER_SETUP_ARG(cfp, calling, ci);
2173  rb_check_arity(calling->argc, 0, 0);
2174  cc->aux.index = 0;
2176  return vm_call_ivar(th, cfp, calling, ci, cc);
2177 
2179  cc->aux.method_missing_reason = 0;
2181  return vm_call_method_missing(th, cfp, calling, ci, cc);
2182 
2185  return vm_call_bmethod(th, cfp, calling, ci, cc);
2186 
2187  case VM_METHOD_TYPE_ALIAS:
2188  cc->me = aliased_callable_method_entry(cc->me);
2189  VM_ASSERT(cc->me != NULL);
2190  return vm_call_method_each_type(th, cfp, calling, ci, cc);
2191 
2193  switch (cc->me->def->body.optimize_type) {
2194  case OPTIMIZED_METHOD_TYPE_SEND:
2196  return vm_call_opt_send(th, cfp, calling, ci, cc);
2197  case OPTIMIZED_METHOD_TYPE_CALL:
2199  return vm_call_opt_call(th, cfp, calling, ci, cc);
2200  default:
2201  rb_bug("vm_call_method: unsupported optimized method type (%d)",
2202  cc->me->def->body.optimize_type);
2203  }
2204 
2205  case VM_METHOD_TYPE_UNDEF:
2206  break;
2207 
2208  case VM_METHOD_TYPE_ZSUPER:
2209  return vm_call_zsuper(th, cfp, calling, ci, cc, RCLASS_ORIGIN(cc->me->owner));
2210 
2211  case VM_METHOD_TYPE_REFINED: {
2212  const rb_cref_t *cref = rb_vm_get_cref(cfp->ep);
2213  VALUE refinements = cref ? CREF_REFINEMENTS(cref) : Qnil;
2214  VALUE refinement;
2215  const rb_callable_method_entry_t *ref_me;
2216 
2217  refinement = find_refinement(refinements, cc->me->owner);
2218 
2219  if (NIL_P(refinement)) {
2220  goto no_refinement_dispatch;
2221  }
2222  ref_me = rb_callable_method_entry(refinement, ci->mid);
2223 
2224  if (ref_me) {
2225  if (cc->call == vm_call_super_method) {
2226  const rb_control_frame_t *top_cfp = current_method_entry(th, cfp);
2227  const rb_callable_method_entry_t *top_me = rb_vm_frame_method_entry(top_cfp);
2228  if (top_me && rb_method_definition_eq(ref_me->def, top_me->def)) {
2229  goto no_refinement_dispatch;
2230  }
2231  }
2232  cc->me = ref_me;
2233  if (ref_me->def->type != VM_METHOD_TYPE_REFINED) {
2234  return vm_call_method(th, cfp, calling, ci, cc);
2235  }
2236  }
2237  else {
2238  cc->me = NULL;
2239  return vm_call_method_nome(th, cfp, calling, ci, cc);
2240  }
2241 
2242  no_refinement_dispatch:
2243  if (cc->me->def->body.refined.orig_me) {
2245  return vm_call_method(th, cfp, calling, ci, cc);
2246  }
2247  else {
2248  return vm_call_zsuper(th, cfp, calling, ci, cc, cc->me->owner);
2249  }
2250  }
2251  }
2252 
2253  rb_bug("vm_call_method: unsupported method type (%d)", cc->me->def->type);
2254 }
2255 
2256 static VALUE
2257 vm_call_method_nome(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
2258 {
2259  /* method missing */
2260  const int stat = ci_missing_reason(ci);
2261 
2262  if (ci->mid == idMethodMissing) {
2263  rb_control_frame_t *reg_cfp = cfp;
2264  VALUE *argv = STACK_ADDR_FROM_TOP(calling->argc);
2265  rb_raise_method_missing(th, calling->argc, argv, calling->recv, stat);
2266  }
2267  else {
2270  return vm_call_method_missing(th, cfp, calling, ci, cc);
2271  }
2272 }
2273 
2274 static inline VALUE
2275 vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
2276 {
2277  VM_ASSERT(callable_method_entry_p(cc->me));
2278 
2279  if (cc->me != NULL) {
2280  switch (METHOD_ENTRY_VISI(cc->me)) {
2281  case METHOD_VISI_PUBLIC: /* likely */
2282  return vm_call_method_each_type(th, cfp, calling, ci, cc);
2283 
2284  case METHOD_VISI_PRIVATE:
2285  if (!(ci->flag & VM_CALL_FCALL)) {
2287  if (ci->flag & VM_CALL_VCALL) stat |= MISSING_VCALL;
2288 
2291  return vm_call_method_missing(th, cfp, calling, ci, cc);
2292  }
2293  return vm_call_method_each_type(th, cfp, calling, ci, cc);
2294 
2295  case METHOD_VISI_PROTECTED:
2296  if (!(ci->flag & VM_CALL_OPT_SEND)) {
2297  if (!rb_obj_is_kind_of(cfp->self, cc->me->defined_class)) {
2299  return vm_call_method_missing(th, cfp, calling, ci, cc);
2300  }
2301  else {
2302  /* caching method info to dummy cc */
2303  struct rb_call_cache cc_entry;
2304  cc_entry = *cc;
2305  cc = &cc_entry;
2306 
2307  VM_ASSERT(cc->me != NULL);
2308  return vm_call_method_each_type(th, cfp, calling, ci, cc);
2309  }
2310  }
2311  return vm_call_method_each_type(th, cfp, calling, ci, cc);
2312 
2313  default:
2314  rb_bug("unreachable");
2315  }
2316  }
2317  else {
2318  return vm_call_method_nome(th, cfp, calling, ci, cc);
2319  }
2320 }
2321 
2322 static VALUE
2323 vm_call_general(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
2324 {
2325  return vm_call_method(th, reg_cfp, calling, ci, cc);
2326 }
2327 
2328 static VALUE
2329 vm_call_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
2330 {
2331  /* this check is required to distinguish with other functions. */
2332  if (cc->call != vm_call_super_method) rb_bug("bug");
2333  return vm_call_method(th, reg_cfp, calling, ci, cc);
2334 }
2335 
2336 /* super */
2337 
2338 static inline VALUE
2340 {
2341  if (BUILTIN_TYPE(klass) == T_ICLASS &&
2342  FL_TEST(RBASIC(klass)->klass, RMODULE_IS_REFINEMENT)) {
2343  klass = RBASIC(klass)->klass;
2344  }
2345  klass = RCLASS_ORIGIN(klass);
2346  return RCLASS_SUPER(klass);
2347 }
2348 
2349 static void
2351 {
2352  rb_raise(rb_eNoMethodError, "super called outside of method");
2353 }
2354 
2355 static void
2357  struct rb_calling_info *calling, struct rb_call_info *ci, struct rb_call_cache *cc)
2358 {
2359  VALUE current_defined_class, klass;
2360  VALUE sigval = TOPN(calling->argc);
2362 
2363  if (!me) {
2364  vm_super_outside();
2365  }
2366 
2367  current_defined_class = me->defined_class;
2368 
2369  if (!NIL_P(RCLASS_REFINED_CLASS(current_defined_class))) {
2370  current_defined_class = RCLASS_REFINED_CLASS(current_defined_class);
2371  }
2372 
2373  if (BUILTIN_TYPE(current_defined_class) != T_MODULE &&
2374  BUILTIN_TYPE(current_defined_class) != T_ICLASS && /* bound UnboundMethod */
2375  !FL_TEST(current_defined_class, RMODULE_INCLUDED_INTO_REFINEMENT) &&
2376  !rb_obj_is_kind_of(calling->recv, current_defined_class)) {
2377  VALUE m = RB_TYPE_P(current_defined_class, T_ICLASS) ?
2378  RBASIC(current_defined_class)->klass : current_defined_class;
2379 
2381  "self has wrong type to call super in this context: "
2382  "%"PRIsVALUE" (expected %"PRIsVALUE")",
2383  rb_obj_class(calling->recv), m);
2384  }
2385 
2386  if (me->def->type == VM_METHOD_TYPE_BMETHOD && !sigval) {
2388  "implicit argument passing of super from method defined"
2389  " by define_method() is not supported."
2390  " Specify all arguments explicitly.");
2391  }
2392 
2393  ci->mid = me->def->original_id;
2395 
2396  if (!klass) {
2397  /* bound instance method of module */
2400  }
2401  else {
2402  /* TODO: use inline cache */
2403  cc->me = rb_callable_method_entry(klass, ci->mid);
2404  CI_SET_FASTPATH(cc, vm_call_super_method, 1);
2405  }
2406 }
2407 
2408 /* yield */
2409 
2410 static inline int
2412 {
2413  rb_proc_t *proc;
2414 
2415  if (procval) {
2416  GetProcPtr(procval, proc);
2417  return proc->is_lambda;
2418  }
2419  else {
2420  return 0;
2421  }
2422 }
2423 
2424 static VALUE
2426 {
2427  VALUE blockarg = Qnil;
2428 
2429  if (block_handler != VM_BLOCK_HANDLER_NONE) {
2430  switch (vm_block_handler_type(block_handler)) {
2432  blockarg = block_handler;
2433  break;
2435  blockarg = rb_sym_to_proc(block_handler);
2436  break;
2439  blockarg = rb_vm_make_proc(th, VM_BH_TO_CAPT_BLOCK(block_handler), rb_cProc);
2440  break;
2441  }
2442  }
2443 
2444  return blockarg;
2445 }
2446 
2447 static VALUE
2449  const struct rb_captured_block *captured,
2450  VALUE self, int argc, const VALUE *argv, VALUE block_handler)
2451 {
2452  int is_lambda = FALSE; /* TODO */
2453  VALUE val, arg, blockarg;
2454  const struct vm_ifunc *ifunc = captured->code.ifunc;
2456  th->passed_bmethod_me = NULL;
2457 
2458  if (is_lambda) {
2459  arg = rb_ary_new4(argc, argv);
2460  }
2461  else if (argc == 0) {
2462  arg = Qnil;
2463  }
2464  else {
2465  arg = argv[0];
2466  }
2467 
2468  blockarg = vm_block_handler_to_proc(th, block_handler);
2469 
2470  vm_push_frame(th, (const rb_iseq_t *)captured->code.ifunc,
2472  self,
2473  VM_GUARDED_PREV_EP(captured->ep),
2474  (VALUE)me,
2475  0, th->cfp->sp, 0, 0);
2476  val = (*ifunc->func)(arg, ifunc->data, argc, argv, blockarg);
2477  rb_vm_pop_frame(th);
2478 
2479  return val;
2480 }
2481 
2482 static VALUE
2483 vm_yield_with_symbol(rb_thread_t *th, VALUE symbol, int argc, const VALUE *argv, VALUE block_handler)
2484 {
2485  return rb_sym_proc_call(SYM2ID(symbol), argc, argv, vm_block_handler_to_proc(th, block_handler));
2486 }
2487 
2488 static inline int
2490 {
2491  int i;
2492  long len = RARRAY_LEN(ary);
2493 
2495 
2496  for (i=0; i<len && i<iseq->body->param.lead_num; i++) {
2497  argv[i] = RARRAY_AREF(ary, i);
2498  }
2499 
2500  return i;
2501 }
2502 
2503 static inline VALUE
2505 {
2506  VALUE ary, arg0 = argv[0];
2507  ary = rb_check_array_type(arg0);
2508  argv[0] = arg0;
2509  return ary;
2510 }
2511 
2512 static int
2513 vm_callee_setup_block_arg(rb_thread_t *th, struct rb_calling_info *calling, const struct rb_call_info *ci, const rb_iseq_t *iseq, VALUE *argv, const enum arg_setup_type arg_setup_type)
2514 {
2515  if (simple_iseq_p(iseq)) {
2516  rb_control_frame_t *cfp = th->cfp;
2517  VALUE arg0;
2518 
2519  CALLER_SETUP_ARG(cfp, calling, ci); /* splat arg */
2520 
2521  if (arg_setup_type == arg_setup_block &&
2522  calling->argc == 1 &&
2523  iseq->body->param.flags.has_lead &&
2524  !iseq->body->param.flags.ambiguous_param0 &&
2526  calling->argc = vm_callee_setup_block_arg_arg0_splat(cfp, iseq, argv, arg0);
2527  }
2528 
2529  if (calling->argc != iseq->body->param.lead_num) {
2530  if (arg_setup_type == arg_setup_block) {
2531  if (calling->argc < iseq->body->param.lead_num) {
2532  int i;
2534  for (i=calling->argc; i<iseq->body->param.lead_num; i++) argv[i] = Qnil;
2535  calling->argc = iseq->body->param.lead_num; /* fill rest parameters */
2536  }
2537  else if (calling->argc > iseq->body->param.lead_num) {
2538  calling->argc = iseq->body->param.lead_num; /* simply truncate arguments */
2539  }
2540  }
2541  else {
2542  argument_arity_error(th, iseq, calling->argc, iseq->body->param.lead_num, iseq->body->param.lead_num);
2543  }
2544  }
2545 
2546  return 0;
2547  }
2548  else {
2549  return setup_parameters_complex(th, iseq, calling, ci, argv, arg_setup_type);
2550  }
2551 }
2552 
2553 static int
2554 vm_yield_setup_args(rb_thread_t *th, const rb_iseq_t *iseq, const int argc, VALUE *argv, VALUE block_handler, enum arg_setup_type arg_setup_type)
2555 {
2556  struct rb_calling_info calling_entry, *calling;
2557  struct rb_call_info ci_entry, *ci;
2558 
2559  calling = &calling_entry;
2560  calling->argc = argc;
2561  calling->block_handler = block_handler;
2562 
2563  ci_entry.flag = 0;
2564  ci = &ci_entry;
2565 
2566  return vm_callee_setup_block_arg(th, calling, ci, iseq, argv, arg_setup_type);
2567 }
2568 
2569 /* ruby iseq -> ruby block */
2570 
2571 static VALUE
2573  struct rb_calling_info *calling, const struct rb_call_info *ci,
2574  int is_lambda, const struct rb_captured_block *captured)
2575 {
2576  const rb_iseq_t *iseq = captured->code.iseq;
2577  const int arg_size = iseq->body->param.size;
2578  VALUE * const rsp = GET_SP() - calling->argc;
2579  int opt_pc = vm_callee_setup_block_arg(th, calling, ci, iseq, rsp, is_lambda ? arg_setup_lambda : arg_setup_block);
2580 
2581  SET_SP(rsp);
2582 
2583  vm_push_frame(th, iseq,
2585  captured->self,
2586  VM_GUARDED_PREV_EP(captured->ep), 0,
2587  iseq->body->iseq_encoded + opt_pc,
2588  rsp + arg_size,
2589  iseq->body->local_table_size - arg_size, iseq->body->stack_max);
2590 
2591  return Qundef;
2592 }
2593 
2594 static VALUE
2596  struct rb_calling_info *calling, const struct rb_call_info *ci,
2597  VALUE symbol)
2598 {
2599  VALUE val;
2600  int argc;
2601  CALLER_SETUP_ARG(th->cfp, calling, ci);
2602  argc = calling->argc;
2603  val = vm_yield_with_symbol(th, symbol, argc, STACK_ADDR_FROM_TOP(argc), VM_BLOCK_HANDLER_NONE);
2604  POPN(argc);
2605  return val;
2606 }
2607 
2608 static VALUE
2610  struct rb_calling_info *calling, const struct rb_call_info *ci,
2611  const struct rb_captured_block *captured)
2612 {
2613  VALUE val;
2614  int argc;
2615  CALLER_SETUP_ARG(th->cfp, calling, ci);
2616  argc = calling->argc;
2617  val = vm_yield_with_cfunc(th, captured, captured->self, argc, STACK_ADDR_FROM_TOP(argc), VM_BLOCK_HANDLER_NONE);
2618  POPN(argc); /* TODO: should put before C/yield? */
2619  return val;
2620 }
2621 
2622 static VALUE
2624 {
2625  const struct rb_block *block = vm_proc_block(procval);
2626 
2627  switch (vm_block_type(block)) {
2628  case block_type_iseq:
2629  return VM_BH_FROM_ISEQ_BLOCK(&block->as.captured);
2630  case block_type_ifunc:
2631  return VM_BH_FROM_IFUNC_BLOCK(&block->as.captured);
2632  case block_type_symbol:
2633  return VM_BH_FROM_SYMBOL(block->as.symbol);
2634  case block_type_proc:
2635  return VM_BH_FROM_PROC(block->as.proc);
2636  }
2637  VM_UNREACHABLE(vm_yield_with_proc);
2638  return Qundef;
2639 }
2640 
2641 static VALUE
2642 vm_invoke_block(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci)
2643 {
2644  VALUE block_handler = VM_CF_BLOCK_HANDLER(reg_cfp);
2645  VALUE type = GET_ISEQ()->body->local_iseq->body->type;
2646  int is_lambda = FALSE;
2647 
2648  if ((type != ISEQ_TYPE_METHOD && type != ISEQ_TYPE_CLASS) ||
2649  block_handler == VM_BLOCK_HANDLER_NONE) {
2650  rb_vm_localjump_error("no block given (yield)", Qnil, 0);
2651  }
2652 
2653  again:
2654  switch (vm_block_handler_type(block_handler)) {
2656  {
2657  const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
2658  return vm_invoke_iseq_block(th, reg_cfp, calling, ci, is_lambda, captured);
2659  }
2661  {
2662  const struct rb_captured_block *captured = VM_BH_TO_IFUNC_BLOCK(block_handler);
2663  return vm_invoke_ifunc_block(th, reg_cfp, calling, ci, captured);
2664  }
2666  is_lambda = block_proc_is_lambda(VM_BH_TO_PROC(block_handler));
2667  block_handler = vm_proc_to_block_handler(VM_BH_TO_PROC(block_handler));
2668  goto again;
2670  return vm_invoke_symbol_block(th, reg_cfp, calling, ci, VM_BH_TO_SYMBOL(block_handler));
2671  }
2672  VM_UNREACHABLE(vm_invoke_block: unreachable);
2673  return Qnil;
2674 }
2675 
2676 static VALUE
2678 {
2679  rb_thread_t *th = GET_THREAD();
2681  struct rb_captured_block *captured;
2682 
2683  if (cfp == 0) {
2684  rb_bug("vm_make_proc_with_iseq: unreachable");
2685  }
2686 
2687  captured = VM_CFP_TO_CAPTURED_BLOCK(cfp);
2688  captured->code.iseq = blockiseq;
2689 
2690  return rb_vm_make_proc(th, captured, rb_cProc);
2691 }
2692 
2693 static VALUE
2695 {
2696  VALUE proc = vm_make_proc_with_iseq((rb_iseq_t *)iseq);
2697  return rb_proc_call_with_block(proc, 0, 0, Qnil);
2698 }
2699 
2700 static VALUE
2702 {
2703  union iseq_inline_storage_entry *is = (union iseq_inline_storage_entry *)data;
2704  is->once.running_thread = NULL;
2705  return Qnil;
2706 }
2707 
2710 {
2711  TOPN(0) = rb_struct_aref(GET_SELF(), TOPN(0));
2712  return reg_cfp;
2713 }
2714 
2717 {
2718  rb_struct_aset(GET_SELF(), TOPN(0), TOPN(1));
2719  return reg_cfp;
2720 }
2721 
2722 /* defined insn */
2723 
2724 static enum defined_type
2726 {
2727  VALUE args[2];
2728  VALUE r;
2729 
2730  args[0] = obj; args[1] = Qfalse;
2731  r = rb_check_funcall(v, idRespond_to_missing, 2, args);
2732  if (r != Qundef && RTEST(r)) {
2733  return DEFINED_METHOD;
2734  }
2735  else {
2736  return 0;
2737  }
2738 }
2739 
2740 static VALUE
2741 vm_defined(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE obj, VALUE needstr, VALUE v)
2742 {
2743  VALUE klass;
2744  enum defined_type expr_type = 0;
2745  enum defined_type type = (enum defined_type)op_type;
2746 
2747  switch (type) {
2748  case DEFINED_IVAR:
2749  if (rb_ivar_defined(GET_SELF(), SYM2ID(obj))) {
2750  expr_type = DEFINED_IVAR;
2751  }
2752  break;
2753  case DEFINED_IVAR2:
2754  klass = vm_get_cbase(GET_EP());
2755  break;
2756  case DEFINED_GVAR:
2757  if (rb_gvar_defined(rb_global_entry(SYM2ID(obj)))) {
2758  expr_type = DEFINED_GVAR;
2759  }
2760  break;
2761  case DEFINED_CVAR: {
2762  const rb_cref_t *cref = rb_vm_get_cref(GET_EP());
2763  klass = vm_get_cvar_base(cref, GET_CFP());
2764  if (rb_cvar_defined(klass, SYM2ID(obj))) {
2765  expr_type = DEFINED_CVAR;
2766  }
2767  break;
2768  }
2769  case DEFINED_CONST:
2770  klass = v;
2771  if (vm_get_ev_const(th, klass, SYM2ID(obj), 1)) {
2772  expr_type = DEFINED_CONST;
2773  }
2774  break;
2775  case DEFINED_FUNC:
2776  klass = CLASS_OF(v);
2777  if (rb_method_boundp(klass, SYM2ID(obj), 0)) {
2778  expr_type = DEFINED_METHOD;
2779  }
2780  else {
2781  expr_type = check_respond_to_missing(obj, v);
2782  }
2783  break;
2784  case DEFINED_METHOD:{
2785  VALUE klass = CLASS_OF(v);
2786  const rb_method_entry_t *me = rb_method_entry(klass, SYM2ID(obj));
2787 
2788  if (me) {
2789  switch (METHOD_ENTRY_VISI(me)) {
2790  case METHOD_VISI_PRIVATE:
2791  break;
2792  case METHOD_VISI_PROTECTED:
2793  if (!rb_obj_is_kind_of(GET_SELF(), rb_class_real(klass))) {
2794  break;
2795  }
2796  case METHOD_VISI_PUBLIC:
2797  expr_type = DEFINED_METHOD;
2798  break;
2799  default:
2800  rb_bug("vm_defined: unreachable: %u", (unsigned int)METHOD_ENTRY_VISI(me));
2801  }
2802  }
2803  else {
2804  expr_type = check_respond_to_missing(obj, v);
2805  }
2806  break;
2807  }
2808  case DEFINED_YIELD:
2810  expr_type = DEFINED_YIELD;
2811  }
2812  break;
2813  case DEFINED_ZSUPER:
2814  {
2816 
2817  if (me) {
2819  ID id = me->def->original_id;
2820 
2821  if (rb_method_boundp(klass, id, 0)) {
2822  expr_type = DEFINED_ZSUPER;
2823  }
2824  }
2825  }
2826  break;
2827  case DEFINED_REF:{
2828  if (vm_getspecial(th, GET_LEP(), Qfalse, FIX2INT(obj)) != Qnil) {
2829  expr_type = DEFINED_GVAR;
2830  }
2831  break;
2832  }
2833  default:
2834  rb_bug("unimplemented defined? type (VM)");
2835  break;
2836  }
2837 
2838  if (expr_type != 0) {
2839  if (needstr != Qfalse) {
2840  return rb_iseq_defined_string(expr_type);
2841  }
2842  else {
2843  return Qtrue;
2844  }
2845  }
2846  else {
2847  return Qnil;
2848  }
2849 }
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1906
rb_control_frame_t * cfp
Definition: vm_core.h:708
unsigned int stack_max
Definition: vm_core.h:387
static int VM_ENV_ESCAPED_P(const VALUE *ep)
Definition: vm_core.h:1097
static VALUE vm_call_bmethod_body(rb_thread_t *th, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, const VALUE *argv)
#define T_OBJECT
Definition: ruby.h:491
rb_control_frame_t * rb_vm_get_ruby_level_next_cfp(const rb_thread_t *th, const rb_control_frame_t *cfp)
Definition: vm.c:489
static const VALUE * VM_CF_LEP(const rb_control_frame_t *const cfp)
Definition: vm.c:62
#define UNDEFINED_METHOD_ENTRY_P(me)
Definition: method.h:171
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
Definition: symbol.c:923
#define RUBY_VM_CHECK_INTS(th)
Definition: vm_core.h:1569
VALUE rb_reg_match_last(VALUE)
Definition: re.c:1699
const VALUE * ep
Definition: vm_core.h:636
static VALUE call_cfunc_0(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1196
RUBY_EXTERN VALUE rb_cFloat
Definition: ruby.h:1889
#define RARRAY_LEN(a)
Definition: ruby.h:1026
#define RUBY_EVENT_C_RETURN
Definition: ruby.h:2065
VALUE rb_proc_call_with_block(VALUE, int argc, const VALUE *argv, VALUE)
Definition: proc.c:899
void rb_bug(const char *fmt,...)
Definition: error.c:482
rb_method_type_t type
Definition: method.h:148
static VALUE vm_defined(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE obj, VALUE needstr, VALUE v)
static VALUE vm_get_iclass(rb_control_frame_t *cfp, VALUE klass)
#define FALSE
Definition: nkf.h:174
ruby_tag_type
Definition: vm_core.h:152
static VALUE make_no_method_exception(VALUE exc, VALUE format, VALUE obj, int argc, const VALUE *argv, int priv)
Definition: vm_eval.c:677
VALUE rb_str_equal(VALUE str1, VALUE str2)
Definition: string.c:3105
const rb_callable_method_entry_t * rb_callable_method_entry_with_refinements(VALUE klass, ID id)
Definition: vm_method.c:873
rb_method_attr_t attr
Definition: method.h:155
static VALUE vm_throw_continue(rb_thread_t *th, VALUE err)
static int setup_parameters_complex(rb_thread_t *const th, const rb_iseq_t *const iseq, struct rb_calling_info *const calling, const struct rb_call_info *ci, VALUE *const locals, const enum arg_setup_type arg_setup_type)
Definition: vm_args.c:518
static VALUE vm_call_zsuper(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, VALUE klass)
#define VM_CALL_FCALL
Definition: vm_core.h:905
Definition: constant.h:31
Definition: st.h:79
static VALUE lep_svar_get(rb_thread_t *th, const VALUE *lep, rb_num_t key)
const rb_callable_method_entry_t * me
Definition: vm_core.h:246
#define GET_LEP()
Definition: vm_insnhelper.h:88
static int vm_env_cref_by_cref(const VALUE *ep)
static rb_cref_t * rb_vm_get_cref(const VALUE *ep)
static int max(int a, int b)
Definition: strftime.c:142
const VALUE owner
Definition: method.h:63
static VALUE vm_get_const_base(const VALUE *ep)
static VALUE find_refinement(VALUE refinements, VALUE klass)
#define d1
static const rb_iseq_t * def_iseq_ptr(rb_method_definition_t *def)
#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)
Definition: vm_core.h:1172
#define GetProcPtr(obj, ptr)
Definition: vm_core.h:863
static int VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
Definition: vm_core.h:1031
const VALUE cref_or_me
Definition: internal.h:769
#define QUOTE_ID(id)
Definition: internal.h:1473
#define vm_check_frame(a, b, c, d)
#define CLASS_OF(v)
Definition: ruby.h:453
static VALUE vm_call_iseq_setup_tailcall(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, int opt_pc)
#define T_MODULE
Definition: ruby.h:494
static void lep_svar_set(rb_thread_t *th, const VALUE *lep, rb_num_t key, VALUE val)
rb_cref_t *const cref
Definition: method.h:124
enum method_missing_reason method_missing_reason
Definition: vm_core.h:812
VALUE symbol
Definition: vm_core.h:625
union iseq_inline_cache_entry::@193 ic_value
#define Qtrue
Definition: ruby.h:437
static const rb_iseq_t * rb_iseq_check(const rb_iseq_t *iseq)
Definition: vm_core.h:416
vm_check_match_type
Definition: vm_core.h:894
struct rb_method_definition_struct *const def
Definition: method.h:61
static int block_proc_is_lambda(const VALUE procval)
VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0)
Definition: gc.c:1971
static int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2)
static int CREF_PUSHED_BY_EVAL(const rb_cref_t *cref)
Definition: eval_intern.h:228
static VALUE call_cfunc_10(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
unsigned int end
Definition: iseq.h:157
struct rb_iseq_constant_body::@196::@197 flags
static void vm_expandarray(rb_control_frame_t *cfp, VALUE ary, rb_num_t num, int flag)
#define sysstack_error
Definition: vm_core.h:1486
VALUE rb_eTypeError
Definition: error.c:762
#define rb_check_arity
Definition: intern.h:303
static void lep_svar_write(rb_thread_t *th, const VALUE *lep, const struct vm_svar *svar)
VALUE rb_vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, int argc, const VALUE *argv, VALUE passed_block_handler)
Definition: vm.c:1150
#define rb_long2int(n)
Definition: ruby.h:319
#define VM_BLOCK_HANDLER_NONE
Definition: vm_core.h:1070
static const rb_callable_method_entry_t * aliased_callable_method_entry(const rb_callable_method_entry_t *me)
#define GET_BLOCK_HANDLER()
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:54
#define SYM2ID(x)
Definition: ruby.h:384
const VALUE owner
Definition: method.h:55
static VALUE VM_BH_TO_SYMBOL(VALUE block_handler)
Definition: vm_core.h:1377
VALUE rb_iseq_defined_string(enum defined_type type)
Definition: iseq.c:2183
struct rb_iseq_constant_body * body
Definition: vm_core.h:395
#define UNDEF
static void argument_arity_error(rb_thread_t *th, const rb_iseq_t *iseq, const int miss_argc, const int min_argc, const int max_argc)
Definition: vm_args.c:722
static VALUE vm_call0(rb_thread_t *, VALUE, ID, int, const VALUE *, const rb_callable_method_entry_t *)
static VALUE vm_invoke_block(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci)
arg_setup_type
Definition: vm_args.c:30
unsigned int index
Definition: vm_core.h:251
#define ROBJECT_IV_INDEX_TBL(o)
Definition: ruby.h:908
static const rb_cref_t * vm_get_const_key_cref(const VALUE *ep)
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2207
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1260
unsigned int flag
Definition: vm_core.h:217
static void method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *def, void *opts)
static VALUE vm_call_method_missing(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *orig_ci, struct rb_call_cache *orig_cc)
struct rb_iseq_constant_body::@196 param
parameter information
#define RB_GC_GUARD(v)
Definition: ruby.h:552
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:690
static rb_callable_method_entry_t * check_method_entry(VALUE obj, int can_be_svar)
int rb_const_defined(VALUE, ID)
Definition: variable.c:2580
#define VM_PROFILE_UP(x)
#define VM_CALL_ARGS_SPLAT
Definition: vm_core.h:903
#define SET_SP(x)
Definition: vm_insnhelper.h:92
const VALUE value
Definition: constant.h:34
void rb_vm_localjump_error(const char *mesg, VALUE value, int reason)
Definition: vm.c:1392
#define FIXNUM_2_P(a, b)
#define SDR()
Definition: vm_core.h:1414
static VALUE CREF_REFINEMENTS(const rb_cref_t *cref)
Definition: eval_intern.h:216
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:867
#define RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)
Definition: vm_core.h:1178
#define STACK_ADDR_FROM_TOP(n)
Definition: vm_insnhelper.h:36
#define T_ARRAY
Definition: ruby.h:498
#define GET_GLOBAL_METHOD_STATE()
st_data_t st_index_t
Definition: st.h:50
#define st_lookup
Definition: regint.h:185
vm_call_handler call
Definition: vm_core.h:248
static VALUE vm_getinstancevariable(VALUE obj, ID id, IC ic)
#define VM_CALL_KWARG
Definition: vm_core.h:909
VALUE rb_reg_match_post(VALUE)
Definition: re.c:1681
static VALUE vm_once_clear(VALUE data)
#define ROBJECT_NUMIV(o)
Definition: ruby.h:900
static int VM_ENV_LOCAL_P(const VALUE *ep)
Definition: vm_core.h:1073
#define DEC_SP(x)
Definition: vm_insnhelper.h:94
RUBY_EXTERN VALUE rb_cProc
Definition: ruby.h:1899
static VALUE vm_call_cfunc(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
static int VM_FRAME_RUBYFRAME_P(const rb_control_frame_t *cfp)
Definition: vm_core.h:1061
const VALUE backref
Definition: internal.h:771
#define FIXNUM_P(f)
Definition: ruby.h:365
static VALUE vm_search_const_defined_class(const VALUE cbase, ID id)
static const VALUE * VM_EP_LEP(const VALUE *ep)
Definition: vm.c:26
unsigned int cont
Definition: iseq.h:158
static VALUE vm_call_ivar(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
VALUE rb_ivar_defined(VALUE, ID)
Definition: variable.c:1421
PUREFUNC(static rb_callable_method_entry_t *check_method_entry(VALUE obj, int can_be_svar))
#define VM_CALL_VCALL
Definition: vm_core.h:906
VALUE rb_reg_last_match(VALUE)
Definition: re.c:1636
static VALUE call_cfunc_11(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
#define IS_ARGS_KEYWORD(ci)
Definition: vm_args.c:863
static void vm_env_write_slowpath(const VALUE *ep, int index, VALUE v)
#define VM_ENV_DATA_INDEX_ME_CREF
Definition: vm_core.h:990
rb_method_iseq_t iseq
Definition: method.h:153
#define RUBY_DTRACE_CMETHOD_ENTRY_HOOK(th, klass, id)
Definition: probes_helper.h:37
#define THROW_DATA_P(err)
Definition: internal.h:787
union rb_block::@203 as
static rb_control_frame_t * vm_push_frame(rb_thread_t *th, const rb_iseq_t *iseq, VALUE type, VALUE self, VALUE specval, VALUE cref_or_me, const VALUE *pc, VALUE *sp, int local_size, int stack_max)
static int vm_callee_setup_block_arg(rb_thread_t *th, struct rb_calling_info *calling, const struct rb_call_info *ci, const rb_iseq_t *iseq, VALUE *argv, const enum arg_setup_type arg_setup_type)
const VALUE * ep
Definition: vm_core.h:600
#define GET_THREAD()
Definition: vm_core.h:1513
#define sym(x)
Definition: date_core.c:3721
static VALUE vm_getivar(VALUE obj, ID id, IC ic, struct rb_call_cache *cc, int is_attr)
static void vm_check_if_namespace(VALUE klass)
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:22
VALUE rb_vm_make_proc(rb_thread_t *th, const struct rb_captured_block *captured, VALUE klass)
Definition: vm.c:863
enum iseq_catch_table_entry::catch_type type
const VALUE * iseq_encoded
Definition: vm_core.h:286
void rb_exc_raise(VALUE mesg)
Definition: eval.c:620
int orig_argc
Definition: vm_core.h:218
static const struct rb_captured_block * VM_BH_TO_CAPT_BLOCK(VALUE block_handler)
Definition: vm_core.h:1246
const rb_callable_method_entry_t * rb_vm_frame_method_entry(const rb_control_frame_t *cfp)
#define FL_SINGLETON
Definition: ruby.h:1215
#define GET_CFP()
Definition: vm_insnhelper.h:85
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
static VALUE opt_eq_func(VALUE recv, VALUE obj, CALL_INFO ci, CALL_CACHE cc)
static VALUE vm_get_cbase(const VALUE *ep)
rb_method_cfunc_t cfunc
Definition: method.h:154
static VALUE call_cfunc_15(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
#define BASIC_OP_UNREDEFINED_P(op, klass)
Definition: vm_core.h:588
#define FL_TEST(x, f)
Definition: ruby.h:1284
const rb_iseq_t * iseq
Definition: vm_core.h:634
VALUE rb_cvar_defined(VALUE, ID)
Definition: variable.c:2949
static void vm_search_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, struct rb_call_info *ci, struct rb_call_cache *cc)
struct rb_cref_struct *const next
Definition: method.h:44
static rb_cref_t * check_cref(VALUE obj, int can_be_svar)
#define ROBJECT_IVPTR(o)
Definition: ruby.h:904
static VALUE vm_proc_to_block_handler(VALUE procval)
#define IS_ARGS_SPLAT(ci)
Definition: vm_args.c:862
static VALUE vm_invoke_iseq_block(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, int is_lambda, const struct rb_captured_block *captured)
static int VM_BH_ISEQ_BLOCK_P(VALUE block_handler)
Definition: vm_core.h:1182
enum method_missing_reason method_missing_reason
Definition: vm_core.h:252
VALUE(* vm_call_handler)(struct rb_thread_struct *th, struct rb_control_frame_struct *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
Definition: vm_core.h:238
static int vm_callee_setup_block_arg_arg0_splat(rb_control_frame_t *cfp, const rb_iseq_t *iseq, VALUE *argv, VALUE ary)
unsigned int local_table_size
Definition: vm_core.h:382
VALUE rb_gvar_defined(struct rb_global_entry *)
Definition: variable.c:859
rb_method_alias_t alias
Definition: method.h:156
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
VALUE rb_eRuntimeError
Definition: error.c:761
static VALUE * vm_base_ptr(const rb_control_frame_t *cfp)
#define POPN(n)
Definition: vm_insnhelper.h:34
#define CALLER_SETUP_ARG(cfp, calling, ci)
Definition: vm_args.c:865
RUBY_EXTERN VALUE rb_cBasicObject
Definition: ruby.h:1871
VALUE rb_ary_new(void)
Definition: array.c:493
#define FLONUM_2_P(a, b)
VALUE rb_sym_proc_call(ID mid, int argc, const VALUE *argv, VALUE passed_proc)
Definition: string.c:9716
union rb_call_cache::@195 aux
static VALUE vm_invoke_symbol_block(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, VALUE symbol)
#define RCLASS_ORIGIN(c)
Definition: internal.h:693
#define CHECK_CMP_NAN(a, b)
#define NIL_P(v)
Definition: ruby.h:451
static VALUE VM_BH_FROM_PROC(VALUE procval)
Definition: vm_core.h:1398
static VALUE vm_call_opt_send(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *orig_ci, struct rb_call_cache *orig_cc)
static VALUE vm_call_general(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
ALWAYS_INLINE(static VALUE vm_getivar(VALUE, ID, IC, struct rb_call_cache *, int))
VALUE rb_eNoMethodError
Definition: error.c:770
static VALUE vm_throw(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_num_t throw_state, VALUE throwobj)
struct rb_method_definition_struct *const def
Definition: method.h:53
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:799
static VALUE call_cfunc_2(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static const struct rb_captured_block * VM_BH_TO_ISEQ_BLOCK(VALUE block_handler)
Definition: vm_core.h:1206
Definition: vm_core.h:195
#define GET_SELF()
static const rb_control_frame_t * rb_vm_search_cf_from_ep(const rb_thread_t *const th, const rb_control_frame_t *cfp, const VALUE *const ep)
Definition: vm.c:35
int rb_public_const_defined_from(VALUE klass, ID id)
Definition: variable.c:2592
int argc
Definition: ruby.c:183
#define Qfalse
Definition: ruby.h:436
static int is_cref(const VALUE v, int can_be_svar)
static vm_call_handler vm_call_iseq_setup_func(const struct rb_call_info *ci, const int param_size, const int local_size)
#define ALLOCA_N(type, n)
Definition: ruby.h:1593
const VALUE defined_class
Definition: method.h:60
#define RUBY_DTRACE_CMETHOD_RETURN_HOOK(th, klass, id)
Definition: probes_helper.h:40
Definition: method.h:50
#define VM_GUARDED_PREV_EP(ep)
Definition: vm_core.h:1069
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1895
Definition: method.h:58
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
#define rb_ary_new4
Definition: intern.h:92
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1845
#define RUBY_EVENT_C_CALL
Definition: ruby.h:2064
int err
Definition: win32.c:135
static VALUE call_cfunc_1(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static VALUE vm_block_handler_to_proc(rb_thread_t *th, VALUE block_handler)
VALUE rb_struct_aset(VALUE, VALUE, VALUE)
Definition: struct.c:923
rb_serial_t method_state
Definition: vm_core.h:242
static VALUE call_cfunc_9(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static VALUE call_cfunc_3(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
#define TOPN(n)
Definition: vm_insnhelper.h:33
void rb_vm_pop_frame(rb_thread_t *th)
struct iseq_inline_storage_entry::@194 once
#define VM_ENV_DATA_INDEX_SPECVAL
Definition: vm_core.h:991
VALUE rb_obj_equal(VALUE obj1, VALUE obj2)
Definition: object.c:139
unsigned long rb_num_t
Definition: vm_core.h:150
rb_method_type_t
Definition: method.h:100
static VALUE vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
#define RCLASS_REFINED_CLASS(c)
Definition: internal.h:694
static VALUE vm_invoke_bmethod(rb_thread_t *th, rb_proc_t *proc, VALUE self, int argc, const VALUE *argv, VALUE block_handler)
Definition: vm.c:1143
#define VM_CALL_SUPER
Definition: vm_core.h:911
Definition: vm_core.h:186
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:2335
static rb_cref_t * vm_cref_push(rb_thread_t *th, VALUE klass, const VALUE *ep, int pushed_by_eval)
static rb_cref_t * vm_env_cref(const VALUE *ep)
VALUE rb_ary_to_ary(VALUE obj)
Definition: array.c:1555
#define STRING_REDEFINED_OP_FLAG
Definition: vm_core.h:577
#define RARRAY_CONST_PTR(a)
Definition: ruby.h:1028
static const struct rb_block * vm_proc_block(VALUE procval)
Definition: vm_core.h:1311
static void VM_FORCE_WRITE(const VALUE *ptr, VALUE v)
Definition: vm_core.h:1148
#define CI_SET_FASTPATH(cc, func, enabled)
static int vm_pop_frame(rb_thread_t *th, rb_control_frame_t *cfp, const VALUE *ep)
VALUE(* invoker)(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
Definition: method.h:129
union rb_method_definition_struct::@144 body
#define TRUE
Definition: nkf.h:175
defined_type
Definition: iseq.h:224
static VALUE call_cfunc_13(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static VALUE vm_once_exec(VALUE iseq)
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1440
const rb_callable_method_entry_t * rb_callable_method_entry(VALUE klass, ID id)
Definition: vm_method.c:834
static rb_cref_t * CREF_NEXT(const rb_cref_t *cref)
Definition: eval_intern.h:204
#define RICLASS_IS_ORIGIN
Definition: internal.h:697
#define VM_ASSERT(expr)
Definition: vm_core.h:54
const rb_method_entry_t * rb_method_entry(VALUE klass, ID id)
Definition: vm_method.c:796
static VALUE vm_get_cvar_base(const rb_cref_t *cref, rb_control_frame_t *cfp)
void rb_raise_method_missing(rb_thread_t *th, int argc, const VALUE *argv, VALUE obj, int call_status)
Definition: vm_eval.c:776
static enum rb_block_handler_type vm_block_handler_type(VALUE block_handler)
Definition: vm_core.h:1254
VALUE(* func)(ANYARGS)
Definition: method.h:128
#define MEMMOVE(p1, p2, type, n)
Definition: ruby.h:1662
static VALUE CREF_CLASS(const rb_cref_t *cref)
Definition: eval_intern.h:198
VALUE proc
Definition: vm_core.h:626
#define VMDEBUG
VM Debug Level.
Definition: vm_core.h:37
#define VM_ENV_DATA_SIZE
Definition: vm_core.h:988
static void VM_STACK_ENV_WRITE(const VALUE *ep, int index, VALUE v)
Definition: vm_core.h:1161
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1364
#define T_IMEMO
Definition: ruby.h:511
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
#define VM_CHECK_MODE
Definition: vm_core.h:20
#define Qnil
Definition: ruby.h:438
rb_control_frame_t *FUNC_FASTCALL() rb_vm_opt_struct_aref(rb_thread_t *th, rb_control_frame_t *reg_cfp)
#define LIKELY(x)
Definition: ffi_common.h:125
VALUE rb_sym_to_proc(VALUE sym)
Definition: proc.c:1203
#define METHOD_ENTRY_VISI(me)
Definition: method.h:66
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
int rb_autoloading_value(VALUE mod, ID id, VALUE *value)
Definition: variable.c:2062
unsigned long VALUE
Definition: ruby.h:85
method_missing_reason
Definition: vm_core.h:203
#define EXEC_EVENT_HOOK(th_, flag_, self_, id_, called_id_, klass_, data_)
Definition: vm_core.h:1628
static void vm_search_method(const struct rb_call_info *ci, struct rb_call_cache *cc, VALUE recv)
#define RBASIC(obj)
Definition: ruby.h:1204
Definition: iseq.h:146
const VALUE defined_class
Definition: method.h:52
void rb_obj_copy_ivar(VALUE dest, VALUE obj)
Definition: object.c:258
#define FIX2INT(x)
Definition: ruby.h:686
VALUE ruby_vm_special_exception_copy(VALUE exc)
Definition: vm_insnhelper.c:25
static VALUE find_defined_class_by_owner(VALUE current_class, VALUE target_owner)
ID called_id
Definition: method.h:62
#define VM_CALL_OPT_SEND
Definition: vm_core.h:912
static enum rb_block_type vm_block_type(const struct rb_block *block)
Definition: vm_core.h:1280
const struct rb_method_entry_struct *const original_me
Definition: method.h:139
static VALUE VM_BH_FROM_IFUNC_BLOCK(const struct rb_captured_block *captured)
Definition: vm_core.h:1230
#define VM_ENV_DATA_INDEX_FLAGS
Definition: vm_core.h:992
static VALUE check_match(VALUE pattern, VALUE target, enum vm_check_match_type type)
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:439
static VALUE call_cfunc_m2(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static const rb_method_cfunc_t * vm_method_cfunc_entry(const rb_callable_method_entry_t *me)
const struct vm_ifunc * ifunc
Definition: vm_core.h:603
static VALUE vm_search_normal_superclass(VALUE klass)
void rb_const_warn_if_deprecated(const rb_const_entry_t *ce, VALUE klass, ID id)
Definition: variable.c:2263
#define isnan(x)
Definition: win32.h:346
Definition: iseq.h:151
static VALUE call_cfunc_12(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static VALUE call_cfunc_5(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static rb_control_frame_t * current_method_entry(rb_thread_t *th, rb_control_frame_t *cfp)
static rb_method_definition_t * method_definition_create(rb_method_type_t type, ID mid)
static VALUE vm_call_iseq_setup_tailcall_0start(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
static VALUE double_cmp_le(double a, double b)
VALUE rb_equal_opt(VALUE obj1, VALUE obj2)
rb_serial_t ic_serial
Definition: vm_core.h:187
enum rb_iseq_constant_body::iseq_type type
const void * block_code
Definition: vm_core.h:637
static void vm_env_write(const VALUE *ep, int index, VALUE v)
#define RUBY_VM_END_CONTROL_FRAME(th)
Definition: vm_core.h:1174
static VALUE double_cmp_gt(double a, double b)
VALUE rb_class_real(VALUE cl)
Definition: object.c:207
const rb_callable_method_entry_t * passed_bmethod_me
Definition: vm_core.h:720
struct rb_captured_block captured
Definition: vm_core.h:624
#define rb_funcallv
Definition: console.c:21
register unsigned int len
Definition: zonetab.h:51
static VALUE vm_getspecial(rb_thread_t *th, const VALUE *lep, rb_num_t key, rb_num_t type)
static VALUE call_cfunc_7(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static unsigned long VM_FRAME_TYPE(const rb_control_frame_t *cfp)
Definition: vm_core.h:1025
static VALUE double_cmp_ge(double a, double b)
const rb_callable_method_entry_t * rb_callable_method_entry_without_refinements(VALUE klass, ID id)
Definition: vm_method.c:887
static enum defined_type check_respond_to_missing(VALUE obj, VALUE v)
static VALUE vm_invoke_ifunc_block(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, const struct rb_captured_block *captured)
const VALUE * root_lep
Definition: vm_core.h:730
int8_t is_lambda
Definition: vm_core.h:870
static int VM_BH_FROM_CFP_P(VALUE block_handler, const rb_control_frame_t *cfp)
Definition: vm.c:162
#define rb_exc_new3
Definition: intern.h:246
void rb_vm_rewrite_cref(rb_cref_t *cref, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr)
static VALUE vm_call_method_nome(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
static rb_cref_t * cref_replace_with_duplicated_cref_each_frame(const VALUE *vptr, int can_be_svar, VALUE parent)
static VALUE VM_BH_TO_PROC(VALUE block_handler)
Definition: vm_core.h:1391
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2586
rb_const_entry_t * rb_const_lookup(VALUE klass, ID id)
Definition: variable.c:3171
#define RFLOAT_VALUE(v)
Definition: ruby.h:940
#define TAG_RETRY
Definition: vm_core.h:166
#define UNLIMITED_ARGUMENTS
Definition: intern.h:44
static void vm_setinstancevariable(VALUE obj, ID id, VALUE val, IC ic)
unsigned int start
Definition: iseq.h:156
#define TAG_THROW
Definition: vm_core.h:169
#define RCLASS_SUPER(c)
Definition: classext.h:16
const struct iseq_catch_table * catch_table
Definition: vm_core.h:366
const rb_callable_method_entry_t * rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class)
Definition: vm_method.c:411
VALUE root_svar
Definition: vm_core.h:731
#define RARRAY_AREF(a, i)
Definition: ruby.h:1040
static const rb_callable_method_entry_t * refined_method_callable_without_refinement(const rb_callable_method_entry_t *me)
static VALUE vm_callee_setup_block_arg_arg0_check(VALUE *argv)
rb_serial_t class_serial
Definition: vm_core.h:243
static VALUE call_cfunc_m1(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
size_t index
Definition: vm_core.h:190
static rb_cref_t * vm_cref_new(VALUE klass, rb_method_visibility_t visi, int module_func, rb_cref_t *prev_cref, int pushed_by_eval)
Definition: vm.c:211
rb_control_frame_t * rb_vm_push_frame(rb_thread_t *th, const rb_iseq_t *iseq, VALUE type, VALUE self, VALUE specval, VALUE cref_or_me, const VALUE *pc, VALUE *sp, int local_size, int stack_max)
static int simple_iseq_p(const rb_iseq_t *iseq)
#define RBASIC_CLASS(obj)
Definition: ruby.h:878
#define ANYARGS
Definition: defines.h:173
struct rb_call_info ci
Definition: vm_core.h:227
#define INC_SP(x)
Definition: vm_insnhelper.h:93
#define FLOAT_REDEFINED_OP_FLAG
Definition: vm_core.h:576
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:635
VALUE rb_struct_aref(VALUE, VALUE)
Definition: struct.c:896
void rb_error_arity(int argc, int min, int max)
#define FUNC_FASTCALL(x)
Definition: vm_core.h:938
struct rb_iseq_struct * local_iseq
Definition: vm_core.h:370
static int vm_callee_setup_arg(rb_thread_t *th, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, const rb_iseq_t *iseq, VALUE *argv, int param_size, int local_size)
VALUE block_handler
Definition: vm_core.h:232
static rb_cref_t * vm_cref_dup(const rb_cref_t *cref)
Definition: vm.c:223
static VALUE call_cfunc_14(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static int THROW_DATA_STATE(const struct vm_throw_data *obj)
static VALUE rb_arity_error_new(int argc, int min, int max)
#define RTEST(v)
Definition: ruby.h:450
const VALUE * pc
Definition: vm_core.h:632
static VALUE VM_BH_FROM_SYMBOL(VALUE symbol)
Definition: vm_core.h:1384
int rb_method_basic_definition_p(VALUE, ID)
Definition: vm_method.c:1880
Definition: id.h:89
#define RB_BUILTIN_TYPE(x)
Definition: ruby.h:517
static VALUE vm_setivar(VALUE obj, ID id, VALUE val, IC ic, struct rb_call_cache *cc, int is_attr)
struct rb_thread_struct * running_thread
Definition: vm_core.h:197
static VALUE vm_call_method_each_type(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
static VALUE vm_call_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
#define INTEGER_REDEFINED_OP_FLAG
Definition: vm_core.h:575
#define GET_EP()
Definition: vm_insnhelper.h:86
static VALUE call_cfunc_6(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
#define VM_UNREACHABLE(func)
Definition: vm_core.h:55
const char * rb_obj_info(VALUE obj)
Definition: gc.c:9369
#define CHECK(sub)
Definition: compile.c:408
static VALUE vm_get_ev_const(rb_thread_t *th, VALUE orig_klass, ID id, int is_defined)
static void VM_ENV_FLAGS_UNSET(const VALUE *ep, VALUE flag)
Definition: vm_core.h:1009
void rb_notimplement(void)
Definition: error.c:2253
#define UNLIKELY(x)
Definition: ffi_common.h:126
static VALUE vm_yield_with_cfunc(rb_thread_t *th, const struct rb_captured_block *captured, VALUE self, int argc, const VALUE *argv, VALUE block_handler)
static enum method_missing_reason ci_missing_reason(const struct rb_call_info *ci)
VALUE(* func)(ANYARGS)
Definition: internal.h:803
#define VM_CALL_TAILCALL
Definition: vm_core.h:910
const rb_iseq_t * iseq
Definition: vm_core.h:602
void rb_gc_writebarrier_remember(VALUE obj)
Definition: gc.c:5967
static VALUE vm_call_attrset(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
static VALUE call_cfunc_4(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
const void * data
Definition: internal.h:804
static rb_cref_t * method_entry_cref(rb_callable_method_entry_t *me)
#define T_CLASS
Definition: ruby.h:492
const struct rb_iseq_struct * parent_iseq
Definition: vm_core.h:369
static VALUE vm_call_bmethod(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
VALUE rb_reg_match_pre(VALUE)
Definition: re.c:1654
#define TAG_RETURN
Definition: vm_core.h:163
static VALUE VM_BH_FROM_ISEQ_BLOCK(const struct rb_captured_block *captured)
Definition: vm_core.h:1198
static VALUE vm_call_iseq_setup_2(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, int opt_pc, int param_size, int local_size)
#define ID2SYM(x)
Definition: ruby.h:383
static VALUE vm_yield_with_symbol(rb_thread_t *th, VALUE symbol, int argc, const VALUE *argv, VALUE block_handler)
#define GET_SP()
Definition: vm_insnhelper.h:91
imemo_type
Definition: internal.h:737
static VALUE vm_call_iseq_setup_normal(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc, int opt_pc, int param_size, int local_size)
NOINLINE(static void vm_env_write_slowpath(const VALUE *ep, int index, VALUE v))
static struct rb_captured_block * VM_CFP_TO_CAPTURED_BLOCK(const rb_control_frame_t *cfp)
Definition: vm.c:146
static rb_cref_t * vm_cref_new_use_prev(VALUE klass, rb_method_visibility_t visi, int module_func, rb_cref_t *prev_cref, int pushed_by_eval)
Definition: vm.c:217
void rb_warning(const char *fmt,...)
Definition: error.c:250
static rb_control_frame_t * vm_get_ruby_level_caller_cfp(const rb_thread_t *th, const rb_control_frame_t *cfp)
#define rb_check_frozen(obj)
Definition: intern.h:276
static void vm_ensure_not_refinement_module(VALUE self)
static void vm_super_outside(void)
#define GET_ISEQ()
void rb_vm_env_write(const VALUE *ep, int index, VALUE v)
VALUE rb_str_intern(VALUE)
Definition: symbol.c:661
static VALUE vm_make_proc_with_iseq(const rb_iseq_t *blockiseq)
rb_control_frame_t *FUNC_FASTCALL() rb_vm_opt_struct_aset(rb_thread_t *th, rb_control_frame_t *reg_cfp)
enum rb_method_definition_struct::@144::method_optimized_type optimize_type
#define TAG_BREAK
Definition: vm_core.h:164
const rb_iseq_t *const iseqptr
Definition: method.h:123
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1249
VALUE rb_public_const_get_from(VALUE klass, ID id)
Definition: variable.c:2347
static int check_cfunc(const rb_callable_method_entry_t *me, VALUE(*func)())
#define CHECK_VM_STACK_OVERFLOW(cfp, margin)
Definition: vm_core.h:1497
static VALUE VM_CF_BLOCK_HANDLER(const rb_control_frame_t *const cfp)
Definition: vm.c:75
static VALUE vm_call_opt_call(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
static struct vm_svar * lep_svar(rb_thread_t *th, const VALUE *lep)
Definition: id.h:88
int rb_method_boundp(VALUE, ID, int)
Definition: vm_method.c:1069
unsigned int size
Definition: vm_core.h:324
#define SYMBOL_P(x)
Definition: ruby.h:382
#define stat(path, st)
Definition: win32.h:183
const struct rb_method_entry_struct *const orig_me
Definition: method.h:143
#define TAG_RAISE
Definition: vm_core.h:168
static VALUE vm_call_iseq_setup_normal_0start(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
static VALUE vm_call_cfunc_with_frame(rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
#define NULL
Definition: _sdbm.c:102
static VALUE VM_ENV_ENVVAL(const VALUE *ep)
Definition: vm_core.h:1114
struct rb_global_entry * rb_global_entry(ID)
Definition: variable.c:481
rb_method_refined_t refined
Definition: method.h:157
#define Qundef
Definition: ruby.h:439
#define T_ICLASS
Definition: ruby.h:493
#define RCLASS_SERIAL(c)
Definition: internal.h:695
static VALUE vm_throw_start(rb_thread_t *const th, rb_control_frame_t *const reg_cfp, enum ruby_tag_type state, const int flag, const rb_num_t level, const VALUE throwobj)
void rb_gc_verify_internal_consistency(void)
Definition: gc.c:5298
static VALUE vm_call_iseq_setup(rb_thread_t *th, rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, struct rb_call_cache *cc)
#define ruby_verbose
Definition: ruby.h:1792
static const VALUE * VM_ENV_PREV_EP(const VALUE *ep)
Definition: vm_core.h:1079
void rb_warn(const char *fmt,...)
Definition: error.c:221
#define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin)
Definition: vm_core.h:1495
#define bp()
Definition: vm_debug.h:25
VALUE rb_eArgError
Definition: error.c:763
union rb_captured_block::@202 code
VALUE rb_autoload_load(VALUE, ID)
Definition: variable.c:2198
static int vm_yield_setup_args(rb_thread_t *th, const rb_iseq_t *iseq, const int argc, VALUE *argv, VALUE block_handler, enum arg_setup_type arg_setup_type)
static VALUE call_cfunc_8(VALUE(*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv)
static VALUE double_cmp_lt(double a, double b)
#define RB_OBJ_WRITE(a, slot, b)
Definition: ruby.h:1437
VALUE rb_reg_nth_match(int, VALUE)
Definition: re.c:1610
static rb_cref_t * vm_cref_replace_with_duplicated_cref(const VALUE *ep)
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1273
static void vm_stackoverflow(void)
Definition: vm_insnhelper.c:33
char ** argv
Definition: ruby.c:184
static struct vm_svar * svar_new(VALUE obj)
static const struct rb_captured_block * VM_BH_TO_IFUNC_BLOCK(VALUE block_handler)
Definition: vm_core.h:1238
VALUE rb_obj_class(VALUE)
Definition: object.c:229
static struct vm_throw_data * THROW_DATA_NEW(VALUE val, const rb_control_frame_t *cf, VALUE st)