tools/mpy-tool.py: Support freezing float literals with obj-repr C.

The tool now generates code for freezing floats in obj-repr A, B or C,
with the specific representation detected at compile time using macros.
This commit is contained in:
Damien George 2016-08-10 13:26:11 +10:00
parent 8a15e0b1c7
commit 72ae3c72c7

View File

@ -42,6 +42,7 @@ else:
# end compatibility code
import sys
import struct
from collections import namedtuple
sys.path.append('../py')
@ -315,9 +316,10 @@ class RawCode:
'{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t[]){%s}}};'
% (obj_name, neg, ndigs, ndigs, bits_per_dig, digs))
elif type(obj) is float:
# works for REPR A and B only
print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
print('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
% (obj_name, obj))
print('#endif')
else:
# TODO
raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,))
@ -328,7 +330,18 @@ class RawCode:
for qst in self.qstrs:
print(' (mp_uint_t)MP_OBJ_NEW_QSTR(%s),' % global_qstrs[qst].qstr_id)
for i in range(len(self.objs)):
print(' (mp_uint_t)&const_obj_%s_%u,' % (self.escaped_name, i))
if type(self.objs[i]) is float:
print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
print(' (mp_uint_t)&const_obj_%s_%u,' % (self.escaped_name, i))
print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
n = ((n & ~0x3) | 2) + 0x80800000
print(' (mp_uint_t)0x%08x,' % (n,))
print('#else')
print('#error "MICROPY_OBJ_REPR_D not supported with floats in frozen mpy files"')
print('#endif')
else:
print(' (mp_uint_t)&const_obj_%s_%u,' % (self.escaped_name, i))
for rc in self.raw_codes:
print(' (mp_uint_t)&raw_code_%s,' % rc.escaped_name)
print('};')