Skip to content

Commit b6a3289

Browse files
committed
tools/mpy-tool.py: Don't generate const_table if it's empty.
1 parent bfc2092 commit b6a3289

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

tools/mpy-tool.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -331,27 +331,29 @@ def freeze(self, parent_name):
331331
# TODO
332332
raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,))
333333

334-
# generate constant table
335-
print('STATIC const mp_rom_obj_t const_table_data_%s[%u] = {'
336-
% (self.escaped_name, len(self.qstrs) + len(self.objs) + len(self.raw_codes)))
337-
for qst in self.qstrs:
338-
print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id)
339-
for i in range(len(self.objs)):
340-
if type(self.objs[i]) is float:
341-
print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
342-
print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
343-
print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
344-
n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
345-
n = ((n & ~0x3) | 2) + 0x80800000
346-
print(' (mp_rom_obj_t)(0x%08x),' % (n,))
347-
print('#else')
348-
print('#error "MICROPY_OBJ_REPR_D not supported with floats in frozen mpy files"')
349-
print('#endif')
350-
else:
351-
print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
352-
for rc in self.raw_codes:
353-
print(' MP_ROM_PTR(&raw_code_%s),' % rc.escaped_name)
354-
print('};')
334+
# generate constant table, if it has any entries
335+
const_table_len = len(self.qstrs) + len(self.objs) + len(self.raw_codes)
336+
if const_table_len:
337+
print('STATIC const mp_rom_obj_t const_table_data_%s[%u] = {'
338+
% (self.escaped_name, const_table_len))
339+
for qst in self.qstrs:
340+
print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id)
341+
for i in range(len(self.objs)):
342+
if type(self.objs[i]) is float:
343+
print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
344+
print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
345+
print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
346+
n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
347+
n = ((n & ~0x3) | 2) + 0x80800000
348+
print(' (mp_rom_obj_t)(0x%08x),' % (n,))
349+
print('#else')
350+
print('#error "MICROPY_OBJ_REPR_D not supported with floats in frozen mpy files"')
351+
print('#endif')
352+
else:
353+
print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
354+
for rc in self.raw_codes:
355+
print(' MP_ROM_PTR(&raw_code_%s),' % rc.escaped_name)
356+
print('};')
355357

356358
# generate module
357359
if self.simple_name.str != '<module>':
@@ -362,7 +364,10 @@ def freeze(self, parent_name):
362364
print(' .n_pos_args = %u,' % self.prelude[3])
363365
print(' .data.u_byte = {')
364366
print(' .bytecode = bytecode_data_%s,' % self.escaped_name)
365-
print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
367+
if const_table_len:
368+
print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
369+
else:
370+
print(' .const_table = NULL,')
366371
print(' #if MICROPY_PERSISTENT_CODE_SAVE')
367372
print(' .bc_len = %u,' % len(self.bytecode))
368373
print(' .n_obj = %u,' % len(self.objs))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy