diff --git a/py/bc.h b/py/bc.h index dc2de2f225..c29817150e 100644 --- a/py/bc.h +++ b/py/bc.h @@ -94,5 +94,5 @@ #define PYBC_IMPORT_FROM (0xe1) #define PYBC_IMPORT_STAR (0xe2) -py_obj_t py_execute_byte_code(const byte *code, const py_obj_t *args, uint n_args); +py_obj_t py_execute_byte_code(const byte *code, const py_obj_t *args, uint n_args, uint n_state); bool py_execute_byte_code_2(const byte **ip_in_out, py_obj_t *fastn, py_obj_t **sp_in_out); diff --git a/py/runtime.c b/py/runtime.c index 6e60b36dbb..9c477ab511 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -133,8 +133,8 @@ struct _py_obj_base_t { } u_fun; struct { // for O_FUN_BC int n_args; + uint n_state; byte *code; - uint len; } u_fun_bc; struct { // for O_FUN_ASM int n_args; @@ -1474,8 +1474,8 @@ py_obj_t rt_make_function_from_id(int unique_code_id) { case PY_CODE_BYTE: o->kind = O_FUN_BC; o->u_fun_bc.n_args = c->n_args; + o->u_fun_bc.n_state = c->n_locals + c->n_stack; o->u_fun_bc.code = c->u_byte.code; - o->u_fun_bc.len = c->u_byte.len; break; case PY_CODE_NATIVE: switch (c->n_args) { @@ -1660,7 +1660,7 @@ py_obj_t rt_call_function_n(py_obj_t fun, int n_args, const py_obj_t *args) { goto bad_n_args; } DEBUG_OP_printf("calling byte code %p(n_args=%d)\n", o->u_fun_bc.code, n_args); - return py_execute_byte_code(o->u_fun_bc.code, args, n_args); + return py_execute_byte_code(o->u_fun_bc.code, args, n_args, o->u_fun_bc.n_state); } else if (IS_O(fun, O_FUN_ASM)) { py_obj_base_t *o = fun; diff --git a/py/vm.c b/py/vm.c index e8e5caeb56..735bb4366b 100644 --- a/py/vm.c +++ b/py/vm.c @@ -21,14 +21,19 @@ #define POP() (*sp++) // args are in reverse order in array -py_obj_t py_execute_byte_code(const byte *code, const py_obj_t *args, uint n_args) { - py_obj_t state[18]; // TODO allocate properly +py_obj_t py_execute_byte_code(const byte *code, const py_obj_t *args, uint n_args, uint n_state) { + py_obj_t temp_state[10]; // TODO allocate properly + py_obj_t *state = &temp_state[0]; + py_obj_t *sp = &state[10]; + if (n_state > 10) { + state = m_new(py_obj_t, n_state); + sp = &state[n_state]; + } // init args for (int i = 0; i < n_args; i++) { assert(i < 8); state[i] = args[n_args - 1 - i]; } - py_obj_t *sp = &state[18]; const byte *ip = code; if (py_execute_byte_code_2(&ip, &state[0], &sp)) { // it shouldn't yield