From ddd1e188011a021537c87c75f0a9e1818e335d5d Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 10 Jan 2015 14:07:24 +0000 Subject: [PATCH] py: Add config option MICROPY_COMP_MODULE_CONST for module consts. Compiler optimises lookup of module.CONST when enabled (an existing feature). Disabled by default; enabled for unix, windows, stmhal. Costs about 100 bytes ROM on stmhal. --- bare-arm/mpconfigport.h | 2 ++ py/compile.c | 17 ++++++----------- py/mpconfig.h | 5 +++++ stmhal/mpconfigport.h | 1 + unix/mpconfigport.h | 1 + windows/mpconfigport.h | 1 + 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/bare-arm/mpconfigport.h b/bare-arm/mpconfigport.h index 0015541f98..e9a755294f 100644 --- a/bare-arm/mpconfigport.h +++ b/bare-arm/mpconfigport.h @@ -6,6 +6,8 @@ #define MICROPY_EMIT_X64 (0) #define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0) +#define MICROPY_COMP_MODULE_CONST (0) +#define MICROPY_COMP_CONST (0) #define MICROPY_MEM_STATS (0) #define MICROPY_DEBUG_PRINTERS (0) #define MICROPY_ENABLE_GC (0) diff --git a/py/compile.c b/py/compile.c index b2ba01cc9d..3ee78bbfc7 100644 --- a/py/compile.c +++ b/py/compile.c @@ -98,6 +98,7 @@ STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const cha comp->compile_error = exc; } +#if MICROPY_COMP_MODULE_CONST STATIC const mp_map_elem_t mp_constants_table[] = { #if MICROPY_PY_UCTYPES { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes }, @@ -105,14 +106,8 @@ STATIC const mp_map_elem_t mp_constants_table[] = { // Extra constants as defined by a port MICROPY_PORT_CONSTANTS }; - -STATIC const mp_map_t mp_constants_map = { - .all_keys_are_qstrs = 1, - .table_is_fixed_array = 1, - .used = MP_ARRAY_SIZE(mp_constants_table), - .alloc = MP_ARRAY_SIZE(mp_constants_table), - .table = (mp_map_elem_t*)mp_constants_table, -}; +STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table); +#endif // this function is essentially a simple preprocessor STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) { @@ -327,6 +322,7 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m } } #endif +#if MICROPY_COMP_MODULE_CONST } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) { // id.id // look it up in constant table, see if it can be replaced with an integer @@ -340,11 +336,10 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m mp_load_method_maybe(elem->value, q_attr, dest); if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) { mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]); - if (MP_SMALL_INT_FITS(val)) { - pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val); - } + pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val); } } +#endif } break; } diff --git a/py/mpconfig.h b/py/mpconfig.h index bf1a8bda52..dcc116daf2 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -152,6 +152,11 @@ /*****************************************************************************/ /* Compiler configuration */ +// Whether to enable lookup of constants in modules; eg module.CONST +#ifndef MICROPY_COMP_MODULE_CONST +#define MICROPY_COMP_MODULE_CONST (0) +#endif + // Whether to enable constant optimisation; id = const(value) #ifndef MICROPY_COMP_CONST #define MICROPY_COMP_CONST (1) diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index a121cd1cbd..c1e4b0f9de 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -33,6 +33,7 @@ #define MICROPY_ALLOC_PATH_MAX (128) #define MICROPY_EMIT_THUMB (1) #define MICROPY_EMIT_INLINE_THUMB (1) +#define MICROPY_COMP_MODULE_CONST (1) #define MICROPY_ENABLE_GC (1) #define MICROPY_ENABLE_FINALISER (1) #define MICROPY_STACK_CHECK (1) diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index a874503a13..3f5d577038 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -39,6 +39,7 @@ #if !defined(MICROPY_EMIT_ARM) && defined(__arm__) #define MICROPY_EMIT_ARM (1) #endif +#define MICROPY_COMP_MODULE_CONST (1) #define MICROPY_ENABLE_GC (1) #define MICROPY_ENABLE_FINALISER (1) #define MICROPY_STACK_CHECK (1) diff --git a/windows/mpconfigport.h b/windows/mpconfigport.h index 59d0dfa741..befd1d9ec2 100644 --- a/windows/mpconfigport.h +++ b/windows/mpconfigport.h @@ -35,6 +35,7 @@ #define MICROPY_EMIT_X64 (0) #define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0) +#define MICROPY_COMP_MODULE_CONST (1) #define MICROPY_ENABLE_GC (1) #define MICROPY_ENABLE_FINALISER (1) #define MICROPY_MEM_STATS (1)