42
42
# end compatibility code
43
43
44
44
import sys
45
+ import struct
45
46
from collections import namedtuple
46
47
47
48
sys .path .append ('../py' )
@@ -315,9 +316,10 @@ def freeze(self, parent_name):
315
316
'{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t[]){%s}}};'
316
317
% (obj_name , neg , ndigs , ndigs , bits_per_dig , digs ))
317
318
elif 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' )
319
320
print ('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
320
321
% (obj_name , obj ))
322
+ print ('#endif' )
321
323
else :
322
324
# TODO
323
325
raise FreezeError (self , 'freezing of object %r is not implemented' % (obj ,))
@@ -328,7 +330,18 @@ def freeze(self, parent_name):
328
330
for qst in self .qstrs :
329
331
print (' (mp_uint_t)MP_OBJ_NEW_QSTR(%s),' % global_qstrs [qst ].qstr_id )
330
332
for 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 ))
332
345
for rc in self .raw_codes :
333
346
print (' (mp_uint_t)&raw_code_%s,' % rc .escaped_name )
334
347
print ('};' )
0 commit comments