8000 py/compile: Combine expr, xor_expr and and_expr into one function. · micropython/micropython@25ae98f · GitHub
[go: up one dir, main page]

Skip to content

Commit 25ae98f

Browse files
committed
py/compile: Combine expr, xor_expr and and_expr into one function.
This and the previous 4 commits combined have change in code size of: bare-arm: -92 minimal x86: -544 unix x64: -544 unix nanbox: -712 stm32: -116 cc3200: -128 esp8266: -348 esp32: -232
1 parent 36e474e commit 25ae98f

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

py/compile.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,15 +1985,6 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
19851985
}
19861986
}
19871987

1988-
STATIC void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
1989-
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
1990-
compile_node(comp, pns->nodes[0]);
1991-
for (int i = 1; i < num_nodes; i += 1) {
1992-
compile_node(comp, pns->nodes[i]);
1993-
EMIT_ARG(binary_op, binary_op);
1994-
}
1995-
}
1996-
19971988
STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
19981989
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
19991990
mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
@@ -2102,16 +2093,16 @@ STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
21022093
compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
21032094
}
21042095

2105-
STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2106-
c_binary_op(comp, pns, MP_BINARY_OP_OR);
2107-
}
2108-
2109-
STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2110-
c_binary_op(comp, pns, MP_BINARY_OP_XOR);
2111-
}
2112-
2113-
STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2114-
c_binary_op(comp, pns, MP_BINARY_OP_AND);
2096+
STATIC void compile_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns) {
2097+
MP_STATIC_ASSERT(MP_BINARY_OP_OR + PN_xor_expr - PN_expr == MP_BINARY_OP_XOR);
2098+
MP_STATIC_ASSERT(MP_BINARY_OP_OR + PN_and_expr - PN_expr == MP_BINARY_OP_AND);
2099+
mp_binary_op_t binary_op = MP_BINARY_OP_OR + MP_PARSE_NODE_STRUCT_KIND(pns) - PN_expr;
2100+
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2101+
compile_node(comp, pns->nodes[0]);
2102+
for (int i = 1; i < num_nodes; ++i) {
2103+
compile_node(comp, pns->nodes[i]);
2104+
EMIT_ARG(binary_op, binary_op);
2105+
}
21152106
}
21162107

21172108
STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {

py/grammar.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ DEF_RULE_NC(comp_op_not_in, and(2), tok(KW_NOT), tok(KW_IN))
241241
DEF_RULE_NC(comp_op_is, and(2), tok(KW_IS), opt_rule(comp_op_is_not))
242242
DEF_RULE_NC(comp_op_is_not, and(1), tok(KW_NOT))
243243
DEF_RULE(star_expr, c(star_expr), and(2), tok(OP_STAR), rule(expr))
244-
DEF_RULE(expr, c(expr), list, rule(xor_expr), tok(OP_PIPE))
245-
DEF_RULE(xor_expr, c(xor_expr), list, rule(and_expr), tok(OP_CARET))
246-
DEF_RULE(and_expr, c(and_expr), list, rule(shift_expr), tok(OP_AMPERSAND))
244+
DEF_RULE(expr, c(binary_op), list, rule(xor_expr), tok(OP_PIPE))
245+
DEF_RULE(xor_expr, c(binary_op), list, rule(and_expr), tok(OP_CARET))
246+
DEF_RULE(and_expr, c(binary_op), list, rule(shift_expr), tok(OP_AMPERSAND))
247247
DEF_RULE(shift_expr, c(term), list, rule(arith_expr), rule(shift_op))
248248
DEF_RULE_NC(shift_op, or(2), tok(OP_DBL_LESS), tok(OP_DBL_MORE))
249249
DEF_RULE(arith_expr, c(term), list, rule(term), rule(arith_op))

0 commit comments

Comments
 (0)
0