Improve allocation of stack for byte code.
This commit is contained in:
parent
03c9cfb015
commit
40fdfe3000
2
py/bc.h
2
py/bc.h
@ -94,5 +94,5 @@
|
|||||||
#define PYBC_IMPORT_FROM (0xe1)
|
#define PYBC_IMPORT_FROM (0xe1)
|
||||||
#define PYBC_IMPORT_STAR (0xe2)
|
#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);
|
bool py_execute_byte_code_2(const byte **ip_in_out, py_obj_t *fastn, py_obj_t **sp_in_out);
|
||||||
|
@ -133,8 +133,8 @@ struct _py_obj_base_t {
|
|||||||
} u_fun;
|
} u_fun;
|
||||||
struct { // for O_FUN_BC
|
struct { // for O_FUN_BC
|
||||||
int n_args;
|
int n_args;
|
||||||
|
uint n_state;
|
||||||
byte *code;
|
byte *code;
|
||||||
uint len;
|
|
||||||
} u_fun_bc;
|
} u_fun_bc;
|
||||||
struct { // for O_FUN_ASM
|
struct { // for O_FUN_ASM
|
||||||
int n_args;
|
int n_args;
|
||||||
@ -1474,8 +1474,8 @@ py_obj_t rt_make_function_from_id(int unique_code_id) {
|
|||||||
case PY_CODE_BYTE:
|
case PY_CODE_BYTE:
|
||||||
o->kind = O_FUN_BC;
|
o->kind = O_FUN_BC;
|
||||||
o->u_fun_bc.n_args = c->n_args;
|
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.code = c->u_byte.code;
|
||||||
o->u_fun_bc.len = c->u_byte.len;
|
|
||||||
break;
|
break;
|
||||||
case PY_CODE_NATIVE:
|
case PY_CODE_NATIVE:
|
||||||
switch (c->n_args) {
|
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;
|
goto bad_n_args;
|
||||||
}
|
}
|
||||||
DEBUG_OP_printf("calling byte code %p(n_args=%d)\n", o->u_fun_bc.code, 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)) {
|
} else if (IS_O(fun, O_FUN_ASM)) {
|
||||||
py_obj_base_t *o = fun;
|
py_obj_base_t *o = fun;
|
||||||
|
11
py/vm.c
11
py/vm.c
@ -21,14 +21,19 @@
|
|||||||
#define POP() (*sp++)
|
#define POP() (*sp++)
|
||||||
|
|
||||||
// args are in reverse order in array
|
// 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 py_execute_byte_code(const byte *code, const py_obj_t *args, uint n_args, uint n_state) {
|
||||||
py_obj_t state[18]; // TODO allocate properly
|
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
|
// init args
|
||||||
for (int i = 0; i < n_args; i++) {
|
for (int i = 0; i < n_args; i++) {
|
||||||
assert(i < 8);
|
assert(i < 8);
|
||||||
state[i] = args[n_args - 1 - i];
|
state[i] = args[n_args - 1 - i];
|
||||||
}
|
}
|
||||||
py_obj_t *sp = &state[18];
|
|
||||||
const byte *ip = code;
|
const byte *ip = code;
|
||||||
if (py_execute_byte_code_2(&ip, &state[0], &sp)) {
|
if (py_execute_byte_code_2(&ip, &state[0], &sp)) {
|
||||||
// it shouldn't yield
|
// it shouldn't yield
|
||||||
|
Loading…
x
Reference in New Issue
Block a user