8000 py/compile: Give the compiler a hint about num nodes being non-zero. · micropython/micropython@cbad559 · GitHub
[go: up one dir, main page]

Skip to content

Commit cbad559

Browse files
committed
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>
1 parent a4eef90 commit cbad559

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

py/compile.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,14 +1115,19 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
11151115
if (!is_as) {
11161116
*q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
11171117
}
1118-
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
1119-
int len = n - 1;
1120-
for (int i = 0; i < n; i++) {
1118+
size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
1119+
if (n == 0) {
1120+
// There must be at least one node in this PN_dotted_name.
1121+
// Let the compiler know this so it doesn't warn, and can generate better code.
1122+
MP_UNREACHABLE;
1123+
}
1124+
size_t len = n - 1;
1125+
for (size_t i = 0; i < n; i++) {
11211126
len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
11221127
}
11231128
char *q_ptr = mp_local_alloc(len);
11241129
char *str_dest = q_ptr;
1125-
for (int i = 0; i < n; i++) {
1130+
for (size_t i = 0; i < n; i++) {
11261131
if (i > 0) {
11271132
*str_dest++ = '.';
11281133
}
@@ -1135,7 +1140,7 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
11351140
mp_local_free(q_ptr);
11361141
EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
11371142
if (is_as) {
1138-
for (int i = 1; i < n; i++) {
1143+
for (size_t i = 1; i < n; i++) {
11391144
EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), MP_EMIT_ATTR_LOAD);
11401145
}
11411146
}

0 commit comments

Comments
 (0)
0