summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-01-10 14:07:24 +0000
committerDamien George <damien.p.george@gmail.com>2015-01-10 14:07:24 +0000
commitddd1e188011a021537c87c75f0a9e1818e335d5d (patch)
tree167b683dbc0bf9b3467592d8b83f506c44b9848c /py
parent7bfe4b21b943d73d277441374d8688df91d6e037 (diff)
downloadmicropython-ddd1e188011a021537c87c75f0a9e1818e335d5d.tar.gz
micropython-ddd1e188011a021537c87c75f0a9e1818e335d5d.zip
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.
Diffstat (limited to 'py')
-rw-r--r--py/compile.c17
-rw-r--r--py/mpconfig.h5
2 files changed, 11 insertions, 11 deletions
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)