Changes `stackent` to `stack` of `mrb_callinfo`
This enhances self-containment.
Previously `mrb_context::stack` had the current call level stack, but now it owns it.
The `mrb_context::stack` field, which is no longer needed, will be removed.
MODIFY
include/mruby.h
include/mruby.h
@@ -151,7 +151,7 @@ typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t, void *ud);
typedef struct {
mrb_sym mid;
const struct RProc *proc;
- mrb_value *stackent;
+ mrb_value *stack;
const mrb_code *pc; /* return address */
const mrb_code *err; /* error position */
int16_t argc;
@@ -174,8 +174,7 @@ enum mrb_fiber_state {
struct mrb_context {
struct mrb_context *prev;
- mrb_value *stack; /* stack of virtual machine */
- mrb_value *stbase, *stend;
+ mrb_value *stbase, *stend; /* stack of virtual machine */
mrb_callinfo *ci;
mrb_callinfo *cibase, *ciend;

MODIFY
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
@@ -683,7 +683,7 @@ main(int argc, char **argv)
}
p(mrb, result, 1);
#ifndef MRB_NO_MIRB_UNDERSCORE
- *(mrb->c->stack + 1) = result;
+ *(mrb->c->ci->stack + 1) = result;
#endif
}
}

MODIFY
mrbgems/mruby-eval/src/eval.c
mrbgems/mruby-eval/src/eval.c
@@ -83,7 +83,7 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
/* do nothing, because e is assigned already */
}
else {
- e = mrb_env_new(mrb, mrb->c, ci, ci->proc->body.irep->nlocals, ci[1].stackent, target_class);
+ e = mrb_env_new(mrb, mrb->c, ci, ci->proc->body.irep->nlocals, ci->stack, target_class);
ci->u.env = e;
}
proc->e.env = e;
@@ -115,7 +115,7 @@ exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc)
return ret;
}
/* clear block */
- mrb->c->stack[1] = mrb_nil_value();
+ mrb->c->ci->stack[1] = mrb_nil_value();
return mrb_exec_irep(mrb, self, proc);
}

