py/compile: Allow new qstrs to be allocated at all compiler passes.

Prior to this commit, all qstrs were required to be allocated (by calling
mp_emit_common_use_qstr) in the MP_PASS_SCOPE pass (the first one).  But
this is an unnecessary restriction, which is lifted by this commit.
Lifting the restriction simplifies the compiler because it can allocate
qstrs in later passes.

This also generates better code, because in some cases (eg when a variable
is closed over) the scope of an identifier is not known until a bit later
and then the identifier no longer needs its qstr allocated in the global
table.

Code size is reduced for all ports with this commit.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2022-05-06 22:43:03 +10:00
parent 1fb01bd6c5
commit 90682f43af
4 changed files with 22 additions and 33 deletions

View File

@@ -34,7 +34,6 @@
qstr_short_t mp_emit_common_use_qstr(mp_emit_common_t *emit, qstr qst) {
mp_map_elem_t *elem = mp_map_lookup(&emit->qstr_map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
if (elem->value == MP_OBJ_NULL) {
assert(emit->pass == MP_PASS_SCOPE);
elem->value = MP_OBJ_NEW_SMALL_INT(emit->qstr_map.used - 1);
}
return MP_OBJ_SMALL_INT_VALUE(elem->value);