Commit b0cbfb04 authored by Damien George's avatar Damien George
Browse files

py/parse: Move function to check for const parse node to parse.[ch].

parent 3f8bb80e
...@@ -243,23 +243,13 @@ STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) ...@@ -243,23 +243,13 @@ STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns)
c_tuple(comp, MP_PARSE_NODE_NULL, pns); c_tuple(comp, MP_PARSE_NODE_NULL, pns);
} }
STATIC bool 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);
}
STATIC bool node_is_const_true(mp_parse_node_t pn) {
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
}
STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) { STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
if (node_is_const_false(pn)) { if (mp_parse_node_is_const_false(pn)) {
if (jump_if == false) { if (jump_if == false) {
EMIT_ARG(jump, label); EMIT_ARG(jump, label);
} }
return; return;
} else if (node_is_const_true(pn)) { } else if (mp_parse_node_is_const_true(pn)) {
if (jump_if == true) { if (jump_if == true) {
EMIT_ARG(jump, label); EMIT_ARG(jump, label);
} }
...@@ -1218,14 +1208,14 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { ...@@ -1218,14 +1208,14 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
uint l_end = comp_next_label(comp); uint l_end = comp_next_label(comp);
// optimisation: don't emit anything when "if False" // optimisation: don't emit anything when "if False"
if (!node_is_const_false(pns->nodes[0])) { if (!mp_parse_node_is_const_false(pns->nodes[0])) {
uint l_fail = comp_next_label(comp); uint l_fail = comp_next_label(comp);
c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
compile_node(comp, pns->nodes[1]); // if block compile_node(comp, pns->nodes[1]); // if block
// optimisation: skip everything else when "if True" // optimisation: skip everything else when "if True"
if (node_is_const_true(pns->nodes[0])) { if (mp_parse_node_is_const_true(pns->nodes[0])) {
goto done; goto done;
} }
...@@ -1250,14 +1240,14 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { ...@@ -1250,14 +1240,14 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i]; mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
// optimisation: don't emit anything when "if False" // optimisation: don't emit anything when "if False"
if (!node_is_const_false(pns_elif->nodes[0])) { if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
uint l_fail = comp_next_label(comp); uint l_fail = comp_next_label(comp);
c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
compile_node(comp, pns_elif->nodes[1]); // elif block compile_node(comp, pns_elif->nodes[1]); // elif block
// optimisation: skip everything else when "elif True" // optimisation: skip everything else when "elif True"
if (node_is_const_true(pns_elif->nodes[0])) { if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
goto done; goto done;
} }
...@@ -1294,9 +1284,9 @@ done: ...@@ -1294,9 +1284,9 @@ done:
STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
START_BREAK_CONTINUE_BLOCK START_BREAK_CONTINUE_BLOCK
if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False" if (!mp_parse_node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
uint top_label = comp_next_label(comp); uint top_label = comp_next_label(comp);
if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True" if (!mp_parse_node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
EMIT_ARG(jump, continue_label); EMIT_ARG(jump, continue_label);
} }
EMIT_ARG(label_assign, top_label); EMIT_ARG(label_assign, top_label);
......
...@@ -234,6 +234,16 @@ mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) { ...@@ -234,6 +234,16 @@ mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
return (mp_parse_node_t)(kind | (arg << 4)); 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);
}
bool mp_parse_node_is_const_true(mp_parse_node_t pn) {
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
|| (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
}
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) { bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
if (MP_PARSE_NODE_IS_SMALL_INT(pn)) { if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
*o = MP_OBJ_NEW_SMALL_INT(MP_PARSE_NODE_LEAF_SMALL_INT(pn)); *o = MP_OBJ_NEW_SMALL_INT(MP_PARSE_NODE_LEAF_SMALL_INT(pn));
......
...@@ -77,6 +77,8 @@ typedef struct _mp_parse_node_struct_t { ...@@ -77,6 +77,8 @@ typedef struct _mp_parse_node_struct_t {
#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8) #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); mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg);
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); bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o);
int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes); int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes);
void mp_parse_node_print(mp_parse_node_t pn, size_t indent); void mp_parse_node_print(mp_parse_node_t pn, size_t indent);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment