py, compile: Combine have_star_arg, have_dbl_star_arg into star_flags.

Small reduction in ROM, heap and stack usage.
This commit is contained in:
Damien George 2014-04-09 12:43:17 +01:00
parent 78035b995f
commit 922ddd6415
5 changed files with 42 additions and 43 deletions

View File

@ -51,8 +51,7 @@ typedef struct _compiler_t {
uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
int n_arg_keyword;
bool have_star_arg;
bool have_dbl_star_arg;
uint8_t star_flags;
bool have_bare_star;
int param_pass;
int param_pass_num_dict_params;
@ -1075,7 +1074,7 @@ void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
// call each decorator
for (int i = 0; i < n - num_built_in_decorators; i++) {
EMIT_ARG(call_function, 1, 0, false, false);
EMIT_ARG(call_function, 1, 0, 0);
}
// store func/class object into name
@ -1422,7 +1421,7 @@ void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
// assertion message
compile_node(comp, pns->nodes[1]);
EMIT_ARG(call_function, 1, 0, false, false);
EMIT_ARG(call_function, 1, 0, 0);
}
EMIT_ARG(raise_varargs, 1);
EMIT_ARG(label_assign, l_end);
@ -1873,7 +1872,7 @@ void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
// for REPL, evaluate then print the expression
EMIT_ARG(load_id, MP_QSTR___repl_print__);
compile_node(comp, pns->nodes[0]);
EMIT_ARG(call_function, 1, 0, false, false);
EMIT_ARG(call_function, 1, 0, 0);
EMIT(pop_top);
} else {
@ -2219,38 +2218,35 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
printf("TypeError: super() call cannot find self\n");
return;
}
EMIT_ARG(call_function, 2, 0, false, false);
EMIT_ARG(call_function, 2, 0, 0);
return;
}
#endif
int old_n_arg_keyword = comp->n_arg_keyword;
bool old_have_star_arg = comp->have_star_arg;
bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
uint old_star_flags = comp->star_flags;
comp->n_arg_keyword = 0;
comp->have_star_arg = false;
comp->have_dbl_star_arg = false;
comp->star_flags = 0;
compile_node(comp, pn_arglist); // arguments to function call; can be null
// compute number of positional arguments
int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
if (comp->have_star_arg) {
if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
n_positional -= 1;
}
if (comp->have_dbl_star_arg) {
if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
n_positional -= 1;
}
if (is_method_call) {
EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
} else {
EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
}
comp->n_arg_keyword = old_n_arg_keyword;
comp->have_star_arg = old_have_star_arg;
comp->have_dbl_star_arg = old_have_dbl_star_arg;
comp->star_flags = old_star_flags;
}
void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
@ -2330,7 +2326,7 @@ void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_
compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
EMIT(get_iter);
EMIT_ARG(call_function, 1, 0, false, false);
EMIT_ARG(call_function, 1, 0, 0);
}
void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
@ -2574,20 +2570,20 @@ void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
}
void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
if (comp->have_star_arg) {
if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
return;
}
comp->have_star_arg = true;
comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
compile_node(comp, pns->nodes[0]);
}
void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
if (comp->have_dbl_star_arg) {
if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
return;
}
comp->have_dbl_star_arg = true;
comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
compile_node(comp, pns->nodes[0]);
}

View File

@ -14,6 +14,9 @@ typedef enum {
PASS_3 = 3, // emit code
} pass_kind_t;
#define MP_EMIT_STAR_FLAG_SINGLE (0x01)
#define MP_EMIT_STAR_FLAG_DOUBLE (0x02)
typedef struct _emit_t emit_t;
typedef struct _emit_method_table_t {
@ -98,8 +101,8 @@ typedef struct _emit_method_table_t {
void (*unpack_ex)(emit_t *emit, int n_left, int n_right);
void (*make_function)(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults);
void (*make_closure)(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults);
void (*call_function)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
void (*call_method)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
void (*call_function)(emit_t *emit, int n_positional, int n_keyword, uint star_flags);
void (*call_method)(emit_t *emit, int n_positional, int n_keyword, uint star_flags);
void (*return_value)(emit_t *emit);
void (*raise_varargs)(emit_t *emit, int n_args);
void (*yield_value)(emit_t *emit);

View File

@ -767,13 +767,13 @@ STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_pos_defaul
}
}
STATIC void emit_bc_call_function_method_helper(emit_t *emit, int stack_adj, uint bytecode_base, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
if (have_star_arg || have_dbl_star_arg) {
if (!have_star_arg) {
STATIC void emit_bc_call_function_method_helper(emit_t *emit, int stack_adj, uint bytecode_base, int n_positional, int n_keyword, uint star_flags) {
if (star_flags) {
if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
// load dummy entry for non-existent pos_seq
emit_bc_load_null(emit);
emit_bc_rot_two(emit);
} else if (!have_dbl_star_arg) {
} else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
// load dummy entry for non-existent kw_dict
emit_bc_load_null(emit);
}
@ -785,12 +785,12 @@ STATIC void emit_bc_call_function_method_helper(emit_t *emit, int stack_adj, uin
}
}
STATIC void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
STATIC void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
}
STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
}
STATIC void emit_bc_return_value(emit_t *emit) {

View File

@ -675,24 +675,24 @@ STATIC void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
}
}
STATIC void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
STATIC void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
int s = 0;
if (have_star_arg) {
if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
s += 1;
}
if (have_dbl_star_arg) {
if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
s += 1;
}
emit_pre(emit, -n_positional - 2 * n_keyword - s, 3);
if (emit->pass == PASS_3) {
if (have_star_arg) {
if (have_dbl_star_arg) {
if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
printf("CALL_FUNCTION_VAR_KW");
} else {
printf("CALL_FUNCTION_VAR");
}
} else {
if (have_dbl_star_arg) {
if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
printf("CALL_FUNCTION_KW");
} else {
printf("CALL_FUNCTION");
@ -702,8 +702,8 @@ STATIC void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword
}
}
STATIC void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
emit_cpy_call_function(emit, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
STATIC void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
emit_cpy_call_function(emit, n_positional, n_keyword, star_flags);
}
STATIC void emit_cpy_return_value(emit_t *emit) {

View File

@ -1200,9 +1200,9 @@ STATIC void emit_native_make_closure(emit_t *emit, scope_t *scope, uint n_pos_de
assert(0);
}
STATIC void emit_native_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
STATIC void emit_native_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
// call special viper runtime routine with type info for args, and wanted type info for return
assert(!have_star_arg && !have_dbl_star_arg);
assert(!star_flags);
/* we no longer have these _n specific call_function's
* they anyway push args into an array
@ -1239,8 +1239,8 @@ STATIC void emit_native_call_function(emit_t *emit, int n_positional, int n_keyw
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
}
STATIC void emit_native_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
assert(!have_star_arg && !have_dbl_star_arg);
STATIC void emit_native_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
assert(!star_flags);
/*
if (n_positional == 0) {