blob: ac7b2c1ccf0c49aebd03d3fbeb9e32773fc15be1 [file] [log] [blame]
Damien George0699c6b2016-01-31 21:45:22 +00001#!/usr/bin/env python3
2#
3# This file is part of the MicroPython project, http://micropython.org/
4#
5# The MIT License (MIT)
6#
7# Copyright (c) 2016 Damien P. George
8#
9# Permission is hereby granted, free of charge, to any person obtaining a copy
10# of this software and associated documentation files (the "Software"), to deal
11# in the Software without restriction, including without limitation the rights
12# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13# copies of the Software, and to permit persons to whom the Software is
14# furnished to do so, subject to the following conditions:
15#
16# The above copyright notice and this permission notice shall be included in
17# all copies or substantial portions of the Software.
18#
19# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25# THE SOFTWARE.
26
Damien Georgec3beb162016-04-15 11:56:10 +010027# Python 2/3 compatibility code
28from __future__ import print_function
29import platform
30if platform.python_version_tuple()[0] == '2':
31 str_cons = lambda val, enc=None: val
32 bytes_cons = lambda val, enc=None: bytearray(val)
33 is_str_type = lambda o: type(o) is str
34 is_bytes_type = lambda o: type(o) is bytearray
35 is_int_type = lambda o: type(o) is int or type(o) is long
36else:
37 str_cons = str
38 bytes_cons = bytes
39 is_str_type = lambda o: type(o) is str
40 is_bytes_type = lambda o: type(o) is bytes
41 is_int_type = lambda o: type(o) is int
42# end compatibility code
43
Damien George0699c6b2016-01-31 21:45:22 +000044import sys
Damien George72ae3c72016-08-10 13:26:11 +100045import struct
Damien George0699c6b2016-01-31 21:45:22 +000046from collections import namedtuple
47
Paul Sokolovsky473e85e2017-05-01 00:01:30 +030048sys.path.append(sys.path[0] + '/../py')
Damien George0699c6b2016-01-31 21:45:22 +000049import makeqstrdata as qstrutil
50
51class FreezeError(Exception):
52 def __init__(self, rawcode, msg):
53 self.rawcode = rawcode
54 self.msg = msg
55
56 def __str__(self):
57 return 'error while freezing %s: %s' % (self.rawcode.source_file, self.msg)
58
59class Config:
Damien Georgeff93fd42017-10-05 10:48:23 +110060 MPY_VERSION = 3
Damien George0699c6b2016-01-31 21:45:22 +000061 MICROPY_LONGINT_IMPL_NONE = 0
62 MICROPY_LONGINT_IMPL_LONGLONG = 1
63 MICROPY_LONGINT_IMPL_MPZ = 2
64config = Config()
65
66MP_OPCODE_BYTE = 0
67MP_OPCODE_QSTR = 1
68MP_OPCODE_VAR_UINT = 2
69MP_OPCODE_OFFSET = 3
70
71# extra bytes:
72MP_BC_MAKE_CLOSURE = 0x62
73MP_BC_MAKE_CLOSURE_DEFARGS = 0x63
74MP_BC_RAISE_VARARGS = 0x5c
75# extra byte if caching enabled:
76MP_BC_LOAD_NAME = 0x1c
77MP_BC_LOAD_GLOBAL = 0x1d
78MP_BC_LOAD_ATTR = 0x1e
79MP_BC_STORE_ATTR = 0x26
80
81def make_opcode_format():
82 def OC4(a, b, c, d):
83 return a | (b << 2) | (c << 4) | (d << 6)
84 U = 0
85 B = 0
86 Q = 1
87 V = 2
88 O = 3
Damien Georgec3beb162016-04-15 11:56:10 +010089 return bytes_cons((
Damien George0699c6b2016-01-31 21:45:22 +000090 # this table is taken verbatim from py/bc.c
91 OC4(U, U, U, U), # 0x00-0x03
92 OC4(U, U, U, U), # 0x04-0x07
93 OC4(U, U, U, U), # 0x08-0x0b
94 OC4(U, U, U, U), # 0x0c-0x0f
95 OC4(B, B, B, U), # 0x10-0x13
96 OC4(V, U, Q, V), # 0x14-0x17
Damien Georgedd11af22017-04-19 09:45:59 +100097 OC4(B, V, V, Q), # 0x18-0x1b
Damien George0699c6b2016-01-31 21:45:22 +000098 OC4(Q, Q, Q, Q), # 0x1c-0x1f
99 OC4(B, B, V, V), # 0x20-0x23
100 OC4(Q, Q, Q, B), # 0x24-0x27
101 OC4(V, V, Q, Q), # 0x28-0x2b
102 OC4(U, U, U, U), # 0x2c-0x2f
103 OC4(B, B, B, B), # 0x30-0x33
104 OC4(B, O, O, O), # 0x34-0x37
105 OC4(O, O, U, U), # 0x38-0x3b
106 OC4(U, O, B, O), # 0x3c-0x3f
107 OC4(O, B, B, O), # 0x40-0x43
Damien George933eab42017-10-10 10:37:38 +1100108 OC4(B, B, O, B), # 0x44-0x47
Damien George0699c6b2016-01-31 21:45:22 +0000109 OC4(U, U, U, U), # 0x48-0x4b
110 OC4(U, U, U, U), # 0x4c-0x4f
Damien George7df92912016-09-23 12:48:57 +1000111 OC4(V, V, U, V), # 0x50-0x53
112 OC4(B, U, V, V), # 0x54-0x57
Damien George0699c6b2016-01-31 21:45:22 +0000113 OC4(V, V, V, B), # 0x58-0x5b
114 OC4(B, B, B, U), # 0x5c-0x5f
115 OC4(V, V, V, V), # 0x60-0x63
116 OC4(V, V, V, V), # 0x64-0x67
117 OC4(Q, Q, B, U), # 0x68-0x6b
118 OC4(U, U, U, U), # 0x6c-0x6f
119
120 OC4(B, B, B, B), # 0x70-0x73
121 OC4(B, B, B, B), # 0x74-0x77
122 OC4(B, B, B, B), # 0x78-0x7b
123 OC4(B, B, B, B), # 0x7c-0x7f
124 OC4(B, B, B, B), # 0x80-0x83
125 OC4(B, B, B, B), # 0x84-0x87
126 OC4(B, B, B, B), # 0x88-0x8b
127 OC4(B, B, B, B), # 0x8c-0x8f
128 OC4(B, B, B, B), # 0x90-0x93
129 OC4(B, B, B, B), # 0x94-0x97
130 OC4(B, B, B, B), # 0x98-0x9b
131 OC4(B, B, B, B), # 0x9c-0x9f
132 OC4(B, B, B, B), # 0xa0-0xa3
133 OC4(B, B, B, B), # 0xa4-0xa7
134 OC4(B, B, B, B), # 0xa8-0xab
135 OC4(B, B, B, B), # 0xac-0xaf
136
137 OC4(B, B, B, B), # 0xb0-0xb3
138 OC4(B, B, B, B), # 0xb4-0xb7
139 OC4(B, B, B, B), # 0xb8-0xbb
140 OC4(B, B, B, B), # 0xbc-0xbf
141
142 OC4(B, B, B, B), # 0xc0-0xc3
143 OC4(B, B, B, B), # 0xc4-0xc7
144 OC4(B, B, B, B), # 0xc8-0xcb
145 OC4(B, B, B, B), # 0xcc-0xcf
146
147 OC4(B, B, B, B), # 0xd0-0xd3
Damien George933eab42017-10-10 10:37:38 +1100148 OC4(U, U, U, B), # 0xd4-0xd7
Damien George0699c6b2016-01-31 21:45:22 +0000149 OC4(B, B, B, B), # 0xd8-0xdb
150 OC4(B, B, B, B), # 0xdc-0xdf
151
152 OC4(B, B, B, B), # 0xe0-0xe3
153 OC4(B, B, B, B), # 0xe4-0xe7
154 OC4(B, B, B, B), # 0xe8-0xeb
155 OC4(B, B, B, B), # 0xec-0xef
156
157 OC4(B, B, B, B), # 0xf0-0xf3
158 OC4(B, B, B, B), # 0xf4-0xf7
Damien George933eab42017-10-10 10:37:38 +1100159 OC4(U, U, U, U), # 0xf8-0xfb
Damien George0699c6b2016-01-31 21:45:22 +0000160 OC4(U, U, U, U), # 0xfc-0xff
161 ))
162
163# this function mirrors that in py/bc.c
164def mp_opcode_format(bytecode, ip, opcode_format=make_opcode_format()):
165 opcode = bytecode[ip]
166 ip_start = ip
167 f = (opcode_format[opcode >> 2] >> (2 * (opcode & 3))) & 3
168 if f == MP_OPCODE_QSTR:
169 ip += 3
170 else:
171 extra_byte = (
172 opcode == MP_BC_RAISE_VARARGS
173 or opcode == MP_BC_MAKE_CLOSURE
174 or opcode == MP_BC_MAKE_CLOSURE_DEFARGS
175 or config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE and (
176 opcode == MP_BC_LOAD_NAME
177 or opcode == MP_BC_LOAD_GLOBAL
178 or opcode == MP_BC_LOAD_ATTR
179 or opcode == MP_BC_STORE_ATTR
180 )
181 )
182 ip += 1
183 if f == MP_OPCODE_VAR_UINT:
184 while bytecode[ip] & 0x80 != 0:
185 ip += 1
186 ip += 1
187 elif f == MP_OPCODE_OFFSET:
188 ip += 2
189 ip += extra_byte
190 return f, ip - ip_start
191
192def decode_uint(bytecode, ip):
193 unum = 0
194 while True:
195 val = bytecode[ip]
196 ip += 1
197 unum = (unum << 7) | (val & 0x7f)
198 if not (val & 0x80):
199 break
200 return ip, unum
201
202def extract_prelude(bytecode):
203 ip = 0
204 ip, n_state = decode_uint(bytecode, ip)
205 ip, n_exc_stack = decode_uint(bytecode, ip)
206 scope_flags = bytecode[ip]; ip += 1
207 n_pos_args = bytecode[ip]; ip += 1
208 n_kwonly_args = bytecode[ip]; ip += 1
209 n_def_pos_args = bytecode[ip]; ip += 1
210 ip2, code_info_size = decode_uint(bytecode, ip)
211 ip += code_info_size
212 while bytecode[ip] != 0xff:
213 ip += 1
214 ip += 1
215 # ip now points to first opcode
216 # ip2 points to simple_name qstr
217 return ip, ip2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args, code_info_size)
218
219class RawCode:
Damien George02fd83b2016-05-03 12:24:39 +0100220 # a set of all escaped names, to make sure they are unique
221 escaped_names = set()
222
Damien George0699c6b2016-01-31 21:45:22 +0000223 def __init__(self, bytecode, qstrs, objs, raw_codes):
224 # set core variables
225 self.bytecode = bytecode
226 self.qstrs = qstrs
227 self.objs = objs
228 self.raw_codes = raw_codes
229
230 # extract prelude
231 self.ip, self.ip2, self.prelude = extract_prelude(self.bytecode)
232 self.simple_name = self._unpack_qstr(self.ip2)
233 self.source_file = self._unpack_qstr(self.ip2 + 2)
234
235 def _unpack_qstr(self, ip):
236 qst = self.bytecode[ip] | self.bytecode[ip + 1] << 8
237 return global_qstrs[qst]
238
239 def dump(self):
240 # dump children first
241 for rc in self.raw_codes:
stijne4ab4042017-08-16 10:37:00 +0200242 rc.freeze('')
Damien George0699c6b2016-01-31 21:45:22 +0000243 # TODO
244
245 def freeze(self, parent_name):
246 self.escaped_name = parent_name + self.simple_name.qstr_esc
247
Damien George02fd83b2016-05-03 12:24:39 +0100248 # make sure the escaped name is unique
249 i = 2
250 while self.escaped_name in RawCode.escaped_names:
251 self.escaped_name = parent_name + self.simple_name.qstr_esc + str(i)
252 i += 1
253 RawCode.escaped_names.add(self.escaped_name)
254
Damien George0699c6b2016-01-31 21:45:22 +0000255 # emit children first
256 for rc in self.raw_codes:
257 rc.freeze(self.escaped_name + '_')
258
259 # generate bytecode data
260 print()
261 print('// frozen bytecode for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str))
Damien George98458a42017-01-05 15:52:52 +1100262 print('STATIC ', end='')
263 if not config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE:
264 print('const ', end='')
265 print('byte bytecode_data_%s[%u] = {' % (self.escaped_name, len(self.bytecode)))
Damien George0699c6b2016-01-31 21:45:22 +0000266 print(' ', end='')
267 for i in range(self.ip2):
268 print(' 0x%02x,' % self.bytecode[i], end='')
269 print()
270 print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,')
271 print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,')
272 print(' ', end='')
273 for i in range(self.ip2 + 4, self.ip):
274 print(' 0x%02x,' % self.bytecode[i], end='')
275 print()
276 ip = self.ip
277 while ip < len(self.bytecode):
278 f, sz = mp_opcode_format(self.bytecode, ip)
279 if f == 1:
280 qst = self._unpack_qstr(ip + 1).qstr_id
281 print(' ', '0x%02x,' % self.bytecode[ip], qst, '& 0xff,', qst, '>> 8,')
282 else:
283 print(' ', ''.join('0x%02x, ' % self.bytecode[ip + i] for i in range(sz)))
284 ip += sz
285 print('};')
286
287 # generate constant objects
288 for i, obj in enumerate(self.objs):
289 obj_name = 'const_obj_%s_%u' % (self.escaped_name, i)
Damien Georgeb6bdf182016-09-02 15:10:45 +1000290 if is_str_type(obj) or is_bytes_type(obj):
291 if is_str_type(obj):
292 obj = bytes_cons(obj, 'utf8')
293 obj_type = 'mp_type_str'
294 else:
295 obj_type = 'mp_type_bytes'
296 print('STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
297 % (obj_name, obj_type, qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
298 len(obj), ''.join(('\\x%02x' % b) for b in obj)))
Damien Georgec3beb162016-04-15 11:56:10 +0100299 elif is_int_type(obj):
Damien George0699c6b2016-01-31 21:45:22 +0000300 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
301 # TODO check if we can actually fit this long-int into a small-int
302 raise FreezeError(self, 'target does not support long int')
303 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
304 # TODO
305 raise FreezeError(self, 'freezing int to long-long is not implemented')
306 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
307 neg = 0
308 if obj < 0:
309 obj = -obj
310 neg = 1
311 bits_per_dig = config.MPZ_DIG_SIZE
312 digs = []
313 z = obj
314 while z:
315 digs.append(z & ((1 << bits_per_dig) - 1))
316 z >>= bits_per_dig
317 ndigs = len(digs)
318 digs = ','.join(('%#x' % d) for d in digs)
319 print('STATIC const mp_obj_int_t %s = {{&mp_type_int}, '
320 '{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t[]){%s}}};'
321 % (obj_name, neg, ndigs, ndigs, bits_per_dig, digs))
322 elif type(obj) is float:
Damien George72ae3c72016-08-10 13:26:11 +1000323 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
Damien George0699c6b2016-01-31 21:45:22 +0000324 print('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
325 % (obj_name, obj))
Damien George72ae3c72016-08-10 13:26:11 +1000326 print('#endif')
Damien Georgec51c8832016-09-03 00:19:02 +1000327 elif type(obj) is complex:
328 print('STATIC const mp_obj_complex_t %s = {{&mp_type_complex}, %.16g, %.16g};'
329 % (obj_name, obj.real, obj.imag))
Damien George0699c6b2016-01-31 21:45:22 +0000330 else:
331 # TODO
332 raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,))
333
Damien Georgeb6a32892017-08-12 22:26:18 +1000334 # generate constant table, if it has any entries
335 const_table_len = len(self.qstrs) + len(self.objs) + len(self.raw_codes)
336 if const_table_len:
337 print('STATIC const mp_rom_obj_t const_table_data_%s[%u] = {'
338 % (self.escaped_name, const_table_len))
339 for qst in self.qstrs:
340 print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id)
341 for i in range(len(self.objs)):
342 if type(self.objs[i]) is float:
343 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
344 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
345 print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
346 n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
347 n = ((n & ~0x3) | 2) + 0x80800000
348 print(' (mp_rom_obj_t)(0x%08x),' % (n,))
349 print('#else')
350 print('#error "MICROPY_OBJ_REPR_D not supported with floats in frozen mpy files"')
351 print('#endif')
352 else:
353 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
354 for rc in self.raw_codes:
355 print(' MP_ROM_PTR(&raw_code_%s),' % rc.escaped_name)
356 print('};')
Damien George0699c6b2016-01-31 21:45:22 +0000357
358 # generate module
359 if self.simple_name.str != '<module>':
360 print('STATIC ', end='')
361 print('const mp_raw_code_t raw_code_%s = {' % self.escaped_name)
362 print(' .kind = MP_CODE_BYTECODE,')
363 print(' .scope_flags = 0x%02x,' % self.prelude[2])
364 print(' .n_pos_args = %u,' % self.prelude[3])
365 print(' .data.u_byte = {')
366 print(' .bytecode = bytecode_data_%s,' % self.escaped_name)
Damien Georgeb6a32892017-08-12 22:26:18 +1000367 if const_table_len:
368 print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
369 else:
370 print(' .const_table = NULL,')
Damien George0699c6b2016-01-31 21:45:22 +0000371 print(' #if MICROPY_PERSISTENT_CODE_SAVE')
372 print(' .bc_len = %u,' % len(self.bytecode))
373 print(' .n_obj = %u,' % len(self.objs))
374 print(' .n_raw_code = %u,' % len(self.raw_codes))
375 print(' #endif')
376 print(' },')
377 print('};')
378
379def read_uint(f):
380 i = 0
381 while True:
Damien Georgec3beb162016-04-15 11:56:10 +0100382 b = bytes_cons(f.read(1))[0]
Damien George0699c6b2016-01-31 21:45:22 +0000383 i = (i << 7) | (b & 0x7f)
384 if b & 0x80 == 0:
385 break
386 return i
387
388global_qstrs = []
389qstr_type = namedtuple('qstr', ('str', 'qstr_esc', 'qstr_id'))
390def read_qstr(f):
391 ln = read_uint(f)
Damien Georgec3beb162016-04-15 11:56:10 +0100392 data = str_cons(f.read(ln), 'utf8')
Damien George0699c6b2016-01-31 21:45:22 +0000393 qstr_esc = qstrutil.qstr_escape(data)
394 global_qstrs.append(qstr_type(data, qstr_esc, 'MP_QSTR_' + qstr_esc))
395 return len(global_qstrs) - 1
396
397def read_obj(f):
398 obj_type = f.read(1)
399 if obj_type == b'e':
400 return Ellipsis
401 else:
402 buf = f.read(read_uint(f))
403 if obj_type == b's':
Damien Georgec3beb162016-04-15 11:56:10 +0100404 return str_cons(buf, 'utf8')
Damien George0699c6b2016-01-31 21:45:22 +0000405 elif obj_type == b'b':
Damien Georgec3beb162016-04-15 11:56:10 +0100406 return bytes_cons(buf)
Damien George0699c6b2016-01-31 21:45:22 +0000407 elif obj_type == b'i':
Damien Georgec3beb162016-04-15 11:56:10 +0100408 return int(str_cons(buf, 'ascii'), 10)
Damien George0699c6b2016-01-31 21:45:22 +0000409 elif obj_type == b'f':
Damien Georgec3beb162016-04-15 11:56:10 +0100410 return float(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000411 elif obj_type == b'c':
Damien Georgec3beb162016-04-15 11:56:10 +0100412 return complex(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000413 else:
414 assert 0
415
416def read_qstr_and_pack(f, bytecode, ip):
417 qst = read_qstr(f)
418 bytecode[ip] = qst & 0xff
419 bytecode[ip + 1] = qst >> 8
420
421def read_bytecode_qstrs(file, bytecode, ip):
422 while ip < len(bytecode):
423 f, sz = mp_opcode_format(bytecode, ip)
424 if f == 1:
425 read_qstr_and_pack(file, bytecode, ip + 1)
426 ip += sz
427
428def read_raw_code(f):
429 bc_len = read_uint(f)
430 bytecode = bytearray(f.read(bc_len))
431 ip, ip2, prelude = extract_prelude(bytecode)
432 read_qstr_and_pack(f, bytecode, ip2) # simple_name
433 read_qstr_and_pack(f, bytecode, ip2 + 2) # source_file
434 read_bytecode_qstrs(f, bytecode, ip)
435 n_obj = read_uint(f)
436 n_raw_code = read_uint(f)
437 qstrs = [read_qstr(f) for _ in range(prelude[3] + prelude[4])]
438 objs = [read_obj(f) for _ in range(n_obj)]
439 raw_codes = [read_raw_code(f) for _ in range(n_raw_code)]
440 return RawCode(bytecode, qstrs, objs, raw_codes)
441
442def read_mpy(filename):
443 with open(filename, 'rb') as f:
Damien Georgec3beb162016-04-15 11:56:10 +0100444 header = bytes_cons(f.read(4))
Damien George0699c6b2016-01-31 21:45:22 +0000445 if header[0] != ord('M'):
446 raise Exception('not a valid .mpy file')
Damien George6a110482017-02-17 00:19:34 +1100447 if header[1] != config.MPY_VERSION:
448 raise Exception('incompatible .mpy version')
Damien George0699c6b2016-01-31 21:45:22 +0000449 feature_flags = header[2]
450 config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE = (feature_flags & 1) != 0
451 config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_flags & 2) != 0
452 config.mp_small_int_bits = header[3]
453 return read_raw_code(f)
454
455def dump_mpy(raw_codes):
456 for rc in raw_codes:
457 rc.dump()
458
Damien Georgeb4790af2016-09-02 15:09:21 +1000459def freeze_mpy(base_qstrs, raw_codes):
Damien George0699c6b2016-01-31 21:45:22 +0000460 # add to qstrs
461 new = {}
462 for q in global_qstrs:
463 # don't add duplicates
464 if q.qstr_esc in base_qstrs or q.qstr_esc in new:
465 continue
466 new[q.qstr_esc] = (len(new), q.qstr_esc, q.str)
467 new = sorted(new.values(), key=lambda x: x[0])
468
469 print('#include "py/mpconfig.h"')
470 print('#include "py/objint.h"')
471 print('#include "py/objstr.h"')
472 print('#include "py/emitglue.h"')
473 print()
474
Damien George98458a42017-01-05 15:52:52 +1100475 print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE)
476 print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"')
Damien George99b47192016-05-16 23:13:30 +0100477 print('#endif')
478 print()
479
480 print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL)
481 print('#error "incompatible MICROPY_LONGINT_IMPL"')
482 print('#endif')
483 print()
484
485 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
486 print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE)
487 print('#error "incompatible MPZ_DIG_SIZE"')
488 print('#endif')
489 print()
490
491
Damien George0699c6b2016-01-31 21:45:22 +0000492 print('#if MICROPY_PY_BUILTINS_FLOAT')
493 print('typedef struct _mp_obj_float_t {')
494 print(' mp_obj_base_t base;')
495 print(' mp_float_t value;')
496 print('} mp_obj_float_t;')
497 print('#endif')
498 print()
499
Damien Georgec51c8832016-09-03 00:19:02 +1000500 print('#if MICROPY_PY_BUILTINS_COMPLEX')
501 print('typedef struct _mp_obj_complex_t {')
502 print(' mp_obj_base_t base;')
503 print(' mp_float_t real;')
504 print(' mp_float_t imag;')
505 print('} mp_obj_complex_t;')
506 print('#endif')
507 print()
508
Damien George0699c6b2016-01-31 21:45:22 +0000509 print('enum {')
510 for i in range(len(new)):
511 if i == 0:
512 print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1])
513 else:
514 print(' MP_QSTR_%s,' % new[i][1])
515 print('};')
516
517 print()
518 print('extern const qstr_pool_t mp_qstr_const_pool;');
519 print('const qstr_pool_t mp_qstr_frozen_const_pool = {')
520 print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool')
521 print(' MP_QSTRnumber_of, // previous pool size')
522 print(' %u, // allocated entries' % len(new))
523 print(' %u, // used entries' % len(new))
524 print(' {')
525 for _, _, qstr in new:
Damien Georgeb4790af2016-09-02 15:09:21 +1000526 print(' %s,'
527 % qstrutil.make_bytes(config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr))
Damien George0699c6b2016-01-31 21:45:22 +0000528 print(' },')
529 print('};')
530
531 for rc in raw_codes:
532 rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_')
533
534 print()
535 print('const char mp_frozen_mpy_names[] = {')
536 for rc in raw_codes:
Damien George9b4c0132016-05-23 12:46:02 +0100537 module_name = rc.source_file.str
Damien George0699c6b2016-01-31 21:45:22 +0000538 print('"%s\\0"' % module_name)
539 print('"\\0"};')
540
541 print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {')
542 for rc in raw_codes:
543 print(' &raw_code_%s,' % rc.escaped_name)
544 print('};')
545
546def main():
547 import argparse
548 cmd_parser = argparse.ArgumentParser(description='A tool to work with MicroPython .mpy files.')
549 cmd_parser.add_argument('-d', '--dump', action='store_true',
550 help='dump contents of files')
551 cmd_parser.add_argument('-f', '--freeze', action='store_true',
552 help='freeze files')
553 cmd_parser.add_argument('-q', '--qstr-header',
554 help='qstr header file to freeze against')
555 cmd_parser.add_argument('-mlongint-impl', choices=['none', 'longlong', 'mpz'], default='mpz',
556 help='long-int implementation used by target (default mpz)')
557 cmd_parser.add_argument('-mmpz-dig-size', metavar='N', type=int, default=16,
558 help='mpz digit size used by target (default 16)')
559 cmd_parser.add_argument('files', nargs='+',
560 help='input .mpy files')
561 args = cmd_parser.parse_args()
562
563 # set config values relevant to target machine
564 config.MICROPY_LONGINT_IMPL = {
565 'none':config.MICROPY_LONGINT_IMPL_NONE,
566 'longlong':config.MICROPY_LONGINT_IMPL_LONGLONG,
567 'mpz':config.MICROPY_LONGINT_IMPL_MPZ,
568 }[args.mlongint_impl]
569 config.MPZ_DIG_SIZE = args.mmpz_dig_size
570
Damien Georgeb4790af2016-09-02 15:09:21 +1000571 # set config values for qstrs, and get the existing base set of qstrs
Damien George0699c6b2016-01-31 21:45:22 +0000572 if args.qstr_header:
573 qcfgs, base_qstrs = qstrutil.parse_input_headers([args.qstr_header])
Damien Georgeb4790af2016-09-02 15:09:21 +1000574 config.MICROPY_QSTR_BYTES_IN_LEN = int(qcfgs['BYTES_IN_LEN'])
575 config.MICROPY_QSTR_BYTES_IN_HASH = int(qcfgs['BYTES_IN_HASH'])
Damien George0699c6b2016-01-31 21:45:22 +0000576 else:
Damien Georgeb4790af2016-09-02 15:09:21 +1000577 config.MICROPY_QSTR_BYTES_IN_LEN = 1
578 config.MICROPY_QSTR_BYTES_IN_HASH = 1
579 base_qstrs = {}
Damien George0699c6b2016-01-31 21:45:22 +0000580
581 raw_codes = [read_mpy(file) for file in args.files]
582
583 if args.dump:
584 dump_mpy(raw_codes)
585 elif args.freeze:
586 try:
Damien Georgeb4790af2016-09-02 15:09:21 +1000587 freeze_mpy(base_qstrs, raw_codes)
Damien George0699c6b2016-01-31 21:45:22 +0000588 except FreezeError as er:
589 print(er, file=sys.stderr)
590 sys.exit(1)
591
592if __name__ == '__main__':
593 main()