Skip to content

Commit d694ac6

Browse files
jimmodpgeorge
authored andcommitted
py/makeqstrdata.py: Ensure that scope names get low qstr values.
Originally implemented in a patch file provided by @ironss-iotec. Fixes issue micropython#14093. Signed-off-by: Jim Mussared <[email protected]>
1 parent 57de9da commit d694ac6

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

py/makeqstrdata.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@
223223
"zip",
224224
]
225225

226-
# Additional QSTRs that must have index <255 because they are stored in
227-
# `mp_binary_op_method_name` and `mp_unary_op_method_name` (see py/objtype.c).
226+
# Additional QSTRs that must have index <255 because they are stored as `byte` values.
228227
# These are not part of the .mpy compatibility list, but we place them in the
229228
# fixed unsorted pool (i.e. QDEF0) to ensure their indices are small.
230-
operator_qstr_list = {
229+
unsorted_qstr_list = {
230+
# From py/objtype.c: used in the `mp_binary_op_method_name` and `mp_unary_op_method_name` tables.
231231
"__bool__",
232232
"__pos__",
233233
"__neg__",
@@ -286,6 +286,13 @@
286286
"__get__",
287287
"__set__",
288288
"__delete__",
289+
# From py/scope.c: used in `scope_simple_name_table` table.
290+
# Note: "<module>" is already in `static_qstr_list`.
291+
"<lambda>",
292+
"<listcomp>",
293+
"<dictcomp>",
294+
"<setcomp>",
295+
"<genexpr>",
289296
}
290297

291298

@@ -404,10 +411,10 @@ def print_qstr_data(qcfgs, qstrs):
404411
print("QDEF0(MP_QSTR_%s, %s)" % (qstr_escape(qstr), qbytes))
405412

406413
# add remaining qstrs to the sorted (by value) pool (unless they're in
407-
# operator_qstr_list, in which case add them to the unsorted pool)
414+
# unsorted_qstr_list, in which case add them to the unsorted pool)
408415
for ident, qstr in sorted(qstrs.values(), key=lambda x: x[1]):
409416
qbytes = make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr)
410-
pool = 0 if qstr in operator_qstr_list else 1
417+
pool = 0 if qstr in unsorted_qstr_list else 1
411418
print("QDEF%d(MP_QSTR_%s, %s)" % (pool, ident, qbytes))
412419

413420

py/objtype.c

+4
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ static mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_arg
370370

371371
// Qstrs for special methods are guaranteed to have a small value, so we use byte
372372
// type to represent them.
373+
// The (unescaped) names appear in `unsorted_str_list` in the QSTR
374+
// generator script py/makeqstrdata.py to ensure they are assigned low numbers.
373375
const byte mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = {
374376
[MP_UNARY_OP_BOOL] = MP_QSTR___bool__,
375377
[MP_UNARY_OP_LEN] = MP_QSTR___len__,
@@ -468,6 +470,8 @@ static mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
468470
// fail). They can be added at the expense of code size for the qstr.
469471
// Qstrs for special methods are guaranteed to have a small value, so we use byte
470472
// type to represent them.
473+
// The (unescaped) names appear in `unsorted_str_list` in the QSTR
474+
// generator script py/makeqstrdata.py to ensure they are assigned low numbers.
471475
const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = {
472476
[MP_BINARY_OP_LESS] = MP_QSTR___lt__,
473477
[MP_BINARY_OP_MORE] = MP_QSTR___gt__,

py/qstr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ size_t qstr_compute_hash(const byte *data, size_t len) {
7979
// it is part of the .mpy ABI. See the top of py/persistentcode.c and
8080
// static_qstr_list in makeqstrdata.py. This pool is unsorted (although in a
8181
// future .mpy version we could re-order them and make it sorted). It also
82-
// contains additional qstrs that must have IDs <256, see operator_qstr_list
82+
// contains additional qstrs that must have IDs <256, see unsorted_qstr_list
8383
// in makeqstrdata.py.
8484
#if MICROPY_QSTR_BYTES_IN_HASH
8585
const qstr_hash_t mp_qstr_const_hashes_static[] = {

py/scope.c

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#if MICROPY_ENABLE_COMPILER
3232

3333
// These low numbered qstrs should fit in 8 bits. See assertions below.
34+
// The (unescaped) names appear in `unsorted_str_list` in the QSTR
35+
// generator script py/makeqstrdata.py to ensure they are assigned low numbers.
3436
static const uint8_t scope_simple_name_table[] = {
3537
[SCOPE_MODULE] = MP_QSTR__lt_module_gt_,
3638
[SCOPE_LAMBDA] = MP_QSTR__lt_lambda_gt_,

0 commit comments

Comments
 (0)