blob: 3077e0d384903b9e5ddf494d8d20817234c797d9 [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 George9ba3de62017-11-15 12:46:08 +1100290 if obj is Ellipsis:
291 print('#define %s mp_const_ellipsis_obj' % obj_name)
292 elif is_str_type(obj) or is_bytes_type(obj):
Damien Georgeb6bdf182016-09-02 15:10:45 +1000293 if is_str_type(obj):
294 obj = bytes_cons(obj, 'utf8')
295 obj_type = 'mp_type_str'
296 else:
297 obj_type = 'mp_type_bytes'
298 print('STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
299 % (obj_name, obj_type, qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
300 len(obj), ''.join(('\\x%02x' % b) for b in obj)))
Damien Georgec3beb162016-04-15 11:56:10 +0100301 elif is_int_type(obj):
Damien George0699c6b2016-01-31 21:45:22 +0000302 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
303 # TODO check if we can actually fit this long-int into a small-int
304 raise FreezeError(self, 'target does not support long int')
305 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
306 # TODO
307 raise FreezeError(self, 'freezing int to long-long is not implemented')
308 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
309 neg = 0
310 if obj < 0:
311 obj = -obj
312 neg = 1
313 bits_per_dig = config.MPZ_DIG_SIZE
314 digs = []
315 z = obj
316 while z:
317 digs.append(z & ((1 << bits_per_dig) - 1))
318 z >>= bits_per_dig
319 ndigs = len(digs)
320 digs = ','.join(('%#x' % d) for d in digs)
321 print('STATIC const mp_obj_int_t %s = {{&mp_type_int}, '
322 '{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t[]){%s}}};'
323 % (obj_name, neg, ndigs, ndigs, bits_per_dig, digs))
324 elif type(obj) is float:
Damien George72ae3c72016-08-10 13:26:11 +1000325 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
Damien George0699c6b2016-01-31 21:45:22 +0000326 print('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
327 % (obj_name, obj))
Damien George72ae3c72016-08-10 13:26:11 +1000328 print('#endif')
Damien Georgec51c8832016-09-03 00:19:02 +1000329 elif type(obj) is complex:
330 print('STATIC const mp_obj_complex_t %s = {{&mp_type_complex}, %.16g, %.16g};'
331 % (obj_name, obj.real, obj.imag))
Damien George0699c6b2016-01-31 21:45:22 +0000332 else:
Damien George0699c6b2016-01-31 21:45:22 +0000333 raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,))
334
Damien Georgeb6a32892017-08-12 22:26:18 +1000335 # generate constant table, if it has any entries
336 const_table_len = len(self.qstrs) + len(self.objs) + len(self.raw_codes)
337 if const_table_len:
338 print('STATIC const mp_rom_obj_t const_table_data_%s[%u] = {'
339 % (self.escaped_name, const_table_len))
340 for qst in self.qstrs:
341 print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id)
342 for i in range(len(self.objs)):
343 if type(self.objs[i]) is float:
344 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
345 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
346 print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
347 n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
348 n = ((n & ~0x3) | 2) + 0x80800000
349 print(' (mp_rom_obj_t)(0x%08x),' % (n,))
Damien George929d10a2018-07-09 12:22:40 +1000350 print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D')
351 n = struct.unpack('<Q', struct.pack('<d', self.objs[i]))[0]
352 n += 0x8004000000000000
353 print(' (mp_rom_obj_t)(0x%016x),' % (n,))
Damien Georgeb6a32892017-08-12 22:26:18 +1000354 print('#endif')
355 else:
356 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
357 for rc in self.raw_codes:
358 print(' MP_ROM_PTR(&raw_code_%s),' % rc.escaped_name)
359 print('};')
Damien George0699c6b2016-01-31 21:45:22 +0000360
361 # generate module
362 if self.simple_name.str != '<module>':
363 print('STATIC ', end='')
364 print('const mp_raw_code_t raw_code_%s = {' % self.escaped_name)
365 print(' .kind = MP_CODE_BYTECODE,')
366 print(' .scope_flags = 0x%02x,' % self.prelude[2])
367 print(' .n_pos_args = %u,' % self.prelude[3])
368 print(' .data.u_byte = {')
369 print(' .bytecode = bytecode_data_%s,' % self.escaped_name)
Damien Georgeb6a32892017-08-12 22:26:18 +1000370 if const_table_len:
371 print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
372 else:
373 print(' .const_table = NULL,')
Damien George0699c6b2016-01-31 21:45:22 +0000374 print(' #if MICROPY_PERSISTENT_CODE_SAVE')
375 print(' .bc_len = %u,' % len(self.bytecode))
376 print(' .n_obj = %u,' % len(self.objs))
377 print(' .n_raw_code = %u,' % len(self.raw_codes))
378 print(' #endif')
379 print(' },')
380 print('};')
381
382def read_uint(f):
383 i = 0
384 while True:
Damien Georgec3beb162016-04-15 11:56:10 +0100385 b = bytes_cons(f.read(1))[0]
Damien George0699c6b2016-01-31 21:45:22 +0000386 i = (i << 7) | (b & 0x7f)
387 if b & 0x80 == 0:
388 break
389 return i
390
391global_qstrs = []
392qstr_type = namedtuple('qstr', ('str', 'qstr_esc', 'qstr_id'))
393def read_qstr(f):
394 ln = read_uint(f)
Damien Georgec3beb162016-04-15 11:56:10 +0100395 data = str_cons(f.read(ln), 'utf8')
Damien George0699c6b2016-01-31 21:45:22 +0000396 qstr_esc = qstrutil.qstr_escape(data)
397 global_qstrs.append(qstr_type(data, qstr_esc, 'MP_QSTR_' + qstr_esc))
398 return len(global_qstrs) - 1
399
400def read_obj(f):
401 obj_type = f.read(1)
402 if obj_type == b'e':
403 return Ellipsis
404 else:
405 buf = f.read(read_uint(f))
406 if obj_type == b's':
Damien Georgec3beb162016-04-15 11:56:10 +0100407 return str_cons(buf, 'utf8')
Damien George0699c6b2016-01-31 21:45:22 +0000408 elif obj_type == b'b':
Damien Georgec3beb162016-04-15 11:56:10 +0100409 return bytes_cons(buf)
Damien George0699c6b2016-01-31 21:45:22 +0000410 elif obj_type == b'i':
Damien Georgec3beb162016-04-15 11:56:10 +0100411 return int(str_cons(buf, 'ascii'), 10)
Damien George0699c6b2016-01-31 21:45:22 +0000412 elif obj_type == b'f':
Damien Georgec3beb162016-04-15 11:56:10 +0100413 return float(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000414 elif obj_type == b'c':
Damien Georgec3beb162016-04-15 11:56:10 +0100415 return complex(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000416 else:
417 assert 0
418
419def read_qstr_and_pack(f, bytecode, ip):
420 qst = read_qstr(f)
421 bytecode[ip] = qst & 0xff
422 bytecode[ip + 1] = qst >> 8
423
424def read_bytecode_qstrs(file, bytecode, ip):
425 while ip < len(bytecode):
426 f, sz = mp_opcode_format(bytecode, ip)
427 if f == 1:
428 read_qstr_and_pack(file, bytecode, ip + 1)
429 ip += sz
430
431def read_raw_code(f):
432 bc_len = read_uint(f)
433 bytecode = bytearray(f.read(bc_len))
434 ip, ip2, prelude = extract_prelude(bytecode)
435 read_qstr_and_pack(f, bytecode, ip2) # simple_name
436 read_qstr_and_pack(f, bytecode, ip2 + 2) # source_file
437 read_bytecode_qstrs(f, bytecode, ip)
438 n_obj = read_uint(f)
439 n_raw_code = read_uint(f)
440 qstrs = [read_qstr(f) for _ in range(prelude[3] + prelude[4])]
441 objs = [read_obj(f) for _ in range(n_obj)]
442 raw_codes = [read_raw_code(f) for _ in range(n_raw_code)]
443 return RawCode(bytecode, qstrs, objs, raw_codes)
444
445def read_mpy(filename):
446 with open(filename, 'rb') as f:
Damien Georgec3beb162016-04-15 11:56:10 +0100447 header = bytes_cons(f.read(4))
Damien George0699c6b2016-01-31 21:45:22 +0000448 if header[0] != ord('M'):
449 raise Exception('not a valid .mpy file')
Damien George6a110482017-02-17 00:19:34 +1100450 if header[1] != config.MPY_VERSION:
451 raise Exception('incompatible .mpy version')
Damien George0699c6b2016-01-31 21:45:22 +0000452 feature_flags = header[2]
453 config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE = (feature_flags & 1) != 0
454 config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_flags & 2) != 0
455 config.mp_small_int_bits = header[3]
456 return read_raw_code(f)
457
458def dump_mpy(raw_codes):
459 for rc in raw_codes:
460 rc.dump()
461
Damien Georgeb4790af2016-09-02 15:09:21 +1000462def freeze_mpy(base_qstrs, raw_codes):
Damien George0699c6b2016-01-31 21:45:22 +0000463 # add to qstrs
464 new = {}
465 for q in global_qstrs:
466 # don't add duplicates
467 if q.qstr_esc in base_qstrs or q.qstr_esc in new:
468 continue
469 new[q.qstr_esc] = (len(new), q.qstr_esc, q.str)
470 new = sorted(new.values(), key=lambda x: x[0])
471
472 print('#include "py/mpconfig.h"')
473 print('#include "py/objint.h"')
474 print('#include "py/objstr.h"')
475 print('#include "py/emitglue.h"')
476 print()
477
Damien George98458a42017-01-05 15:52:52 +1100478 print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE)
479 print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"')
Damien George99b47192016-05-16 23:13:30 +0100480 print('#endif')
481 print()
482
483 print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL)
484 print('#error "incompatible MICROPY_LONGINT_IMPL"')
485 print('#endif')
486 print()
487
488 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
489 print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE)
490 print('#error "incompatible MPZ_DIG_SIZE"')
491 print('#endif')
492 print()
493
494
Damien George0699c6b2016-01-31 21:45:22 +0000495 print('#if MICROPY_PY_BUILTINS_FLOAT')
496 print('typedef struct _mp_obj_float_t {')
497 print(' mp_obj_base_t base;')
498 print(' mp_float_t value;')
499 print('} mp_obj_float_t;')
500 print('#endif')
501 print()
502
Damien Georgec51c8832016-09-03 00:19:02 +1000503 print('#if MICROPY_PY_BUILTINS_COMPLEX')
504 print('typedef struct _mp_obj_complex_t {')
505 print(' mp_obj_base_t base;')
506 print(' mp_float_t real;')
507 print(' mp_float_t imag;')
508 print('} mp_obj_complex_t;')
509 print('#endif')
510 print()
511
Damien George0699c6b2016-01-31 21:45:22 +0000512 print('enum {')
513 for i in range(len(new)):
514 if i == 0:
515 print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1])
516 else:
517 print(' MP_QSTR_%s,' % new[i][1])
518 print('};')
519
520 print()
521 print('extern const qstr_pool_t mp_qstr_const_pool;');
522 print('const qstr_pool_t mp_qstr_frozen_const_pool = {')
523 print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool')
524 print(' MP_QSTRnumber_of, // previous pool size')
525 print(' %u, // allocated entries' % len(new))
526 print(' %u, // used entries' % len(new))
527 print(' {')
528 for _, _, qstr in new:
Damien Georgeb4790af2016-09-02 15:09:21 +1000529 print(' %s,'
530 % qstrutil.make_bytes(config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr))
Damien George0699c6b2016-01-31 21:45:22 +0000531 print(' },')
532 print('};')
533
534 for rc in raw_codes:
535 rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_')
536
537 print()
538 print('const char mp_frozen_mpy_names[] = {')
539 for rc in raw_codes:
Damien George9b4c0132016-05-23 12:46:02 +0100540 module_name = rc.source_file.str
Damien George0699c6b2016-01-31 21:45:22 +0000541 print('"%s\\0"' % module_name)
542 print('"\\0"};')
543
544 print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {')
545 for rc in raw_codes:
546 print(' &raw_code_%s,' % rc.escaped_name)
547 print('};')
548
549def main():
550 import argparse
551 cmd_parser = argparse.ArgumentParser(description='A tool to work with MicroPython .mpy files.')
552 cmd_parser.add_argument('-d', '--dump', action='store_true',
553 help='dump contents of files')
554 cmd_parser.add_argument('-f', '--freeze', action='store_true',
555 help='freeze files')
556 cmd_parser.add_argument('-q', '--qstr-header',
557 help='qstr header file to freeze against')
558 cmd_parser.add_argument('-mlongint-impl', choices=['none', 'longlong', 'mpz'], default='mpz',
559 help='long-int implementation used by target (default mpz)')
560 cmd_parser.add_argument('-mmpz-dig-size', metavar='N', type=int, default=16,
561 help='mpz digit size used by target (default 16)')
562 cmd_parser.add_argument('files', nargs='+',
563 help='input .mpy files')
564 args = cmd_parser.parse_args()
565
566 # set config values relevant to target machine
567 config.MICROPY_LONGINT_IMPL = {
568 'none':config.MICROPY_LONGINT_IMPL_NONE,
569 'longlong':config.MICROPY_LONGINT_IMPL_LONGLONG,
570 'mpz':config.MICROPY_LONGINT_IMPL_MPZ,
571 }[args.mlongint_impl]
572 config.MPZ_DIG_SIZE = args.mmpz_dig_size
573
Damien Georgeb4790af2016-09-02 15:09:21 +1000574 # set config values for qstrs, and get the existing base set of qstrs
Damien George0699c6b2016-01-31 21:45:22 +0000575 if args.qstr_header:
576 qcfgs, base_qstrs = qstrutil.parse_input_headers([args.qstr_header])
Damien Georgeb4790af2016-09-02 15:09:21 +1000577 config.MICROPY_QSTR_BYTES_IN_LEN = int(qcfgs['BYTES_IN_LEN'])
578 config.MICROPY_QSTR_BYTES_IN_HASH = int(qcfgs['BYTES_IN_HASH'])
Damien George0699c6b2016-01-31 21:45:22 +0000579 else:
Damien Georgeb4790af2016-09-02 15:09:21 +1000580 config.MICROPY_QSTR_BYTES_IN_LEN = 1
581 config.MICROPY_QSTR_BYTES_IN_HASH = 1
582 base_qstrs = {}
Damien George0699c6b2016-01-31 21:45:22 +0000583
584 raw_codes = [read_mpy(file) for file in args.files]
585
586 if args.dump:
587 dump_mpy(raw_codes)
588 elif args.freeze:
589 try:
Damien Georgeb4790af2016-09-02 15:09:21 +1000590 freeze_mpy(base_qstrs, raw_codes)
Damien George0699c6b2016-01-31 21:45:22 +0000591 except FreezeError as er:
592 print(er, file=sys.stderr)
593 sys.exit(1)
594
595if __name__ == '__main__':
596 main()