Ruby  1.9.3p551(2014-11-13revision48407)
vm_insnhelper.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  vm_insnhelper.c - instruction helper functions.
4 
5  $Author: usa $
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 
17 /* control stack frame */
18 
19 #ifndef INLINE
20 #define INLINE inline
21 #endif
22 
24 
25 static inline rb_control_frame_t *
26 vm_push_frame(rb_thread_t * th, const rb_iseq_t * iseq,
27  VALUE type, VALUE self, VALUE specval,
28  const VALUE *pc, VALUE *sp, VALUE *lfp,
29  int local_size)
30 {
31  rb_control_frame_t * const cfp = th->cfp - 1;
32  int i;
33 
34  if ((void *)(sp + local_size) >= (void *)cfp) {
36  }
37  th->cfp = cfp;
38  /* setup vm value stack */
39 
40  /* nil initialize */
41  for (i=0; i < local_size; i++) {
42  *sp = Qnil;
43  sp++;
44  }
45 
46  /* set special val */
47  *sp = GC_GUARDED_PTR(specval);
48 
49  if (lfp == 0) {
50  lfp = sp;
51  }
52 
53  /* setup vm control frame stack */
54 
55  cfp->pc = (VALUE *)pc;
56  cfp->sp = sp + 1;
57  cfp->bp = sp + 1;
58  cfp->iseq = (rb_iseq_t *) iseq;
59  cfp->flag = type;
60  cfp->self = self;
61  cfp->lfp = lfp;
62  cfp->dfp = sp;
63  cfp->block_iseq = 0;
64  cfp->proc = 0;
65  cfp->me = 0;
66 
67 #define COLLECT_PROFILE 0
68 #if COLLECT_PROFILE
69  cfp->prof_time_self = clock();
70  cfp->prof_time_chld = 0;
71 #endif
72 
73  if (VMDEBUG == 2) {
74  SDR();
75  }
76 
77  return cfp;
78 }
79 
80 static inline void
82 {
83 #if COLLECT_PROFILE
84  rb_control_frame_t *cfp = th->cfp;
85 
86  if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
87  VALUE current_time = clock();
88  rb_control_frame_t *cfp = th->cfp;
89  cfp->prof_time_self = current_time - cfp->prof_time_self;
90  (cfp+1)->prof_time_chld += cfp->prof_time_self;
91 
92  cfp->iseq->profile.count++;
93  cfp->iseq->profile.time_cumu = cfp->prof_time_self;
94  cfp->iseq->profile.time_self = cfp->prof_time_self - cfp->prof_time_chld;
95  }
96  else if (0 /* c method? */) {
97 
98  }
99 #endif
101 
102  if (VMDEBUG == 2) {
103  SDR();
104  }
105 }
106 
107 /* method dispatch */
108 
109 NORETURN(static void argument_error(const rb_iseq_t *iseq, int miss_argc, int correct_argc));
110 static void
111 argument_error(const rb_iseq_t *iseq, int miss_argc, int correct_argc)
112 {
113  VALUE mesg = rb_sprintf("wrong number of arguments (%d for %d)", miss_argc, correct_argc);
114  VALUE exc = rb_exc_new3(rb_eArgError, mesg);
115  VALUE bt = rb_make_backtrace();
116  VALUE err_line = 0;
117 
118  if (iseq) {
119  int line_no = 1;
120 
121  if (iseq->insn_info_size) {
122  line_no = iseq->insn_info_table[0].line_no;
123  }
124 
125  err_line = rb_sprintf("%s:%d:in `%s'",
126  RSTRING_PTR(iseq->filename),
127  line_no, RSTRING_PTR(iseq->name));
128  rb_funcall(bt, rb_intern("unshift"), 1, err_line);
129  }
130 
131  rb_funcall(exc, rb_intern("set_backtrace"), 1, bt);
132  rb_exc_raise(exc);
133 }
134 
135 #define VM_CALLEE_SETUP_ARG(ret, th, iseq, orig_argc, orig_argv, block) \
136  if (LIKELY((iseq)->arg_simple & 0x01)) { \
137  /* simple check */ \
138  if ((orig_argc) != (iseq)->argc) { \
139  argument_error((iseq), (orig_argc), (iseq)->argc); \
140  } \
141  (ret) = 0; \
142  } \
143  else { \
144  (ret) = vm_callee_setup_arg_complex((th), (iseq), (orig_argc), (orig_argv), (block)); \
145  }
146 
147 static inline int
149  int orig_argc, VALUE * orig_argv,
150  const rb_block_t **block)
151 {
152  const int m = iseq->argc;
153  int argc = orig_argc;
154  VALUE *argv = orig_argv;
155  rb_num_t opt_pc = 0;
156 
157  th->mark_stack_len = argc + iseq->arg_size;
158 
159  /* mandatory */
160  if (argc < (m + iseq->arg_post_len)) { /* check with post arg */
161  argument_error(iseq, argc, m + iseq->arg_post_len);
162  }
163 
164  argv += m;
165  argc -= m;
166 
167  /* post arguments */
168  if (iseq->arg_post_len) {
169  if (!(orig_argc < iseq->arg_post_start)) {
170  VALUE *new_argv = ALLOCA_N(VALUE, argc);
171  MEMCPY(new_argv, argv, VALUE, argc);
172  argv = new_argv;
173  }
174 
175  MEMCPY(&orig_argv[iseq->arg_post_start], &argv[argc -= iseq->arg_post_len],
176  VALUE, iseq->arg_post_len);
177  }
178 
179  /* opt arguments */
180  if (iseq->arg_opts) {
181  const int opts = iseq->arg_opts - 1 /* no opt */;
182 
183  if (iseq->arg_rest == -1 && argc > opts) {
184  argument_error(iseq, orig_argc, m + opts + iseq->arg_post_len);
185  }
186 
187  if (argc > opts) {
188  argc -= opts;
189  argv += opts;
190  opt_pc = iseq->arg_opt_table[opts]; /* no opt */
191  }
192  else {
193  int i;
194  for (i = argc; i<opts; i++) {
195  orig_argv[i + m] = Qnil;
196  }
197  opt_pc = iseq->arg_opt_table[argc];
198  argc = 0;
199  }
200  }
201 
202  /* rest arguments */
203  if (iseq->arg_rest != -1) {
204  orig_argv[iseq->arg_rest] = rb_ary_new4(argc, argv);
205  argc = 0;
206  }
207 
208  /* block arguments */
209  if (block && iseq->arg_block != -1) {
210  VALUE blockval = Qnil;
211  const rb_block_t *blockptr = *block;
212 
213  if (argc != 0) {
214  argument_error(iseq, orig_argc, m + iseq->arg_post_len);
215  }
216 
217  if (blockptr) {
218  /* make Proc object */
219  if (blockptr->proc == 0) {
220  rb_proc_t *proc;
221  blockval = rb_vm_make_proc(th, blockptr, rb_cProc);
222  GetProcPtr(blockval, proc);
223  *block = &proc->block;
224  }
225  else {
226  blockval = blockptr->proc;
227  }
228  }
229 
230  orig_argv[iseq->arg_block] = blockval; /* Proc or nil */
231  }
232 
233  th->mark_stack_len = 0;
234  return (int)opt_pc;
235 }
236 
237 static inline int
239  int argc, rb_iseq_t *blockiseq, rb_block_t **block)
240 {
241  rb_block_t *blockptr = 0;
242 
243  if (block) {
244  if (flag & VM_CALL_ARGS_BLOCKARG_BIT) {
245  rb_proc_t *po;
246  VALUE proc;
247 
248  proc = *(--cfp->sp);
249 
250  if (proc != Qnil) {
251  if (!rb_obj_is_proc(proc)) {
252  VALUE b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
253  if (NIL_P(b) || !rb_obj_is_proc(b)) {
255  "wrong argument type %s (expected Proc)",
256  rb_obj_classname(proc));
257  }
258  proc = b;
259  }
260  GetProcPtr(proc, po);
261  blockptr = &po->block;
262  RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp)->proc = proc;
263  *block = blockptr;
264  }
265  }
266  else if (blockiseq) {
267  blockptr = RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp);
268  blockptr->iseq = blockiseq;
269  blockptr->proc = 0;
270  *block = blockptr;
271  }
272  }
273 
274  /* expand top of stack? */
275  if (flag & VM_CALL_ARGS_SPLAT_BIT) {
276  VALUE ary = *(cfp->sp - 1);
277  VALUE *ptr;
278  int i;
279  VALUE tmp = rb_check_convert_type(ary, T_ARRAY, "Array", "to_a");
280 
281  if (NIL_P(tmp)) {
282  /* do nothing */
283  }
284  else {
285  long len = RARRAY_LEN(tmp);
286  ptr = RARRAY_PTR(tmp);
287  cfp->sp -= 1;
288 
289  CHECK_STACK_OVERFLOW(cfp, len);
290 
291  for (i = 0; i < len; i++) {
292  *cfp->sp++ = ptr[i];
293  }
294  argc += i-1;
295  }
296  }
297 
298  return argc;
299 }
300 
301 static inline VALUE
303  int len, int argc, const VALUE *argv)
304 {
305  /* printf("len: %d, argc: %d\n", len, argc); */
306 
307  if (len >= 0 && argc != len) {
308  rb_raise(rb_eArgError, "wrong number of arguments(%d for %d)",
309  argc, len);
310  }
311 
312  switch (len) {
313  case -2:
314  return (*func) (recv, rb_ary_new4(argc, argv));
315  break;
316  case -1:
317  return (*func) (argc, argv, recv);
318  break;
319  case 0:
320  return (*func) (recv);
321  break;
322  case 1:
323  return (*func) (recv, argv[0]);
324  break;
325  case 2:
326  return (*func) (recv, argv[0], argv[1]);
327  break;
328  case 3:
329  return (*func) (recv, argv[0], argv[1], argv[2]);
330  break;
331  case 4:
332  return (*func) (recv, argv[0], argv[1], argv[2], argv[3]);
333  break;
334  case 5:
335  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4]);
336  break;
337  case 6:
338  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
339  argv[5]);
340  break;
341  case 7:
342  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
343  argv[5], argv[6]);
344  break;
345  case 8:
346  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
347  argv[5], argv[6], argv[7]);
348  break;
349  case 9:
350  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
351  argv[5], argv[6], argv[7], argv[8]);
352  break;
353  case 10:
354  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
355  argv[5], argv[6], argv[7], argv[8], argv[9]);
356  break;
357  case 11:
358  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
359  argv[5], argv[6], argv[7], argv[8], argv[9],
360  argv[10]);
361  break;
362  case 12:
363  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
364  argv[5], argv[6], argv[7], argv[8], argv[9],
365  argv[10], argv[11]);
366  break;
367  case 13:
368  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
369  argv[5], argv[6], argv[7], argv[8], argv[9], argv[10],
370  argv[11], argv[12]);
371  break;
372  case 14:
373  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
374  argv[5], argv[6], argv[7], argv[8], argv[9], argv[10],
375  argv[11], argv[12], argv[13]);
376  break;
377  case 15:
378  return (*func) (recv, argv[0], argv[1], argv[2], argv[3], argv[4],
379  argv[5], argv[6], argv[7], argv[8], argv[9], argv[10],
380  argv[11], argv[12], argv[13], argv[14]);
381  break;
382  default:
383  rb_raise(rb_eArgError, "too many arguments(%d)", len);
384  return Qundef; /* not reached */
385  }
386 }
387 
388 static inline VALUE
390  int num, volatile VALUE recv, const rb_block_t *blockptr,
391  const rb_method_entry_t *me)
392 {
393  volatile VALUE val = 0;
394  const rb_method_definition_t *def = me->def;
395  rb_control_frame_t *cfp;
396 
397  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_CALL, recv, me->called_id, me->klass);
398 
399  cfp = vm_push_frame(th, 0, VM_FRAME_MAGIC_CFUNC,
400  recv, (VALUE) blockptr, 0, reg_cfp->sp, 0, 1);
401  cfp->me = me;
402  reg_cfp->sp -= num + 1;
403 
404  val = call_cfunc(def->body.cfunc.func, recv, (int)def->body.cfunc.argc, num, reg_cfp->sp + 1);
405 
406  if (reg_cfp != th->cfp + 1) {
407  rb_bug("cfp consistency error - send");
408  }
409 
410  vm_pop_frame(th);
411 
412  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, recv, me->called_id, me->klass);
413 
414  return val;
415 }
416 
417 static inline VALUE
419  const rb_block_t *blockptr, const rb_method_entry_t *me)
420 {
421  rb_proc_t *proc;
422  VALUE val;
423 
424  EXEC_EVENT_HOOK(th, RUBY_EVENT_CALL, recv, me->called_id, me->klass);
425 
426  /* control block frame */
427  th->passed_me = me;
428  GetProcPtr(me->def->body.proc, proc);
429  val = rb_vm_invoke_proc(th, proc, recv, argc, argv, blockptr);
430 
431  EXEC_EVENT_HOOK(th, RUBY_EVENT_RETURN, recv, me->called_id, me->klass);
432 
433  return val;
434 }
435 
436 static inline void
438  int num, const rb_block_t *blockptr, int opt)
439 {
440  rb_control_frame_t * const reg_cfp = th->cfp;
441  MEMCPY(argv, STACK_ADDR_FROM_TOP(num + 1), VALUE, num + 1);
442  th->method_missing_reason = opt;
443  th->passed_block = blockptr;
444  POPN(num + 1);
445 }
446 
447 static inline VALUE
449  int num, const rb_block_t *blockptr, int opt)
450 {
451  VALUE *argv = ALLOCA_N(VALUE, num + 1);
452  vm_method_missing_args(th, argv, num, blockptr, opt);
453  argv[0] = ID2SYM(id);
454  return rb_funcall2(recv, idMethodMissing, num + 1, argv);
455 }
456 
457 static inline void
459  VALUE recv, int argc, const rb_block_t *blockptr, VALUE flag,
460  const rb_method_entry_t *me)
461 {
462  int opt_pc, i;
463  VALUE *sp, *rsp = cfp->sp - argc;
464  rb_iseq_t *iseq = me->def->body.iseq;
465 
466  VM_CALLEE_SETUP_ARG(opt_pc, th, iseq, argc, rsp, &blockptr);
467 
468  /* stack overflow check */
469  CHECK_STACK_OVERFLOW(cfp, iseq->stack_max);
470 
471  sp = rsp + iseq->arg_size;
472 
473  if (LIKELY(!(flag & VM_CALL_TAILCALL_BIT))) {
474  if (0) printf("local_size: %d, arg_size: %d\n",
475  iseq->local_size, iseq->arg_size);
476 
477  /* clear local variables */
478  for (i = 0; i < iseq->local_size - iseq->arg_size; i++) {
479  *sp++ = Qnil;
480  }
481 
482  vm_push_frame(th, iseq,
483  VM_FRAME_MAGIC_METHOD, recv, (VALUE) blockptr,
484  iseq->iseq_encoded + opt_pc, sp, 0, 0);
485 
486  cfp->sp = rsp - 1 /* recv */;
487  }
488  else {
489  VALUE *p_rsp;
490  th->cfp++; /* pop cf */
491  p_rsp = th->cfp->sp;
492 
493  /* copy arguments */
494  for (i=0; i < (sp - rsp); i++) {
495  p_rsp[i] = rsp[i];
496  }
497 
498  sp -= rsp - p_rsp;
499 
500  /* clear local variables */
501  for (i = 0; i < iseq->local_size - iseq->arg_size; i++) {
502  *sp++ = Qnil;
503  }
504 
505  vm_push_frame(th, iseq,
506  VM_FRAME_MAGIC_METHOD, recv, (VALUE) blockptr,
507  iseq->iseq_encoded + opt_pc, sp, 0, 0);
508  }
509 }
510 
511 static inline VALUE
513  int num, const rb_block_t *blockptr, VALUE flag,
514  ID id, const rb_method_entry_t *me, VALUE recv)
515 {
516  VALUE val;
517 
518  start_method_dispatch:
519 
520  if (me != 0) {
521  if ((me->flag == 0)) {
522  normal_method_dispatch:
523  switch (me->def->type) {
524  case VM_METHOD_TYPE_ISEQ:{
525  vm_setup_method(th, cfp, recv, num, blockptr, flag, me);
526  return Qundef;
527  }
529  case VM_METHOD_TYPE_CFUNC:{
530  val = vm_call_cfunc(th, cfp, num, recv, blockptr, me);
531  break;
532  }
534  if (num != 1) {
535  rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", num);
536  }
537  val = rb_ivar_set(recv, me->def->body.attr.id, *(cfp->sp - 1));
538  cfp->sp -= 2;
539  break;
540  }
541  case VM_METHOD_TYPE_IVAR:{
542  if (num != 0) {
543  rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", num);
544  }
545  val = rb_attr_get(recv, me->def->body.attr.id);
546  cfp->sp -= 1;
547  break;
548  }
550  VALUE *argv = ALLOCA_N(VALUE, num+1);
551  argv[0] = ID2SYM(me->def->original_id);
552  MEMCPY(argv+1, cfp->sp - num, VALUE, num);
553  cfp->sp += - num - 1;
554  th->passed_block = blockptr;
555  val = rb_funcall2(recv, rb_intern("method_missing"), num+1, argv);
556  break;
557  }
559  VALUE *argv = ALLOCA_N(VALUE, num);
560  MEMCPY(argv, cfp->sp - num, VALUE, num);
561  cfp->sp += - num - 1;
562  val = vm_call_bmethod(th, recv, num, argv, blockptr, me);
563  break;
564  }
565  case VM_METHOD_TYPE_ZSUPER:{
566  VALUE klass = RCLASS_SUPER(me->klass);
567  me = rb_method_entry(klass, id);
568 
569  if (me != 0) {
570  goto normal_method_dispatch;
571  }
572  else {
573  goto start_method_dispatch;
574  }
575  }
577  switch (me->def->body.optimize_type) {
578  case OPTIMIZED_METHOD_TYPE_SEND: {
579  rb_control_frame_t *reg_cfp = cfp;
580  rb_num_t i = num - 1;
581  VALUE sym;
582 
583  if (num == 0) {
584  rb_raise(rb_eArgError, "no method name given");
585  }
586 
587  sym = TOPN(i);
588  id = SYMBOL_P(sym) ? SYM2ID(sym) : rb_to_id(sym);
589  /* shift arguments */
590  if (i > 0) {
591  MEMMOVE(&TOPN(i), &TOPN(i-1), VALUE, i);
592  }
593  me = rb_method_entry(CLASS_OF(recv), id);
594  num -= 1;
595  DEC_SP(1);
597 
598  goto start_method_dispatch;
599  }
600  case OPTIMIZED_METHOD_TYPE_CALL: {
601  rb_proc_t *proc;
602  int argc = num;
603  VALUE *argv = ALLOCA_N(VALUE, num);
604  GetProcPtr(recv, proc);
605  MEMCPY(argv, cfp->sp - num, VALUE, num);
606  cfp->sp -= num + 1;
607 
608  val = rb_vm_invoke_proc(th, proc, proc->block.self, argc, argv, blockptr);
609  break;
610  }
611  default:
612  rb_bug("eval_invoke_method: unsupported optimized method type (%d)",
613  me->def->body.optimize_type);
614  }
615  break;
616  }
617  default:{
618  rb_bug("eval_invoke_method: unsupported method type (%d)", me->def->type);
619  break;
620  }
621  }
622  }
623  else {
624  int noex_safe;
625 
626  if (!(flag & VM_CALL_FCALL_BIT) &&
627  (me->flag & NOEX_MASK) & NOEX_PRIVATE) {
628  int stat = NOEX_PRIVATE;
629 
630  if (flag & VM_CALL_VCALL_BIT) {
631  stat |= NOEX_VCALL;
632  }
633  val = vm_method_missing(th, id, recv, num, blockptr, stat);
634  }
635  else if (!(flag & VM_CALL_OPT_SEND_BIT) && (me->flag & NOEX_MASK) & NOEX_PROTECTED) {
636  VALUE defined_class = me->klass;
637 
638  if (RB_TYPE_P(defined_class, T_ICLASS)) {
639  defined_class = RBASIC(defined_class)->klass;
640  }
641 
642  if (!rb_obj_is_kind_of(cfp->self, defined_class)) {
643  val = vm_method_missing(th, id, recv, num, blockptr, NOEX_PROTECTED);
644  }
645  else {
646  goto normal_method_dispatch;
647  }
648  }
649  else if ((noex_safe = NOEX_SAFE(me->flag)) > th->safe_level &&
650  (noex_safe > 2)) {
651  rb_raise(rb_eSecurityError, "calling insecure method: %s", rb_id2name(id));
652  }
653  else {
654  goto normal_method_dispatch;
655  }
656  }
657  }
658  else {
659  /* method missing */
660  int stat = 0;
661  if (flag & VM_CALL_VCALL_BIT) {
662  stat |= NOEX_VCALL;
663  }
664  if (flag & VM_CALL_SUPER_BIT) {
665  stat |= NOEX_SUPER;
666  }
667  if (id == idMethodMissing) {
668  VALUE *argv = ALLOCA_N(VALUE, num);
669  vm_method_missing_args(th, argv, num - 1, 0, stat);
670  rb_raise_method_missing(th, num, argv, recv, stat);
671  }
672  else {
673  val = vm_method_missing(th, id, recv, num, blockptr, stat);
674  }
675  }
676 
678  return val;
679 }
680 
681 /* yield */
682 
683 static inline int
685 {
686  rb_proc_t *proc;
687 
688  if (procval) {
689  GetProcPtr(procval, proc);
690  return proc->is_lambda;
691  }
692  else {
693  return 0;
694  }
695 }
696 
697 static inline VALUE
699  VALUE self, int argc, const VALUE *argv,
700  const rb_block_t *blockargptr)
701 {
702  NODE *ifunc = (NODE *) block->iseq;
703  VALUE val, arg, blockarg;
704  int lambda = block_proc_is_lambda(block->proc);
705 
706  if (lambda) {
707  arg = rb_ary_new4(argc, argv);
708  }
709  else if (argc == 0) {
710  arg = Qnil;
711  }
712  else {
713  arg = argv[0];
714  }
715 
716  if (blockargptr) {
717  if (blockargptr->proc) {
718  blockarg = blockargptr->proc;
719  }
720  else {
721  blockarg = rb_vm_make_proc(th, blockargptr, rb_cProc);
722  }
723  }
724  else {
725  blockarg = Qnil;
726  }
727 
729  self, (VALUE)block->dfp,
730  0, th->cfp->sp, block->lfp, 1);
731 
732  if (blockargptr) {
733  th->cfp->lfp[0] = GC_GUARDED_PTR((VALUE)blockargptr);
734  }
735  val = (*ifunc->nd_cfnc) (arg, ifunc->nd_tval, argc, argv, blockarg);
736 
737  th->cfp++;
738  return val;
739 }
740 
741 
742 /*--
743  * @brief on supplied all of optional, rest and post parameters.
744  * @pre iseq is block style (not lambda style)
745  */
746 static inline int
748  int argc, VALUE *argv)
749 {
750  rb_num_t opt_pc = 0;
751  int i;
752  const int m = iseq->argc;
753  const int r = iseq->arg_rest;
754  int len = iseq->arg_post_len;
755  int start = iseq->arg_post_start;
756  int rsize = argc > m ? argc - m : 0; /* # of arguments which did not consumed yet */
757  int psize = rsize > len ? len : rsize; /* # of post arguments */
758  int osize = 0; /* # of opt arguments */
759  VALUE ary;
760 
761  /* reserves arguments for post parameters */
762  rsize -= psize;
763 
764  if (iseq->arg_opts) {
765  const int opts = iseq->arg_opts - 1;
766  if (rsize > opts) {
767  osize = opts;
768  opt_pc = iseq->arg_opt_table[opts];
769  }
770  else {
771  osize = rsize;
772  opt_pc = iseq->arg_opt_table[rsize];
773  }
774  }
775  rsize -= osize;
776 
777  if (0) {
778  printf(" argc: %d\n", argc);
779  printf(" len: %d\n", len);
780  printf("start: %d\n", start);
781  printf("rsize: %d\n", rsize);
782  }
783 
784  if (r == -1) {
785  /* copy post argument */
786  MEMMOVE(&argv[start], &argv[m+osize], VALUE, psize);
787  }
788  else {
789  ary = rb_ary_new4(rsize, &argv[r]);
790 
791  /* copy post argument */
792  MEMMOVE(&argv[start], &argv[m+rsize+osize], VALUE, psize);
793  argv[r] = ary;
794  }
795 
796  for (i=psize; i<len; i++) {
797  argv[start + i] = Qnil;
798  }
799 
800  return (int)opt_pc;
801 }
802 
803 static inline int
805  int orig_argc, VALUE *argv,
806  const rb_block_t *blockptr)
807 {
808  int i;
809  int argc = orig_argc;
810  const int m = iseq->argc;
811  VALUE ary, arg0;
812  int opt_pc = 0;
813 
814  th->mark_stack_len = argc;
815 
816  /*
817  * yield [1, 2]
818  * => {|a|} => a = [1, 2]
819  * => {|a, b|} => a, b = [1, 2]
820  */
821  arg0 = argv[0];
822  if (!(iseq->arg_simple & 0x02) && /* exclude {|a|} */
823  (m + iseq->arg_post_len) > 0 && /* this process is meaningful */
824  argc == 1 && !NIL_P(ary = rb_check_array_type(arg0))) { /* rhs is only an array */
825  th->mark_stack_len = argc = RARRAY_LENINT(ary);
826 
827  CHECK_STACK_OVERFLOW(th->cfp, argc);
828 
829  MEMCPY(argv, RARRAY_PTR(ary), VALUE, argc);
830  }
831  else {
832  argv[0] = arg0;
833  }
834 
835  for (i=argc; i<m; i++) {
836  argv[i] = Qnil;
837  }
838 
839  if (iseq->arg_rest == -1 && iseq->arg_opts == 0) {
840  const int arg_size = iseq->arg_size;
841  if (arg_size < argc) {
842  /*
843  * yield 1, 2
844  * => {|a|} # truncate
845  */
846  th->mark_stack_len = argc = arg_size;
847  }
848  }
849  else {
850  int r = iseq->arg_rest;
851 
852  if (iseq->arg_post_len ||
853  iseq->arg_opts) { /* TODO: implement simple version for (iseq->arg_post_len==0 && iseq->arg_opts > 0) */
854  opt_pc = vm_yield_setup_block_args_complex(th, iseq, argc, argv);
855  }
856  else {
857  if (argc < r) {
858  /* yield 1
859  * => {|a, b, *r|}
860  */
861  for (i=argc; i<r; i++) {
862  argv[i] = Qnil;
863  }
864  argv[r] = rb_ary_new();
865  }
866  else {
867  argv[r] = rb_ary_new4(argc-r, &argv[r]);
868  }
869  }
870 
871  th->mark_stack_len = iseq->arg_size;
872  }
873 
874  /* {|&b|} */
875  if (iseq->arg_block != -1) {
876  VALUE procval = Qnil;
877 
878  if (blockptr) {
879  if (blockptr->proc == 0) {
880  procval = rb_vm_make_proc(th, blockptr, rb_cProc);
881  }
882  else {
883  procval = blockptr->proc;
884  }
885  }
886 
887  argv[iseq->arg_block] = procval;
888  }
889 
890  th->mark_stack_len = 0;
891  return opt_pc;
892 }
893 
894 static inline int
895 vm_yield_setup_args(rb_thread_t * const th, const rb_iseq_t *iseq,
896  int argc, VALUE *argv,
897  const rb_block_t *blockptr, int lambda)
898 {
899  if (0) { /* for debug */
900  printf(" argc: %d\n", argc);
901  printf("iseq argc: %d\n", iseq->argc);
902  printf("iseq opts: %d\n", iseq->arg_opts);
903  printf("iseq rest: %d\n", iseq->arg_rest);
904  printf("iseq post: %d\n", iseq->arg_post_len);
905  printf("iseq blck: %d\n", iseq->arg_block);
906  printf("iseq smpl: %d\n", iseq->arg_simple);
907  printf(" lambda: %s\n", lambda ? "true" : "false");
908  }
909 
910  if (lambda) {
911  /* call as method */
912  int opt_pc;
913  VM_CALLEE_SETUP_ARG(opt_pc, th, iseq, argc, argv, &blockptr);
914  return opt_pc;
915  }
916  else {
917  return vm_yield_setup_block_args(th, iseq, argc, argv, blockptr);
918  }
919 }
920 
921 static VALUE
923 {
924  const rb_block_t *block = GET_BLOCK_PTR();
925  rb_iseq_t *iseq;
926  int argc = (int)num;
927  VALUE type = GET_ISEQ()->local_iseq->type;
928 
929  if ((type != ISEQ_TYPE_METHOD && type != ISEQ_TYPE_CLASS) || block == 0) {
930  rb_vm_localjump_error("no block given (yield)", Qnil, 0);
931  }
932  iseq = block->iseq;
933 
934  argc = caller_setup_args(th, GET_CFP(), flag, argc, 0, 0);
935 
936  if (BUILTIN_TYPE(iseq) != T_NODE) {
937  int opt_pc;
938  const int arg_size = iseq->arg_size;
939  VALUE * const rsp = GET_SP() - argc;
940  SET_SP(rsp);
941 
943  opt_pc = vm_yield_setup_args(th, iseq, argc, rsp, 0,
944  block_proc_is_lambda(block->proc));
945 
946  vm_push_frame(th, iseq,
947  VM_FRAME_MAGIC_BLOCK, block->self, (VALUE) block->dfp,
948  iseq->iseq_encoded + opt_pc, rsp + arg_size, block->lfp,
949  iseq->local_size - arg_size);
950 
951  return Qundef;
952  }
953  else {
954  VALUE val = vm_yield_with_cfunc(th, block, block->self, argc, STACK_ADDR_FROM_TOP(argc), 0);
955  POPN(argc); /* TODO: should put before C/yield? */
956  return val;
957  }
958 }
959 
960 /* svar */
961 
962 static inline NODE *
964 {
965  VALUE *svar;
966 
967  if (lfp && th->local_lfp != lfp) {
968  svar = &lfp[-1];
969  }
970  else {
971  svar = &th->local_svar;
972  }
973  if (NIL_P(*svar)) {
974  *svar = (VALUE)NEW_IF(Qnil, Qnil, Qnil);
975  }
976  return (NODE *)*svar;
977 }
978 
979 static VALUE
981 {
982  NODE *svar = lfp_svar_place(th, lfp);
983 
984  switch (key) {
985  case 0:
986  return svar->u1.value;
987  case 1:
988  return svar->u2.value;
989  default: {
990  const VALUE ary = svar->u3.value;
991 
992  if (NIL_P(ary)) {
993  return Qnil;
994  }
995  else {
996  return rb_ary_entry(ary, key - DEFAULT_SPECIAL_VAR_COUNT);
997  }
998  }
999  }
1000 }
1001 
1002 static void
1004 {
1005  NODE *svar = lfp_svar_place(th, lfp);
1006 
1007  switch (key) {
1008  case 0:
1009  svar->u1.value = val;
1010  return;
1011  case 1:
1012  svar->u2.value = val;
1013  return;
1014  default: {
1015  VALUE ary = svar->u3.value;
1016 
1017  if (NIL_P(ary)) {
1018  svar->u3.value = ary = rb_ary_new();
1019  }
1020  rb_ary_store(ary, key - DEFAULT_SPECIAL_VAR_COUNT, val);
1021  }
1022  }
1023 }
1024 
1025 static inline VALUE
1027 {
1028  VALUE val;
1029 
1030  if (type == 0) {
1031  val = lfp_svar_get(th, lfp, key);
1032  }
1033  else {
1034  VALUE backref = lfp_svar_get(th, lfp, 1);
1035 
1036  if (type & 0x01) {
1037  switch (type >> 1) {
1038  case '&':
1039  val = rb_reg_last_match(backref);
1040  break;
1041  case '`':
1042  val = rb_reg_match_pre(backref);
1043  break;
1044  case '\'':
1045  val = rb_reg_match_post(backref);
1046  break;
1047  case '+':
1048  val = rb_reg_match_last(backref);
1049  break;
1050  default:
1051  rb_bug("unexpected back-ref");
1052  }
1053  }
1054  else {
1055  val = rb_reg_nth_match((int)(type >> 1), backref);
1056  }
1057  }
1058  return val;
1059 }
1060 
1061 static NODE *
1062 vm_get_cref0(const rb_iseq_t *iseq, const VALUE *lfp, const VALUE *dfp)
1063 {
1064  while (1) {
1065  if (lfp == dfp) {
1066  if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) return NULL;
1067  return iseq->cref_stack;
1068  }
1069  else if (dfp[-1] != Qnil) {
1070  return (NODE *)dfp[-1];
1071  }
1072  dfp = GET_PREV_DFP(dfp);
1073  }
1074 }
1075 
1076 static NODE *
1077 vm_get_cref(const rb_iseq_t *iseq, const VALUE *lfp, const VALUE *dfp)
1078 {
1079  NODE *cref = vm_get_cref0(iseq, lfp, dfp);
1080 
1081  if (cref == 0) {
1082  rb_bug("vm_get_cref: unreachable");
1083  }
1084  return cref;
1085 }
1086 
1087 static NODE *
1088 vm_cref_push(rb_thread_t *th, VALUE klass, int noex, rb_block_t *blockptr)
1089 {
1091  NODE *cref = NEW_BLOCK(klass);
1092  cref->nd_visi = noex;
1093 
1094  if (blockptr) {
1095  cref->nd_next = vm_get_cref0(blockptr->iseq, blockptr->lfp, blockptr->dfp);
1096  }
1097  else if (cfp) {
1098  cref->nd_next = vm_get_cref0(cfp->iseq, cfp->lfp, cfp->dfp);
1099  }
1100 
1101  return cref;
1102 }
1103 
1104 static inline VALUE
1105 vm_get_cbase(const rb_iseq_t *iseq, const VALUE *lfp, const VALUE *dfp)
1106 {
1107  NODE *cref = vm_get_cref(iseq, lfp, dfp);
1108  VALUE klass = Qundef;
1109 
1110  while (cref) {
1111  if ((klass = cref->nd_clss) != 0) {
1112  break;
1113  }
1114  cref = cref->nd_next;
1115  }
1116 
1117  return klass;
1118 }
1119 
1120 static inline VALUE
1121 vm_get_const_base(const rb_iseq_t *iseq, const VALUE *lfp, const VALUE *dfp)
1122 {
1123  NODE *cref = vm_get_cref(iseq, lfp, dfp);
1124  VALUE klass = Qundef;
1125 
1126  while (cref) {
1127  if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) &&
1128  (klass = cref->nd_clss) != 0) {
1129  break;
1130  }
1131  cref = cref->nd_next;
1132  }
1133 
1134  return klass;
1135 }
1136 
1137 static inline void
1139 {
1140  VALUE str;
1141  switch (TYPE(klass)) {
1142  case T_CLASS:
1143  case T_MODULE:
1144  break;
1145  default:
1146  str = rb_inspect(klass);
1147  rb_raise(rb_eTypeError, "%s is not a class/module",
1148  StringValuePtr(str));
1149  }
1150 }
1151 
1152 static inline VALUE
1154  VALUE orig_klass, ID id, int is_defined)
1155 {
1156  VALUE val;
1157 
1158  if (orig_klass == Qnil) {
1159  /* in current lexical scope */
1160  const NODE *root_cref = vm_get_cref(iseq, th->cfp->lfp, th->cfp->dfp);
1161  const NODE *cref;
1162  VALUE klass = orig_klass;
1163 
1164  while (root_cref && root_cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) {
1165  root_cref = root_cref->nd_next;
1166  }
1167  cref = root_cref;
1168  while (cref && cref->nd_next) {
1169  if (cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) {
1170  klass = Qnil;
1171  }
1172  else {
1173  klass = cref->nd_clss;
1174  }
1175  cref = cref->nd_next;
1176 
1177  if (!NIL_P(klass)) {
1178  VALUE am = 0;
1179  st_data_t data;
1180  search_continue:
1181  if (RCLASS_CONST_TBL(klass) &&
1182  st_lookup(RCLASS_CONST_TBL(klass), id, &data)) {
1183  val = ((rb_const_entry_t*)data)->value;
1184  if (val == Qundef) {
1185  if (am == klass) break;
1186  am = klass;
1187  if (is_defined) return 1;
1188  rb_autoload_load(klass, id);
1189  goto search_continue;
1190  }
1191  else {
1192  if (is_defined) {
1193  return 1;
1194  }
1195  else {
1196  return val;
1197  }
1198  }
1199  }
1200  }
1201  }
1202 
1203  /* search self */
1204  if (root_cref && !NIL_P(root_cref->nd_clss)) {
1205  klass = root_cref->nd_clss;
1206  }
1207  else {
1208  klass = CLASS_OF(th->cfp->self);
1209  }
1210 
1211  if (is_defined) {
1212  return rb_const_defined(klass, id);
1213  }
1214  else {
1215  return rb_const_get(klass, id);
1216  }
1217  }
1218  else {
1219  vm_check_if_namespace(orig_klass);
1220  if (is_defined) {
1221  return rb_public_const_defined_from(orig_klass, id);
1222  }
1223  else {
1224  return rb_public_const_get_from(orig_klass, id);
1225  }
1226  }
1227 }
1228 
1229 static inline VALUE
1231 {
1232  VALUE klass;
1233 
1234  while (cref && cref->nd_next &&
1235  (NIL_P(cref->nd_clss) || FL_TEST(cref->nd_clss, FL_SINGLETON) ||
1236  (cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL))) {
1237  cref = cref->nd_next;
1238 
1239  if (!cref->nd_next) {
1240  rb_warn("class variable access from toplevel");
1241  }
1242  }
1243 
1244  klass = cref->nd_clss;
1245 
1246  if (NIL_P(klass)) {
1247  rb_raise(rb_eTypeError, "no class variables available");
1248  }
1249  return klass;
1250 }
1251 
1252 static VALUE
1254 {
1255  if (rb_const_defined_at(cbase, id)) return cbase;
1256  if (cbase == rb_cObject) {
1257  VALUE tmp = RCLASS_SUPER(cbase);
1258  while (tmp) {
1259  if (rb_const_defined_at(tmp, id)) return tmp;
1260  tmp = RCLASS_SUPER(tmp);
1261  }
1262  }
1263  return 0;
1264 }
1265 
1266 #ifndef USE_IC_FOR_IVAR
1267 #define USE_IC_FOR_IVAR 1
1268 #endif
1269 
1270 static VALUE
1271 vm_getivar(VALUE obj, ID id, IC ic)
1272 {
1273 #if USE_IC_FOR_IVAR
1274  if (TYPE(obj) == T_OBJECT) {
1275  VALUE val = Qundef;
1276  VALUE klass = RBASIC(obj)->klass;
1277 
1278  if (LIKELY(ic->ic_class == klass &&
1279  ic->ic_vmstat == GET_VM_STATE_VERSION())) {
1280  long index = ic->ic_value.index;
1281  long len = ROBJECT_NUMIV(obj);
1282  VALUE *ptr = ROBJECT_IVPTR(obj);
1283 
1284  if (index < len) {
1285  val = ptr[index];
1286  }
1287  }
1288  else {
1289  st_data_t index;
1290  long len = ROBJECT_NUMIV(obj);
1291  VALUE *ptr = ROBJECT_IVPTR(obj);
1292  struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
1293 
1294  if (iv_index_tbl) {
1295  if (st_lookup(iv_index_tbl, id, &index)) {
1296  if ((long)index < len) {
1297  val = ptr[index];
1298  }
1299  ic->ic_class = klass;
1300  ic->ic_value.index = index;
1302  }
1303  }
1304  }
1305  if (UNLIKELY(val == Qundef)) {
1306  rb_warning("instance variable %s not initialized", rb_id2name(id));
1307  val = Qnil;
1308  }
1309  return val;
1310  }
1311  else {
1312  return rb_ivar_get(obj, id);
1313  }
1314 #else
1315  return rb_ivar_get(obj, id);
1316 #endif
1317 }
1318 
1319 static void
1320 vm_setivar(VALUE obj, ID id, VALUE val, IC ic)
1321 {
1322 #if USE_IC_FOR_IVAR
1323  if (!OBJ_UNTRUSTED(obj) && rb_safe_level() >= 4) {
1324  rb_raise(rb_eSecurityError, "Insecure: can't modify instance variable");
1325  }
1326 
1327  rb_check_frozen(obj);
1328 
1329  if (TYPE(obj) == T_OBJECT) {
1330  VALUE klass = RBASIC(obj)->klass;
1331  st_data_t index;
1332 
1333  if (LIKELY(ic->ic_class == klass &&
1334  ic->ic_vmstat == GET_VM_STATE_VERSION())) {
1335  long index = ic->ic_value.index;
1336  long len = ROBJECT_NUMIV(obj);
1337  VALUE *ptr = ROBJECT_IVPTR(obj);
1338 
1339  if (index < len) {
1340  ptr[index] = val;
1341  return; /* inline cache hit */
1342  }
1343  }
1344  else {
1345  struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
1346 
1347  if (iv_index_tbl && st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
1348  ic->ic_class = klass;
1349  ic->ic_value.index = index;
1351  }
1352  /* fall through */
1353  }
1354  }
1355  rb_ivar_set(obj, id, val);
1356 #else
1357  rb_ivar_set(obj, id, val);
1358 #endif
1359 }
1360 
1361 static inline const rb_method_entry_t *
1363 {
1364  rb_method_entry_t *me;
1365 #if OPT_INLINE_METHOD_CACHE
1366  if (LIKELY(klass == ic->ic_class &&
1367  GET_VM_STATE_VERSION() == ic->ic_vmstat)) {
1368  me = ic->ic_value.method;
1369  }
1370  else {
1371  me = rb_method_entry(klass, id);
1372  ic->ic_class = klass;
1373  ic->ic_value.method = me;
1375  }
1376 #else
1377  me = rb_method_entry(klass, id);
1378 #endif
1379  return me;
1380 }
1381 
1382 static inline VALUE
1384 {
1385  if (BUILTIN_TYPE(klass) == T_CLASS) {
1386  return RCLASS_SUPER(klass);
1387  }
1388  else if (BUILTIN_TYPE(klass) == T_MODULE) {
1389  VALUE k = CLASS_OF(recv);
1390  while (k) {
1391  if (BUILTIN_TYPE(k) == T_ICLASS && RBASIC(k)->klass == klass) {
1392  return RCLASS_SUPER(k);
1393  }
1394  k = RCLASS_SUPER(k);
1395  }
1396  return rb_cObject;
1397  }
1398  else {
1399  rb_bug("vm_search_normal_superclass: should not be reach here");
1400  }
1401 }
1402 
1403 static void
1405  VALUE recv, VALUE sigval,
1406  ID *idp, VALUE *klassp)
1407 {
1408  ID id;
1409  VALUE klass;
1410 
1411  while (iseq && !iseq->klass) {
1412  iseq = iseq->parent_iseq;
1413  }
1414 
1415  if (iseq == 0) {
1416  rb_raise(rb_eNoMethodError, "super called outside of method");
1417  }
1418 
1419  id = iseq->defined_method_id;
1420 
1421  if (iseq != iseq->local_iseq) {
1422  /* defined by Module#define_method() */
1423  rb_control_frame_t *lcfp = GET_CFP();
1424 
1425  if (!sigval) {
1426  /* zsuper */
1427  rb_raise(rb_eRuntimeError, "implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly.");
1428  }
1429 
1430  while (lcfp->iseq != iseq) {
1431  rb_thread_t *th = GET_THREAD();
1432  VALUE *tdfp = GET_PREV_DFP(lcfp->dfp);
1433  while (1) {
1434  lcfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(lcfp);
1437  "super called outside of method");
1438  }
1439  if (lcfp->dfp == tdfp) {
1440  break;
1441  }
1442  }
1443  }
1444 
1445  /* temporary measure for [Bug #2420] [Bug #3136] */
1446  if (!lcfp->me) {
1447  rb_raise(rb_eNoMethodError, "super called outside of method");
1448  }
1449 
1450  id = lcfp->me->def->original_id;
1451  klass = vm_search_normal_superclass(lcfp->me->klass, recv);
1452  }
1453  else {
1454  klass = vm_search_normal_superclass(iseq->klass, recv);
1455  }
1456 
1457  *idp = id;
1458  *klassp = klass;
1459 }
1460 
1461 static VALUE
1463  rb_num_t throw_state, VALUE throwobj)
1464 {
1465  int state = (int)(throw_state & 0xff);
1466  int flag = (int)(throw_state & 0x8000);
1467  rb_num_t level = throw_state >> 16;
1468 
1469  if (state != 0) {
1470  VALUE *pt = 0;
1471  if (flag != 0) {
1472  pt = (void *) 1;
1473  }
1474  else {
1475  if (state == TAG_BREAK) {
1476  rb_control_frame_t *cfp = GET_CFP();
1477  VALUE *dfp = GET_DFP();
1478  int is_orphan = 1;
1479  rb_iseq_t *base_iseq = GET_ISEQ();
1480 
1481  search_parent:
1482  if (cfp->iseq->type != ISEQ_TYPE_BLOCK) {
1483  if (cfp->iseq->type == ISEQ_TYPE_CLASS) {
1484  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1485  dfp = cfp->dfp;
1486  goto search_parent;
1487  }
1488  dfp = GC_GUARDED_PTR_REF((VALUE *) *dfp);
1489  base_iseq = base_iseq->parent_iseq;
1490 
1491  while ((VALUE *) cfp < th->stack + th->stack_size) {
1492  if (cfp->dfp == dfp) {
1493  goto search_parent;
1494  }
1495  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1496  }
1497  rb_bug("VM (throw): can't find break base.");
1498  }
1499 
1500  if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_LAMBDA) {
1501  /* lambda{... break ...} */
1502  is_orphan = 0;
1503  pt = cfp->dfp;
1504  state = TAG_RETURN;
1505  }
1506  else {
1507  dfp = GC_GUARDED_PTR_REF((VALUE *) *dfp);
1508 
1509  while ((VALUE *)cfp < th->stack + th->stack_size) {
1510  if (cfp->dfp == dfp) {
1511  VALUE epc = cfp->pc - cfp->iseq->iseq_encoded;
1512  rb_iseq_t *iseq = cfp->iseq;
1513  int i;
1514 
1515  for (i=0; i<iseq->catch_table_size; i++) {
1516  struct iseq_catch_table_entry *entry = &iseq->catch_table[i];
1517 
1518  if (entry->type == CATCH_TYPE_BREAK &&
1519  entry->start < epc && entry->end >= epc) {
1520  if (entry->cont == epc) {
1521  goto found;
1522  }
1523  else {
1524  break;
1525  }
1526  }
1527  }
1528  break;
1529 
1530  found:
1531  pt = dfp;
1532  is_orphan = 0;
1533  break;
1534  }
1535  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1536  }
1537  }
1538 
1539  if (is_orphan) {
1540  rb_vm_localjump_error("break from proc-closure", throwobj, TAG_BREAK);
1541  }
1542  }
1543  else if (state == TAG_RETRY) {
1544  rb_num_t i;
1545  pt = GC_GUARDED_PTR_REF((VALUE *) * GET_DFP());
1546  for (i = 0; i < level; i++) {
1547  pt = GC_GUARDED_PTR_REF((VALUE *) * pt);
1548  }
1549  }
1550  else if (state == TAG_RETURN) {
1551  rb_control_frame_t *cfp = GET_CFP();
1552  VALUE *dfp = GET_DFP();
1553  VALUE *lfp = GET_LFP();
1554  int in_class_frame = 0;
1555 
1556  /* check orphan and get dfp */
1557  while ((VALUE *) cfp < th->stack + th->stack_size) {
1558  if (!lfp) {
1559  lfp = cfp->lfp;
1560  }
1561  if (cfp->dfp == lfp && cfp->iseq->type == ISEQ_TYPE_CLASS) {
1562  in_class_frame = 1;
1563  lfp = 0;
1564  }
1565 
1566  if (cfp->lfp == lfp) {
1567  if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_LAMBDA) {
1568  VALUE *tdfp = dfp;
1569 
1570  if (in_class_frame) {
1571  /* lambda {class A; ... return ...; end} */
1572  dfp = cfp->dfp;
1573  goto valid_return;
1574  }
1575 
1576  while (lfp != tdfp) {
1577  if (cfp->dfp == tdfp) {
1578  /* in lambda */
1579  dfp = cfp->dfp;
1580  goto valid_return;
1581  }
1582  tdfp = GC_GUARDED_PTR_REF((VALUE *)*tdfp);
1583  }
1584  }
1585  }
1586 
1587  if (cfp->dfp == lfp && cfp->iseq->type == ISEQ_TYPE_METHOD) {
1588  dfp = lfp;
1589  goto valid_return;
1590  }
1591 
1592  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1593  }
1594 
1595  rb_vm_localjump_error("unexpected return", throwobj, TAG_RETURN);
1596 
1597  valid_return:
1598  pt = dfp;
1599  }
1600  else {
1601  rb_bug("isns(throw): unsupport throw type");
1602  }
1603  }
1604  th->state = state;
1605  return (VALUE)NEW_THROW_OBJECT(throwobj, (VALUE) pt, state);
1606  }
1607  else {
1608  /* continue throw */
1609  VALUE err = throwobj;
1610 
1611  if (FIXNUM_P(err)) {
1612  th->state = FIX2INT(err);
1613  }
1614  else if (SYMBOL_P(err)) {
1615  th->state = TAG_THROW;
1616  }
1617  else if (BUILTIN_TYPE(err) == T_NODE) {
1618  th->state = GET_THROWOBJ_STATE(err);
1619  }
1620  else {
1621  th->state = TAG_RAISE;
1622  /*th->state = FIX2INT(rb_ivar_get(err, idThrowState));*/
1623  }
1624  return err;
1625  }
1626 }
1627 
1628 static inline void
1630 {
1631  int is_splat = flag & 0x01;
1632  rb_num_t space_size = num + is_splat;
1633  VALUE *base = cfp->sp, *ptr;
1634  volatile VALUE tmp_ary;
1635  rb_num_t len;
1636 
1637  if (TYPE(ary) != T_ARRAY) {
1638  ary = rb_ary_to_ary(ary);
1639  }
1640 
1641  cfp->sp += space_size;
1642 
1643  tmp_ary = ary;
1644  ptr = RARRAY_PTR(ary);
1645  len = (rb_num_t)RARRAY_LEN(ary);
1646 
1647  if (flag & 0x02) {
1648  /* post: ..., nil ,ary[-1], ..., ary[0..-num] # top */
1649  rb_num_t i = 0, j;
1650 
1651  if (len < num) {
1652  for (i=0; i<num-len; i++) {
1653  *base++ = Qnil;
1654  }
1655  }
1656  for (j=0; i<num; i++, j++) {
1657  VALUE v = ptr[len - j - 1];
1658  *base++ = v;
1659  }
1660  if (is_splat) {
1661  *base = rb_ary_new4(len - j, ptr);
1662  }
1663  }
1664  else {
1665  /* normal: ary[num..-1], ary[num-2], ary[num-3], ..., ary[0] # top */
1666  rb_num_t i;
1667  VALUE *bptr = &base[space_size - 1];
1668 
1669  for (i=0; i<num; i++) {
1670  if (len <= i) {
1671  for (; i<num; i++) {
1672  *bptr-- = Qnil;
1673  }
1674  break;
1675  }
1676  *bptr-- = ptr[i];
1677  }
1678  if (is_splat) {
1679  if (num > len) {
1680  *bptr = rb_ary_new();
1681  }
1682  else {
1683  *bptr = rb_ary_new4(len - num, ptr + num);
1684  }
1685  }
1686  }
1687 }
1688 
1689 static inline int
1691 {
1692  if (me && me->def->type == VM_METHOD_TYPE_CFUNC &&
1693  me->def->body.cfunc.func == func) {
1694  return 1;
1695  }
1696  else {
1697  return 0;
1698  }
1699 }
1700 
1701 static
1702 #ifndef NO_BIG_INLINE
1703 inline
1704 #endif
1705 VALUE
1706 opt_eq_func(VALUE recv, VALUE obj, IC ic)
1707 {
1708  if (FIXNUM_2_P(recv, obj) &&
1710  return (recv == obj) ? Qtrue : Qfalse;
1711  }
1712  else if (!SPECIAL_CONST_P(recv) && !SPECIAL_CONST_P(obj)) {
1713  if (HEAP_CLASS_OF(recv) == rb_cFloat &&
1714  HEAP_CLASS_OF(obj) == rb_cFloat &&
1716  double a = RFLOAT_VALUE(recv);
1717  double b = RFLOAT_VALUE(obj);
1718 
1719  if (isnan(a) || isnan(b)) {
1720  return Qfalse;
1721  }
1722  return (a == b) ? Qtrue : Qfalse;
1723  }
1724  else if (HEAP_CLASS_OF(recv) == rb_cString &&
1725  HEAP_CLASS_OF(obj) == rb_cString &&
1727  return rb_str_equal(recv, obj);
1728  }
1729  }
1730 
1731  {
1732  const rb_method_entry_t *me = vm_method_search(idEq, CLASS_OF(recv), ic);
1733 
1734  if (check_cfunc(me, rb_obj_equal)) {
1735  return recv == obj ? Qtrue : Qfalse;
1736  }
1737  }
1738 
1739  return Qundef;
1740 }
1741 
1742