Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
vm.c
Go to the documentation of this file.
1  /*
2 ** vm.c - virtual machine for mruby
3 **
4 ** See Copyright Notice in mruby.h
5 */
6 
7 #include <string.h>
8 #include <setjmp.h>
9 #include <stddef.h>
10 #include <stdarg.h>
11 #include "mruby.h"
12 #include "mruby/array.h"
13 #include "mruby/class.h"
14 #include "mruby/hash.h"
15 #include "mruby/irep.h"
16 #include "mruby/numeric.h"
17 #include "mruby/proc.h"
18 #include "mruby/range.h"
19 #include "mruby/string.h"
20 #include "mruby/variable.h"
21 #include "error.h"
22 #include "opcode.h"
23 #include "value_array.h"
24 
25 #ifndef ENABLE_STDIO
26 #if defined(__cplusplus)
27 extern "C" {
28 #endif
29 void abort(void);
30 #if defined(__cplusplus)
31 } /* extern "C" { */
32 #endif
33 #endif
34 
35 #define SET_TRUE_VALUE(r) MRB_SET_VALUE(r, MRB_TT_TRUE, value.i, 1)
36 #define SET_FALSE_VALUE(r) MRB_SET_VALUE(r, MRB_TT_FALSE, value.i, 1)
37 #define SET_NIL_VALUE(r) MRB_SET_VALUE(r, MRB_TT_FALSE, value.i, 0)
38 #define SET_INT_VALUE(r,n) MRB_SET_VALUE(r, MRB_TT_FIXNUM, value.i, (n))
39 #define SET_SYM_VALUE(r,v) MRB_SET_VALUE(r, MRB_TT_SYMBOL, value.sym, (v))
40 #define SET_OBJ_VALUE(r,v) MRB_SET_VALUE(r, (((struct RObject*)(v))->tt), value.p, (v))
41 #ifdef MRB_NAN_BOXING
42 #define SET_FLT_VALUE(mrb,r,v) r.f = (v)
43 #elif defined(MRB_WORD_BOXING)
44 #define SET_FLT_VALUE(mrb,r,v) r = mrb_float_value(mrb, (v))
45 #else
46 #define SET_FLT_VALUE(mrb,r,v) MRB_SET_VALUE(r, MRB_TT_FLOAT, value.f, (v))
47 #endif
48 
49 #define STACK_INIT_SIZE 128
50 #define CALLINFO_INIT_SIZE 32
51 
52 /* Define amount of linear stack growth. */
53 #ifndef MRB_STACK_GROWTH
54 #define MRB_STACK_GROWTH 128
55 #endif
56 
57 /* Maximum stack depth. Should be set lower on memory constrained systems.
58 The value below allows about 60000 recursive calls in the simplest case. */
59 #ifndef MRB_STACK_MAX
60 #define MRB_STACK_MAX (0x40000 - MRB_STACK_GROWTH)
61 #endif
62 
63 #ifdef VM_DEBUG
64 # define DEBUG(x) (x)
65 #else
66 # define DEBUG(x)
67 #endif
68 
69 #define TO_STR(x) TO_STR_(x)
70 #define TO_STR_(x) #x
71 
72 static inline void
73 stack_clear(mrb_value *from, size_t count)
74 {
75  const mrb_value mrb_value_zero = { { 0 } };
76 
77  while (count-- > 0) {
78  *from++ = mrb_value_zero;
79  }
80 }
81 
82 static inline void
83 stack_copy(mrb_value *dst, const mrb_value *src, size_t size)
84 {
85  while (size-- > 0) {
86  *dst++ = *src++;
87  }
88 }
89 
90 static void
91 stack_init(mrb_state *mrb)
92 {
93  struct mrb_context *c = mrb->c;
94 
95  /* mrb_assert(mrb->stack == NULL); */
96  c->stbase = (mrb_value *)mrb_calloc(mrb, STACK_INIT_SIZE, sizeof(mrb_value));
97  c->stend = c->stbase + STACK_INIT_SIZE;
98  c->stack = c->stbase;
99 
100  /* mrb_assert(ci == NULL); */
102  c->ciend = c->cibase + CALLINFO_INIT_SIZE;
103  c->ci = c->cibase;
104  c->ci->target_class = mrb->object_class;
105 }
106 
107 static inline void
108 envadjust(mrb_state *mrb, mrb_value *oldbase, mrb_value *newbase)
109 {
110  mrb_callinfo *ci = mrb->c->cibase;
111 
112  while (ci <= mrb->c->ci) {
113  struct REnv *e = ci->env;
114  if (e && e->cioff >= 0) {
115  ptrdiff_t off = e->stack - oldbase;
116 
117  e->stack = newbase + off;
118  }
119  ci++;
120  }
121 }
122 
125 static void
126 stack_extend(mrb_state *mrb, int room, int keep)
127 {
128  if (mrb->c->stack + room >= mrb->c->stend) {
129  int size, off;
130 
131  mrb_value *oldbase = mrb->c->stbase;
132 
133  size = mrb->c->stend - mrb->c->stbase;
134  off = mrb->c->stack - mrb->c->stbase;
135 
136  /* do not leave uninitialized malloc region */
137  if (keep > size) keep = size;
138 
139  /* Use linear stack growth.
140  It is slightly slower than doubling thestack space,
141  but it saves memory on small devices. */
142  if (room <= size)
143  size += MRB_STACK_GROWTH;
144  else
145  size += room;
146 
147  mrb->c->stbase = (mrb_value *)mrb_realloc(mrb, mrb->c->stbase, sizeof(mrb_value) * size);
148  mrb->c->stack = mrb->c->stbase + off;
149  mrb->c->stend = mrb->c->stbase + size;
150  envadjust(mrb, oldbase, mrb->c->stbase);
151  /* Raise an exception if the new stack size will be too large,
152  to prevent infinite recursion. However, do this only after resizing the stack, so mrb_raise has stack space to work with. */
153  if (size > MRB_STACK_MAX) {
154  mrb_raise(mrb, E_RUNTIME_ERROR, "stack level too deep. (limit=" TO_STR(MRB_STACK_MAX) ")");
155  }
156  }
157 
158  if (room > keep) {
159 #ifndef MRB_NAN_BOXING
160  stack_clear(&(mrb->c->stack[keep]), room - keep);
161 #else
162  struct mrb_context *c = mrb->c;
163  int i;
164  for (i=keep; i<room; i++) {
165  SET_NIL_VALUE(c->stack[i]);
166  }
167 #endif
168  }
169 }
170 
171 static inline struct REnv*
172 uvenv(mrb_state *mrb, int up)
173 {
174  struct REnv *e = mrb->c->ci->proc->env;
175 
176  while (up--) {
177  if (!e) return NULL;
178  e = (struct REnv*)e->c;
179  }
180  return e;
181 }
182 
183 static inline mrb_bool
184 is_strict(mrb_state *mrb, struct REnv *e)
185 {
186  int cioff = e->cioff;
187 
188  if (cioff >= 0 && mrb->c->cibase[cioff].proc &&
189  MRB_PROC_STRICT_P(mrb->c->cibase[cioff].proc)) {
190  return TRUE;
191  }
192  return FALSE;
193 }
194 
195 static inline struct REnv*
196 top_env(mrb_state *mrb, struct RProc *proc)
197 {
198  struct REnv *e = proc->env;
199 
200  if (is_strict(mrb, e)) return e;
201  while (e->c) {
202  e = (struct REnv*)e->c;
203  if (is_strict(mrb, e)) return e;
204  }
205  return e;
206 }
207 
208 #define CI_ACC_SKIP -1
209 #define CI_ACC_DIRECT -2
210 
211 static mrb_callinfo*
212 cipush(mrb_state *mrb)
213 {
214  struct mrb_context *c = mrb->c;
215  mrb_callinfo *ci = c->ci;
216 
217  int eidx = ci->eidx;
218  int ridx = ci->ridx;
219 
220  if (ci + 1 == c->ciend) {
221  size_t size = ci - c->cibase;
222 
223  c->cibase = (mrb_callinfo *)mrb_realloc(mrb, c->cibase, sizeof(mrb_callinfo)*size*2);
224  c->ci = c->cibase + size;
225  c->ciend = c->cibase + size * 2;
226  }
227  ci = ++c->ci;
228  ci->nregs = 2; /* protect method_missing arg and block */
229  ci->eidx = eidx;
230  ci->ridx = ridx;
231  ci->env = 0;
232  return ci;
233 }
234 
235 static void
236 cipop(mrb_state *mrb)
237 {
238  struct mrb_context *c = mrb->c;
239 
240  if (c->ci->env) {
241  struct REnv *e = c->ci->env;
242  size_t len = (size_t)e->flags;
243  mrb_value *p = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value)*len);
244 
245  e->cioff = -1;
246  stack_copy(p, e->stack, len);
247  e->stack = p;
248  }
249 
250  c->ci--;
251 }
252 
253 static void
254 ecall(mrb_state *mrb, int i)
255 {
256  struct RProc *p;
257  mrb_callinfo *ci;
258  mrb_value *self = mrb->c->stack;
259  struct RObject *exc;
260 
261  p = mrb->c->ensure[i];
262  if (!p) return;
263  ci = cipush(mrb);
264  ci->stackidx = mrb->c->stack - mrb->c->stbase;
265  ci->mid = ci[-1].mid;
266  ci->acc = CI_ACC_SKIP;
267  ci->argc = 0;
268  ci->proc = p;
269  ci->nregs = p->body.irep->nregs;
270  ci->target_class = p->target_class;
271  mrb->c->stack = mrb->c->stack + ci[-1].nregs;
272  exc = mrb->exc; mrb->exc = 0;
273  mrb_run(mrb, p, *self);
274  mrb->c->ensure[i] = NULL;
275  if (!mrb->exc) mrb->exc = exc;
276 }
277 
278 #ifndef MRB_FUNCALL_ARGC_MAX
279 #define MRB_FUNCALL_ARGC_MAX 16
280 #endif
281 
282 mrb_value
283 mrb_funcall(mrb_state *mrb, mrb_value self, const char *name, int argc, ...)
284 {
285  mrb_sym mid = mrb_intern_cstr(mrb, name);
286 
287  if (argc == 0) {
288  return mrb_funcall_argv(mrb, self, mid, 0, 0);
289  }
290  else if (argc == 1) {
291  mrb_value v;
292  va_list ap;
293 
294  va_start(ap, argc);
295  v = va_arg(ap, mrb_value);
296  va_end(ap);
297  return mrb_funcall_argv(mrb, self, mid, 1, &v);
298  }
299  else {
301  va_list ap;
302  int i;
303 
304  if (argc > MRB_FUNCALL_ARGC_MAX) {
305  mrb_raise(mrb, E_ARGUMENT_ERROR, "Too long arguments. (limit=" TO_STR(MRB_FUNCALL_ARGC_MAX) ")");
306  }
307 
308  va_start(ap, argc);
309  for (i = 0; i < argc; i++) {
310  argv[i] = va_arg(ap, mrb_value);
311  }
312  va_end(ap);
313  return mrb_funcall_argv(mrb, self, mid, argc, argv);
314  }
315 }
316 
317 mrb_value
318 mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, int argc, mrb_value *argv, mrb_value blk)
319 {
320  mrb_value val;
321 
322  if (!mrb->jmp) {
323  jmp_buf c_jmp;
324  mrb_callinfo *old_ci = mrb->c->ci;
325 
326  if (setjmp(c_jmp) != 0) { /* error */
327  while (old_ci != mrb->c->ci) {
328  mrb->c->stack = mrb->c->stbase + mrb->c->ci->stackidx;
329  cipop(mrb);
330  }
331  mrb->jmp = 0;
332  val = mrb_obj_value(mrb->exc);
333  }
334  else {
335  mrb->jmp = &c_jmp;
336  /* recursive call */
337  val = mrb_funcall_with_block(mrb, self, mid, argc, argv, blk);
338  mrb->jmp = 0;
339  }
340  }
341  else {
342  struct RProc *p;
343  struct RClass *c;
344  mrb_sym undef = 0;
345  mrb_callinfo *ci;
346  int n;
347 
348  if (!mrb->c->stack) {
349  stack_init(mrb);
350  }
351  n = mrb->c->ci->nregs;
352  if (argc < 0) {
353  mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative argc for funcall (%S)", mrb_fixnum_value(argc));
354  }
355  c = mrb_class(mrb, self);
356  p = mrb_method_search_vm(mrb, &c, mid);
357  if (!p) {
358  undef = mid;
359  mid = mrb_intern2(mrb, "method_missing", 14);
360  p = mrb_method_search_vm(mrb, &c, mid);
361  n++; argc++;
362  }
363  ci = cipush(mrb);
364  ci->mid = mid;
365  ci->proc = p;
366  ci->stackidx = mrb->c->stack - mrb->c->stbase;
367  ci->argc = argc;
368  ci->target_class = c;
369  if (MRB_PROC_CFUNC_P(p)) {
370  ci->nregs = argc + 2;
371  }
372  else {
373  ci->nregs = p->body.irep->nregs + n;
374  }
375  mrb->c->stack = mrb->c->stack + n;
376 
377  stack_extend(mrb, ci->nregs, 0);
378  mrb->c->stack[0] = self;
379  if (undef) {
380  mrb->c->stack[1] = mrb_symbol_value(undef);
381  stack_copy(mrb->c->stack+2, argv, argc-1);
382  }
383  else if (argc > 0) {
384  stack_copy(mrb->c->stack+1, argv, argc);
385  }
386  mrb->c->stack[argc+1] = blk;
387 
388  if (MRB_PROC_CFUNC_P(p)) {
389  int ai = mrb_gc_arena_save(mrb);
390 
391  ci->acc = CI_ACC_DIRECT;
392  val = p->body.func(mrb, self);
393  mrb->c->stack = mrb->c->stbase + mrb->c->ci->stackidx;
394  cipop(mrb);
395  mrb_gc_arena_restore(mrb, ai);
396  }
397  else {
398  ci->acc = CI_ACC_SKIP;
399  val = mrb_run(mrb, p, self);
400  }
401  }
402  mrb_gc_protect(mrb, val);
403  return val;
404 }
405 
406 mrb_value
407 mrb_funcall_argv(mrb_state *mrb, mrb_value self, mrb_sym mid, int argc, mrb_value *argv)
408 {
409  return mrb_funcall_with_block(mrb, self, mid, argc, argv, mrb_nil_value());
410 }
411 
412 mrb_value
413 mrb_yield_internal(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv, mrb_value self, struct RClass *c)
414 {
415  struct RProc *p;
416  mrb_sym mid = mrb->c->ci->mid;
417  mrb_callinfo *ci;
418  int n = mrb->c->ci->nregs;
419  mrb_value val;
420 
421  if (mrb_nil_p(b)) {
422  mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
423  }
424  p = mrb_proc_ptr(b);
425  ci = cipush(mrb);
426  ci->mid = mid;
427  ci->proc = p;
428  ci->stackidx = mrb->c->stack - mrb->c->stbase;
429  ci->argc = argc;
430  ci->target_class = c;
431  if (MRB_PROC_CFUNC_P(p)) {
432  ci->nregs = argc + 2;
433  }
434  else {
435  ci->nregs = p->body.irep->nregs + 1;
436  }
437  ci->acc = CI_ACC_SKIP;
438  mrb->c->stack = mrb->c->stack + n;
439 
440  stack_extend(mrb, ci->nregs, 0);
441  mrb->c->stack[0] = self;
442  if (argc > 0) {
443  stack_copy(mrb->c->stack+1, argv, argc);
444  }
445  mrb->c->stack[argc+1] = mrb_nil_value();
446 
447  if (MRB_PROC_CFUNC_P(p)) {
448  val = p->body.func(mrb, self);
449  mrb->c->stack = mrb->c->stbase + mrb->c->ci->stackidx;
450  cipop(mrb);
451  }
452  else {
453  val = mrb_run(mrb, p, self);
454  }
455  return val;
456 }
457 
458 mrb_value
460 {
461  struct RProc *p = mrb_proc_ptr(b);
462 
463  return mrb_yield_internal(mrb, b, argc, argv, p->env->stack[0], p->target_class);
464 }
465 
466 mrb_value
468 {
469  struct RProc *p = mrb_proc_ptr(b);
470 
471  return mrb_yield_internal(mrb, b, 1, &arg, p->env->stack[0], p->target_class);
472 }
473 
474 typedef enum {
479 
480 static void
481 localjump_error(mrb_state *mrb, localjump_error_kind kind)
482 {
483  char kind_str[3][7] = { "return", "break", "yield" };
484  char kind_str_len[] = { 6, 5, 5 };
485  static const char lead[] = "unexpected ";
486  mrb_value msg;
487  mrb_value exc;
488 
489  msg = mrb_str_buf_new(mrb, sizeof(lead) + 7);
490  mrb_str_buf_cat(mrb, msg, lead, sizeof(lead) - 1);
491  mrb_str_buf_cat(mrb, msg, kind_str[kind], kind_str_len[kind]);
492  exc = mrb_exc_new3(mrb, E_LOCALJUMP_ERROR, msg);
493  mrb->exc = mrb_obj_ptr(exc);
494 }
495 
496 static void
497 argnum_error(mrb_state *mrb, int num)
498 {
499  mrb_value exc;
500  mrb_value str;
501 
502  if (mrb->c->ci->mid) {
503  str = mrb_format(mrb, "'%S': wrong number of arguments (%S for %S)",
504  mrb_sym2str(mrb, mrb->c->ci->mid),
505  mrb_fixnum_value(mrb->c->ci->argc), mrb_fixnum_value(num));
506  }
507  else {
508  str = mrb_format(mrb, "wrong number of arguments (%S for %S)",
509  mrb_fixnum_value(mrb->c->ci->argc), mrb_fixnum_value(num));
510  }
511  exc = mrb_exc_new3(mrb, E_ARGUMENT_ERROR, str);
512  mrb->exc = mrb_obj_ptr(exc);
513 }
514 
515 #ifdef ENABLE_DEBUG
516 #define CODE_FETCH_HOOK(mrb, irep, pc, regs) ((mrb)->code_fetch_hook ? (mrb)->code_fetch_hook((mrb), (irep), (pc), (regs)) : NULL)
517 #else
518 #define CODE_FETCH_HOOK(mrb, irep, pc, regs)
519 #endif
520 
521 #ifdef __GNUC__
522 #define DIRECT_THREADED
523 #endif
524 
525 #ifndef DIRECT_THREADED
526 
527 #define INIT_DISPATCH for (;;) { i = *pc; CODE_FETCH_HOOK(mrb, irep, pc, regs); switch (GET_OPCODE(i)) {
528 #define CASE(op) case op:
529 #define NEXT pc++; break
530 #define JUMP break
531 #define END_DISPATCH }}
532 
533 #else
534 
535 #define INIT_DISPATCH JUMP; return mrb_nil_value();
536 #define CASE(op) L_ ## op:
537 #define NEXT i=*++pc; CODE_FETCH_HOOK(mrb, irep, pc, regs); goto *optable[GET_OPCODE(i)]
538 #define JUMP i=*pc; CODE_FETCH_HOOK(mrb, irep, pc, regs); goto *optable[GET_OPCODE(i)]
539 
540 #define END_DISPATCH
541 
542 #endif
543 
545 void mrb_gv_val_set(mrb_state *mrb, mrb_sym sym, mrb_value val);
546 
547 #define CALL_MAXARGS 127
548 
549 mrb_value
550 mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self)
551 {
552  /* mrb_assert(mrb_proc_cfunc_p(proc)) */
553  mrb_irep *irep = proc->body.irep;
554  mrb_code *pc = irep->iseq;
555  mrb_value *pool = irep->pool;
556  mrb_sym *syms = irep->syms;
557  mrb_value *regs = NULL;
558  mrb_code i;
559  int ai = mrb_gc_arena_save(mrb);
560  jmp_buf *prev_jmp = (jmp_buf *)mrb->jmp;
561  jmp_buf c_jmp;
562 
563 #ifdef DIRECT_THREADED
564  static void *optable[] = {
565  &&L_OP_NOP, &&L_OP_MOVE,
566  &&L_OP_LOADL, &&L_OP_LOADI, &&L_OP_LOADSYM, &&L_OP_LOADNIL,
567  &&L_OP_LOADSELF, &&L_OP_LOADT, &&L_OP_LOADF,
568  &&L_OP_GETGLOBAL, &&L_OP_SETGLOBAL, &&L_OP_GETSPECIAL, &&L_OP_SETSPECIAL,
569  &&L_OP_GETIV, &&L_OP_SETIV, &&L_OP_GETCV, &&L_OP_SETCV,
570  &&L_OP_GETCONST, &&L_OP_SETCONST, &&L_OP_GETMCNST, &&L_OP_SETMCNST,
571  &&L_OP_GETUPVAR, &&L_OP_SETUPVAR,
572  &&L_OP_JMP, &&L_OP_JMPIF, &&L_OP_JMPNOT,
573  &&L_OP_ONERR, &&L_OP_RESCUE, &&L_OP_POPERR, &&L_OP_RAISE, &&L_OP_EPUSH, &&L_OP_EPOP,
574  &&L_OP_SEND, &&L_OP_SENDB, &&L_OP_FSEND,
575  &&L_OP_CALL, &&L_OP_SUPER, &&L_OP_ARGARY, &&L_OP_ENTER,
576  &&L_OP_KARG, &&L_OP_KDICT, &&L_OP_RETURN, &&L_OP_TAILCALL, &&L_OP_BLKPUSH,
577  &&L_OP_ADD, &&L_OP_ADDI, &&L_OP_SUB, &&L_OP_SUBI, &&L_OP_MUL, &&L_OP_DIV,
578  &&L_OP_EQ, &&L_OP_LT, &&L_OP_LE, &&L_OP_GT, &&L_OP_GE,
579  &&L_OP_ARRAY, &&L_OP_ARYCAT, &&L_OP_ARYPUSH, &&L_OP_AREF, &&L_OP_ASET, &&L_OP_APOST,
580  &&L_OP_STRING, &&L_OP_STRCAT, &&L_OP_HASH,
581  &&L_OP_LAMBDA, &&L_OP_RANGE, &&L_OP_OCLASS,
582  &&L_OP_CLASS, &&L_OP_MODULE, &&L_OP_EXEC,
583  &&L_OP_METHOD, &&L_OP_SCLASS, &&L_OP_TCLASS,
584  &&L_OP_DEBUG, &&L_OP_STOP, &&L_OP_ERR,
585  };
586 #endif
587 
588 
589  if (setjmp(c_jmp) == 0) {
590  mrb->jmp = &c_jmp;
591  }
592  else {
593  goto L_RAISE;
594  }
595  if (!mrb->c->stack) {
596  stack_init(mrb);
597  }
598  stack_extend(mrb, irep->nregs, irep->nregs);
599  mrb->c->ci->proc = proc;
600  mrb->c->ci->nregs = irep->nregs + 1;
601  regs = mrb->c->stack;
602  regs[0] = self;
603 
604  INIT_DISPATCH {
605  CASE(OP_NOP) {
606  /* do nothing */
607  NEXT;
608  }
609 
610  CASE(OP_MOVE) {
611  /* A B R(A) := R(B) */
612  regs[GETARG_A(i)] = regs[GETARG_B(i)];
613  NEXT;
614  }
615 
616  CASE(OP_LOADL) {
617  /* A Bx R(A) := Pool(Bx) */
618  regs[GETARG_A(i)] = pool[GETARG_Bx(i)];
619  NEXT;
620  }
621 
622  CASE(OP_LOADI) {
623  /* A Bx R(A) := sBx */
624  SET_INT_VALUE(regs[GETARG_A(i)], GETARG_sBx(i));
625  NEXT;
626  }
627 
628  CASE(OP_LOADSYM) {
629  /* A B R(A) := Sym(B) */
630  SET_SYM_VALUE(regs[GETARG_A(i)], syms[GETARG_Bx(i)]);
631  NEXT;
632  }
633 
634  CASE(OP_LOADSELF) {
635  /* A R(A) := self */
636  regs[GETARG_A(i)] = regs[0];
637  NEXT;
638  }
639 
640  CASE(OP_LOADT) {
641  /* A R(A) := true */
642  SET_TRUE_VALUE(regs[GETARG_A(i)]);
643  NEXT;
644  }
645 
646  CASE(OP_LOADF) {
647  /* A R(A) := false */
648  SET_FALSE_VALUE(regs[GETARG_A(i)]);
649  NEXT;
650  }
651 
652  CASE(OP_GETGLOBAL) {
653  /* A B R(A) := getglobal(Sym(B)) */
654  regs[GETARG_A(i)] = mrb_gv_get(mrb, syms[GETARG_Bx(i)]);
655  NEXT;
656  }
657 
658  CASE(OP_SETGLOBAL) {
659  /* setglobal(Sym(b), R(A)) */
660  mrb_gv_set(mrb, syms[GETARG_Bx(i)], regs[GETARG_A(i)]);
661  NEXT;
662  }
663 
665  /* A Bx R(A) := Special[Bx] */
666  regs[GETARG_A(i)] = mrb_vm_special_get(mrb, GETARG_Bx(i));
667  NEXT;
668  }
669 
671  /* A Bx Special[Bx] := R(A) */
672  mrb_vm_special_set(mrb, GETARG_Bx(i), regs[GETARG_A(i)]);
673  NEXT;
674  }
675 
676  CASE(OP_GETIV) {
677  /* A Bx R(A) := ivget(Bx) */
678  regs[GETARG_A(i)] = mrb_vm_iv_get(mrb, syms[GETARG_Bx(i)]);
679  NEXT;
680  }
681 
682  CASE(OP_SETIV) {
683  /* ivset(Sym(B),R(A)) */
684  mrb_vm_iv_set(mrb, syms[GETARG_Bx(i)], regs[GETARG_A(i)]);
685  NEXT;
686  }
687 
688  CASE(OP_GETCV) {
689  /* A B R(A) := ivget(Sym(B)) */
690  regs[GETARG_A(i)] = mrb_vm_cv_get(mrb, syms[GETARG_Bx(i)]);
691  NEXT;
692  }
693 
694  CASE(OP_SETCV) {
695  /* ivset(Sym(B),R(A)) */
696  mrb_vm_cv_set(mrb, syms[GETARG_Bx(i)], regs[GETARG_A(i)]);
697  NEXT;
698  }
699 
700  CASE(OP_GETCONST) {
701  /* A B R(A) := constget(Sym(B)) */
702  regs[GETARG_A(i)] = mrb_vm_const_get(mrb, syms[GETARG_Bx(i)]);
703  NEXT;
704  }
705 
706  CASE(OP_SETCONST) {
707  /* A B constset(Sym(B),R(A)) */
708  mrb_vm_const_set(mrb, syms[GETARG_Bx(i)], regs[GETARG_A(i)]);
709  NEXT;
710  }
711 
712  CASE(OP_GETMCNST) {
713  /* A B C R(A) := R(C)::Sym(B) */
714  int a = GETARG_A(i);
715 
716  regs[a] = mrb_const_get(mrb, regs[a], syms[GETARG_Bx(i)]);
717  NEXT;
718  }
719 
720  CASE(OP_SETMCNST) {
721  /* A B C R(A+1)::Sym(B) := R(A) */
722  int a = GETARG_A(i);
723 
724  mrb_const_set(mrb, regs[a+1], syms[GETARG_Bx(i)], regs[a]);
725  NEXT;
726  }
727 
728  CASE(OP_GETUPVAR) {
729  /* A B C R(A) := uvget(B,C) */
730  mrb_value *regs_a = regs + GETARG_A(i);
731  int up = GETARG_C(i);
732 
733  struct REnv *e = uvenv(mrb, up);
734 
735  if (!e) {
736  *regs_a = mrb_nil_value();
737  }
738  else {
739  int idx = GETARG_B(i);
740  *regs_a = e->stack[idx];
741  }
742  NEXT;
743  }
744 
745  CASE(OP_SETUPVAR) {
746  /* A B C uvset(B,C,R(A)) */
747  /* A B C R(A) := uvget(B,C) */
748  int up = GETARG_C(i);
749 
750  struct REnv *e = uvenv(mrb, up);
751 
752  if (e) {
753  mrb_value *regs_a = regs + GETARG_A(i);
754  int idx = GETARG_B(i);
755  e->stack[idx] = *regs_a;
756  mrb_write_barrier(mrb, (struct RBasic*)e);
757  }
758  NEXT;
759  }
760 
761  CASE(OP_JMP) {
762  /* sBx pc+=sBx */
763  pc += GETARG_sBx(i);
764  JUMP;
765  }
766 
767  CASE(OP_JMPIF) {
768  /* A sBx if R(A) pc+=sBx */
769  if (mrb_test(regs[GETARG_A(i)])) {
770  pc += GETARG_sBx(i);
771  JUMP;
772  }
773  NEXT;
774  }
775 
776  CASE(OP_JMPNOT) {
777  /* A sBx if R(A) pc+=sBx */
778  if (!mrb_test(regs[GETARG_A(i)])) {
779  pc += GETARG_sBx(i);
780  JUMP;
781  }
782  NEXT;
783  }
784 
785  CASE(OP_ONERR) {
786  /* sBx pc+=sBx on exception */
787  if (mrb->c->rsize <= mrb->c->ci->ridx) {
788  if (mrb->c->rsize == 0) mrb->c->rsize = 16;
789  else mrb->c->rsize *= 2;
790  mrb->c->rescue = (mrb_code **)mrb_realloc(mrb, mrb->c->rescue, sizeof(mrb_code*) * mrb->c->rsize);
791  }
792  mrb->c->rescue[mrb->c->ci->ridx++] = pc + GETARG_sBx(i);
793  NEXT;
794  }
795 
796  CASE(OP_RESCUE) {
797  /* A R(A) := exc; clear(exc) */
798  SET_OBJ_VALUE(regs[GETARG_A(i)], mrb->exc);
799  mrb->exc = 0;
800  NEXT;
801  }
802 
803  CASE(OP_POPERR) {
804  int a = GETARG_A(i);
805 
806  while (a--) {
807  mrb->c->ci->ridx--;
808  }
809  NEXT;
810  }
811 
812  CASE(OP_RAISE) {
813  /* A raise(R(A)) */
814  mrb->exc = mrb_obj_ptr(regs[GETARG_A(i)]);
815  goto L_RAISE;
816  }
817 
818  CASE(OP_EPUSH) {
819  /* Bx ensure_push(SEQ[Bx]) */
820  struct RProc *p;
821 
822  p = mrb_closure_new(mrb, mrb->irep[irep->idx+GETARG_Bx(i)]);
823  /* push ensure_stack */
824  if (mrb->c->esize <= mrb->c->ci->eidx) {
825  if (mrb->c->esize == 0) mrb->c->esize = 16;
826  else mrb->c->esize *= 2;
827  mrb->c->ensure = (struct RProc **)mrb_realloc(mrb, mrb->c->ensure, sizeof(struct RProc*) * mrb->c->esize);
828  }
829  mrb->c->ensure[mrb->c->ci->eidx++] = p;
830  mrb_gc_arena_restore(mrb, ai);
831  NEXT;
832  }
833 
834  CASE(OP_EPOP) {
835  /* A A.times{ensure_pop().call} */
836  int n;
837  int a = GETARG_A(i);
838 
839  for (n=0; n<a; n++) {
840  ecall(mrb, --mrb->c->ci->eidx);
841  }
842  mrb_gc_arena_restore(mrb, ai);
843  NEXT;
844  }
845 
846  CASE(OP_LOADNIL) {
847  /* A B R(A) := nil */
848  int a = GETARG_A(i);
849 
850  SET_NIL_VALUE(regs[a]);
851  NEXT;
852  }
853 
854  CASE(OP_SENDB) {
855  /* fall through */
856  };
857 
858  L_SEND:
859  CASE(OP_SEND) {
860  /* A B C R(A) := call(R(A),Sym(B),R(A+1),... ,R(A+C-1)) */
861  int a = GETARG_A(i);
862  int n = GETARG_C(i);
863  struct RProc *m;
864  struct RClass *c;
865  mrb_callinfo *ci;
866  mrb_value recv, result;
867  mrb_sym mid = syms[GETARG_B(i)];
868 
869  recv = regs[a];
870  if (GET_OPCODE(i) != OP_SENDB) {
871  if (n == CALL_MAXARGS) {
872  SET_NIL_VALUE(regs[a+2]);
873  }
874  else {
875  SET_NIL_VALUE(regs[a+n+1]);
876  }
877  }
878  c = mrb_class(mrb, recv);
879  m = mrb_method_search_vm(mrb, &c, mid);
880  if (!m) {
881  mrb_value sym = mrb_symbol_value(mid);
882 
883  mid = mrb_intern2(mrb, "method_missing", 14);
884  m = mrb_method_search_vm(mrb, &c, mid);
885  if (n == CALL_MAXARGS) {
886  mrb_ary_unshift(mrb, regs[a+1], sym);
887  }
888  else {
889  value_move(regs+a+2, regs+a+1, ++n);
890  regs[a+1] = sym;
891  }
892  }
893 
894  /* push callinfo */
895  ci = cipush(mrb);
896  ci->mid = mid;
897  ci->proc = m;
898  ci->stackidx = mrb->c->stack - mrb->c->stbase;
899  if (n == CALL_MAXARGS) {
900  ci->argc = -1;
901  }
902  else {
903  ci->argc = n;
904  }
905  if (c->tt == MRB_TT_ICLASS) {
906  ci->target_class = c->c;
907  }
908  else {
909  ci->target_class = c;
910  }
911 
912  ci->pc = pc + 1;
913  ci->acc = a;
914 
915  /* prepare stack */
916  mrb->c->stack += a;
917 
918  if (MRB_PROC_CFUNC_P(m)) {
919  if (n == CALL_MAXARGS) {
920  ci->nregs = 3;
921  }
922  else {
923  ci->nregs = n + 2;
924  }
925  result = m->body.func(mrb, recv);
926  mrb->c->stack[0] = result;
927  mrb_gc_arena_restore(mrb, ai);
928  if (mrb->exc) goto L_RAISE;
929  /* pop stackpos */
930  ci = mrb->c->ci;
931  if (!ci->target_class) { /* return from context modifying method (resume/yield) */
932  if (!MRB_PROC_CFUNC_P(ci[-1].proc)) {
933  irep = ci[-1].proc->body.irep;
934  pool = irep->pool;
935  syms = irep->syms;
936  }
937  }
938  regs = mrb->c->stack = mrb->c->stbase + ci->stackidx;
939  pc = ci->pc;
940  cipop(mrb);
941  JUMP;
942  }
943  else {
944  /* setup environment for calling method */
945  proc = mrb->c->ci->proc = m;
946  irep = m->body.irep;
947  pool = irep->pool;
948  syms = irep->syms;
949  ci->nregs = irep->nregs;
950  if (ci->argc < 0) {
951  stack_extend(mrb, (irep->nregs < 3) ? 3 : irep->nregs, 3);
952  }
953  else {
954  stack_extend(mrb, irep->nregs, ci->argc+2);
955  }
956  regs = mrb->c->stack;
957  pc = irep->iseq;
958  JUMP;
959  }
960  }
961 
962  CASE(OP_FSEND) {
963  /* A B C R(A) := fcall(R(A),Sym(B),R(A+1),... ,R(A+C)) */
964  NEXT;
965  }
966 
967  CASE(OP_CALL) {
968  /* A R(A) := self.call(frame.argc, frame.argv) */
969  mrb_callinfo *ci;
970  mrb_value recv = mrb->c->stack[0];
971  struct RProc *m = mrb_proc_ptr(recv);
972 
973  /* replace callinfo */
974  ci = mrb->c->ci;
975  ci->target_class = m->target_class;
976  ci->proc = m;
977  if (m->env) {
978  if (m->env->mid) {
979  ci->mid = m->env->mid;
980  }
981  if (!m->env->stack) {
982  m->env->stack = mrb->c->stack;
983  }
984  }
985 
986  /* prepare stack */
987  if (MRB_PROC_CFUNC_P(m)) {
988  recv = m->body.func(mrb, recv);
989  mrb_gc_arena_restore(mrb, ai);
990  if (mrb->exc) goto L_RAISE;
991  /* pop stackpos */
992  ci = mrb->c->ci;
993  regs = mrb->c->stack = mrb->c->stbase + ci->stackidx;
994  regs[ci->acc] = recv;
995  pc = ci->pc;
996  cipop(mrb);
997  irep = mrb->c->ci->proc->body.irep;
998  pool = irep->pool;
999  syms = irep->syms;
1000  JUMP;
1001  }
1002  else {
1003  /* setup environment for calling method */
1004  proc = m;
1005  irep = m->body.irep;
1006  if (!irep) {
1007  mrb->c->stack[0] = mrb_nil_value();
1008  goto L_RETURN;
1009  }
1010  pool = irep->pool;
1011  syms = irep->syms;
1012  ci->nregs = irep->nregs;
1013  if (ci->argc < 0) {
1014  stack_extend(mrb, (irep->nregs < 3) ? 3 : irep->nregs, 3);
1015  }
1016  else {
1017  stack_extend(mrb, irep->nregs, ci->argc+2);
1018  }
1019  regs = mrb->c->stack;
1020  regs[0] = m->env->stack[0];
1021  pc = m->body.irep->iseq;
1022  JUMP;
1023  }
1024  }
1025 
1026  CASE(OP_SUPER) {
1027  /* A B C R(A) := super(R(A+1),... ,R(A+C-1)) */
1028  mrb_value recv;
1029  mrb_callinfo *ci = mrb->c->ci;
1030  struct RProc *m;
1031  struct RClass *c;
1032  mrb_sym mid = ci->mid;
1033  int a = GETARG_A(i);
1034  int n = GETARG_C(i);
1035 
1036  recv = regs[0];
1037  c = mrb->c->ci->target_class->super;
1038  m = mrb_method_search_vm(mrb, &c, mid);
1039  if (!m) {
1040  mid = mrb_intern2(mrb, "method_missing", 14);
1041  m = mrb_method_search_vm(mrb, &c, mid);
1042  if (n == CALL_MAXARGS) {
1043  mrb_ary_unshift(mrb, regs[a+1], mrb_symbol_value(ci->mid));
1044  }
1045  else {
1046  value_move(regs+a+2, regs+a+1, ++n);
1047  SET_SYM_VALUE(regs[a+1], ci->mid);
1048  }
1049  }
1050 
1051  /* push callinfo */
1052  ci = cipush(mrb);
1053  ci->mid = mid;
1054  ci->proc = m;
1055  ci->stackidx = mrb->c->stack - mrb->c->stbase;
1056  if (n == CALL_MAXARGS) {
1057  ci->argc = -1;
1058  }
1059  else {
1060  ci->argc = n;
1061  }
1062  ci->target_class = c;
1063  ci->pc = pc + 1;
1064 
1065  /* prepare stack */
1066  mrb->c->stack += a;
1067  mrb->c->stack[0] = recv;
1068 
1069  if (MRB_PROC_CFUNC_P(m)) {
1070  mrb->c->stack[0] = m->body.func(mrb, recv);
1071  mrb_gc_arena_restore(mrb, ai);
1072  if (mrb->exc) goto L_RAISE;
1073  /* pop stackpos */
1074  regs = mrb->c->stack = mrb->c->stbase + mrb->c->ci->stackidx;
1075  cipop(mrb);
1076  NEXT;
1077  }
1078  else {
1079  /* fill callinfo */
1080  ci->acc = a;
1081 
1082  /* setup environment for calling method */
1083  ci->proc = m;
1084  irep = m->body.irep;
1085  pool = irep->pool;
1086  syms = irep->syms;
1087  ci->nregs = irep->nregs;
1088  if (n == CALL_MAXARGS) {
1089  stack_extend(mrb, (irep->nregs < 3) ? 3 : irep->nregs, 3);
1090  }
1091  else {
1092  stack_extend(mrb, irep->nregs, ci->argc+2);
1093  }
1094  regs = mrb->c->stack;
1095  pc = irep->iseq;
1096  JUMP;
1097  }
1098  }
1099 
1100  CASE(OP_ARGARY) {
1101  /* A Bx R(A) := argument array (16=6:1:5:4) */
1102  int a = GETARG_A(i);
1103  int bx = GETARG_Bx(i);
1104  int m1 = (bx>>10)&0x3f;
1105  int r = (bx>>9)&0x1;
1106  int m2 = (bx>>4)&0x1f;
1107  int lv = (bx>>0)&0xf;
1108  mrb_value *stack;
1109 
1110  if (lv == 0) stack = regs + 1;
1111  else {
1112  struct REnv *e = uvenv(mrb, lv-1);
1113  if (!e) {
1114  mrb_value exc;
1115  static const char m[] = "super called outside of method";
1116  exc = mrb_exc_new(mrb, E_NOMETHOD_ERROR, m, sizeof(m) - 1);
1117  mrb->exc = mrb_obj_ptr(exc);
1118  goto L_RAISE;
1119  }
1120  stack = e->stack + 1;
1121  }
1122  if (r == 0) {
1123  regs[a] = mrb_ary_new_from_values(mrb, m1+m2, stack);
1124  }
1125  else {
1126  mrb_value *pp = NULL;
1127  struct RArray *rest;
1128  int len = 0;
1129 
1130  if (mrb_array_p(stack[m1])) {
1131  struct RArray *ary = mrb_ary_ptr(stack[m1]);
1132 
1133  pp = ary->ptr;
1134  len = ary->len;
1135  }
1136  regs[a] = mrb_ary_new_capa(mrb, m1+len+m2);
1137  rest = mrb_ary_ptr(regs[a]);
1138  stack_copy(rest->ptr, stack, m1);
1139  if (len > 0) {
1140  stack_copy(rest->ptr+m1, pp, len);
1141  }
1142  if (m2 > 0) {
1143  stack_copy(rest->ptr+m1+len, stack+m1+1, m2);
1144  }
1145  rest->len = m1+len+m2;
1146  }
1147  regs[a+1] = stack[m1+r+m2];
1148  mrb_gc_arena_restore(mrb, ai);
1149  NEXT;
1150  }
1151 
1152  CASE(OP_ENTER) {
1153  /* Ax arg setup according to flags (24=5:5:1:5:5:1:1) */
1154  /* number of optional arguments times OP_JMP should follow */
1155  mrb_aspec ax = GETARG_Ax(i);
1156  int m1 = (ax>>18)&0x1f;
1157  int o = (ax>>13)&0x1f;
1158  int r = (ax>>12)&0x1;
1159  int m2 = (ax>>7)&0x1f;
1160  /* unused
1161  int k = (ax>>2)&0x1f;
1162  int kd = (ax>>1)&0x1;
1163  int b = (ax>>0)& 0x1;
1164  */
1165  int argc = mrb->c->ci->argc;
1166  mrb_value *argv = regs+1;
1167  mrb_value *argv0 = argv;
1168  int len = m1 + o + r + m2;
1169  mrb_value *blk = &argv[argc < 0 ? 1 : argc];
1170 
1171  if (argc < 0) {
1172  struct RArray *ary = mrb_ary_ptr(regs[1]);
1173  argv = ary->ptr;
1174  argc = ary->len;
1175  mrb_gc_protect(mrb, regs[1]);
1176  }
1177  if (mrb->c->ci->proc && MRB_PROC_STRICT_P(mrb->c->ci->proc)) {
1178  if (argc >= 0) {
1179  if (argc < m1 + m2 || (r == 0 && argc > len)) {
1180  argnum_error(mrb, m1+m2);
1181  goto L_RAISE;
1182  }
1183  }
1184  }
1185  else if (len > 1 && argc == 1 && mrb_array_p(argv[0])) {
1186  argc = mrb_ary_ptr(argv[0])->len;
1187  argv = mrb_ary_ptr(argv[0])->ptr;
1188  }
1189  mrb->c->ci->argc = len;
1190  if (argc < len) {
1191  regs[len+1] = *blk; /* move block */
1192  if (argv0 != argv) {
1193  value_move(&regs[1], argv, argc-m2); /* m1 + o */
1194  }
1195  if (m2) {
1196  int mlen = m2;
1197  if (argc-m2 <= m1) {
1198  mlen = argc - m1;
1199  }
1200  value_move(&regs[len-m2+1], &argv[argc-mlen], mlen);
1201  }
1202  if (r) {
1203  regs[m1+o+1] = mrb_ary_new_capa(mrb, 0);
1204  }
1205  if (o == 0) pc++;
1206  else
1207  pc += argc - m1 - m2 + 1;
1208  }
1209  else {
1210  if (argv0 != argv) {
1211  regs[len+1] = *blk; /* move block */
1212  value_move(&regs[1], argv, m1+o);
1213  }
1214  if (r) {
1215  regs[m1+o+1] = mrb_ary_new_from_values(mrb, argc-m1-o-m2, argv+m1+o);
1216  }
1217  if (m2) {
1218  if (argc-m2 > m1) {
1219  value_move(&regs[m1+o+r+1], &argv[argc-m2], m2);
1220  }
1221  }
1222  if (argv0 == argv) {
1223  regs[len+1] = *blk; /* move block */
1224  }
1225  pc += o + 1;
1226  }
1227  JUMP;
1228  }
1229 
1230  CASE(OP_KARG) {
1231  /* A B C R(A) := kdict[Sym(B)]; if C kdict.rm(Sym(B)) */
1232  /* if C == 2; raise unless kdict.empty? */
1233  /* OP_JMP should follow to skip init code */
1234  NEXT;
1235  }
1236 
1237  CASE(OP_KDICT) {
1238  /* A C R(A) := kdict */
1239  NEXT;
1240  }
1241 
1242  L_RETURN:
1244  /* fall through */
1245  CASE(OP_RETURN) {
1246  /* A return R(A) */
1247  if (mrb->exc) {
1248  mrb_callinfo *ci;
1249  int eidx;
1250 
1251  L_RAISE:
1252  ci = mrb->c->ci;
1253  mrb_obj_iv_ifnone(mrb, mrb->exc, mrb_intern2(mrb, "lastpc", 6), mrb_cptr_value(mrb, pc));
1254  mrb_obj_iv_ifnone(mrb, mrb->exc, mrb_intern2(mrb, "ciidx", 5), mrb_fixnum_value(ci - mrb->c->cibase));
1255  eidx = ci->eidx;
1256  if (ci == mrb->c->cibase) {
1257  if (ci->ridx == 0) goto L_STOP;
1258  goto L_RESCUE;
1259  }
1260  while (eidx > ci[-1].eidx) {
1261  ecall(mrb, --eidx);
1262  }
1263  while (ci[0].ridx == ci[-1].ridx) {
1264  cipop(mrb);
1265  ci = mrb->c->ci;
1266  mrb->c->stack = mrb->c->stbase + ci[1].stackidx;
1267  if (ci[1].acc == CI_ACC_SKIP && prev_jmp) {
1268  mrb->jmp = prev_jmp;
1269  mrb_longjmp(mrb);
1270  }
1271  while (eidx > ci->eidx) {
1272  ecall(mrb, --eidx);
1273  }
1274  if (ci == mrb->c->cibase) {
1275  if (ci->ridx == 0) {
1276  regs = mrb->c->stack = mrb->c->stbase;
1277  goto L_STOP;
1278  }
1279  break;
1280  }
1281  }
1282  L_RESCUE:
1283  irep = ci->proc->body.irep;
1284  pool = irep->pool;
1285  syms = irep->syms;
1286  regs = mrb->c->stack = mrb->c->stbase + ci[1].stackidx;
1287  pc = mrb->c->rescue[--ci->ridx];
1288  }
1289  else {
1290  mrb_callinfo *ci = mrb->c->ci;
1291  int acc, eidx = mrb->c->ci->eidx;
1292  mrb_value v = regs[GETARG_A(i)];
1293 
1294  switch (GETARG_B(i)) {
1295  case OP_R_RETURN:
1296  // Fall through to OP_R_NORMAL otherwise
1297  if (proc->env && !MRB_PROC_STRICT_P(proc)) {
1298  struct REnv *e = top_env(mrb, proc);
1299 
1300  if (e->cioff < 0) {
1301  localjump_error(mrb, LOCALJUMP_ERROR_RETURN);
1302  goto L_RAISE;
1303  }
1304  ci = mrb->c->cibase + e->cioff;
1305  if (ci == mrb->c->cibase) {
1306  localjump_error(mrb, LOCALJUMP_ERROR_RETURN);
1307  goto L_RAISE;
1308  }
1309  mrb->c->ci = ci;
1310  break;
1311  }
1312  case OP_R_NORMAL:
1313  if (ci == mrb->c->cibase) {
1314  if (!mrb->c->prev) { /* toplevel return */
1315  localjump_error(mrb, LOCALJUMP_ERROR_RETURN);
1316  goto L_RAISE;
1317  }
1318  if (mrb->c->prev->ci == mrb->c->prev->cibase) {
1319  mrb_value exc = mrb_exc_new3(mrb, E_RUNTIME_ERROR, mrb_str_new(mrb, "double resume", 13));
1320  mrb->exc = mrb_obj_ptr(exc);
1321  goto L_RAISE;
1322  }
1323  /* automatic yield at the end */
1324  mrb->c->status = MRB_FIBER_TERMINATED;
1325  mrb->c = mrb->c->prev;
1326  }
1327  ci = mrb->c->ci;
1328  break;
1329  case OP_R_BREAK:
1330  if (proc->env->cioff < 0) {
1331  localjump_error(mrb, LOCALJUMP_ERROR_BREAK);
1332  goto L_RAISE;
1333  }
1334  ci = mrb->c->ci = mrb->c->cibase + proc->env->cioff + 1;
1335  break;
1336  default:
1337  /* cannot happen */
1338  break;
1339  }
1340  while (eidx > mrb->c->ci[-1].eidx) {
1341  ecall(mrb, --eidx);
1342  }
1343  cipop(mrb);
1344  acc = ci->acc;
1345  pc = ci->pc;
1346  regs = mrb->c->stack = mrb->c->stbase + ci->stackidx;
1347  if (acc == CI_ACC_SKIP) {
1348  mrb->jmp = prev_jmp;
1349  return v;
1350  }
1351  DEBUG(printf("from :%s\n", mrb_sym2name(mrb, ci->mid)));
1352  proc = mrb->c->ci->proc;
1353  irep = proc->body.irep;
1354  pool = irep->pool;
1355  syms = irep->syms;
1356 
1357  regs[acc] = v;
1358  }
1359  JUMP;
1360  }
1361 
1362  CASE(OP_TAILCALL) {
1363  /* A B C return call(R(A),Sym(B),R(A+1),... ,R(A+C-1)) */
1364  int a = GETARG_A(i);
1365  int n = GETARG_C(i);
1366  struct RProc *m;
1367  struct RClass *c;
1368  mrb_callinfo *ci;
1369  mrb_value recv;
1370  mrb_sym mid = syms[GETARG_B(i)];
1371 
1372  recv = regs[a];
1373  c = mrb_class(mrb, recv);
1374  m = mrb_method_search_vm(mrb, &c, mid);
1375  if (!m) {
1376  mrb_value sym = mrb_symbol_value(mid);
1377 
1378  mid = mrb_intern2(mrb, "method_missing", 14);
1379  m = mrb_method_search_vm(mrb, &c, mid);
1380  if (n == CALL_MAXARGS) {
1381  mrb_ary_unshift(mrb, regs[a+1], sym);
1382  }
1383  else {
1384  value_move(regs+a+2, regs+a+1, ++n);
1385  regs[a+1] = sym;
1386  }
1387  }
1388 
1389  /* replace callinfo */
1390  ci = mrb->c->ci;
1391  ci->mid = mid;
1392  ci->target_class = c;
1393  if (n == CALL_MAXARGS) {
1394  ci->argc = -1;
1395  }
1396  else {
1397  ci->argc = n;
1398  }
1399 
1400  /* move stack */
1401  value_move(mrb->c->stack, &regs[a], ci->argc+1);
1402 
1403  if (MRB_PROC_CFUNC_P(m)) {
1404  mrb->c->stack[0] = m->body.func(mrb, recv);
1405  mrb_gc_arena_restore(mrb, ai);
1406  goto L_RETURN;
1407  }
1408  else {
1409  /* setup environment for calling method */
1410  irep = m->body.irep;
1411  pool = irep->pool;
1412  syms = irep->syms;
1413  if (ci->argc < 0) {
1414  stack_extend(mrb, (irep->nregs < 3) ? 3 : irep->nregs, 3);
1415  }
1416  else {
1417  stack_extend(mrb, irep->nregs, ci->argc+2);
1418  }
1419  regs = mrb->c->stack;
1420  pc = irep->iseq;
1421  }
1422  JUMP;
1423  }
1424 
1425  CASE(OP_BLKPUSH) {
1426  /* A Bx R(A) := block (16=6:1:5:4) */
1427  int a = GETARG_A(i);
1428  int bx = GETARG_Bx(i);
1429  int m1 = (bx>>10)&0x3f;
1430  int r = (bx>>9)&0x1;
1431  int m2 = (bx>>4)&0x1f;
1432  int lv = (bx>>0)&0xf;
1433  mrb_value *stack;
1434 
1435  if (lv == 0) stack = regs + 1;
1436  else {
1437  struct REnv *e = uvenv(mrb, lv-1);
1438  if (!e) {
1439  localjump_error(mrb, LOCALJUMP_ERROR_YIELD);
1440  goto L_RAISE;
1441  }
1442  stack = e->stack + 1;
1443  }
1444  regs[a] = stack[m1+r+m2];
1445  NEXT;
1446  }
1447 
1448 #define attr_i value.i
1449 #ifdef MRB_NAN_BOXING
1450 #define attr_f f
1451 #elif defined(MRB_WORD_BOXING)
1452 #define attr_f value.fp->f
1453 #else
1454 #define attr_f value.f
1455 #endif
1456 
1457 #define TYPES2(a,b) ((((uint16_t)(a))<<8)|(((uint16_t)(b))&0xff))
1458 #define OP_MATH_BODY(op,v1,v2) do {\
1459  regs[a].v1 = regs[a].v1 op regs[a+1].v2;\
1460 } while(0)
1461 
1462  CASE(OP_ADD) {
1463  /* A B C R(A) := R(A)+R(A+1) (Syms[B]=:+,C=1)*/
1464  int a = GETARG_A(i);
1465 
1466  /* need to check if op is overridden */
1467  switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {
1469  {
1470  mrb_int x, y, z;
1471  mrb_value *regs_a = regs + a;
1472 
1473  x = mrb_fixnum(regs_a[0]);
1474  y = mrb_fixnum(regs_a[1]);
1475  z = x + y;
1476 #ifdef MRB_WORD_BOXING
1477  z = (z << MRB_FIXNUM_SHIFT) / (1 << MRB_FIXNUM_SHIFT);
1478 #endif
1479  if ((x < 0) != (z < 0) && ((x < 0) ^ (y < 0)) == 0) {
1480  /* integer overflow */
1481  SET_FLT_VALUE(mrb, regs_a[0], (mrb_float)x + (mrb_float)y);
1482  break;
1483  }
1484  SET_INT_VALUE(regs[a], z);
1485  }
1486  break;
1488  {
1489  mrb_int x = mrb_fixnum(regs[a]);
1490  mrb_float y = mrb_float(regs[a+1]);
1491  SET_FLT_VALUE(mrb, regs[a], (mrb_float)x + y);
1492  }
1493  break;
1495 #ifdef MRB_WORD_BOXING
1496  {
1497  mrb_float x = mrb_float(regs[a]);
1498  mrb_int y = mrb_fixnum(regs[a+1]);
1499  SET_FLT_VALUE(mrb, regs[a], x + y);
1500  }
1501 #else
1502  OP_MATH_BODY(+,attr_f,attr_i);
1503 #endif
1504  break;
1506 #ifdef MRB_WORD_BOXING
1507  {
1508  mrb_float x = mrb_float(regs[a]);
1509  mrb_float y = mrb_float(regs[a+1]);
1510  SET_FLT_VALUE(mrb, regs[a], x + y);
1511  }
1512 #else
1513  OP_MATH_BODY(+,attr_f,attr_f);
1514 #endif
1515  break;
1517  regs[a] = mrb_str_plus(mrb, regs[a], regs[a+1]);
1518  break;
1519  default:
1520  goto L_SEND;
1521  }
1522  mrb_gc_arena_restore(mrb, ai);
1523  NEXT;
1524  }
1525 
1526  CASE(OP_SUB) {
1527  /* A B C R(A) := R(A)-R(A+1) (Syms[B]=:-,C=1)*/
1528  int a = GETARG_A(i);
1529 
1530  /* need to check if op is overridden */
1531  switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {
1533  {
1534  mrb_int x, y, z;
1535 
1536  x = mrb_fixnum(regs[a]);
1537  y = mrb_fixnum(regs[a+1]);
1538  z = x - y;
1539 #ifdef MRB_WORD_BOXING
1540  z = (z << MRB_FIXNUM_SHIFT) / (1 << MRB_FIXNUM_SHIFT);
1541 #endif
1542  if (((x < 0) ^ (y < 0)) != 0 && (x < 0) != (z < 0)) {
1543  /* integer overflow */
1544  SET_FLT_VALUE(mrb, regs[a], (mrb_float)x - (mrb_float)y);
1545  break;
1546  }
1547  SET_INT_VALUE(regs[a], z);
1548  }
1549  break;
1551  {
1552  mrb_int x = mrb_fixnum(regs[a]);
1553  mrb_float y = mrb_float(regs[a+1]);
1554  SET_FLT_VALUE(mrb, regs[a], (mrb_float)x - y);
1555  }
1556  break;
1558 #ifdef MRB_WORD_BOXING
1559  {
1560  mrb_float x = mrb_float(regs[a]);
1561  mrb_int y = mrb_fixnum(regs[a+1]);
1562  SET_FLT_VALUE(mrb, regs[a], x - y);
1563  }
1564 #else
1565  OP_MATH_BODY(-,attr_f,attr_i);
1566 #endif
1567  break;
1569 #ifdef MRB_WORD_BOXING
1570  {
1571  mrb_float x = mrb_float(regs[a]);
1572  mrb_float y = mrb_float(regs[a+1]);
1573  SET_FLT_VALUE(mrb, regs[a], x - y);
1574  }
1575 #else
1576  OP_MATH_BODY(-,attr_f,attr_f);
1577 #endif
1578  break;
1579  default:
1580  goto L_SEND;
1581  }
1582  NEXT;
1583  }
1584 
1585  CASE(OP_MUL) {
1586  /* A B C R(A) := R(A)*R(A+1) (Syms[B]=:*,C=1)*/
1587  int a = GETARG_A(i);
1588 
1589  /* need to check if op is overridden */
1590  switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {
1592  {
1593  mrb_int x, y, z;
1594 
1595  x = mrb_fixnum(regs[a]);
1596  y = mrb_fixnum(regs[a+1]);
1597  z = x * y;
1598 #ifdef MRB_WORD_BOXING
1599  z = (z << MRB_FIXNUM_SHIFT) / (1 << MRB_FIXNUM_SHIFT);
1600 #endif
1601  if (x != 0 && z/x != y) {
1602  SET_FLT_VALUE(mrb, regs[a], (mrb_float)x * (mrb_float)y);
1603  }
1604  else {
1605  SET_INT_VALUE(regs[a], z);
1606  }
1607  }
1608  break;
1610  {
1611  mrb_int x = mrb_fixnum(regs[a]);
1612  mrb_float y = mrb_float(regs[a+1]);
1613  SET_FLT_VALUE(mrb, regs[a], (mrb_float)x * y);
1614  }
1615  break;
1617 #ifdef MRB_WORD_BOXING
1618  {
1619  mrb_float x = mrb_float(regs[a]);
1620  mrb_int y = mrb_fixnum(regs[a+1]);
1621  SET_FLT_VALUE(mrb, regs[a], x * y);
1622  }
1623 #else
1624  OP_MATH_BODY(*,attr_f,attr_i);
1625 #endif
1626  break;
1628 #ifdef MRB_WORD_BOXING
1629  {
1630  mrb_float x = mrb_float(regs[a]);
1631  mrb_float y = mrb_float(regs[a+1]);
1632  SET_FLT_VALUE(mrb, regs[a], x * y);
1633  }
1634 #else
1635  OP_MATH_BODY(*,attr_f,attr_f);
1636 #endif
1637  break;
1638  default:
1639  goto L_SEND;
1640  }
1641  NEXT;
1642  }
1643 
1644  CASE(OP_DIV) {
1645  /* A B C R(A) := R(A)/R(A+1) (Syms[B]=:/,C=1)*/
1646  int a = GETARG_A(i);
1647 
1648  /* need to check if op is overridden */
1649  switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {
1651  {
1652  mrb_int x = mrb_fixnum(regs[a]);
1653  mrb_int y = mrb_fixnum(regs[a+1]);
1654  SET_FLT_VALUE(mrb, regs[a], (mrb_float)x / (mrb_float)y);
1655  }
1656  break;
1658  {
1659  mrb_int x = mrb_fixnum(regs[a]);
1660  mrb_float y = mrb_float(regs[a+1]);
1661  SET_FLT_VALUE(mrb, regs[a], (mrb_float)x / y);
1662  }
1663  break;
1665 #ifdef MRB_WORD_BOXING
1666  {
1667  mrb_float x = mrb_float(regs[a]);
1668  mrb_int y = mrb_fixnum(regs[a+1]);
1669  SET_FLT_VALUE(mrb, regs[a], x / y);
1670  }
1671 #else
1672  OP_MATH_BODY(/,attr_f,attr_i);
1673 #endif
1674  break;
1676 #ifdef MRB_WORD_BOXING
1677  {
1678  mrb_float x = mrb_float(regs[a]);
1679  mrb_float y = mrb_float(regs[a+1]);
1680  SET_FLT_VALUE(mrb, regs[a], x / y);
1681  }
1682 #else
1683  OP_MATH_BODY(/,attr_f,attr_f);
1684 #endif
1685  break;
1686  default:
1687  goto L_SEND;
1688  }
1689  NEXT;
1690  }
1691 
1692  CASE(OP_ADDI) {
1693  /* A B C R(A) := R(A)+C (Syms[B]=:+)*/
1694  int a = GETARG_A(i);
1695 
1696  /* need to check if + is overridden */
1697  switch (mrb_type(regs[a])) {
1698  case MRB_TT_FIXNUM:
1699  {
1700  mrb_int x = regs[a].attr_i;
1701  mrb_int y = GETARG_C(i);
1702  mrb_int z = x + y;
1703 
1704  if (((x < 0) ^ (y < 0)) == 0 && (x < 0) != (z < 0)) {
1705  /* integer overflow */
1706  SET_FLT_VALUE(mrb, regs[a], (mrb_float)x + (mrb_float)y);
1707  break;
1708  }
1709  regs[a].attr_i = z;
1710  }
1711  break;
1712  case MRB_TT_FLOAT:
1713 #ifdef MRB_WORD_BOXING
1714  {
1715  mrb_float x = mrb_float(regs[a]);
1716  SET_FLT_VALUE(mrb, regs[a], x + GETARG_C(i));
1717  }
1718 #else
1719  regs[a].attr_f += GETARG_C(i);
1720 #endif
1721  break;
1722  default:
1723  SET_INT_VALUE(regs[a+1], GETARG_C(i));
1724  i = MKOP_ABC(OP_SEND, a, GETARG_B(i), 1);
1725  goto L_SEND;
1726  }
1727  NEXT;
1728  }
1729 
1730  CASE(OP_SUBI) {
1731  /* A B C R(A) := R(A)-C (Syms[B]=:-)*/
1732  int a = GETARG_A(i);
1733  mrb_value *regs_a = regs + a;
1734 
1735  /* need to check if + is overridden */
1736  switch (mrb_type(regs_a[0])) {
1737  case MRB_TT_FIXNUM:
1738  {
1739  mrb_int x = regs_a[0].attr_i;
1740  mrb_int y = GETARG_C(i);
1741  mrb_int z = x - y;
1742 
1743  if ((x < 0) != (z < 0) && ((x < 0) ^ (y < 0)) != 0) {
1744  /* integer overflow */
1745  SET_FLT_VALUE(mrb, regs_a[0], (mrb_float)x - (mrb_float)y);
1746  }
1747  else {
1748  regs_a[0].attr_i = z;
1749  }
1750  }
1751  break;
1752  case MRB_TT_FLOAT:
1753 #ifdef MRB_WORD_BOXING
1754  {
1755  mrb_float x = mrb_float(regs[a]);
1756  SET_FLT_VALUE(mrb, regs[a], x - GETARG_C(i));
1757  }
1758 #else
1759  regs_a[0].attr_f -= GETARG_C(i);
1760 #endif
1761  break;
1762  default:
1763  SET_INT_VALUE(regs_a[1], GETARG_C(i));
1764  i = MKOP_ABC(OP_SEND, a, GETARG_B(i), 1);
1765  goto L_SEND;
1766  }
1767  NEXT;
1768  }
1769 
1770 #define OP_CMP_BODY(op,v1,v2) do {\
1771  if (regs[a].v1 op regs[a+1].v2) {\
1772  SET_TRUE_VALUE(regs[a]);\
1773  }\
1774  else {\
1775  SET_FALSE_VALUE(regs[a]);\
1776  }\
1777 } while(0)
1778 
1779 #define OP_CMP(op) do {\
1780  int a = GETARG_A(i);\
1781  /* need to check if - is overridden */\
1782  switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {\
1783  case TYPES2(MRB_TT_FIXNUM,MRB_TT_FIXNUM):\
1784  OP_CMP_BODY(op,attr_i,attr_i);\
1785  break;\
1786  case TYPES2(MRB_TT_FIXNUM,MRB_TT_FLOAT):\
1787  OP_CMP_BODY(op,attr_i,attr_f);\
1788  break;\
1789  case TYPES2(MRB_TT_FLOAT,MRB_TT_FIXNUM):\
1790  OP_CMP_BODY(op,attr_f,attr_i);\
1791  break;\
1792  case TYPES2(MRB_TT_FLOAT,MRB_TT_FLOAT):\
1793  OP_CMP_BODY(op,attr_f,attr_f);\
1794  break;\
1795  default:\
1796  goto L_SEND;\
1797  }\
1798 } while(0)
1799 
1800  CASE(OP_EQ) {
1801  /* A B C R(A) := R(A)<R(A+1) (Syms[B]=:==,C=1)*/
1802  int a = GETARG_A(i);
1803  if (mrb_obj_eq(mrb, regs[a], regs[a+1])) {
1804  SET_TRUE_VALUE(regs[a]);
1805  }
1806  else {
1807  OP_CMP(==);
1808  }
1809  NEXT;
1810  }
1811 
1812  CASE(OP_LT) {
1813  /* A B C R(A) := R(A)<R(A+1) (Syms[B]=:<,C=1)*/
1814  OP_CMP(<);
1815  NEXT;
1816  }
1817 
1818  CASE(OP_LE) {
1819  /* A B C R(A) := R(A)<=R(A+1) (Syms[B]=:<=,C=1)*/
1820  OP_CMP(<=);
1821  NEXT;
1822  }
1823 
1824  CASE(OP_GT) {
1825  /* A B C R(A) := R(A)<R(A+1) (Syms[B]=:<,C=1)*/
1826  OP_CMP(>);
1827  NEXT;
1828  }
1829 
1830  CASE(OP_GE) {
1831  /* A B C R(A) := R(A)<=R(A+1) (Syms[B]=:<=,C=1)*/
1832  OP_CMP(>=);
1833  NEXT;
1834  }
1835 
1836  CASE(OP_ARRAY) {
1837  /* A B C R(A) := ary_new(R(B),R(B+1)..R(B+C)) */
1838  regs[GETARG_A(i)] = mrb_ary_new_from_values(mrb, GETARG_C(i), &regs[GETARG_B(i)]);
1839  mrb_gc_arena_restore(mrb, ai);
1840  NEXT;
1841  }
1842 
1843  CASE(OP_ARYCAT) {
1844  /* A B mrb_ary_concat(R(A),R(B)) */
1845  mrb_ary_concat(mrb, regs[GETARG_A(i)],
1846  mrb_ary_splat(mrb, regs[GETARG_B(i)]));
1847  mrb_gc_arena_restore(mrb, ai);
1848  NEXT;
1849  }
1850 
1851  CASE(OP_ARYPUSH) {
1852  /* A B R(A).push(R(B)) */
1853  mrb_ary_push(mrb, regs[GETARG_A(i)], regs[GETARG_B(i)]);
1854  NEXT;
1855  }
1856 
1857  CASE(OP_AREF) {
1858  /* A B C R(A) := R(B)[C] */
1859  int a = GETARG_A(i);
1860  int c = GETARG_C(i);
1861  mrb_value v = regs[GETARG_B(i)];
1862 
1863  if (!mrb_array_p(v)) {
1864  if (c == 0) {
1865  regs[GETARG_A(i)] = v;
1866  }
1867  else {
1868  SET_NIL_VALUE(regs[a]);
1869  }
1870  }
1871  else {
1872  regs[GETARG_A(i)] = mrb_ary_ref(mrb, v, c);
1873  }
1874  NEXT;
1875  }
1876 
1877  CASE(OP_ASET) {
1878  /* A B C R(B)[C] := R(A) */
1879  mrb_ary_set(mrb, regs[GETARG_B(i)], GETARG_C(i), regs[GETARG_A(i)]);
1880  NEXT;
1881  }
1882 
1883  CASE(OP_APOST) {
1884  /* A B C *R(A),R(A+1)..R(A+C) := R(A) */
1885  int a = GETARG_A(i);
1886  mrb_value v = regs[a];
1887  int pre = GETARG_B(i);
1888  int post = GETARG_C(i);
1889 
1890  if (!mrb_array_p(v)) {
1891  regs[a++] = mrb_ary_new_capa(mrb, 0);
1892  while (post--) {
1893  SET_NIL_VALUE(regs[a]);
1894  a++;
1895  }
1896  }
1897  else {
1898  struct RArray *ary = mrb_ary_ptr(v);
1899  int len = ary->len;
1900  int i;
1901 
1902  if (len > pre + post) {
1903  regs[a++] = mrb_ary_new_from_values(mrb, len - pre - post, ary->ptr+pre);
1904  while (post--) {
1905  regs[a++] = ary->ptr[len-post-1];
1906  }
1907  }
1908  else {
1909  regs[a++] = mrb_ary_new_capa(mrb, 0);
1910  for (i=0; i+pre<len; i++) {
1911  regs[a+i] = ary->ptr[pre+i];
1912  }
1913  while (i < post) {
1914  SET_NIL_VALUE(regs[a+i]);
1915  i++;
1916  }
1917  }
1918  }
1919  mrb_gc_arena_restore(mrb, ai);
1920  NEXT;
1921  }
1922 
1923  CASE(OP_STRING) {
1924  /* A Bx R(A) := str_new(Lit(Bx)) */
1925  regs[GETARG_A(i)] = mrb_str_literal(mrb, pool[GETARG_Bx(i)]);
1926  mrb_gc_arena_restore(mrb, ai);
1927  NEXT;
1928  }
1929 
1930  CASE(OP_STRCAT) {
1931  /* A B R(A).concat(R(B)) */
1932  mrb_str_concat(mrb, regs[GETARG_A(i)], regs[GETARG_B(i)]);
1933  NEXT;
1934  }
1935 
1936  CASE(OP_HASH) {
1937  /* A B C R(A) := hash_new(R(B),R(B+1)..R(B+C)) */
1938  int b = GETARG_B(i);
1939  int c = GETARG_C(i);
1940  int lim = b+c*2;
1941  mrb_value hash = mrb_hash_new_capa(mrb, c);
1942 
1943  while (b < lim) {
1944  mrb_hash_set(mrb, hash, regs[b], regs[b+1]);
1945  b+=2;
1946  }
1947  regs[GETARG_A(i)] = hash;
1948  mrb_gc_arena_restore(mrb, ai);
1949  NEXT;
1950  }
1951 
1952  CASE(OP_LAMBDA) {
1953  /* A b c R(A) := lambda(SEQ[b],c) (b:c = 14:2) */
1954  struct RProc *p;
1955  int c = GETARG_c(i);
1956 
1957  if (c & OP_L_CAPTURE) {
1958  p = mrb_closure_new(mrb, mrb->irep[irep->idx+GETARG_b(i)]);
1959  }
1960  else {
1961  p = mrb_proc_new(mrb, mrb->irep[irep->idx+GETARG_b(i)]);
1962  }
1963  if (c & OP_L_STRICT) p->flags |= MRB_PROC_STRICT;
1964  regs[GETARG_A(i)] = mrb_obj_value(p);
1965  mrb_gc_arena_restore(mrb, ai);
1966  NEXT;
1967  }
1968 
1969  CASE(OP_OCLASS) {
1970  /* A R(A) := ::Object */
1971  regs[GETARG_A(i)] = mrb_obj_value(mrb->object_class);
1972  NEXT;
1973  }
1974 
1975  CASE(OP_CLASS) {
1976  /* A B R(A) := newclass(R(A),Sym(B),R(A+1)) */
1977  struct RClass *c = 0;
1978  int a = GETARG_A(i);
1979  mrb_value base, super;
1980  mrb_sym id = syms[GETARG_B(i)];
1981 
1982  base = regs[a];
1983  super = regs[a+1];
1984  if (mrb_nil_p(base)) {
1985  base = mrb_obj_value(mrb->c->ci->target_class);
1986  }
1987  c = mrb_vm_define_class(mrb, base, super, id);
1988  regs[a] = mrb_obj_value(c);
1989  mrb_gc_arena_restore(mrb, ai);
1990  NEXT;
1991  }
1992 
1993  CASE(OP_MODULE) {
1994  /* A B R(A) := newmodule(R(A),Sym(B)) */
1995  struct RClass *c = 0;
1996  int a = GETARG_A(i);
1997  mrb_value base;
1998  mrb_sym id = syms[GETARG_B(i)];
1999 
2000  base = regs[a];
2001  if (mrb_nil_p(base)) {
2002  base = mrb_obj_value(mrb->c->ci->target_class);
2003  }
2004  c = mrb_vm_define_module(mrb, base, id);
2005  regs[a] = mrb_obj_value(c);
2006  mrb_gc_arena_restore(mrb, ai);
2007  NEXT;
2008  }
2009 
2010  CASE(OP_EXEC) {
2011  /* A Bx R(A) := blockexec(R(A),SEQ[Bx]) */
2012  int a = GETARG_A(i);
2013  mrb_callinfo *ci;
2014  mrb_value recv = regs[a];
2015  struct RProc *p;
2016 
2017  /* prepare stack */
2018  ci = cipush(mrb);
2019  ci->pc = pc + 1;
2020  ci->acc = a;
2021  ci->mid = 0;
2022  ci->stackidx = mrb->c->stack - mrb->c->stbase;
2023  ci->argc = 0;
2024  ci->target_class = mrb_class_ptr(recv);
2025 
2026  /* prepare stack */
2027  mrb->c->stack += a;
2028 
2029  p = mrb_proc_new(mrb, mrb->irep[irep->idx+GETARG_Bx(i)]);
2030  p->target_class = ci->target_class;
2031  ci->proc = p;
2032 
2033  if (MRB_PROC_CFUNC_P(p)) {
2034  mrb->c->stack[0] = p->body.func(mrb, recv);
2035  mrb_gc_arena_restore(mrb, ai);
2036  if (mrb->exc) goto L_RAISE;
2037  /* pop stackpos */
2038  regs = mrb->c->stack = mrb->c->stbase + mrb->c->ci->stackidx;
2039  cipop(mrb);
2040  NEXT;
2041  }
2042  else {
2043  irep = p->body.irep;
2044  pool = irep->pool;
2045  syms = irep->syms;
2046  stack_extend(mrb, irep->nregs, 1);
2047  ci->nregs = irep->nregs;
2048  regs = mrb->c->stack;
2049  pc = irep->iseq;
2050  JUMP;
2051  }
2052  }
2053 
2054  CASE(OP_METHOD) {
2055  /* A B R(A).newmethod(Sym(B),R(A+1)) */
2056  int a = GETARG_A(i);
2057  struct RClass *c = mrb_class_ptr(regs[a]);
2058 
2059  mrb_define_method_vm(mrb, c, syms[GETARG_B(i)], regs[a+1]);
2060  mrb_gc_arena_restore(mrb, ai);
2061  NEXT;
2062  }
2063 
2064  CASE(OP_SCLASS) {
2065  /* A B R(A) := R(B).singleton_class */
2066  regs[GETARG_A(i)] = mrb_singleton_class(mrb, regs[GETARG_B(i)]);
2067  mrb_gc_arena_restore(mrb, ai);
2068  NEXT;
2069  }
2070 
2071  CASE(OP_TCLASS) {
2072  /* A B R(A) := target_class */
2073  if (!mrb->c->ci->target_class) {
2074  static const char msg[] = "no target class or module";
2075  mrb_value exc = mrb_exc_new(mrb, E_TYPE_ERROR, msg, sizeof(msg) - 1);
2076  mrb->exc = mrb_obj_ptr(exc);
2077  goto L_RAISE;
2078  }
2079  regs[GETARG_A(i)] = mrb_obj_value(mrb->c->ci->target_class);
2080  NEXT;
2081  }
2082 
2083  CASE(OP_RANGE) {
2084  /* A B C R(A) := range_new(R(B),R(B+1),C) */
2085  int b = GETARG_B(i);
2086  regs[GETARG_A(i)] = mrb_range_new(mrb, regs[b], regs[b+1], GETARG_C(i));
2087  mrb_gc_arena_restore(mrb, ai);
2088  NEXT;
2089  }
2090 
2091  CASE(OP_DEBUG) {
2092  /* A debug print R(A),R(B),R(C) */
2093 #ifdef ENABLE_STDIO
2094  printf("OP_DEBUG %d %d %d\n", GETARG_A(i), GETARG_B(i), GETARG_C(i));
2095 #else
2096  abort();
2097 #endif
2098  NEXT;
2099  }
2100 
2101  CASE(OP_STOP) {
2102  /* stop VM */
2103  L_STOP:
2104  {
2105  int n = mrb->c->ci->eidx;
2106 
2107  while (n--) {
2108  ecall(mrb, n);
2109  }
2110  }
2111  mrb->jmp = prev_jmp;
2112  if (mrb->exc) {
2113  return mrb_obj_value(mrb->exc);
2114  }
2115  return regs[irep->nlocals];
2116  }
2117 
2118  CASE(OP_ERR) {
2119  /* Bx raise RuntimeError with message Lit(Bx) */
2120  mrb_value msg = pool[GETARG_Bx(i)];
2121  mrb_value exc;
2122 
2123  if (GETARG_A(i) == 0) {
2124  exc = mrb_exc_new3(mrb, E_RUNTIME_ERROR, msg);
2125  }
2126  else {
2127  exc = mrb_exc_new3(mrb, E_LOCALJUMP_ERROR, msg);
2128  }
2129  mrb->exc = mrb_obj_ptr(exc);
2130  goto L_RAISE;
2131  }
2132  }
2133  END_DISPATCH;
2134 }
2135 
2136 void
2138 {
2139  longjmp(*(jmp_buf*)mrb->jmp, 1);
2140 }