aboutsummaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-06-08 14:43:02 +1000
committerDamien George <damien@micropython.org>2022-06-08 14:59:43 +1000
commitcbad5593668fa011a22d0816e27b6f8992ac7015 (patch)
tree6af358ce30f4febf36e51beee009d532b9b3b70d /py
parenta4eef90b2274d2e34486b6990191687500cb3893 (diff)
py/compile: Give the compiler a hint about num nodes being non-zero.
Without this, newer versions of gcc (eg 11.2.0) used with -O2 can warn about `q_ptr` being maybe uninitialized, because it doesn't know that there is at least one qstr being written in to this (alloca'd) memory. As part of this, change the type of `n` to `size_t` so the compiler knows it's unsigned and can generate better code. Code size change for this commit: bare-arm: -28 -0.049% minimal x86: -4 -0.002% unix x64: +0 +0.000% unix nanbox: -16 -0.003% stm32: -24 -0.006% PYBV10 cc3200: -32 -0.017% esp8266: +8 +0.001% GENERIC esp32: -52 -0.003% GENERIC nrf: -24 -0.013% pca10040 rp2: -32 -0.006% PICO samd: -28 -0.020% ADAFRUIT_ITSYBITSY_M4_EXPRESS Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py')
-rw-r--r--py/compile.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/py/compile.c b/py/compile.c
index 4dee9eff7..9cca5df40 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -1115,14 +1115,19 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
if (!is_as) {
*q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
}
- int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
- int len = n - 1;
- for (int i = 0; i < n; i++) {
+ size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
+ if (n == 0) {
+ // There must be at least one node in this PN_dotted_name.
+ // Let the compiler know this so it doesn't warn, and can generate better code.
+ MP_UNREACHABLE;
+ }
+ size_t len = n - 1;
+ for (size_t i = 0; i < n; i++) {
len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
}
char *q_ptr = mp_local_alloc(len);
char *str_dest = q_ptr;
- for (int i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; i++) {
if (i > 0) {
*str_dest++ = '.';
}
@@ -1135,7 +1140,7 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
mp_local_free(q_ptr);
EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
if (is_as) {
- for (int i = 1; i < n; i++) {
+ for (size_t i = 1; i < n; i++) {
EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), MP_EMIT_ATTR_LOAD);
}
}