Skip to content

Commit e52f14d

Browse files
committed
py/parse: Factor obj extract code to mp_parse_node_extract_const_object.
Signed-off-by: Damien George <[email protected]>
1 parent 42d0bd2 commit e52f14d

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

py/compile.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -2763,12 +2763,7 @@ STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pn
27632763
#endif
27642764

27652765
STATIC mp_obj_t get_const_object(mp_parse_node_struct_t *pns) {
2766-
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2767-
// nodes are 32-bit pointers, but need to extract 64-bit object
2768-
return (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
2769-
#else
2770-
return (mp_obj_t)pns->nodes[0];
2771-
#endif
2766+
return mp_parse_node_extract_const_object(pns);
27722767
}
27732768

27742769
STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {

py/parse.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,7 @@ bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
333333
return true;
334334
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, RULE_const_object)) {
335335
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
336-
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
337-
// nodes are 32-bit pointers, but need to extract 64-bit object
338-
*o = (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
339-
#else
340-
*o = (mp_obj_t)pns->nodes[0];
341-
#endif
336+
*o = mp_parse_node_extract_const_object(pns);
342337
return mp_obj_is_int(*o);
343338
} else {
344339
return false;
@@ -397,10 +392,11 @@ void mp_parse_node_print(const mp_print_t *print, mp_parse_node_t pn, size_t ind
397392
// node must be a mp_parse_node_struct_t
398393
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
399394
if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) {
395+
mp_obj_t obj = mp_parse_node_extract_const_object(pns);
400396
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
401-
mp_printf(print, "literal const(%016llx)\n", (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
397+
mp_printf(print, "literal const(%016llx)\n", obj);
402398
#else
403-
mp_printf(print, "literal const(%p)\n", (mp_obj_t)pns->nodes[0]);
399+
mp_printf(print, "literal const(%p)\n", obj);
404400
#endif
405401
} else {
406402
size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);

py/parse.h

+11
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,20 @@ typedef struct _mp_parse_node_struct_t {
7777
static inline mp_parse_node_t mp_parse_node_new_small_int(mp_int_t val) {
7878
return (mp_parse_node_t)(MP_PARSE_NODE_SMALL_INT | ((mp_uint_t)val << 1));
7979
}
80+
8081
static inline mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
8182
return (mp_parse_node_t)(kind | ((mp_uint_t)arg << 4));
8283
}
84+
85+
static inline mp_obj_t mp_parse_node_extract_const_object(mp_parse_node_struct_t *pns) {
86+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
87+
// nodes are 32-bit pointers, but need to extract 64-bit object
88+
return (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
89+
#else
90+
return (mp_obj_t)pns->nodes[0];
91+
#endif
92+
}
93+
8394
bool mp_parse_node_is_const_false(mp_parse_node_t pn);
8495
bool mp_parse_node_is_const_true(mp_parse_node_t pn);
8596
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o);

0 commit comments

Comments
 (0)