py/parse: Make mp_parse_node_new_leaf an inline function.
It is split into 2 functions, one to make small ints and the other to make a non-small-int leaf node. This reduces code size by 32 bytes on bare-arm, 64 bytes on unix (x64-64) and 144 bytes on stmhal.
This commit is contained in:
parent
b0cbfb0492
commit
ed9c93f0f1
@ -1403,13 +1403,13 @@ STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
if (1 <= n_args && n_args <= 3) {
|
||||
optimize = true;
|
||||
if (n_args == 1) {
|
||||
pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
|
||||
pn_range_start = mp_parse_node_new_small_int(0);
|
||||
pn_range_end = args[0];
|
||||
pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
|
||||
pn_range_step = mp_parse_node_new_small_int(1);
|
||||
} else if (n_args == 2) {
|
||||
pn_range_start = args[0];
|
||||
pn_range_end = args[1];
|
||||
pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
|
||||
pn_range_step = mp_parse_node_new_small_int(1);
|
||||
} else {
|
||||
pn_range_start = args[0];
|
||||
pn_range_end = args[1];
|
||||
|
13
py/parse.c
13
py/parse.c
@ -227,13 +227,6 @@ STATIC void pop_rule(parser_t *parser, const rule_t **rule, size_t *arg_i, size_
|
||||
*src_line = parser->rule_stack[parser->rule_stack_top].src_line;
|
||||
}
|
||||
|
||||
mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
|
||||
if (kind == MP_PARSE_NODE_SMALL_INT) {
|
||||
return (mp_parse_node_t)(kind | (arg << 1));
|
||||
}
|
||||
return (mp_parse_node_t)(kind | (arg << 4));
|
||||
}
|
||||
|
||||
bool mp_parse_node_is_const_false(mp_parse_node_t pn) {
|
||||
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
|
||||
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
|
||||
@ -418,7 +411,7 @@ STATIC void push_result_token(parser_t *parser, const rule_t *rule) {
|
||||
mp_map_elem_t *elem;
|
||||
if (rule->rule_id == RULE_atom
|
||||
&& (elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP)) != NULL) {
|
||||
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
|
||||
pn = mp_parse_node_new_small_int(MP_OBJ_SMALL_INT_VALUE(elem->value));
|
||||
} else {
|
||||
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_ID, id);
|
||||
}
|
||||
@ -429,7 +422,7 @@ STATIC void push_result_token(parser_t *parser, const rule_t *rule) {
|
||||
} else if (lex->tok_kind == MP_TOKEN_INTEGER) {
|
||||
mp_obj_t o = mp_parse_num_integer(lex->vstr.buf, lex->vstr.len, 0, lex);
|
||||
if (MP_OBJ_IS_SMALL_INT(o)) {
|
||||
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(o));
|
||||
pn = mp_parse_node_new_small_int(MP_OBJ_SMALL_INT_VALUE(o));
|
||||
} else {
|
||||
pn = make_node_const_object(parser, lex->tok_line, o);
|
||||
}
|
||||
@ -658,7 +651,7 @@ STATIC bool fold_constants(parser_t *parser, const rule_t *rule, size_t num_args
|
||||
pop_result(parser);
|
||||
}
|
||||
if (MP_OBJ_IS_SMALL_INT(arg0)) {
|
||||
push_result_node(parser, mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(arg0)));
|
||||
push_result_node(parser, mp_parse_node_new_small_int(MP_OBJ_SMALL_INT_VALUE(arg0)));
|
||||
} else {
|
||||
// TODO reuse memory for parse node struct?
|
||||
push_result_node(parser, make_node_const_object(parser, 0, arg0));
|
||||
|
@ -76,7 +76,12 @@ typedef struct _mp_parse_node_struct_t {
|
||||
#define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff)
|
||||
#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
|
||||
|
||||
mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg);
|
||||
static inline mp_parse_node_t mp_parse_node_new_small_int(mp_int_t val) {
|
||||
return (mp_parse_node_t)(MP_PARSE_NODE_SMALL_INT | ((mp_uint_t)val << 1));
|
||||
}
|
||||
static inline mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
|
||||
return (mp_parse_node_t)(kind | ((mp_uint_t)arg << 4));
|
||||
}
|
||||
bool mp_parse_node_is_const_false(mp_parse_node_t pn);
|
||||
bool mp_parse_node_is_const_true(mp_parse_node_t pn);
|
||||
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o);
|
||||
|
Loading…
Reference in New Issue
Block a user