MODIFY
mrbgems/mruby-fiber/src/fiber.c
mrbgems/mruby-fiber/src/fiber.c
@@ -93,7 +93,6 @@ fiber_init(mrb_state *mrb, mrb_value self)
}
c->stbase = (mrb_value *)mrb_malloc(mrb, slen*sizeof(mrb_value));
c->stend = c->stbase + slen;
- c->stack = c->stbase;
#ifdef MRB_NAN_BOXING
{
@@ -110,13 +109,12 @@ fiber_init(mrb_state *mrb, mrb_value self)
#endif
/* copy receiver from a block */
- c->stack[0] = mrb->c->stack[0];
+ c->stbase[0] = mrb->c->ci->stack[0];
/* initialize callinfo stack */
c->cibase = (mrb_callinfo *)mrb_calloc(mrb, FIBER_CI_INIT_SIZE, sizeof(mrb_callinfo));
c->ciend = c->cibase + FIBER_CI_INIT_SIZE;
c->ci = c->cibase;
- c->ci->stackent = c->stack;
/* adjust return callinfo */
ci = c->ci;
@@ -124,6 +122,7 @@ fiber_init(mrb_state *mrb, mrb_value self)
ci->proc = p;
mrb_field_write_barrier(mrb, (struct RBasic*)mrb_obj_ptr(self), (struct RBasic*)p);
ci->pc = p->body.irep->iseq;
+ ci->stack = c->stbase;
ci[1] = ci[0];
c->ci++; /* push dummy callinfo */
@@ -214,7 +213,7 @@ fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mr
mrb_raise(mrb, E_FIBER_ERROR, "double resume (current)");
}
mrb_stack_extend(mrb, len+2); /* for receiver and (optional) block */
- b = c->stack+1;
+ b = c->stbase+1;
e = b + len;
while (b<e) {
*b++ = *a++;
@@ -223,7 +222,7 @@ fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mr
c->ci--; /* pop dummy callinfo */
}
c->cibase->argc = (int)len;
- value = c->stack[0] = MRB_PROC_ENV(c->cibase->proc)->stack[0];
+ value = c->stbase[0] = MRB_PROC_ENV(c->cibase->proc)->stack[0];
}
else {
value = fiber_result(mrb, a, len);

MODIFY
src/class.c
src/class.c
@@ -808,7 +808,7 @@ mrb_get_argc(mrb_state *mrb)
mrb_int argc = mrb->c->ci->argc;
if (argc < 0) {
- struct RArray *a = mrb_ary_ptr(mrb->c->stack[1]);
+ struct RArray *a = mrb_ary_ptr(mrb->c->ci->stack[1]);
argc = ARY_LEN(a);
}
@@ -819,7 +819,7 @@ MRB_API const mrb_value*
mrb_get_argv(mrb_state *mrb)
{
mrb_int argc = mrb->c->ci->argc;
- mrb_value *array_argv = mrb->c->stack + 1;
+ mrb_value *array_argv = mrb->c->ci->stack + 1;
if (argc < 0) {
struct RArray *a = mrb_ary_ptr(*array_argv);
@@ -832,7 +832,7 @@ MRB_API mrb_value
mrb_get_arg1(mrb_state *mrb)
{
mrb_int argc = mrb->c->ci->argc;
- mrb_value *array_argv = mrb->c->stack + 1;
+ mrb_value *array_argv = mrb->c->ci->stack + 1;
if (argc < 0) {
struct RArray *a = mrb_ary_ptr(*array_argv);
@@ -887,7 +887,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_int i = 0;
va_list ap;
mrb_int argc = mrb->c->ci->argc;
- mrb_value *array_argv = mrb->c->stack+1;
+ mrb_value *array_argv = mrb->c->ci->stack+1;
mrb_bool argv_on_stack = argc >= 0;
mrb_bool opt = FALSE;
mrb_bool opt_skip = TRUE;
@@ -1200,10 +1200,10 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
p = va_arg(ap, mrb_value*);
if (mrb->c->ci->argc < 0) {
- bp = mrb->c->stack + 2;
+ bp = mrb->c->ci->stack + 2;
}
else {
- bp = mrb->c->stack + mrb->c->ci->argc + 1;
+ bp = mrb->c->ci->stack + mrb->c->ci->argc + 1;
}
if (altmode && mrb_nil_p(*bp)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");

MODIFY
src/gc.c
src/gc.c
@@ -626,11 +626,14 @@ mark_context_stack(mrb_state *mrb, struct mrb_context *c)
size_t e;
mrb_value nil;
- if (c->stack == NULL) return;
- e = c->stack - c->stbase;
+ if (c->stbase == NULL) return;
if (c->ci) {
+ e = (c->ci->stack ? c->ci->stack - c->stbase : 0);
e += ci_nregs(c->ci);
}
+ else {
+ e = 0;
+ }
if (c->stbase + e > c->stend) e = c->stend - c->stbase;
for (i=0; i<e; i++) {
mrb_value v = c->stbase[i];
@@ -1001,7 +1004,7 @@ gc_gray_counts(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj)
if (!c || c->status == MRB_FIBER_TERMINATED) break;
/* mark stack */
- i = c->stack - c->stbase;
+ i = c->ci->stack - c->stbase;
if (c->ci) {
i += ci_nregs(c->ci);

MODIFY
src/kernel.c
src/kernel.c
@@ -185,7 +185,7 @@ mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
bp = &e->stack[bidx];
}
else {
- bp = ci[1].stackent+1;
+ bp = ci->stack+1;
if (ci->argc >= 0) {
bp += ci->argc;
}

MODIFY
src/proc.c
src/proc.c
@@ -89,7 +89,7 @@ closure_setup(mrb_state *mrb, struct RProc *p)
else if (up) {
struct RClass *tc = MRB_PROC_TARGET_CLASS(p);
- e = mrb_env_new(mrb, mrb->c, ci, up->body.irep->nlocals, mrb->c->stack, tc);
+ e = mrb_env_new(mrb, mrb->c, ci, up->body.irep->nlocals, ci->stack, tc);
ci->u.env = e;
if (MRB_PROC_ENV_P(up) && MRB_PROC_ENV(up)->cxt == NULL) {
e->mid = MRB_PROC_ENV(up)->mid;

MODIFY
src/vm.c
src/vm.c
@@ -127,14 +127,13 @@ stack_init(mrb_state *mrb)
/* mrb_assert(mrb->stack == NULL); */
c->stbase = (mrb_value *)mrb_calloc(mrb, STACK_INIT_SIZE, sizeof(mrb_value));
c->stend = c->stbase + STACK_INIT_SIZE;
- c->stack = c->stbase;
/* mrb_assert(ci == NULL); */
c->cibase = (mrb_callinfo *)mrb_calloc(mrb, CALLINFO_INIT_SIZE, sizeof(mrb_callinfo));
c->ciend = c->cibase + CALLINFO_INIT_SIZE;
c->ci = c->cibase;
c->ci->u.target_class = mrb->object_class;
- c->ci->stackent = c->stack;
+ c->ci->stack = c->stbase;
}
static inline void
@@ -165,7 +164,7 @@ envadjust(mrb_state *mrb, mrb_value *oldbase, mrb_value *newbase, size_t oldsize
}
}
- ci->stackent = newbase + (ci->stackent - oldbase);
+ ci->stack = newbase + (ci->stack - oldbase);
ci++;
}
}
@@ -179,7 +178,7 @@ stack_extend_alloc(mrb_state *mrb, mrb_int room)
mrb_value *newstack;
size_t oldsize = mrb->c->stend - mrb->c->stbase;
size_t size = oldsize;
- size_t off = mrb->c->stack - mrb->c->stbase;
+ size_t off = mrb->c->ci->stack ? mrb->c->stend - mrb->c->ci->stack : 0;
if (off > size) size = off;
#ifdef MRB_STACK_EXTEND_DOUBLING
@@ -204,7 +203,6 @@ stack_extend_alloc(mrb_state *mrb, mrb_int room)
stack_clear(&(newstack[oldsize]), size - oldsize);
envadjust(mrb, oldbase, newstack, oldsize);
mrb->c->stbase = newstack;
- mrb->c->stack = mrb->c->stbase + off;
mrb->c->stend = mrb->c->stbase + size;
/* Raise an exception if the new stack size will be too large,
@@ -217,7 +215,7 @@ stack_extend_alloc(mrb_state *mrb, mrb_int room)
MRB_API void
mrb_stack_extend(mrb_state *mrb, mrb_int room)
{
- if (mrb->c->stack + room >= mrb->c->stend) {
+ if (!mrb->c->ci->stack || mrb->c->ci->stack + room >= mrb->c->stend) {
stack_extend_alloc(mrb, room);
}
}
@@ -280,13 +278,12 @@ cipush(mrb_state *mrb, const mrb_code *pc, mrb_int push_stacks, mrb_int acc,
ci = ++c->ci;
ci->mid = mid;
ci->proc = proc;
- ci->stackent = c->stack;
+ ci->stack = ci[-1].stack + push_stacks;
ci->pc = pc;
ci->argc = argc;
ci->acc = acc;
ci->u.target_class = target_class;
ci->err = 0;
- c->stack += push_stacks;
return ci;
}
@@ -318,7 +315,6 @@ cipop(mrb_state *mrb)
struct mrb_context *c = mrb->c;
struct REnv *env = mrb_vm_ci_env(c->ci);
- mrb->c->stack = c->ci->stackent;
c->ci--;
if (env) mrb_env_unshare(mrb, env);
return c->ci;
@@ -427,7 +423,7 @@ mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, mrb_int argc
mrb_int n = ci_nregs(mrb->c->ci);
ptrdiff_t voff = -1;
- if (!mrb->c->stack) {
+ if (!mrb->c->stbase) {
stack_init(mrb);
}
if (argc < 0) {
@@ -444,7 +440,7 @@ mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, mrb_int argc
}
mrb_ary_unshift(mrb, args, mrb_symbol_value(mid));
mrb_stack_extend(mrb, n+2);
- mrb->c->stack[n+1] = args;
+ mrb->c->ci->stack[n+1] = args;
argc = -1;
}
if (mrb->c->ci - mrb->c->cibase > MRB_FUNCALL_DEPTH_MAX) {
@@ -458,7 +454,7 @@ mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, mrb_int argc
if (argc >= CALL_MAXARGS) {
mrb_value args = mrb_ary_new_from_values(mrb, argc, argv);
- mrb->c->stack[1] = args;
+ mrb->c->ci->stack[1] = args;
ci->argc = -1;
argc = 1;
}
@@ -474,11 +470,11 @@ mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, mrb_int argc
if (voff >= 0) {
argv = mrb->c->stbase + voff;
}
- mrb->c->stack[0] = self;
+ mrb->c->ci->stack[0] = self;
if (ci->argc > 0) {
- stack_copy(mrb->c->stack+1, argv, argc);
+ stack_copy(mrb->c->ci->stack+1, argv, argc);
}
- mrb->c->stack[argc+1] = blk;
+ mrb->c->ci->stack[argc+1] = blk;
if (MRB_METHOD_CFUNC_P(m)) {
ci->acc = CI_ACC_DIRECT;
@@ -507,7 +503,7 @@ mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p)
mrb_callinfo *ci = mrb->c->ci;
mrb_int keep, nregs;
- mrb->c->stack[0] = self;
+ mrb->c->ci->stack[0] = self;
ci->proc = p;
if (MRB_PROC_CFUNC_P(p)) {
return MRB_PROC_CFUNC(p)(mrb, self);
@@ -520,7 +516,7 @@ mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p)
}
else {
mrb_stack_extend(mrb, nregs);
- stack_clear(mrb->c->stack+keep, nregs-keep);
+ stack_clear(mrb->c->ci->stack+keep, nregs-keep);
}
cipush(mrb, p->body.irep->iseq, 0, 0, NULL, NULL, 0, 0);
@@ -573,7 +569,7 @@ mrb_f_send(mrb_state *mrb, mrb_value self)
ci->mid = name;
ci->u.target_class = c;
- regs = mrb->c->stack+1;
+ regs = mrb->c->ci->stack+1;
/* remove first symbol from arguments */
if (ci->argc >= 0) {
for (i=0,len=ci->argc; i<len; i++) {
@@ -615,17 +611,17 @@ eval_under(mrb_state *mrb, mrb_value self, mrb_value blk, struct RClass *c)
ci->mid = ci[-1].mid;
if (MRB_PROC_CFUNC_P(p)) {
mrb_stack_extend(mrb, 3);
- mrb->c->stack[0] = self;
- mrb->c->stack[1] = self;
- mrb->c->stack[2] = mrb_nil_value();
+ mrb->c->ci->stack[0] = self;
+ mrb->c->ci->stack[1] = self;
+ mrb->c->ci->stack[2] = mrb_nil_value();
return MRB_PROC_CFUNC(p)(mrb, self);
}
nregs = p->body.irep->nregs;
if (nregs < 3) nregs = 3;
mrb_stack_extend(mrb, nregs);
- mrb->c->stack[0] = self;
- mrb->c->stack[1] = self;
- stack_clear(mrb->c->stack+2, nregs-2);
+ mrb->c->ci->stack[0] = self;
+ mrb->c->ci->stack[1] = self;
+ stack_clear(mrb->c->ci->stack+2, nregs-2);
ci = cipush(mrb, p->body.irep->iseq, 0, 0, NULL, NULL, 0, 0);
return self;
@@ -712,15 +708,15 @@ mrb_yield_with_class(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value
n = argc + 2;
}
mrb_stack_extend(mrb, n);
- mrb->c->stack[0] = self;
+ mrb->c->ci->stack[0] = self;
if (ci->argc < 0) {
- mrb->c->stack[1] = mrb_ary_new_from_values(mrb, argc, argv);
+ mrb->c->ci->stack[1] = mrb_ary_new_from_values(mrb, argc, argv);
argc = 1;
}
else if (argc > 0) {
- stack_copy(mrb->c->stack+1, argv, argc);
+ stack_copy(mrb->c->ci->stack+1, argv, argc);
}
- mrb->c->stack[argc+1] = mrb_nil_value();
+ mrb->c->ci->stack[argc+1] = mrb_nil_value();
if (MRB_PROC_CFUNC_P(p)) {
val = MRB_PROC_CFUNC(p)(mrb, self);
@@ -765,8 +761,8 @@ mrb_yield_cont(mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const
ci = mrb->c->ci;
mrb_stack_extend(mrb, 3);
- mrb->c->stack[1] = mrb_ary_new_from_values(mrb, argc, argv);
- mrb->c->stack[2] = mrb_nil_value();
+ mrb->c->ci->stack[1] = mrb_ary_new_from_values(mrb, argc, argv);
+ mrb->c->ci->stack[2] = mrb_nil_value();
ci->argc = -1;
return mrb_exec_irep(mrb, self, p);
}
@@ -852,7 +848,7 @@ argnum_error(mrb_state *mrb, mrb_int num)
mrb_int argc = mrb->c->ci->argc;
if (argc < 0) {
- mrb_value args = mrb->c->stack[1];
+ mrb_value args = mrb->c->ci->stack[1];
if (mrb_array_p(args)) {
argc = RARRAY_LEN(args);
}
@@ -980,14 +976,14 @@ mrb_vm_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int sta
ptrdiff_t cioff = c->ci - c->cibase;
mrb_int nregs = irep->nregs;
- if (!c->stack) {
+ if (!c->stbase) {
stack_init(mrb);
}
if (stack_keep > nregs)
nregs = stack_keep;
mrb_stack_extend(mrb, nregs);
- stack_clear(c->stack + stack_keep, nregs - stack_keep);
- c->stack[0] = self;
+ stack_clear(c->ci->stack + stack_keep, nregs - stack_keep);
+ c->ci->stack[0] = self;
result = mrb_vm_exec(mrb, proc, irep->iseq);
if (mrb->c != c) {
if (mrb->c->fib) {
@@ -1056,7 +1052,7 @@ RETRY_TRY_BLOCK:
mrb->jmp = &c_jmp;
mrb->c->ci->proc = proc;
-#define regs (mrb->c->stack)
+#define regs (mrb->c->ci->stack)
INIT_DISPATCH {
CASE(OP_NOP, Z) {
/* do nothing */
@@ -1492,7 +1488,7 @@ RETRY_TRY_BLOCK:
syms = irep->syms;
}
}
- mrb->c->stack[0] = recv;
+ mrb->c->ci->stack[0] = recv;
/* pop stackpos */
pc = ci->pc;
cipop(mrb);
@@ -1512,7 +1508,7 @@ RETRY_TRY_BLOCK:
CASE(OP_CALL, Z) {
mrb_callinfo *ci;
- mrb_value recv = mrb->c->stack[0];
+ mrb_value recv = mrb->c->ci->stack[0];
struct RProc *m = mrb_proc_ptr(recv);
/* replace callinfo */
@@ -1544,7 +1540,7 @@ RETRY_TRY_BLOCK:
proc = m;
irep = m->body.irep;
if (!irep) {
- mrb->c->stack[0] = mrb_nil_value();
+ mrb->c->ci->stack[0] = mrb_nil_value();
a = 0;
c = OP_R_NORMAL;
goto L_RETURN;
@@ -1645,7 +1641,7 @@ RETRY_TRY_BLOCK:
ci = cipush(mrb, pc, a, 0, cls, NULL, mid, argc);
/* prepare stack */
- mrb->c->stack[0] = recv;
+ mrb->c->ci->stack[0] = recv;
if (MRB_METHOD_CFUNC_P(m)) {
mrb_value v;
@@ -1671,7 +1667,7 @@ RETRY_TRY_BLOCK:
syms = irep->syms;
}
}
- mrb->c->stack[0] = v;
+ mrb->c->ci->stack[0] = v;
pc = ci->pc;
cipop(mrb);
JUMP;
@@ -1978,7 +1974,7 @@ RETRY_TRY_BLOCK:
if (ch == NULL) {
L_FTOP: /* fiber top */
if (mrb->c == mrb->root_c) {
- mrb->c->stack = mrb->c->stbase;
+ mrb->c->ci->stack = mrb->c->stbase;
goto L_STOP;
}
else {
@@ -2003,9 +1999,6 @@ RETRY_TRY_BLOCK:
irep = proc->body.irep;
pool = irep->pool;
syms = irep->syms;
- if (ci < ci0) {
- mrb->c->stack = ci[1].stackent;
- }
mrb_stack_extend(mrb, irep->nregs);
pc = irep->iseq + mrb_irep_catch_handler_unpack(ch->target);
}
@@ -2168,7 +2161,6 @@ RETRY_TRY_BLOCK:
mrb_assert(!"wrong break tag");
}
}
- mrb->c->stack = ci->stackent;
while (mrb->c->cibase < ci && ci[-1].proc != proc->upper) {
if (ci[-1].acc == CI_ACC_SKIP) {
goto L_BREAK_ERROR;

Changes `stackent` to `stack` of `mrb_callinfo`

This enhances self-containment.

Previously `mrb_context::stack` had the current call level stack, but now it owns it.
The `mrb_context::stack` field, which is no longer needed, will be removed.