8000 tools/mpy-tool.py: Fix calc of opcode size for opcodes with map caching. · msuszko/micropython@814d580 · GitHub
[go: up one dir, main page]

Skip to content

Commit 814d580

Browse files
committed
tools/mpy-tool.py: Fix calc of opcode size for opcodes with map caching.
Following an equivalent fix to py/bc.c. The reason the incorrect values for the opcode constants were not previously causing a bug is because they were never being used: these opcodes always have qstr arguments so the part of the code that was comparing them would never be reached. Thanks to @malinah for finding the problem and providing the initial patch.
1 parent 6bf8ecf commit 814d580

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

tools/mpy-tool.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ class Config:
7373
MP_BC_MAKE_CLOSURE_DEFARGS = 0x63
7474
MP_BC_RAISE_VARARGS = 0x5c
7575
# extra byte if caching enabled:
76-
MP_BC_LOAD_NAME = 0x1c
77-
MP_BC_LOAD_GLOBAL = 0x1d
78-
MP_BC_LOAD_ATTR = 0x1e
76+
MP_BC_LOAD_NAME = 0x1b
77+
MP_BC_LOAD_GLOBAL = 0x1c
78+
MP_BC_LOAD_ATTR = 0x1d
7979
MP_BC_STORE_ATTR = 0x26
8080

8181
def make_opcode_format():
@@ -166,18 +166,18 @@ def mp_opcode_format(bytecode, ip, opcode_format=make_opcode_format()):
166166
ip_start = ip
167167
f = (opcode_format[opcode >> 2] >> (2 * (opcode & 3))) & 3
168168
if f == MP_OPCODE_QSTR:
169+
if config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE:
170+
if (opcode == MP_BC_LOAD_NAME
171+
or opcode == MP_BC_LOAD_GLOBAL
172+
or opcode == MP_BC_LOAD_ATTR
173+
or opcode == MP_BC_STORE_ATTR):
174+
ip += 1
169175
ip += 3
170176
else:
171177
extra_byte = (
172178
opcode == MP_BC_RAISE_VARARGS
173179
or opcode == MP_BC_MAKE_CLOSURE
174180
or opcode == MP_BC_MAKE_CLOSURE_DEFARGS
175-
or config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE and (
176-
opcode == MP_BC_LOAD_NAME
177-
or opcode == MP_BC_LOAD_GLOBAL
178-
or opcode == MP_BC_LOAD_ATTR
179-
or opcode == MP_BC_STORE_ATTR
180-
)
181181
)
182182
ip += 1
183183
if f == MP_OPCODE_VAR_UINT:
@@ -278,7 +278,8 @@ def freeze(self, parent_name):
278278
f, sz = mp_opcode_format(self.bytecode, ip)
279279
if f == 1:
280280
qst = self._unpack_qstr(ip + 1).qstr_id
281-
print(' ', '0x%02x,' % self.bytecode[ip], qst, '& 0xff,', qst, '>> 8,')
281+
extra = '' if sz == 3 else ' 0x%02x,' % self.bytecode[ip + 3]
282+
print(' ', '0x%02x,' % self.bytecode[ip], qst, '& 0xff,', qst, '>> 8,', extra)
282283
else:
283284
print(' ', ''.join('0x%02x, ' % self.bytecode[ip + i] for i in range(sz)))
284285
ip += sz

0 commit comments

Comments
 (0)
0