From 72ae3c72c7341656bcc610a5671e5cc23f7cf280 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 Aug 2016 13:26:11 +1000 Subject: [PATCH] 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. --- tools/mpy-tool.py | 17 +++++++++++++++-- 1 file 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('