8000 py/objfun: Factor out macro for decoding codestate size. · lvgl/lv_micropython@fca1d1a · GitHub
[go: up one dir, main page]

Skip to content

Commit fca1d1a

Browse files
committed
py/objfun: Factor out macro for decoding codestate size.
fun_bc_call() starts with almost the same code as mp_obj_fun_bc_prepare_codestate(), the only difference is a way to allocate the codestate object (heap vs stack with heap fallback). Still, would be nice to avoid code duplication to make further refactoring easier. So, this commit factors out the common code before the allocation - decoding and calculating codestate size. It produces two values, so structured as a macro which writes to 2 variables passed as arguments.
1 parent dea3fb9 commit fca1d1a

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

py/objfun.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,30 @@ STATIC void dump_args(const mp_obj_t *a, size_t sz) {
195195
// than this will try to use the heap, with fallback to stack allocation.
196196
#define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
197197

198-
// Set this to enable a simple stack overflow check.
198+
// Set this to 1 to enable a simple stack overflow check.
199199
#define VM_DETECT_STACK_OVERFLOW (0)
200200

201+
#define DECODE_CODESTATE_SIZE(bytecode, n_state_out_var, state_size_out_var) \
202+
{ \
203+
/* bytecode prelude: state size and exception stack size */ \
204+
n_state_out_var = mp_decode_uint_value(bytecode); \
205+
size_t n_exc_stack = mp_decode_uint_value(mp_decode_uint_skip(bytecode)); \
206+
\
207+
n_state += VM_DETECT_STACK_OVERFLOW; \
208+
\
209+
/* state size in bytes */ \
210+
state_size_out_var = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t); \
211+
}
212+
201213
#if MICROPY_STACKLESS
202214
mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
203215
MP_STACK_CHECK();
204216
mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
205217

206-
// bytecode prelude: state size and exception stack size
207-
size_t n_state = mp_decode_uint_value(self->bytecode);
208-
size_t n_exc_stack = mp_decode_uint_value(mp_decode_uint_skip(self->bytecode));
218+
size_t n_state, state_size;
219+
DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
209220

210221
// allocate state for locals and stack
211-
size_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
212222
mp_code_state_t *code_state;
213223
code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
214224
if (!code_state) {
@@ -238,16 +248,10 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
238248
mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
239249
DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
240250

241-
// bytecode prelude: state size and exception stack size
242-
size_t n_state = mp_decode_uint_value(self->bytecode);
243-
size_t n_exc_stack = mp_decode_uint_value(mp_decode_uint_skip(self->bytecode));
244-
245-
#if VM_DETECT_STACK_OVERFLOW
246-
n_state += 1;
247-
#endif
251+
size_t n_state, state_size;
252+
DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
248253

249254
// allocate state for locals and stack
250-
size_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
251255
mp_code_state_t *code_state = NULL;
252256
if (state_size > VM_MAX_STATE_ON_STACK) {
253257
code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);

0 commit comments

Comments
 (0)
0