4242# end compatibility code
4343
4444import sys
45+ import struct
4546from collections import namedtuple
4647
4748sys .path .append ('../py' )
@@ -315,9 +316,10 @@ def freeze(self, parent_name):
315316'{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t[]){%s}}};'
316317% (obj_name ,neg ,ndigs ,ndigs ,bits_per_dig ,digs ))
317318elif type (obj )is float :
318- # works for REPR A and B only
319+ print ( '#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B' )
319320print ('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
320321% (obj_name ,obj ))
322+ print ('#endif' )
321323else :
322324# TODO
323325raise FreezeError (self ,'freezing of object %r is not implemented' % (obj ,))
@@ -328,7 +330,18 @@ def freeze(self, parent_name):
328330for qst in self .qstrs :
329331print (' (mp_uint_t)MP_OBJ_NEW_QSTR(%s),' % global_qstrs [qst ].qstr_id )
330332for i in range (len (self .objs )):
331- print (' (mp_uint_t)&const_obj_%s_%u,' % (self .escaped_name ,i ))
333+ if type (self .objs [i ])is float :
334+ print ('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B' )
335+ print (' (mp_uint_t)&const_obj_%s_%u,' % (self .escaped_name ,i ))
336+ print ('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C' )
337+ n = struct .unpack ('<I' ,struct .pack ('<f' ,self .objs [i ]))[0 ]
338+ n = ((n & ~ 0x3 )| 2 )+ 0x80800000
339+ print (' (mp_uint_t)0x%08x,' % (n ,))
340+ print ('#else' )
341+ print ('#error "MICROPY_OBJ_REPR_D not supported with floats in frozen mpy files"' )
342+ print ('#endif' )
343+ else :
344+ print (' (mp_uint_t)&const_obj_%s_%u,' % (self .escaped_name ,i ))
332345for rc in self .raw_codes :
333346print (' (mp_uint_t)&raw_code_%s,' % rc .escaped_name )
334347print ('};' )