summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-08-10 13:26:11 +1000
committerDamien George <damien.p.george@gmail.com>2016-08-10 13:26:11 +1000
commit72ae3c72c7341656bcc610a5671e5cc23f7cf280 (patch)
treef9cad6709f801a587178c6af4830419df59121fc
parent8a15e0b1c74f0251ad549b1c8ec8f41538b4a2ac (diff)
downloadmicropython-72ae3c72c7341656bcc610a5671e5cc23f7cf280.tar.gz
micropython-72ae3c72c7341656bcc610a5671e5cc23f7cf280.zip
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.
-rwxr-xr-xtools/mpy-tool.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py
index 972ede8325..a7c3d8f918 100755
--- a/tools/mpy-tool.py
+++ b/tools/mpy-tool.py
@@ -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('};')