diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-20 17:50:40 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-20 17:50:40 +0100 |
commit | 3558f62fb5c21a19a518c2ba75860f0b5963219e (patch) | |
tree | cf8626118be5d0169ec0f1cd0482563595704c4e /py/emitbc.c | |
parent | bc5f0c19775e23b4f0621d52de47fb9438a78ba2 (diff) |
py: Making closures now passes pointer to stack, not a tuple for vars.
Closed over variables are now passed on the stack, instead of creating a
tuple and passing that. This way memory for the closed over variables
can be allocated within the closure object itself. See issue #510 for
background.
Diffstat (limited to 'py/emitbc.c')
-rw-r--r-- | py/emitbc.c | 27 |
1 files changed, 7 insertions, 20 deletions
diff --git a/py/emitbc.c b/py/emitbc.c index d7b309a37..c445d9748 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -761,35 +761,21 @@ STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, uint n_pos_defau emit_bc_pre(emit, 1); emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code); } else { - if (n_pos_defaults == 0) { - // load dummy entry for non-existent positional default tuple - emit_bc_load_null(emit); - emit_bc_rot_two(emit); - } else if (n_kw_defaults == 0) { - // load dummy entry for non-existent keyword default dict - emit_bc_load_null(emit); - } emit_bc_pre(emit, -1); emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code); } } -STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) { +STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_closed_over, uint n_pos_defaults, uint n_kw_defaults) { if (n_pos_defaults == 0 && n_kw_defaults == 0) { - emit_bc_pre(emit, 0); + emit_bc_pre(emit, -n_closed_over + 1); emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code); + emit_write_byte_code_byte(emit, n_closed_over); } else { - if (n_pos_defaults == 0) { - // load dummy entry for non-existent positional default tuple - emit_bc_load_null(emit); - emit_bc_rot_three(emit); - } else if (n_kw_defaults == 0) { - // load dummy entry for non-existent keyword default dict - emit_bc_load_null(emit); - emit_bc_rot_two(emit); - } - emit_bc_pre(emit, -2); + assert(n_closed_over <= 255); + emit_bc_pre(emit, -2 - n_closed_over + 1); emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code); + emit_write_byte_code_byte(emit, n_closed_over); } } @@ -870,6 +856,7 @@ const emit_method_table_t emit_bc_method_table = { emit_bc_load_const_id, emit_bc_load_const_str, emit_bc_load_const_verbatim_str, + emit_bc_load_null, emit_bc_load_fast, emit_bc_load_deref, emit_bc_load_closure, |