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>
This commit is contained in:
Damien George 2022-06-08 14:43:02 +10:00
parent a4eef90b22
commit cbad559366
1 changed files with 10 additions and 5 deletions

View File

@ -1115,14 +1115,19 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
if (!is_as) { if (!is_as) {
*q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
} }
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
int len = n - 1; if (n == 0) {
for (int i = 0; i < n; i++) { // 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])); len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
} }
char *q_ptr = mp_local_alloc(len); char *q_ptr = mp_local_alloc(len);
char *str_dest = q_ptr; char *str_dest = q_ptr;
for (int i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
if (i > 0) { if (i > 0) {
*str_dest++ = '.'; *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); mp_local_free(q_ptr);
EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME); EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
if (is_as) { 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); EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), MP_EMIT_ATTR_LOAD);
} }
} }