8000 py/bc: Fix calculation of opcode size for opcodes with map caching. · msuszko/micropython@6bf8ecf · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bf8ecf

Browse files
committed
py/bc: Fix calculation of opcode size for opcodes with map caching.
All 4 opcodes that can have caching bytes also have qstrs, so the test for them must go in the qstr part of the code. The reason this incorrect calculation of the opcode size did not lead to a bug is because the caching byte is at the end of the opcode (byte, qstr, qstr, cache) and is always 0x00 when saving/loading, so was just treated as a single byte no-op opcode. Hence these opcodes were being saved/loaded/decoded correctly. Thanks to @malinah for finding the problem and providing the initial patch.
1 parent fbb8335 commit 6bf8ecf

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

py/bc.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ continue2:;
292292
// MP_BC_MAKE_CLOSURE_DEFARGS
293293
// MP_BC_RAISE_VARARGS
294294
// There are 4 special opcodes that have an extra byte only when
295-
// MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE is enabled:
295+
// MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE is enabled (and they take a qstr):
296296
// MP_BC_LOAD_NAME
297297
// MP_BC_LOAD_GLOBAL
298298
// MP_BC_LOAD_ATTR
@@ -386,18 +386,20 @@ uint mp_opcode_format(const byte *ip, size_t *opcode_size) {
386386
uint f = (opcode_format_table[*ip >> 2] >> (2 * (*ip & 3))) & 3;
387387
const byte *ip_start = ip;
388388
if (f == MP_OPCODE_QSTR) {
389+
if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
390+
if (*ip == MP_BC_LOAD_NAME
391+
|| *ip == MP_BC_LOAD_GLOBAL
392+
|| *ip == MP_BC_LOAD_ATTR
393+
|| *ip == MP_BC_STORE_ATTR) {
394+
ip += 1;
395+
}
396+
}
389397
ip += 3;
390398
} else {
391399
int extra_byte = (
392400
*ip == MP_BC_RAISE_VARARGS
393401
|| *ip == MP_BC_MAKE_CLOSURE
394402
|| *ip == MP_BC_MAKE_CLOSURE_DEFARGS
395-
#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
396-
|| *ip == MP_BC_LOAD_NAME
397-
|| *ip == MP_BC_LOAD_GLOBAL
398-
|| *ip == MP_BC_LOAD_ATTR
399-
|| *ip == MP_BC_STORE_ATTR
400-
#endif
401403
);
402404
ip += 1;
403405
if (f == MP_OPCODE_VAR_UINT) {

0 commit comments

Comments
 (0)
0